#ifndef _WINDOWS #define RTexit(x) exit(x) #define RTwrite(a,b,c) send(a,b,c,0) #define RTread(a,b,c) recv(a,b,c,0) #define RTclose(x) close(x) #include #include #include #include #include #include #include #else #define RTwrite(a,b,c) send(a,b,c,0) #define RTread(a,b,c) recv(a,b,c,0) #define RTclose(x) closesocket(x) #include #include #include #include #define SWAP int fStarted=0; int RTexit(int x){ fprintf(stderr,"Error! %d Press any key\n",(x)); getchar(); QuitUserInterface (0); return 0; } void DoStartUp ( void ) { WSADATA wsaData; INT iRetVal; iRetVal = WSAStartup ( MAKEWORD ( 1,1 ), &wsaData ); if ( 0 != iRetVal) { perror ( "DoStartup WSAStartup" ); RTexit(iRetVal); } /* Set the global flag */ fStarted = TRUE; return; } #endif #include #include #include #include #include #include "iou2f.h" #ifdef SWAP #include "swap.h" #endif #define PORT 43210 #define MAXMSG 4096 char data[MAXDATA]; int *idata=(int*) data; short debug; int test_endian(void){ short int a=0x1000; char *b; b=(char *)&a; if (*(b+1)==0x10) { return 1; } else { return 0 ;} } int make_socket (unsigned short int port) { int sock; struct sockaddr_in name; /* create the socket. */ sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("socket"); RTexit (-1); } /* give the socket name */ name.sin_family = AF_INET; name.sin_port = htons (port); name.sin_addr.s_addr = htonl (INADDR_ANY); if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { perror ("bind"); RTexit (-1); } return sock; } void write_tcpip (int sock_num, char *buff, int len) { int status; do { if (0 > (status = RTwrite (sock_num, buff, len))) { perror ("\nwrite(send_hp)"); RTexit (-1); } len -= status; buff += status; } while (len); } void read_tcpip (int sock_num, char * buff, int len) { int status; do { if (0 > (status = RTread (sock_num, buff, len))) { perror ("\nread(recv_hp)"); RTexit(-1); } len -= status; buff += status; } while (len); } void init_sockaddr ( struct sockaddr_in *name, char *hostname,unsigned short int port) { struct hostent *hostinfo; name->sin_family = AF_INET; name->sin_port = htons (port); hostinfo = gethostbyname (hostname); /* hostinfo = gethostbyaddr (SERVERHOST); */ if (hostinfo == NULL) { fprintf (stderr, "Unknown host %s. \n", hostname); RTexit (-1); } name->sin_addr = *(struct in_addr *) hostinfo->h_addr; } int init_soc (char *hostname, int *sock1,int port ) { int sock; struct sockaddr_in servername; int size, status ; /* Create the socket */ sock = socket (AF_INET, SOCK_STREAM, 0); *sock1=sock; if (sock < 0) { perror ("socket (client)"); RTexit (-1); } /* Connect to the server */ init_sockaddr (&servername, hostname, port); if (0 > connect (sock, (struct sockaddr *) &servername,sizeof (servername))) { perror ("connect (client)"); RTexit(0); return (0); } return (0) ; } int server () { int sock_org, sock_cam, sock_new; int hdr[4]; int status, blen = 0, connected = 0; #ifdef DEBUG int *dp[10]; #endif fd_set active_fd_set, read_fd_set; struct sockaddr_in clientname; size_t size; blen=0; /* Create the socket and set it up to accept connections */ sock_org = make_socket (PORT); if (listen (sock_org, 1) < 0) { perror ("listen"); RTexit (-1); } /* Initialize the set of active sockets */ FD_ZERO (&active_fd_set); FD_SET (sock_org, &active_fd_set); while (1) { read_fd_set = active_fd_set; if (select (32, &read_fd_set, 0, 0, 0) <0) { perror ("select"); RTexit (-1); } if (FD_ISSET (sock_org, &read_fd_set)) { size = sizeof (clientname); sock_new = accept (sock_org, (struct sockaddr *) &clientname, &size); if (sock_new < 0) { perror ("accept"); RTexit (-1); } fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port)); if (connected) { hdr[0] = 0; hdr[1] = 8; status = send (sock_new, (char *) hdr, 8, 0); if (status < 0) { perror ("send"); RTexit (-1); } RTclose(sock_new); } else { hdr[0] = 1; hdr[1] = 8; status = send (sock_new, (char *) hdr, 8, 0); if (status < 0) { perror ("send"); RTexit (-1); } sock_cam = sock_new; FD_SET (sock_cam, &active_fd_set); connected = 1; } } else { /* Data ariving on an already-connected socket. */ status = recv (sock_cam, (char *) hdr, 8, 0); if (status < 0) { perror ("recv"); RTexit (-1); } if (status == 0) hdr[0] = 0; #ifdef DEBUG fprintf (stderr, "[%d] Server: Executing: Bytes to receive %d\n", hdr[0], hdr[1]); #endif blen=hdr[1]-8; if (blen > 0) read_tcpip (sock_cam, data, blen); hdr[1]=0; #ifdef DEBUG fprintf (stderr, "[%d] Server: Calling U2F[%d] %d\n", hdr[0], hdr[0],blen); #endif // if (blen > 0) fprintf(stderr, "u2f arguments %d %d\n",((u_int*)data)[0],((u_int*)data)[0] ); if ( hdr[0]< MAXU2FCALLS ) u2f[hdr[0]](blen, data, &hdr[1] ); blen=hdr[1]; hdr[1]+=8; #ifdef DEBUG fprintf (stderr, "[%d] Server: Sending back U2F[%d] %d\n", hdr[0], hdr[0],blen); #endif write_tcpip (sock_cam, (char *) hdr, 8); if (blen>0) write_tcpip (sock_cam, data, blen); if (!hdr[0]){ fprintf (stderr, "Server: disconnecting\n"); RTclose (sock_cam); FD_CLR (sock_cam, &active_fd_set); connected = 0; } #ifdef DEBUG fprintf (stderr, "[%d] Server: Data sent %d bytes\n",hdr[0], blen); #endif } } } //---------------------------------------------------------------------- int SendReceive(int sock, int code, char *data, int *size,int isswap) { int hdr[2] ; fprintf (stderr,"Code [%d] size=%d ->\t",code, *size); hdr[0]=code; hdr[1]=*size+8; fprintf (stderr,"SendHdr:%d %d ->\t",hdr[0],hdr[1]); #ifdef SWAP if (isswap) swap4n(hdr,2); #endif write_tcpip( sock,(char *) hdr,8 ) ; if ( *size > 0 ) { fprintf (stderr,"Send data:%d \t",*size); write_tcpip( sock, data, *size ) ; } // usleep(100000); read_tcpip( sock,(char *) hdr,8 ) ; #ifdef SWAP if (isswap) swap4n(hdr,2); #endif fprintf (stderr,"ReadHdr:%d %d ->\n",hdr[0],hdr[1]); *size= hdr[1]-8; if ( *size > 0 ) { read_tcpip( sock,data, *size) ; fprintf (stderr,"Read Data %d\n",*size); } return hdr[1]-8; } int testclient ( int neve, char * hostname, int ndata) { int code,sock ; int nb,i,j ; int socket[2] ,port[1]={PORT}; int isswap; u_int *udata=(u_int *)data; #ifdef _WINDOWS DoStartUp(); #endif if (strstr(hostname,"f9rt01")!=NULL) isswap=1; else isswap=0; if (isswap) fprintf(stderr,"Data will be swapped!!\n"); for ( i=0;i<8;i++) udata[i]=0xffff ; for ( i=0;i<1;i++) init_soc (hostname,&socket[i],port[i] ) ; sock=socket[0]; fprintf (stderr,"Connected socket=%d\n",sock); j=0; int datasize[5]={0,32,8,4,4}; nb=SendReceive(sock, 1 ,data,&datasize[1],isswap); while (j3) ndata=atoi(argv[3]); if (argc>2) neve =atoi(argv[2]); if (argc>1) testclient(neve,argv[1],ndata); else server(); return 0; } #endif