aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortradke <tradke@1faa4e14-9dd3-4be0-9f0e-ffe519881164>2002-05-14 19:15:41 +0000
committertradke <tradke@1faa4e14-9dd3-4be0-9f0e-ffe519881164>2002-05-14 19:15:41 +0000
commit912e58d9572387be082d80debe89c2df6d8f1362 (patch)
tree1b76dc9bd2cfe05364461cf9f88cfbfd174f5469
parent97951e409ead94523f6d0b3017715131ea90c081 (diff)
Moved some functionality over into thorn Socket. Now that HTTPD inherits from
this thorn, you need to add it to your thornlist (in addition to HTTPD). git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPD/trunk@164 1faa4e14-9dd3-4be0-9f0e-ffe519881164
-rw-r--r--src/Sockets.c221
1 files changed, 27 insertions, 194 deletions
diff --git a/src/Sockets.c b/src/Sockets.c
index caad22a..872d4b6 100644
--- a/src/Sockets.c
+++ b/src/Sockets.c
@@ -13,6 +13,7 @@
#include "cctk.h"
#include "cctk_Parameters.h"
#include "util_Network.h"
+#include "SocketUtils.h"
#include <stdio.h>
#include <stdlib.h>
@@ -45,8 +46,6 @@
#endif /* HAVE_WINSOCK2_H */
#include <errno.h>
-#include "util_Network.h"
-
#include "httpd.h"
static const char *rcsid = "$Header$";
@@ -56,23 +55,6 @@ CCTK_FILEVERSION(CactusConnect_HTTPD_Sockets_c)
/********************************************************************
********************* Local Data Types ***********************
********************************************************************/
-
-#ifndef HAVE_SOCKET_TYPE
-#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
-
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
@@ -91,14 +73,10 @@ typedef struct HTTPSocket
********************* Local Routine Prototypes *********************
********************************************************************/
-SOCKET HTTP_MakeSocket (unsigned long port, int *hunt);
-
static httpSocket *SocketCreate(unsigned long int filedes);
static void SocketDestroy(httpSocket *this);
static void SocketClose(httpSocket *this);
-static int InitialiseTCP(void);
-
/********************************************************************
********************* Other Routine Prototypes *********************
********************************************************************/
@@ -140,21 +118,19 @@ static unsigned long int httpport = 0;
int HTTP_SetupServer(int port, int queue_size, int hunt)
{
char hostname[1025];
- int realport;
+ unsigned int realport;
/* Some systems need special logic for starting up TCP. */
- InitialiseTCP();
+ Socket_InitializeSocketLayer ();
/* Create the socket and set it up to accept connections. */
- sock = HTTP_MakeSocket (port, hunt ? &realport : NULL);
-
- if (ERROR_CHECK(listen (sock, queue_size)))
+ sock = Socket_TCPOpenServerSocket (port, hunt ? &realport : NULL, queue_size);
+ if (sock == INVALID_SOCKET)
{
- perror ("listen");
+ fprintf (stderr, "Failed to create server socket\n");
exit (EXIT_FAILURE);
}
-
Util_GetHostName(hostname, 1024);
httpport = hunt ? realport : port;
@@ -202,7 +178,7 @@ int HTTP_ShutdownServer(void)
{
if(FD_ISSET(i, &active_fd_set))
{
- CLOSESOCKET(i);
+ Socket_CloseSocket (i);
}
}
@@ -232,34 +208,21 @@ int HTTP_Poll(cGH *cctkGH, long sec, long usec)
#else
int size;
#endif
-
+ SOCKET new;
fd_set read_fd_set;
-
struct sockaddr_in clientname;
struct timeval timeout;
- struct timeval *real_timeout;
-
- httpSocket *this;
- httpSocket *next;
- DECLARE_CCTK_PARAMETERS
+ httpSocket *this, *next;
- if(sec >=0)
- {
- timeout.tv_sec = sec;
- timeout.tv_usec = usec;
-
- real_timeout = &timeout;
- }
- else
- {
- real_timeout = NULL;
- }
+ timeout.tv_sec = sec;
+ timeout.tv_usec = usec;
/* Check if any input is available on one or more active sockets. */
read_fd_set = active_fd_set;
- if (ERROR_CHECK(select (FD_SETSIZE, &read_fd_set, NULL, NULL, real_timeout)))
+ if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, sec >= 0 ? &timeout : NULL)
+ == SOCKET_ERROR)
{
perror ("select");
CCTK_Abort(cctkGH, EXIT_FAILURE);
@@ -269,19 +232,17 @@ int HTTP_Poll(cGH *cctkGH, long sec, long usec)
if (FD_ISSET (sock, &read_fd_set))
{
/* Connection request on original socket. */
- SOCKET new;
size = sizeof (clientname);
- new = accept (sock,
- (struct sockaddr *) &clientname,
- &size);
- if (ERROR_CHECK(new))
+ new = accept (sock, (struct sockaddr *) &clientname, &size);
+ if (new == INVALID_SOCKET)
{
perror ("accept");
/*CCTK_Abort(cctkGH, EXIT_FAILURE);*/
}
else
{
- if (verbose)
+ if (*(const CCTK_INT *) CCTK_ParameterGet ("verbose", CCTK_THORNSTRING,
+ NULL))
{
CCTK_VInfo (CCTK_THORNSTRING,
"Server: connect from host %s, port %hd",
@@ -295,20 +256,18 @@ int HTTP_Poll(cGH *cctkGH, long sec, long usec)
for(this = socklist; this; this = next)
{
next = this->next;
- if(FD_ISSET (this->filedes, &read_fd_set))
+ /* Data arriving on an already-connected socket. */
+ if (FD_ISSET (this->filedes, &read_fd_set) &&
+ HTTP_ReadFromClient (cctkGH, this) < 0)
{
- /* Data arriving on an already-connected socket. */
- if (HTTP_ReadFromClient (cctkGH, this) < 0)
- {
- SocketDestroy(this);
- }
+ SocketDestroy(this);
}
}
-
- return 0;
+ return (0);
}
+
/*@@
@routine HTTP_Write
@date Fri Sep 15 18:47:41 2000
@@ -351,7 +310,7 @@ int HTTP_Write(httpRequest *request, const char *buffer, size_t count)
MSG_NOSIGNAL);
/* Check for errors */
- if(ERROR_CHECK(retval))
+ if(retval < 0)
{
switch(errno)
{
@@ -423,11 +382,11 @@ int HTTP_Write(httpRequest *request, const char *buffer, size_t count)
bytes_sent += retval;
}
- if(!(ERROR_CHECK(retval)) && bytes_sent < count)
+ if(retval >= 0 && bytes_sent < count)
{
/* Wait until can write */
FD_SET (connection->filedes, &this_set);
- if (ERROR_CHECK(select (FD_SETSIZE, NULL, &this_set, NULL, NULL)))
+ if (select (FD_SETSIZE, NULL, &this_set, NULL, NULL) < 0)
{
perror ("select");
CCTK_Abort(NULL, EXIT_FAILURE);
@@ -519,85 +478,6 @@ unsigned long int HTTP_Port(void)
/********************************************************************
********************* Local Routines *************************
********************************************************************/
-
- /*@@
- @routine HTTP_MakeSocket
- @date Wed Sep 13 20:39:15 2000
- @author Tom Goodale
- @desc
- Creates a socket.
- @enddesc
- @calls
- @calledby
- @history
-
- @endhistory
-
-@@*/
-SOCKET HTTP_MakeSocket (unsigned long port, int *hunt)
-{
- SOCKET this_sock;
- struct sockaddr_in name;
- int opt;
- int done;
- int realport;
- int error;
-
- /* Create the socket. */
- this_sock = socket (PF_INET, SOCK_STREAM, 0);
- if (ERROR_CHECK(sock))
- {
- perror ("socket");
- CCTK_Abort(NULL, EXIT_FAILURE);
- }
-
- /* Try to reuse the port if possible */
-#ifdef SO_REUSEADDR
- opt = 1;
- setsockopt(this_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
-#endif
-
- done = 0;
- realport = port;
-
- while(!done)
- {
- /* Give the socket a name. */
- name.sin_family = AF_INET;
- name.sin_port = htons (realport);
- name.sin_addr.s_addr = htonl (INADDR_ANY);
- if (ERROR_CHECK(error = bind (this_sock, (struct sockaddr *) &name, sizeof (name))))
- {
- if(hunt)
- {
- /* Hunt for a new port */
- fprintf(stderr, "Port %u taken, trying %u\n", realport, realport+1);
- realport++;
- }
- else
- {
- done = 1;
- }
- }
- else
- {
- done = 1;
- }
- }
-
- if(ERROR_CHECK(error))
- {
- perror ("bind");
- CCTK_Abort(NULL,EXIT_FAILURE);
- }
- else if(hunt)
- {
- *hunt = realport;
- }
-
- return this_sock;
-}
-
/*@@
@routine SocketCreate
@date Thu Sep 21 15:03:32 2000
@@ -699,56 +579,9 @@ static void SocketClose(httpSocket *this)
{
if(this->state == open)
{
- CLOSESOCKET(this->filedes);
+ Socket_CloseSocket (this->filedes);
this->state=closed;
FD_CLR (this->filedes, &active_fd_set);
}
}
}
-
-
-/******************************************************************************
- ******************************************************************************
- ******************************************************************************/
-
-/* 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.\n");
- }
-
- return errnumber;
-}
-
-#else
-
-static int InitialiseTCP(void)
-{
- return 0;
-}
-
-#endif /* defined HAVE_WINSOCK2_H */