// Author Johannes Muelmenstadt /* $Id: turbosock.c,v 1.4 2004/03/06 03:06:56 jmuelmen Exp $ */ #ifndef NOT_VC #include #include #include #else #include #include #endif #include #include #include #include // #include // #include // #include #define TURBOSOCK_READ_PORT 14161 #define TURBOSOCK_WRITE_PORT 14162 #define BUFLEN 1024 /* prototypes from John: */ int SetPrimfile (char primfile[]); int ExecutePrimlist(void); static int _init = 0; static int sock; static struct sockaddr_in sn, rn; int turbosock_init () { int err, length; #ifndef NOT_VC WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); #endif /* make a socket */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { #ifndef NOT_VC switch (WSAGetLastError()) { case WSANOTINITIALISED: printf("Not initialized\n"); break; case WSAEAFNOSUPPORT: printf("No support\n"); break; default: printf("Some other error\n"); } #endif perror("opening datagram socket"); exit(1); } /* Create name to receive from. */ rn.sin_family = AF_INET; rn.sin_addr.s_addr = INADDR_ANY; rn.sin_port = htons(TURBOSOCK_READ_PORT); if (bind(sock, (struct sockaddr *)&rn, sizeof(rn))) { #ifndef NOT_VC switch (err = WSAGetLastError()) { case WSANOTINITIALISED: printf("Not initialized\n"); break; case WSAEAFNOSUPPORT: printf("No support\n"); break; default: printf("Some other error\n"); } #endif perror("binding datagram socket"); exit(1); } length = sizeof(rn); if (getsockname(sock, (struct sockaddr *)&rn, &length)) { perror("getting socket name"); exit(1); } /* printf("Socket has port %d... ", ntohs(rn.sin_port)); */ if (ntohs(rn.sin_port) != TURBOSOCK_READ_PORT) { printf("Could not bind to TURBOSOCK_READ_PORT.\n"); return 1; } /* now make a sockaddr_in to send to */ sn.sin_addr.s_addr = inet_addr("127.0.0.1"); sn.sin_family = AF_INET; sn.sin_port = htons(TURBOSOCK_WRITE_PORT); _init = 1; return 0; } int turbosock (void) { int length; char buf[BUFLEN]; int err; if (!_init) turbosock_init(); /* now go into a loop and pass data along to turbodaq */ while ((length = recv(sock, buf, BUFLEN, 0)) > 0) { int ret; buf[length] = 0; /* if the message is "exit", we have to leave the loop */ if (!strncmp(buf, "exit", 4)) { ret = 0; if ((length = sendto(sock, (char *)&ret, sizeof(ret), 0, (struct sockaddr *)&sn, sizeof(sn))) < 0) perror("sending return code datagram"); break; } /* the buffer should contain a primlist file name */ /* printf("Read %s\n", buf); */ fflush(stdout); SetPrimfile(buf); ret = htonl(ExecutePrimlist()); /* send the return back to ambush */ if ((length = sendto(sock, (char *)&ret, sizeof(ret), 0, (struct sockaddr *)&sn, sizeof(sn))) < 0) perror("sending return code datagram"); /* else printf("Sent %d bytes\n", length); */ } return 0; }