aboutsummaryrefslogtreecommitdiff
path: root/src/Sockets.c
diff options
context:
space:
mode:
authorgoodale <goodale@1faa4e14-9dd3-4be0-9f0e-ffe519881164>2000-09-18 14:22:45 +0000
committergoodale <goodale@1faa4e14-9dd3-4be0-9f0e-ffe519881164>2000-09-18 14:22:45 +0000
commitd01adbf300905ea2935df28759d4b538081de976 (patch)
tree45e607f6eb1d527878ba596efd40620b700eacdc /src/Sockets.c
parent6fa64fcbdd789e74fbe2661728110a2a0c9486b1 (diff)
Changes necessary for NT.
Tom git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPD/trunk@42 1faa4e14-9dd3-4be0-9f0e-ffe519881164
Diffstat (limited to 'src/Sockets.c')
-rw-r--r--src/Sockets.c110
1 files changed, 96 insertions, 14 deletions
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 <sys/types.h>
+#include <windows.h>
+#include <stdio.h>
+
+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 */