// ~UDP_Socket(): // destructor UDP_Socket::~UDP_Socket() { #ifdef _WIN32 closesocket(sockfd); #else close(sockfd); #endif // cleanup sockets cleanup_sockets(); }
void SocketSystemShutDown(void) { #if defined(SOCKETLIB) cleanup_sockets(); CloseLibrary(SockBase); #elif defined(BSDSOCKETLIB) CloseLibrary(SocketBase); #elif defined(WIN32) WSACleanup(); #endif }
void my_abort(int sig ) { #ifdef WIN32 /* clean up nicely */ timeKillEvent(mmID); #endif if(fp) { fprintf(fp, "%s\n%s\n%d\n", Output[0], Output[1], proxies); fclose(fp); } cleanup_sockets(); exit(0); }
static void AmigaCleanup(void) { #ifdef AMITCP if(UserGroupBase) { CloseLibrary(UserGroupBase); UserGroupBase=NULL; } if(SocketBase) { CloseLibrary(SocketBase); SocketBase=NULL; } #endif #ifdef INET225 if(SockBase) { cleanup_sockets(); CloseLibrary(SockBase); SockBase = NULL; } #endif if(from_WB) Delay(112); // small exit delay before closing WB window }
int __cdecl #else int #endif httpsSEND_File(const char *URL, const char *page, const char *cookie, const char *contentType, const char *filename, unsigned int port, bool returnHeader, bool doPOST, char **resPtr) { int i, j, nleft, nwrite, sizeData, ok; char *gMsg, *data, *oldData, *nextData; char outBuf[OUTBUF_SIZE], inBuf[OUTBUF_SIZE]; socket_type sockfd; struct stat statResults; FILE *fp; SSL *ssl; SSL_CTX *ctx; SSL_METHOD *client_method; int err; // in case we fail, set return pointer to 0 *resPtr = 0; // get filesize int fileSize = 0; if (stat(filename, &statResults) == 0) fileSize = statResults.st_size; if (fileSize == 0) { fprintf(stderr, "file %s has 0 fileSize; either no data or file does not exist!\n", filename); return -1; } fp = fopen(filename,"rb"); if (fp == 0) { fprintf(stderr, "cannot open file %s for reading - is it still open for writing!\n", filename); return -1; } /* * init SSL library * code taken from O'Reilly book: 'http: the definitive guide' */ SSLeay_add_ssl_algorithms(); client_method = SSLv2_client_method(); SSL_load_error_strings(); ctx = SSL_CTX_new(client_method); /* end of code taken from http: the definitive guide */ // invoke startup sockets startup_sockets(); // open a socket sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "postData: failed to establis connection\n"); return -1; } /* * SSL handshake * code taken from O'Reilly book: 'http: the definitive guide' */ ssl = SSL_new(ctx); SSL_set_fd(ssl, sockfd); err = SSL_connect(ssl); /* end of code taken from http: the definitive guide */ if (doPOST == true) sprintf(outBuf, "POST %s HTTP/1.1\nHost: %s\n", page, URL); else sprintf(outBuf, "PUT %s HTTP/1.1\nHost: %s\n", page, URL); if (cookie != 0) strcat(outBuf, cookie); if (contentType == 0) strcat(outBuf, "Content-Type:text/plain\n"); else { sprintf(inBuf, "Content-Type: %s\n", contentType); strcat(outBuf, inBuf); } sprintf(inBuf, "Content-Length: %d\n\n", fileSize); strcat(outBuf, inBuf); // strcat(outBuf, dataToPost); nleft = strlen(outBuf); //send the heading // if o.k. get a ponter to the data in the message and // place the incoming data there nwrite = 0; gMsg = outBuf; while (nleft > 0) { nwrite = SSL_write(ssl, gMsg, nleft); nleft -= nwrite; gMsg += nwrite; } int done = 0; while (done == 0) { nleft = fread((void *)outBuf, 1, OUTBUF_SIZE, fp); gMsg = outBuf; if (nleft < OUTBUF_SIZE) done = 1; while (nleft > 0) { nwrite = SSL_write(ssl, gMsg, nleft); nleft -= nwrite; gMsg += nwrite; } if (feof(fp) != 0) done = 1; } fclose(fp); // err = SSL_write(ssl, outBuf, nleft); ok = 1; nleft = 4095; sizeData = 0; oldData = 0; data = 0; while (ok > 0) { gMsg = inBuf; ok = SSL_read(ssl, gMsg, nleft); if (ok > 0) { oldData = data; data = (char *)malloc((sizeData+ok+1)*sizeof(char)); if (data != 0) { if (oldData != 0) { for (i=0; i<sizeData; i++) data[i]=oldData[i]; free(oldData); } for (i=0, j=sizeData; i<ok; i++, j++) data[j]=inBuf[i]; sizeData += ok; strcpy(&data[sizeData],""); } } if (ok < 4095) ok = 0; } // now we need to strip off the response header if (returnHeader == false) { oldData = data; gMsg = data; nextData = strstr(data,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); data = (char *)malloc((sizeData+1)*sizeof(char)); for (i=0; i<nwrite; i++) data[i]=nextData[i]; free(oldData); // strcpy(&data[nwrite],""); /we already placed a end-of-string marker there above } } *resPtr = data; /* * shut-down ssl, close socket & free related memory */ SSL_shutdown(ssl); #ifdef _WIN32 closesocket(sockfd); #else close(sockfd); #endif SSL_free(ssl); SSL_CTX_free(ctx); cleanup_sockets(); return 0; }
int __cdecl #else int #endif httpsGET_File(char const *URL, char const *page, const char *cookie, unsigned int port, const char *filename) { int nleft, sizeData, ok; char *gMsg, *data, *nextData; char outBuf[OUTBUF_SIZE], inBuf[OUTBUF_SIZE]; socket_type sockfd; FILE *fp; SSL *ssl; SSL_CTX *ctx; SSL_METHOD *client_method; int err; /* ****************************************************** * init SSL library from http: the definitive guide * **************************************************** */ SSLeay_add_ssl_algorithms(); client_method = SSLv2_client_method(); SSL_load_error_strings(); ctx = SSL_CTX_new(client_method); /* ********************* from http:the definitive guide */ // invoke startup sockets startup_sockets(); // open a socket sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "postData: failed to establis connection\n"); return -1; } /* ****************************************************** * init SSL handshake from http: the definitive guide * **************************************************** */ ssl = SSL_new(ctx); SSL_set_fd(ssl, sockfd); err = SSL_connect(ssl); /* ********************* from http:the definitive guide */ sprintf(outBuf, "GET %s HTTP/1.1\nHost:%s\n",page,URL); if (cookie != 0) strcat(outBuf, cookie); strcat(outBuf, "Connection:close\n\n"); nleft = strlen(outBuf); err = SSL_write(ssl, outBuf, nleft); ok = 1; nleft = 4095; sizeData = 0; nextData = 0; data = 0; // // open file for writing fp = fopen(filename,"wb"); if (fp == 0) { fprintf(stderr, "cannot open file %s for reading - is it still open for writing!\n", filename); return -1; } while (ok > 0) { gMsg = inBuf; ok = SSL_read(ssl, gMsg, nleft); if (ok < 0) fwrite((void *)gMsg, 1, nleft, fp); /* fprintf(stderr,"\n\nREAD %d\n", nleft); for (int i=0; i<nleft; i++) fprintf(stderr,"%c", inBuf[i]); */ /* if (ok > 0) { // now we need to strip off the response header if (headerStripped == false) { gMsg = data; nextData = strstr(data,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); fwrite((void *)nextData, 1, nwrite, fp); headerStripped = true; } } else { fwrite((void *)gMsg, 1, nleft, fp); } } */ } fclose(fp); cleanup_sockets(); return 0; }
int __cdecl #else int #endif httpsGet(char const *URL, char const *page, char const *cookie, unsigned int port, char **dataPtr) { int i, j, nleft, nwrite, sizeData, ok; char *gMsg, *data, *nextData; char outBuf[OUTBUF_SIZE], inBuf[OUTBUF_SIZE]; socket_type sockfd; SSL *ssl; SSL_CTX *ctx; SSL_METHOD *client_method; int err; /* ****************************************************** * init SSL library from http: the definitive guide * **************************************************** */ SSLeay_add_ssl_algorithms(); client_method = SSLv2_client_method(); SSL_load_error_strings(); ctx = SSL_CTX_new(client_method); /* ********************* from http:the definitive guide */ // in case we fail, set return pointer to 0 *dataPtr = 0; // invoke startup sockets startup_sockets(); // open a socket sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "postData: failed to establis connection\n"); return -1; } /* ****************************************************** * init SSL handshake from http: the definitive guide * **************************************************** */ ssl = SSL_new(ctx); SSL_set_fd(ssl, sockfd); err = SSL_connect(ssl); /* ********************* from http:the definitive guide */ sprintf(outBuf, "GET %s HTTP/1.1\nHost:%s\n",page,URL); if (cookie != 0) strcat(outBuf, cookie); strcat(outBuf, "Connection:close\n\n"); nleft = strlen(outBuf); err = SSL_write(ssl, outBuf, nleft); ok = 1; nleft = 4095; sizeData = 0; nextData = 0; data = 0; while (ok > 0) { gMsg = inBuf; ok = SSL_read(ssl, gMsg, nleft); if (ok > 0) { nextData = data; data = (char *)malloc((sizeData+ok+1)*sizeof(char)); if (data != 0) { if (nextData != 0) { for (i=0; i<sizeData; i++) data[i]=nextData[i]; free(nextData); } for (i=0, j=sizeData; i<ok; i++, j++) data[j]=inBuf[i]; sizeData += ok; strcpy(&data[sizeData],""); } } } // now we need to strip off the response header gMsg = data; nextData = strstr(data,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); data = (char *)malloc((sizeData+1)*sizeof(char)); for (i=0; i<nwrite; i++) data[i]=nextData[i]; // strcpy(&data[nwrite],""); /we already placed a end-of-string marker there above } *dataPtr = data; SSL_shutdown(ssl); #ifdef _WIN32 closesocket(sockfd); #else close(sockfd); #endif SSL_free(ssl); SSL_CTX_free(ctx); cleanup_sockets(); return 0; }
int __cdecl #else int #endif httpGET_File(char const *URL, char const *page, unsigned int port, const char *filename) { int nleft, nwrite, sizeData, ok; char *gMsg, *data, *nextData; char outBuf[OUTBUF_SIZE], inBuf[OUTBUF_SIZE]; socket_type sockfd; FILE *fp = 0; fprintf(stderr, "httpGetFile URL: %s page %s\n", URL, page); // invoke startup sockets startup_sockets(); // open a socket sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "postData: failed to establis connection\n"); return -1; } sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "httpGet: failed to establis connection\n"); return -1; } // add the header information to outBuf sprintf(outBuf, "GET /%s HTTP/1.1\nHost:%s\n", page, URL); strcat(outBuf,"Keep-Alive:300\n"); strcat(outBuf, "Connection:keep-alive\n\n"); nleft = strlen(outBuf); //send the data // if o.k. get a ponter to the data in the message and // place the incoming data there nwrite = 0; gMsg = outBuf; while (nleft > 0) { nwrite = send(sockfd, gMsg, nleft, 0); nleft -= nwrite; gMsg += nwrite; } ok = 1; nleft = 4095; sizeData = 0; nextData = 0; data = 0; // // open file for writing // int fileOpened = 0; bool headerStripped = false; while (ok > 0) { gMsg = inBuf; ok = recv(sockfd, gMsg, nleft, 0); fprintf(stderr, "ok %d nleft %d\n", ok, nleft); if (ok > 0) { // now we need to strip off the response header nextData = strstr(gMsg, "Bad"); if (nextData != NULL) { fprintf(stderr, "Bad Request\n"); return -1; } if (fileOpened == 0) { fp = fopen(filename,"wb"); if (fp == 0) { fprintf(stderr, "cannot open file %s for reading - is it still open for writing!\n", filename); return -1; } else fileOpened = 1; } if (headerStripped == false) { gMsg = inBuf; nextData = strstr(gMsg,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); fwrite((void *)nextData, 1, nwrite, fp); headerStripped = true; } } else { fwrite((void *)gMsg, 1, nleft, fp); } } } fprintf(stderr,"DONE\n"); if (fileOpened == 1) fclose(fp); cleanup_sockets(); return 0; }
int __cdecl #else int #endif httpGet(char const *URL, char const *page, unsigned int port, char **dataPtr) { int i, j, nleft, nwrite, sizeData, ok; char *gMsg, *data, *nextData; socket_type sockfd; // in case we fail, set return pointer to 0 *dataPtr = 0; startup_sockets(); sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "httpGet: failed to establis connection\n"); return -1; } // add the header information to outBuf sprintf(outBuf, "GET %s HTTP/1.1\nHost:%s\n",page,URL); strcat(outBuf,"Accept:text/xml,text/html\n"); strcat(outBuf,"Accept-Language:en-us,en\n"); strcat(outBuf,"Accept-Charset:ISO-8859-1,utf-8\n"); strcat(outBuf,"Keep-Alive:300\n"); strcat(outBuf, "Connection:keep-alive\n\n"); nleft = strlen(outBuf); //send the data // if o.k. get a ponter to the data in the message and // place the incoming data there nwrite = 0; gMsg = outBuf; while (nleft > 0) { nwrite = send(sockfd, gMsg, nleft, 0); nleft -= nwrite; gMsg += nwrite; } ok = 1; nleft = 4095; sizeData = 0; nextData = 0; data = 0; while (ok > 0) { gMsg = inBuf; ok = recv(sockfd, gMsg, nleft, 0); inBuf[ok+1]='\0'; if (ok > 0) { nextData = data; data = (char *)malloc((sizeData+ok+1)*sizeof(char)); if (data != 0) { if (nextData != 0) { for (i=0; i<sizeData; i++) data[i]=nextData[i]; free(nextData); } for (i=0, j=sizeData; i<ok; i++, j++) data[j]=inBuf[i]; sizeData += ok; strcpy(&data[sizeData],""); } } if (strstr(inBuf,"</html>") != NULL) ok = 0; } if (sizeData == 0) { if (lastURL != 0) free(lastURL); lastURL = 0; #ifdef _WIN32 closesocket(sockfd); #else close(sockfd); #endif return -1; } // now we need to strip off the response header gMsg = data; nextData = strstr(data,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); data = (char *)malloc((sizeData+1)*sizeof(char)); for (i=0; i<nwrite; i++) data[i]=nextData[i]; } *dataPtr = data; free(gMsg); #ifdef _WIN32 closesocket(sockfd); #else close(sockfd); #endif sockfd = 0; cleanup_sockets(); return 0; }
main(int argc, char *argv[]) { char ipaddr[80], filename[127]; int oport, iport; int return_code = 0; char buf[80]; struct hostent *h; struct in_addr in; /* initialize these now in case the 20 second timer runs out */ strcpy(Output[0], "no_port"); strcpy(Output[1], "UNKNOWN"); if(argc != 4) { fprintf(stderr, "auth v6.2\n"); fprintf(stderr, "Syntax: %s ip_address oport iport\n", argv[0]); exit(0); } init_sockets(); set_abort_timer(20); sprintf(filename, "%s/auth/lookup.%d", get_log_path(), getpid()); fp = fopen(filename, "w"); strcpy(ipaddr, argv[1]); oport = atoi(argv[2]); iport = atoi(argv[3]); in.s_addr = inet_addr(ipaddr); /* Perform the host name lookup on the IP address*/ h = gethostbyaddr((char *)&in, sizeof(in), AF_INET); if(!h) strcpy(Output[1], "UNKNOWN"); else strcpy(Output[1], h->h_name); if( strncmp(ipaddr, "127.0", 5)) { if(check_proxies(ipaddr, 1080)) { proxies = 1; } else if(check_proxies(ipaddr, 8088)) { proxies = 1; } else if(check_proxies(ipaddr, 23)) { proxies = 1; } } /* Begin the user id lookup */ if(proxies != 1) { if(start_auth(&in, oport, iport, buf)) strcpy(Output[0], buf); else strcpy(Output[0], "no_port"); } /* Save the results to a file so frp can look them up */ if(fp) { fprintf(fp, "%s\n%s\n%d\n", Output[0], Output[1], proxies); fclose(fp); } else { return_code = -1; } #ifdef WIN32 timeKillEvent(mmID); #endif cleanup_sockets(); return(return_code); }
void amiga_sockexit() { cleanup_sockets(); CloseLibrary(SockBase); SockBase = NULL; }
static void test_gpoll (void) { SOCKET sockets[NUM_POLLEES]; GPollFD fds[NUM_POLLFDS]; SOCKET opp_sockets[NUM_POLLEES]; gint i; gint activatable; gint64 times[REPEAT][2]; #define BUCKET_COUNT 25 gint64 bucket_limits[BUCKET_COUNT] = {3, 5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 70, 80, 90, 100, 120, 150, 180, 220, 280, 350, 450, 600, 800, 1000}; gint buckets[BUCKET_COUNT]; gint64 times_avg = 0, times_min = G_MAXINT64, times_max = 0; prepare_sockets (sockets, opp_sockets, fds, NUM_POLLEES); prepare_fds (sockets, fds, NUM_POLLEES); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); for (i = 0; i < REPEAT; i++) { gint r; gint64 diff; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 0); times[i][1] = g_get_monotonic_time (); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("\nempty poll time:\n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); activatable = 0; for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; MSG msg; gboolean found_app; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = send (opp_sockets[activatable], (const char *) &t, 1, 0); g_assert (PostMessage (NULL, WM_APP, 1, 2)); /* This is to ensure that all sockets catch up, otherwise some might not poll active */ g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); v = recv (sockets[activatable], (char *) &t, 1, 0); found_app = FALSE; while (!found_app && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) if (msg.message == WM_APP && msg.wParam == 1 && msg.lParam == 2) found_app = TRUE; g_assert (s == 1); g_assert (r == 2); g_assert (v == 1); g_assert (found_app); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; activatable = (activatable + 1) % NUM_POLLEES; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("1-socket + msg poll time:\n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); activatable = 0; for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = send (opp_sockets[activatable], (const char *) &t, 1, 0); g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); v = recv (sockets[activatable], (char *) &t, 1, 0); g_assert (s == 1); g_assert (r == 1); g_assert (v == 1); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; activatable = (activatable + 1) % NUM_POLLEES; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("1-socket poll time:\n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; gint j; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = v = 0; for (j = 0; j < NUM_POLLEES / 2; j++) s += send (opp_sockets[j], (const char *) &t, 1, 0) == 1 ? 1 : 0; g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); for (j = 0; j < NUM_POLLEES / 2; j++) v += recv (sockets[j], (char *) &t, 1, 0) == 1 ? 1 : 0; g_assert (s == NUM_POLLEES / 2); g_assert (r == NUM_POLLEES / 2); g_assert (v == NUM_POLLEES / 2); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("half-socket poll time:\n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; gint j; MSG msg; gboolean found_app; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = v = 0; for (j = 0; j < NUM_POLLEES / 2; j++) s += send (opp_sockets[j], (const char *) &t, 1, 0) == 1 ? 1 : 0; g_assert (PostMessage (NULL, WM_APP, 1, 2)); /* This is to ensure that all sockets catch up, otherwise some might not poll active */ g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); for (j = 0; j < NUM_POLLEES / 2; j++) v += recv (sockets[j], (char *) &t, 1, 0) == 1 ? 1 : 0; found_app = FALSE; while (!found_app && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) if (msg.message == WM_APP && msg.wParam == 1 && msg.lParam == 2) found_app = TRUE; g_assert (s == NUM_POLLEES / 2); g_assert (r == NUM_POLLEES / 2 + 1); g_assert (v == NUM_POLLEES / 2); g_assert (found_app); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("half-socket + msg poll time:\n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; gint j; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = v = 0; for (j = 0; j < NUM_POLLEES; j++) s += send (opp_sockets[j], (const char *) &t, 1, 0) == 1 ? 1 : 0; g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); for (j = 0; j < NUM_POLLEES; j++) v += recv (sockets[j], (char *) &t, 1, 0) == 1 ? 1 : 0; g_assert (s == NUM_POLLEES); g_assert (r == NUM_POLLEES); g_assert (v == NUM_POLLEES); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); } times_avg /= NUM_POLLEES; g_print ("%d-socket poll time: \n%4lldns - %4lldns, average %4lldns\n", NUM_POLLEES, times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); activatable = 0; times_avg = 0; times_min = G_MAXINT64; times_max = 0; memset (buckets, 0, sizeof (gint) * BUCKET_COUNT); for (i = 0; i < REPEAT; i++) { gint r, s, v, t; gint64 diff; gint j; MSG msg; gboolean found_app; reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); s = v = 0; for (j = 0; j < activatable; j++) s += send (opp_sockets[j], (const char *) &t, 1, 0) == 1 ? 1 : 0; g_assert (PostMessage (NULL, WM_APP, 1, 2)); g_usleep (G_USEC_PER_SEC / 1000); times[i][0] = g_get_monotonic_time (); r = g_poll (fds, NUM_POLLFDS, 1000); times[i][1] = g_get_monotonic_time (); check_fds (sockets, fds, NUM_POLLEES); for (j = 0; j < activatable; j++) v += recv (sockets[j], (char *) &t, 1, 0) == 1 ? 1 : 0; found_app = FALSE; while (!found_app && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) if (msg.message == WM_APP && msg.wParam == 1 && msg.lParam == 2) found_app = TRUE; g_assert (s == activatable); g_assert (r == activatable + 1); g_assert (v == activatable); g_assert (found_app); reset_fds (fds, NUM_POLLEES); reset_fds_msg (fds, NUM_POLLFDS); r = g_poll (fds, NUM_POLLFDS, 0); check_fds (sockets, fds, NUM_POLLEES); g_assert (r == 0); diff = times[i][1] - times[i][0]; if (times_min > diff) times_min = diff; if (times_max < diff) times_max = diff; times_avg += diff; bucketize (diff, buckets, bucket_limits, BUCKET_COUNT); activatable = (activatable + 1) % NUM_POLLEES; } times_avg /= NUM_POLLEES; g_print ("variable socket number + msg poll time: \n%4lldns - %4lldns, average %4lldns\n", times_min, times_max, times_avg); print_buckets (buckets, bucket_limits, BUCKET_COUNT); cleanup_sockets (sockets, opp_sockets, NUM_POLLEES); }
int httpGet(const char *URL, const char *page, unsigned int port, char **dataPtr) { int i, j, nleft, nwrite, sizeData, ok; char *gMsg, *data, *nextData; char outBuf[4096], inBuf[4096]; socket_type sockfd; // in case we fail, set return pointer to 0 *dataPtr = 0; // invoke startup sockets startup_sockets(); // open a socket sockfd = establishHTTPConnection(URL, port); if (sockfd < 0) { fprintf(stderr, "postData: failed to establis connection\n"); return -1; } sprintf(outBuf, "GET %s HTTP/1.1\nHost:%s\n",page,URL); strcat(outBuf, "Connection:close\n\n"); nleft = strlen(outBuf); //send the data // if o.k. get a ponter to the data in the message and // place the incoming data there nwrite = 0; gMsg = outBuf; while (nleft > 0) { nwrite = send(sockfd, gMsg, nleft, 0); nleft -= nwrite; gMsg += nwrite; } ok = 1; nleft = 4095; sizeData = 0; nextData = 0; data = 0; while (ok > 0) { gMsg = inBuf; ok = recv(sockfd, gMsg, nleft, 0); if (ok > 0) { nextData = data; data = (char *)malloc((sizeData+ok+1)*sizeof(char)); if (data != 0) { if (nextData != 0) { for (i=0; i<sizeData; i++) data[i]=nextData[i]; free(nextData); } for (i=0, j=sizeData; i<ok; i++, j++) data[j]=inBuf[i]; sizeData += ok; strcpy(&data[sizeData],""); } } } // now we need to strip off the response header gMsg = data; nextData = strstr(data,"Content-Type"); if (nextData != NULL) { nextData = strchr(nextData,'\n'); nextData += 3; nwrite = sizeData+1-(nextData-data); data = (char *)malloc((sizeData+1)*sizeof(char)); for (i=0; i<nwrite; i++) data[i]=nextData[i]; // strcpy(&data[nwrite],""); /we already placed a end-of-string marker there above } *dataPtr = data; cleanup_sockets(); return 0; }
void startup_mordor(void) { int i; /* static void mvc_log(); */ /* paths are now configurable in mordor.cf */ /* but not read i during the normal readcf */ /* to avoid problems when reloading the .cf file */ load_paths(); init_guild(); i=readcf(); #ifndef _DEBUG if(CRASHTRAP){ signal(SIGABRT, crash); /* abnormal termination triggered by abort call */ signal(SIGFPE, crash); /* floating point exception */ signal(SIGILL, crash); /* illegal instruction - invalid function image */ signal(SIGSEGV, crash); /* segment violation */ } #endif /* clear the spy flags */ clear_spy(); /* clear the player array */ zero( Ply, sizeof(plystruct) * PMAX ); if(AUTOSHUTDOWN) if (!Shutdown.interval){ Shutdown.ltime = time(0); Shutdown.interval = 43200L; } umask(000); srand(getpid() + time(0)); load_lockouts(); sock_init(PORTNUM); #ifndef DEBUG #ifndef WIN32 if(fork()) exit(0); /* go into background */ #else FreeConsole(); #endif close(0); close(1); close(2); /* close stdio */ #endif /* DEBUG */ if(RECORD_ALL) mvc_log(); StartTime = time(0); sprintf(g_buffer, "--- Game Up: %u --- [%s]\n", (int)PORTNUM, VERSION ); elog_broad(g_buffer); plog(g_buffer); /* function to catch exit calls */ atexit(log_exit); sock_loop(); cleanup_sockets(); return; }