|
DataMuseum.dkPresents historical artifacts from the history of: Rational R1000/400 Tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about Rational R1000/400 Tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - downloadIndex: ┃ S T ┃
Length: 15796 (0x3db4) Types: TextFile Names: »SERVER_C«
└─⟦afbc8121e⟧ Bits:30000532 8mm tape, Rational 1000, MC68020_OS2000 7_2_2 └─ ⟦77aa8350c⟧ »DATA« └─⟦f794ecd1d⟧ └─⟦this⟧
/*========================================================================*/ /* SERVER.C */ /* */ /* */ /* Purpose: Server for File transfer with a remote host. */ /* */ /* */ /* nopen() a TCP/IP connection */ /* nsocket() get socket_id 1 */ /* nsocket() get socket_id 2 */ /* loop */ /* naccept() a TCP/IP connection */ /* nread() transfer type and the port_id for connection */ /* transfer types are: */ /* e - host to server executable file */ /* f - host to server other file */ /* g - server to host file */ /* connect() */ /* nread() the local filename for transfer */ /* open or create the file */ /* nwrite() Success if OK; Error_code if not */ /* if receiving then */ /* nread() receive from host ... */ /* write() and write to file until count = 0 */ /* else */ /* read() read from the file, until count = 0 */ /* nwrite() and write to host */ /* end if; */ /* close the file. */ /* nclose() the TCP/IP connection */ /* end loop; */ /* nsoclose(s_id1) */ /* nsoclose(s_id2) */ /*------------------------------------------------------------------------*/ #include <stdio.h> #include <modes.h> #include <errno.h> #include "/H0/TCP/INCL/msocket.h" #define LOCAL_PORT 0x010D /* local socket - wait for commands */ /* from host; since OS-9 treats the */ /* sockets < (4,0) as privileged, the */ /* server must use privileged ports. */ #define REM_HOST_ADR 0x59400359 /* Barney host address */ /* not used since server is passive */ typedef char REQUEST; #define RQ_PUT_EXE 'E' #define RQ_PUT_FIL 'F' #define RQ_GET_FIL 'G' typedef char FILE_STATUS; #define ST_SUCCESS 'S' #define ST_NAM_ERR 'N' #define ST_USE_ERR 'U' #define ST_DEV_ERR 'D' #define ST_OTH_ERR 'O' extern int tcp_errno; extern int errno; extern int priv_port; struct sockaddr asa_l; /* used by nsocket() - defines local port */ struct sockaddr asa_r; /* used by naccept - returns remote port + addr */ struct sockaddr asa_w; /* used by nconnect - */ int debug = 0 ; int rem_host_address; int remote_port ; REQUEST request; int count_r,count_w; int status; int s_id1,s_id2; char r_buff[1024]; char stat_buff[2]; int i; int local_port; int id = 0 ; /* id returned from nopen, used by all nxxx calls*/ int fpath = 0 ; /* path for file */ short e_attr = 0x3f; /* all permissions */ short rw_attr = 0x1b; /* read/write permissions */ FILE *fdes; /*---------------------------------------------------------------------------*/ /*\f */ /*===========================================================================*/ int makeconnection () { /*----------------------------------connect-------------------------------*/ asa_w.sin_family = AF_INET; asa_w.sin_port = remote_port ; asa_w.sin_addr = rem_host_address ; for(i=0 ; i<8 ; i++) asa_w.sin_data[i] = 0; /* not required ? */ if(debug>1) printf ("SERVER: Connect at Remote_Port =x%x Remote_Host_addr = x%x \n", asa_w.sin_port, asa_w.sin_addr); status = nconnect(id,s_id2,&asa_w); if (status == -1) { printf ("SERVER: *** ERROR at nconnect() call. ErrCode = x%x\n",tcp_errno); return -1; } if (debug>1) printf ("SERVER: Returned from nconnect(). Status= %d Rem adr= x%x Rem port= x%x\n", status, asa_w.sin_addr, asa_w.sin_port); return 0; } /*end makeconnection */ /*\f */ int getfilename() { if(debug>1) printf ("SERVER: Get file name from Remote.\n"); count_r = nread(id,s_id1,r_buff,1024); if (count_r == -1) { printf ("SERVER: *** ERROR at nread() call. Status = x%x ErrCode = x%x\n", count_r,tcp_errno) ; return -1; } r_buff[count_r] = 0 ; /* add terminator */ printf("SERVER: Transfering file = %s \n",r_buff); return 0; } /*end getfilename */ /*\f */ int xferfromhost () { int total_r = 0; if(debug>1) printf("SERVER: Enter mode: 'read from TCP and write to file.\n"); while(1) { /* .begin while read TCP - write file.................................*/ count_r = nread(id,s_id2,r_buff,1024); if (debug>1) printf("SERVER: Number of bytes read = %d \n",count_r); if (count_r == -1) { printf ("SERVER: *** ERROR at nread() call. Status = x%x Errcode = %x%\n", count_r,tcp_errno); return -1; } if (count_r == 0) { printf("SERVER: Number bytes transferred = %d \n",total_r); break; } count_w = fwrite(r_buff,1,count_r,fdes); if (count_w == 0) { printf ("SERVER: *** ERROR at fwrite() call.\n"); return -1; } total_r = total_r + count_r; if (debug>1) printf("SERVER: Number of bytes written = %d \n",count_w); } /* .end while read from TCP - write to file...........................*/ return 0; } /*end xferfromhost */ /*\f */ int xfertohost() { int total_w = 0; if(debug>1) printf("SERVER: Enter mode: 'read from file and transmit to TCP.\n"); while(1) { /* .begin while read file - nwrite .....................................*/ count_r = fread(r_buff,1,128,fdes); /*----nwrite ----*/ count_w = count_r ; { status = nwrite(id,s_id2,r_buff,count_w); /* echo back */ if (status == -1) { printf("SERVER: *** ERROR at nwrite() call. ErrCode = x%x\n",tcp_errno); return -1; } if (debug>1) printf("SERVER: Number of written bytes = x%x \n",status); } if (count_r == 0) { printf("SERVER: Number of bytes transferred = %d \n",total_w); break; } total_w = total_w + count_w; } /* .end while read from file - nwrite .................................*/ return 0; } /*end xfertohost */ /*\f */ main(argc,argv) int argc; char *argv[]; { char *ap; char c; debug = 0; if(argc>0) /* turn on debug if argument = 'd' */ { ap = argv[1]; if (*ap == 'd') debug = 2 ; if (*ap == 'D') debug = 2 ; } printf("SERVER: Starting TCP/IP server\n"); /*-------------get local port----------------------------------------------*/ local_port = LOCAL_PORT; again: /*-------------open-------------------------------------------------------*/ if(debug>1) printf("\nSERVER: Calling nopen().\n"); id = nopen(); /* id = global variable to be used by nxxxx calls */ if(id < 0) { printf("SERVER: *** ERROR at nopen. Status = x%x\n",id); goto error ; } if(debug>1) printf("SERVER: Returned from nopen(). id = x%x\n",id); /*-------------socket for accept------------------------------------------*/ if(debug>1) printf("SERVER: Calling nsocket() to get a socket for accept.\n"); asa_l.sin_family = AF_INET; asa_l.sin_port = local_port; asa_l.sin_addr = 0; s_id1 = nsocket(id,SOCK_STREAM,(struct sockproto*)0, &asa_l,SO_ACCEPTCONN); priv_port = 1; /* enable privileged port */ if (s_id1 == -1) { printf ("SERVER: *** ERROR at nsocket() call: Status = x%x. ErrCode = x%x \n", s_id1,tcp_errno); goto error_cleanup ; } if(debug>1) printf ("SERVER: Returned from nsocket(). Socket id = x%x \n",s_id1); /*-------------socket for connect ----------------------------------------*/ if(debug>1) printf("SERVER: Calling nsocket() to get a socket for connect.\n"); s_id2 = nsocket(id,SOCK_STREAM,(struct sockproto*)0,(struct sockaddr*)0,0); if (s_id2 == -1) { printf ("SERVER: *** ERROR at nsocket() call: ErrCode = x%x\n", tcp_errno); goto error_cleanup; } if (debug>1) printf("SERVER: Returned from nsocket(). Socket id = x%x\n",s_id2); /*-------------accept----------------------------------------------------*/ if(debug>1) printf ("SERVER: Calling naccept(). Waiting for connection at port = x%x\n", asa_l.sin_port); status = naccept(id,s_id1,&asa_r); if (status == -1) { printf ("SERVER: *** ERROR at naccept() call. Status = %d ErrCode = x%x\n", status,tcp_errno) ; goto error_cleanup; } if(debug>1)printf ("SERVER: Ret from naccept(). Stat=x%x Rem_adr=x%x Rem_port=x%x\n", status, asa_r.sin_addr, asa_r.sin_port); rem_host_address = asa_r.sin_addr ; /* to be used by nconnect() */ /*------------------------------------------------------------------------*/ /*-------------nread to get port to make connect at ---------------------*/ if(debug>1) printf ("SERVER: Get port number from Remote.\n"); count_r = nread(id,s_id1,r_buff,1024); if (count_r == -1) { printf ("SERVER: *** ERROR at nread() call. Status = x%x ErrCode = x%x\n", count_r,tcp_errno) ; goto error_cleanup; } if (debug>1) printf("SERVER: Count=x%x. Remote_port_Data = %x %x \n",count_r, r_buff[0] & 0xff, r_buff[1] & 0xff ); if (debug>1) printf("SERVER: Request= '%c'.\n",r_buff[2] ); remote_port = ((r_buff[0] & 0xff) << 8) + (r_buff[1] & 0xff) ; request = r_buff[2]; switch (request) { case RQ_PUT_EXE: printf("\nSERVER: Receiving executable file from host.\n"); break; case RQ_PUT_FIL: printf("\nSERVER: Receiving file from host.\n"); break; case RQ_GET_FIL: printf("\nSERVER: Sending file to host.\n"); break; default: printf("\nSERVER: Unknown command: '%c'\n", request ); goto error_cleanup; } /*----------------- establish the transfer connection----------------*/ status = makeconnection(); if (status < 0) goto error_cleanup; /*------------------------ get filename -----------------------------*/ status = getfilename(); if (status < 0) goto error_cleanup; /*------------ create the file -----------------------------*/ switch (request) { case RQ_PUT_EXE: fdes = fopen(r_buff,"wx"); break; case RQ_PUT_FIL: fdes = fopen(r_buff,"w"); break; case RQ_GET_FIL: fdes = fopen(r_buff,"r"); break; default: printf("\nSERVER: Unknown command: '%c'\n", request ); goto error_cleanup; } stat_buff[0] = '?'; stat_buff[1] = ST_SUCCESS; if (fdes == NULL) { printf("SERVER: *** ERROR can't open file %s\n",r_buff); printf(" *** errno = %d\n", errno); switch (errno) { case E_BPNAM: /* Bad path name */ case E_BNAM: /* Bad name */ case E_PNNF: /* Path name not found */ stat_buff[1] = ST_NAM_ERR; break; case E_PERMIT: /* Need permissions */ case E_FNA: /* File not accessible */ case E_SHARE: /* Nonshareable file is busy */ stat_buff[1] = ST_USE_ERR; break; case E_WP: /* Write protected */ case E_UNIT: /* Illegal unit (drive) */ case E_READ: /* Read error */ case E_WRITE: /* Write error */ case E_NOTRDY: /* Device not ready */ case E_DEVBSY: /* Device is busy */ case E_FULL: /* Media is full */ stat_buff[1] = ST_DEV_ERR; break; default: stat_buff[1] = ST_OTH_ERR; break; } } status = nwrite(id,s_id2,stat_buff,2); /* write error status */ if (status == -1) { printf("SERVER: *** ERROR at nwrite() call. ErrCode = x%x\n",tcp_errno); goto error_cleanup; } if (fdes == NULL) goto error_cleanup; fpath = fileno(fdes); switch (request) { case RQ_PUT_EXE: /*----- transfer file to host --------*/ status = _ss_attr(fpath,e_attr); /* set file attributes */ if (status == -1) printf("SERVER: *** ERROR unable to set file attributes\n"); status = xferfromhost (); break; case RQ_PUT_FIL: /*----- transfer file from host ------*/ status = _ss_attr(fpath,rw_attr); /* set file attributes */ if (status == -1) printf("SERVER: *** ERROR unable to set file attributes\n"); status = xferfromhost (); break; case RQ_GET_FIL: /*----- transfer file to host --------*/ status = xfertohost (); break; default: printf("\nSERVER: Unknown command: '%c'\n", request ); goto error_cleanup; } /*--------- done with transfer --------------------------------------------*/ error_cleanup: /*--------- close, nsoclose(), nclose -------------------------------------*/ fclose(fdes); if(debug>1) printf("SERVER: Calling nsoclose().\n"); status = nsoclose(id,s_id1); if (status == -1) { printf("SERVER: *** ERROR at nsoclose(). Status = x%x. ErrCod = x%x\n", status,tcp_errno) ; } if(debug>1) printf("SERVER: Calling nsoclose().\n"); status = nsoclose(id,s_id2); if (status == -1) { printf("SERVER: *** ERROR at nsoclose(). Status = x%x. ErrCod = x%x\n", status,tcp_errno) ; } status = nclose(id,0); if (status == -1) { printf("SERVER: *** ERROR at nclose(). Status = x%x. ErrCod = x%x\n", status,tcp_errno) ; goto error ; } if(debug>1) printf("SERVER: Returned from nclose(). Status = x%x\n",status); /*------------------------------------------------------------------------*/ goto again; error: printf("SERVER: *** TCP/IP server terminated due to error.\n"); } /*end main*/