aboutsummaryrefslogtreecommitdiff
path: root/src/Sockets.c
blob: 19845316fdc59a52bb4eeeff0209a8fe011ab7d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
 /*@@
   @file      Sockets.c
   @date      Wed Sep 13 20:39:15 2000
   @author    Tom Goodale
   @desc 
   Routines which deal with sockets.  
   These should probably move into thorn Socket at some point if
   they aren't already there.
   @enddesc
   @version $Header$
 @@*/

#include "cctk.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif /* HAVE_NETDB_H */
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif /* ARPA_INET_H */
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#endif /* HAVE_WINSOCK2_H */

#include "httpd.h"

static char *rcsid = "$Header$";

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 *********************
 ********************************************************************/

SOCKET HTTP_MakeSocket (unsigned long port);

static int InitialiseTCP(void);

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

/* Active file descriptors */
static fd_set active_fd_set;

/* Main server socket */
static SOCKET sock;
static SOCKET minsock;
static SOCKET maxsock;

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/


 /*@@
   @routine    HTTP_SetupServer
   @date       Wed Sep 13 20:39:15 2000
   @author     Tom Goodale
   @desc 
   Creates a socket to listen on.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
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 (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);

  return 0;
}

 /*@@
   @routine    HTTP_ShutdownServer
   @date       Wed Sep 13 20:39:15 2000
   @author     Tom Goodale
   @desc 
   Closes all sockets we are interested in.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int HTTP_ShutdownServer(void)
{
  unsigned int i;

  /* Close all sockets in our active set */
  for(i = maxsock; i >= minsock; i--)
  {
    if(FD_ISSET(i, &active_fd_set))
    {
      CLOSESOCKET(i);
    }
  }

  return 0;
}

 /*@@
   @routine    HTTP_Poll
   @date       Wed Sep 13 20:39:15 2000
   @author     Tom Goodale
   @desc 
   Main workhorse routine.
   Looks for activity on any of the sockets which are open
   and dispatches work.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int HTTP_Poll(cGH *cctkGH, long sec, long usec)
{
  unsigned int i;

#ifdef HAVE_SOCKLEN_T
  socklen_t size;
#else
  int size;
#endif

  fd_set read_fd_set;

  struct sockaddr_in clientname;
  struct timeval timeout;
  struct timeval *real_timeout;

  if(sec >=0)
  {
    timeout.tv_sec = sec;
    timeout.tv_usec = usec;

    real_timeout = &timeout;
  }
  else
  {
    real_timeout = NULL;
  }

  /* 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)))
  {
    perror ("select");
    CCTK_Abort(cctkGH, EXIT_FAILURE);
  }
    
  /* Service all the sockets with input pending. */
  for (i = minsock; i <= maxsock; ++i)
  {
    if (FD_ISSET (i, &read_fd_set))
    {
      if (i == sock)
      {
        /* Connection request on original socket. */
        SOCKET new;
        size = sizeof (clientname);
        new = accept (sock,
                      (struct sockaddr *) &clientname,
                      &size);
        if (ERROR_CHECK(new))
        {
          perror ("accept");
          CCTK_Abort(cctkGH, EXIT_FAILURE);
        }
        fprintf (stderr,
                 "Server: connect from host %s, port %hd.\n",
                 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)
        {
          CLOSESOCKET(i);
          FD_CLR (i, &active_fd_set);
        }
      }
    }
  }

  return 0;
}

 /*@@
   @routine    HTTP_Write
   @date       Fri Sep 15 18:47:41 2000
   @author     Tom Goodale
   @desc 
   Writes part or all of an HTTP reply.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int HTTP_Write(httpRequest *request, const char *buffer, size_t count)
{
  int retval;
  
  /* Currently don't do anything fancy. */
  retval = send(request->filedes, buffer, count,0);

  return retval;
}

 /*@@
   @routine    HTTP_Read
   @date       Mon Sep 18 10:14:03 2000
   @author     Tom Goodale
   @desc 
   Reads part or all of an HTTP request.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int HTTP_Read(httpRequest *request, char *buffer, size_t count)
{
  int retval;
  
  /* Currently don't do anything fancy. */
  retval = recv(request->filedes, buffer, count,0);

  return retval;
}

/********************************************************************
 *********************     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 

@@*/
int HTTP_MakeSocket (unsigned long port)
{
  int sock;
  struct sockaddr_in name;
  int opt;

  /* Create the socket. */
  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(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#endif
 
  /* Give the socket a name. */
  name.sin_family = AF_INET;
  name.sin_port = htons (port);
  name.sin_addr.s_addr = htonl (INADDR_ANY);
  if (ERROR_CHECK(bind (sock, (struct sockaddr *) &name, sizeof (name))))
  {
    perror ("bind");
    CCTK_Abort(NULL,EXIT_FAILURE);
  }

  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 */