From d01adbf300905ea2935df28759d4b538081de976 Mon Sep 17 00:00:00 2001 From: goodale Date: Mon, 18 Sep 2000 14:22:45 +0000 Subject: Changes necessary for NT. Tom git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPD/trunk@42 1faa4e14-9dd3-4be0-9f0e-ffe519881164 --- src/Sockets.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 14 deletions(-) (limited to 'src/Sockets.c') diff --git a/src/Sockets.c b/src/Sockets.c index 07609cb..f329d5a 100644 --- a/src/Sockets.c +++ b/src/Sockets.c @@ -49,11 +49,29 @@ CCTK_FILEVERSION(DevThorns_httpd_Socket_c) ********************* Local Data Types *********************** ********************************************************************/ +#ifndef SOCKET +#define SOCKET int +#endif + +#ifdef SOCKET_ERROR +#define ERROR_CHECK(a) ((a) == SOCKET_ERROR) +#else +#define ERROR_CHECK(a) ((a) < 0) +#endif + +#ifdef HAVE_WINSOCK2_H +#define CLOSESOCKET(a) closesocket(a) +#else +#define CLOSESOCKET(a) close(a) +#endif + /******************************************************************** ********************* Local Routine Prototypes ********************* ********************************************************************/ -int HTTP_MakeSocket (unsigned long port); +SOCKET HTTP_MakeSocket (unsigned long port); + +static int InitialiseTCP(void); /******************************************************************** ********************* Other Routine Prototypes ********************* @@ -67,7 +85,9 @@ int HTTP_MakeSocket (unsigned long port); static fd_set active_fd_set; /* Main server socket */ -static int sock; +static SOCKET sock; +static SOCKET minsock; +static SOCKET maxsock; /******************************************************************** ********************* External Routines ********************** @@ -90,14 +110,20 @@ static int sock; @@*/ int HTTP_SetupServer(int port, int queue_size) { + /* Some systems need special logic for starting up TCP. */ + InitialiseTCP(); + /* Create the socket and set it up to accept connections. */ sock = HTTP_MakeSocket (port); - if (listen (sock, queue_size) < 0) + if (ERROR_CHECK(listen (sock, queue_size))) { perror ("listen"); exit (EXIT_FAILURE); } + minsock = sock; + maxsock = sock; + /* Initialize the set of active sockets. */ FD_ZERO (&active_fd_set); FD_SET (sock, &active_fd_set); @@ -121,14 +147,14 @@ int HTTP_SetupServer(int port, int queue_size) @@*/ int HTTP_ShutdownServer(void) { - int i; + unsigned int i; /* Close all sockets in our active set */ - for(i = FD_SETSIZE-1; i >=0; i--) + for(i = maxsock; i >= minsock; i--) { if(FD_ISSET(i, &active_fd_set)) { - close(i); + CLOSESOCKET(i); } } @@ -153,7 +179,7 @@ int HTTP_ShutdownServer(void) @@*/ int HTTP_Poll(cGH *cctkGH, long sec, long usec) { - int i; + unsigned int i; #ifdef HAVE_SOCKLEN_T socklen_t size; @@ -182,26 +208,26 @@ int HTTP_Poll(cGH *cctkGH, long sec, long usec) /* Check if any input is available on one or more active sockets. */ read_fd_set = active_fd_set; - if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, &timeout) < 0) + if (ERROR_CHECK(select (FD_SETSIZE, &read_fd_set, NULL, NULL, &timeout))) { perror ("select"); exit (EXIT_FAILURE); } /* Service all the sockets with input pending. */ - for (i = 0; i < FD_SETSIZE; ++i) + for (i = minsock; i <= maxsock; ++i) { if (FD_ISSET (i, &read_fd_set)) { if (i == sock) { /* Connection request on original socket. */ - int new; + SOCKET new; size = sizeof (clientname); new = accept (sock, (struct sockaddr *) &clientname, &size); - if (new < 0) + if (ERROR_CHECK(new)) { perror ("accept"); exit (EXIT_FAILURE); @@ -211,13 +237,22 @@ int HTTP_Poll(cGH *cctkGH, long sec, long usec) inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port)); FD_SET (new, &active_fd_set); + + if(new > maxsock) + { + maxsock = new; + } + if(new < minsock) + { + minsock = new; + } } else { /* Data arriving on an already-connected socket. */ if (HTTP_ReadFromClient (cctkGH, i) < 0) { - close (i); + CLOSESOCKET(i); FD_CLR (i, &active_fd_set); } } @@ -301,7 +336,7 @@ int HTTP_MakeSocket (unsigned long port) /* Create the socket. */ sock = socket (PF_INET, SOCK_STREAM, 0); - if (sock < 0) + if (ERROR_CHECK(sock)) { perror ("socket"); CCTK_Abort(NULL, EXIT_FAILURE); @@ -317,7 +352,7 @@ int HTTP_MakeSocket (unsigned long port) 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) + if (ERROR_CHECK(bind (sock, (struct sockaddr *) &name, sizeof (name)))) { perror ("bind"); CCTK_Abort(NULL,EXIT_FAILURE); @@ -325,3 +360,50 @@ int HTTP_MakeSocket (unsigned long port) return sock; } + + +/****************************************************************************** + ****************************************************************************** + ******************************************************************************/ + +/* Special code for starting up the socket layer. */ + +#ifdef HAVE_WINSOCK2_H +#ifndef _M_IX86 +#define _M_IX86 400 +#endif + +#include +#include +#include + +static int InitialiseTCP(void) +{ + WORD wVersionRequested; + WSADATA wsaData; + int errnumber; + + wVersionRequested = MAKEWORD( 2, 0 ); + + errnumber = WSAStartup( wVersionRequested, &wsaData ); + + if (errnumber) + { + fprintf(stderr, "Couldn't start Windows socket layer\n"); + } + else + { + printf("Windows socket layer initialized."); + } + + return errnumber; +} + +#else + +static int InitialiseTCP(void) +{ + return 0; +} + +#endif /* defined HAVE_WINSOCK2_H */ -- cgit v1.2.3