- cleaned up unused library functions for codemux
[codemux.git] / codemux.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/stat.h>
4 #include <sys/wait.h>
5 #include <netinet/in.h>
6 #include <arpa/inet.h>
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include "codemuxlib.h"
17
18 #define CONF_FILE "/etc/codemux/codemux.conf"
19 #define DEMUX_PORT 80
20 #define REAL_WEBSERVER_CONFLINE "* root 1080"
21 #define TARG_SETSIZE 4096
22
23 /* set aside some small number of fds for us, allow the rest for
24    connections */
25 #define MAX_CONNS ((TARG_SETSIZE-20)/2)
26
27 /* no single service can take more than half the connections */
28 #define SERVICE_MAX (MAX_CONNS/2)
29
30 /* how many total connections before we get concerned about fairness
31    among them */
32 #define FAIRNESS_CUTOFF (MAX_CONNS * 0.85)
33
34
35 typedef struct FlowBuf {
36   int fb_refs;                  /* num refs */
37   char *fb_buf;                 /* actual buffer */
38   int fb_used;                  /* bytes used in buffer */
39 } FlowBuf;
40 #define FB_SIZE 3800            /* max usable size */
41 #define FB_ALLOCSIZE 4000       /* extra to include IP address */
42
43 typedef struct SockInfo {
44   int si_peerFd;                /* fd of peer */
45   struct in_addr si_cliAddr;    /* address of client */
46   int si_blocked;               /* are we blocked? */
47   int si_needsHeaderSince;      /* since when are we waiting for a header */
48   int si_whichService;          /* index of service */
49   FlowBuf *si_readBuf;          /* read data into this buffer */
50   FlowBuf *si_writeBuf;         /* drain this buffer for writing */
51 } SockInfo;
52
53 static SockInfo sockInfo[TARG_SETSIZE]; /* fd number of peer socket */
54
55 typedef struct ServiceSig {
56   char *ss_host;                /* suffix in host */
57   char *ss_slice;
58   short ss_port;
59   int ss_slicePos;              /* position in slices array */
60 } ServiceSig;
61
62 static ServiceSig *serviceSig;
63 static int numServices;
64 static int confFileReadTime;
65 static int now;
66
67 typedef struct SliceInfo {
68   char *si_sliceName;
69   int si_inUse;                 /* do any services refer to this? */
70   int si_numConns;
71   int si_xid;
72 } SliceInfo;
73
74 static SliceInfo *slices;
75 static int numSlices;
76 static int numActiveSlices;
77 static int numTotalSliceConns;
78 static int anySliceXidsNeeded;
79
80 typedef struct OurFDSet {
81   long __fds_bits[TARG_SETSIZE/32];
82 } OurFDSet;
83 static OurFDSet masterReadSet, masterWriteSet;
84 static int highestSetFd;
85 static int numNeedingHeaders;   /* how many conns waiting on headers? */
86
87 static int numForks;
88
89 //HANDLE hdebugLog;
90
91 #ifndef SO_SETXID
92 #define SO_SETXID SO_PEERCRED
93 #endif
94 /*-----------------------------------------------------------------*/
95 static SliceInfo *
96 ServiceToSlice(int whichService)
97 {
98   if (whichService < 0)
99     return(NULL);
100   return(&slices[serviceSig[whichService].ss_slicePos]);
101 }
102 /*-----------------------------------------------------------------*/
103 static void
104 DumpStatus(int fd)
105 {
106   char buf[65535];
107   char *start = buf;
108   int i;
109   int len;
110
111   sprintf(start, 
112           "numForks %d, numActiveSlices %d, numTotalSliceConns %d\n"
113           "numNeedingHeaders %d, anySliceXidsNeeded %d\n",
114           numForks, numActiveSlices, numTotalSliceConns,
115           numNeedingHeaders, anySliceXidsNeeded);
116   start += strlen(start);
117
118   for (i = 0; i < numSlices; i++) {
119     SliceInfo *si = &slices[i];
120     sprintf(start, "Slice %d: %s xid %d, %d conns, inUse %d\n", 
121             i, si->si_sliceName, si->si_xid, si->si_numConns,
122             si->si_inUse);
123     start += strlen(start);
124   }
125
126   for (i = 0; i < numServices; i++) {
127     ServiceSig *ss = &serviceSig[i];
128     sprintf(start, "Service %d: %s %s port %d, slice# %d\n", i, ss->ss_host,
129             ss->ss_slice, (int) ss->ss_port, ss->ss_slicePos);
130     start += strlen(start);
131   }
132
133   len = start - buf;
134   write(fd, buf, len);
135 }
136 /*-----------------------------------------------------------------*/
137 static void
138 GetSliceXids(void)
139 {
140   /* walks through /etc/passwd, and gets the uid for every slice we
141      have */
142   FILE *f;
143   char *line;
144   int i;
145
146   if (!anySliceXidsNeeded)
147     return;
148
149   for (i = 0; i < numSlices; i++) {
150     SliceInfo *si = &slices[i];
151     si->si_inUse = 0;
152   }
153   for (i = 0; i < numServices; i++) {
154     SliceInfo *si = ServiceToSlice(i);
155     if (si != NULL)
156       si->si_inUse++;
157   }  
158
159   if ((f = fopen("/etc/passwd", "r")) == NULL)
160     return;
161
162   while ((line = GetNextLine(f)) != NULL) {
163     char *temp;
164     int xid;
165
166     if ((temp = strchr(line, ':')) == NULL)
167       continue;                 /* weird line */
168     *temp = '\0';               /* terminate slice name */
169     temp++;
170     if ((temp = strchr(temp+1, ':')) == NULL)
171       continue;                 /* weird line */
172     if ((xid = atoi(temp+1)) < 1)
173       continue;                 /* weird xid */
174
175     /* we've got a slice name and xid, let's try to match */
176     for (i = 0; i < numSlices; i++) {
177       if (slices[i].si_xid == 0 &&
178           strcasecmp(slices[i].si_sliceName, line) == 0) {
179         slices[i].si_xid = xid;
180         break;
181       }
182     }
183   }
184
185   /* assume service 0 is the root service, and don't check it since
186      it'll have xid zero */
187   anySliceXidsNeeded = FALSE;
188   for (i = 1; i < numSlices; i++) {
189     if (slices[i].si_xid == 0 && slices[i].si_inUse > 0) {
190       anySliceXidsNeeded = TRUE;
191       break;
192     }
193   }
194
195   fclose(f);
196 }
197 /*-----------------------------------------------------------------*/
198 static void
199 SliceConnsInc(int whichService)
200 {
201   SliceInfo *si = ServiceToSlice(whichService);
202
203   if (si == NULL)
204     return;
205   numTotalSliceConns++;
206   si->si_numConns++;
207   if (si->si_numConns == 1)
208     numActiveSlices++;
209 }
210 /*-----------------------------------------------------------------*/
211 static void
212 SliceConnsDec(int whichService)
213 {
214   SliceInfo *si = ServiceToSlice(whichService);
215
216   if (si == NULL)
217     return;
218   numTotalSliceConns--;
219   si->si_numConns--;
220   if (si->si_numConns == 0)
221     numActiveSlices--;
222 }
223 /*-----------------------------------------------------------------*/
224 static int
225 WhichSlicePos(char *slice)
226 {
227   /* adds the new slice if necessary, returns the index into slice
228      array. Never change the ordering of existing slices */
229   int i;
230   static int numSlicesAlloc;
231
232   for (i = 0; i < numSlices; i++) {
233     if (strcasecmp(slice, slices[i].si_sliceName) == 0)
234       return(i);
235   }
236
237   if (numSlices >= numSlicesAlloc) {
238     numSlicesAlloc = MAX(8, numSlicesAlloc * 2);
239     slices = realloc(slices, numSlicesAlloc * sizeof(SliceInfo));
240   }
241
242   memset(&slices[numSlices], 0, sizeof(SliceInfo));
243   slices[numSlices].si_sliceName = strdup(slice);
244   numSlices++;
245   return(numSlices-1);
246 }
247 /*-----------------------------------------------------------------*/
248 static void
249 ReadConfFile(void)
250 {
251   int numAlloc = 0;
252   int num = 0;
253   ServiceSig *servs = NULL;
254   FILE *f;
255   char *line = NULL;
256   struct stat statBuf;
257   int i;
258
259   if (stat(CONF_FILE, &statBuf) != 0) {
260     fprintf(stderr, "failed stat on codemux.conf\n");
261     if (numServices)
262       return;
263     exit(-1);
264   }
265   if (statBuf.st_mtime == confFileReadTime)
266     return;
267
268   if ((f = fopen(CONF_FILE, "r")) == NULL) {
269     fprintf(stderr, "failed reading codemux.conf\n");
270     if (numServices)
271       return;
272     exit(-1);
273   }
274
275   /* conf file entries look like
276      coblitz.codeen.org princeton_coblitz 3125
277   */
278
279   while (1) {
280     ServiceSig serv;
281     int port;
282     if (line != NULL)
283       free(line);
284
285     /* on the first pass, put in a fake entry for apache */
286     if (num == 0)
287       line = strdup(REAL_WEBSERVER_CONFLINE);
288     else {
289       if ((line = GetNextLine(f)) == NULL)
290         break;
291     }
292
293     memset(&serv, 0, sizeof(serv));
294     if (WordCount(line) != 3) {
295       fprintf(stderr, "bad line: %s\n", line);
296       continue;
297     }
298     serv.ss_port = port = atoi(GetField(line, 2));
299     if (port < 1 || port > 65535 || port == DEMUX_PORT) {
300       fprintf(stderr, "bad port: %s\n", line);
301       continue;
302     }
303
304     serv.ss_host = GetWord(line, 0);
305     serv.ss_slice = GetWord(line, 1);
306     if (num >= numAlloc) {
307       numAlloc = MAX(numAlloc * 2, 8);
308       servs = realloc(servs, numAlloc * sizeof(ServiceSig));
309     }
310     serv.ss_slicePos = WhichSlicePos(serv.ss_slice);
311     if (slices[serv.ss_slicePos].si_inUse == 0 &&
312         slices[serv.ss_slicePos].si_xid < 1)
313       anySliceXidsNeeded = TRUE; /* if new/inactive, we need xid */
314     servs[num] = serv;
315     num++;
316   }
317
318   fclose(f);
319
320   if (num == 1) {
321     if (numServices == 0) {
322       fprintf(stderr, "nothing found in codemux.conf\n");
323       exit(-1);
324     }
325     return;
326   }
327
328   for (i = 0; i < numServices; i++) {
329     free(serviceSig[i].ss_host);
330     free(serviceSig[i].ss_slice);
331   }
332   free(serviceSig);
333   serviceSig = servs;
334   numServices = num;
335   confFileReadTime = statBuf.st_mtime;
336 }
337 /*-----------------------------------------------------------------*/
338 static char *err400BadRequest =
339 "HTTP/1.0 400 Bad Request\r\n"
340 "Content-Type: text/html\r\n"
341 "\r\n"
342 "You are trying to access a PlanetLab node, and your\n"
343 "request header exceeded the allowable size. Please\n"
344 "try again if you believe this error is temporary.\n";
345 /*-----------------------------------------------------------------*/
346 static char *err503Unavailable =
347 "HTTP/1.0 503 Service Unavailable\r\n"
348 "Content-Type: text/html\r\n"
349 "\r\n"
350 "You are trying to access a PlanetLab node, but the service\n"
351 "seems to be unavailable at the moment. Please try again.\n";
352 /*-----------------------------------------------------------------*/
353 static char *err503TooBusy =
354 "HTTP/1.0 503 Service Unavailable\r\n"
355 "Content-Type: text/html\r\n"
356 "\r\n"
357 "You are trying to access a PlanetLab node, but the service\n"
358 "seems to be overloaded at the moment. Please try again.\n";
359 /*-----------------------------------------------------------------*/
360 static void
361 SetFd(int fd, OurFDSet *set)
362 {
363   if (highestSetFd < fd)
364     highestSetFd = fd;
365   FD_SET(fd, set);
366 }
367 /*-----------------------------------------------------------------*/
368 static void
369 ClearFd(int fd, OurFDSet *set)
370 {
371   FD_CLR(fd, set);
372 }
373 /*-----------------------------------------------------------------*/
374 static int
375 RemoveHeader(char *lower, char *real, int totalSize, char *header)
376 {
377   /* returns number of characters removed */
378   char h2[256];
379   int start, end, len;
380   char *temp, *conn;
381
382   sprintf(h2, "\n%s", header);
383
384   if ((conn = strstr(lower, h2)) == NULL)
385     return(0);
386
387   conn++;
388   /* determine how many characters to remove */
389   if ((temp = strchr(conn, '\n')) != NULL)
390     len = (temp - conn) + 1;
391   else
392     len = strlen(conn) + 1;
393   start = conn - lower;
394   end = start + len;
395   memmove(&real[start], &real[end], totalSize - end);
396   memmove(&lower[start], &lower[end], totalSize - end);
397
398   return(len);
399 }
400 /*-----------------------------------------------------------------*/
401 static int
402 InsertHeader(char *buf, int totalSize, char *header)
403 {
404   /* returns number of bytes inserted */
405   
406   char h2[256];
407   char *temp;
408   int len;
409   
410   sprintf(h2, "%s\r\n", header);
411   len = strlen(h2);
412
413   /* if we don't encounter a \n, it means that we have only a single
414      line, and we'd converted the \n to a \0 */
415   if ((temp = strchr(buf, '\n')) == NULL)
416     temp = strchr(buf, '\0');
417   temp++;
418   
419   memmove(temp + len, temp, totalSize - (temp - buf));
420   memcpy(temp, h2, len);
421   
422   return(len);
423 }
424 /*-----------------------------------------------------------------*/
425 static int
426 FindService(FlowBuf *fb, int *whichService, struct in_addr addr)
427 {
428   char *end;
429   char *lowerBuf;
430   char *hostVal;
431   char *buf = fb->fb_buf;
432   char orig[256];
433 #if 0
434   char *url;
435   int i;
436   int len;
437 #endif
438     
439   if (strstr(buf, "\n\r\n") == NULL && strstr(buf, "\n\n") == NULL)
440     return(FAILURE);
441
442   /* insert client info after first line */
443   sprintf(orig, "X-CoDemux-Client: %s", inet_ntoa(addr));
444   fb->fb_used += InsertHeader(buf, fb->fb_used + 1, orig);
445     
446   /* get just the header, so we can work on it */
447   LOCAL_STR_DUP_LOWER(lowerBuf, buf);
448   if ((end = strstr(lowerBuf, "\n\r\n")) == NULL)
449     end = strstr(lowerBuf, "\n\n");
450   *end = '\0';
451   
452   /* remove any existing connection, keep-alive headers, add ours */
453   fb->fb_used -= RemoveHeader(lowerBuf, buf, fb->fb_used + 1, "keep-alive:");
454   fb->fb_used -= RemoveHeader(lowerBuf, buf, fb->fb_used + 1, "connection:");
455   fb->fb_used += InsertHeader(buf, fb->fb_used + 1, "Connection: close");
456   InsertHeader(lowerBuf, fb->fb_used + 1, "connection: close");
457
458   /* isolate host, see if it matches */
459   if ((hostVal = strstr(lowerBuf, "\nhost:")) != NULL) {
460     int i;
461     hostVal += strlen("\nhost:");
462     if ((end = strchr(hostVal, '\n')) != NULL)
463       *end = '\0';
464     if ((end = strchr(hostVal, ':')) != NULL)
465       *end = '\0';
466     while (isspace(*hostVal))
467       hostVal++;
468     if (strlen(hostVal) > 0) {
469       hostVal = GetWord(hostVal, 0);
470       for (i = 1; i < numServices; i++) {
471         if (serviceSig[i].ss_host != NULL &&
472             DoesDotlessSuffixMatch(hostVal, 0, serviceSig[i].ss_host)) {
473           *whichService = i;
474           free(hostVal);
475           /* printf("%s", buf); */
476           return(SUCCESS);
477         }
478       }
479       free(hostVal);
480     }
481   }
482
483 #if 0
484   /* see if URL prefix matches */
485   if ((end = strchr(lowerBuf, '\n')) != NULL)
486     *end = 0;
487   if ((url = GetField(lowerBuf, 1)) == NULL ||
488       url[0] != '/') {
489     /* bad request - let apache handle it ? */
490     *whichService = 0;
491     return(SUCCESS);
492   }
493   url++;                        /* skip the leading slash */
494   for (i = 1; i < numServices; i++) {
495     if (serviceSig[i].ss_prefix != NULL &&
496         (len = strlen(serviceSig[i].ss_prefix)) > 0 &&
497         strncmp(url, serviceSig[i].ss_prefix, len) == 0 &&
498         (url[len] == ' ' || url[len] == '/')) {
499       int startPos = url - lowerBuf;
500       int stripLen = len + ((url[len] == '/') ? 1 : 0);
501       /* strip out prefix */
502       fb->fb_used -= stripLen;
503       memmove(&buf[startPos], &buf[startPos+stripLen], 
504               fb->fb_used + 1 - startPos);
505       /* printf("%s", buf); */
506       *whichService = i;
507       return(SUCCESS);
508     }
509   }
510 #endif
511
512   /* default to first service */
513   *whichService = 0;
514   return(SUCCESS);
515 }
516 /*-----------------------------------------------------------------*/
517 static int
518 StartConnect(int origFD, int whichService)
519 {
520   int sock;
521   struct sockaddr_in dest;
522   SockInfo *si;
523
524   /* create socket */
525   if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
526     return(FAILURE);
527   }
528   
529   /* make socket non-blocking */
530   if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
531     close(sock);
532     return(FAILURE);
533   }
534   
535   /* set addr structure */
536   memset(&dest, 0, sizeof(dest));
537   dest.sin_family = AF_INET;
538   dest.sin_port = htons(serviceSig[whichService].ss_port);
539   dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
540   
541   /* start connection process - we should be told that it's in
542      progress */
543   if (connect(sock, (struct sockaddr *) &dest, sizeof(dest)) != -1 || 
544       errno != EINPROGRESS) {
545     close(sock);
546     return(FAILURE);
547   }
548
549   SetFd(sock, &masterWriteSet); /* determine when connect finishes */
550   sockInfo[origFD].si_peerFd = sock;
551   si = &sockInfo[sock];
552   memset(si, 0, sizeof(SockInfo));
553   si->si_peerFd = origFD;
554   si->si_blocked = TRUE;        /* still connecting */
555   si->si_whichService = whichService;
556   si->si_writeBuf = sockInfo[origFD].si_readBuf;
557   sockInfo[origFD].si_readBuf->fb_refs++;
558   if (whichService >= 0)
559     SliceConnsInc(whichService);
560
561   return(SUCCESS);
562 }
563 /*-----------------------------------------------------------------*/
564 static int
565 WriteAvailData(int fd)
566 {
567   SockInfo *si = &sockInfo[fd];
568   FlowBuf *fb = si->si_writeBuf;
569   int res;
570
571   /* printf("trying to write fd %d\n", fd); */
572   if (fb->fb_used < 1 || si->si_blocked)
573     return(SUCCESS);
574
575   /* printf("trying to write %d bytes\n", fb->fb_used); */
576   /* write(STDOUT_FILENO, fb->fb_buf, fb->fb_used); */
577   if ((res = write(fd, fb->fb_buf, fb->fb_used)) > 0) {
578     fb->fb_used -= res;
579     if (fb->fb_used > 0) {
580       /* couldn't write all - assume blocked */
581       memmove(fb->fb_buf, &fb->fb_buf[res], fb->fb_used);
582       si->si_blocked = TRUE;
583       SetFd(fd, &masterWriteSet);
584     }
585     /* printf("wrote %d\n", res); */
586     return(SUCCESS);
587   }
588
589   /* we might have been full but didn't realize it */
590   if (res == -1 && errno == EAGAIN) {
591     si->si_blocked = TRUE;
592     SetFd(fd, &masterWriteSet);
593     return(SUCCESS);
594   }
595
596   /* otherwise, assume the worst */
597   return(FAILURE);
598 }
599 /*-----------------------------------------------------------------*/
600 static OurFDSet socksToCloseVec;
601 static int numSocksToClose;
602 static int whichSocksToClose[TARG_SETSIZE];
603 /*-----------------------------------------------------------------*/
604 static void
605 CloseSock(int fd)
606 {
607   if (FD_ISSET(fd, &socksToCloseVec))
608     return;
609   SetFd(fd, &socksToCloseVec);
610   whichSocksToClose[numSocksToClose] = fd;
611   numSocksToClose++;
612 }
613 /*-----------------------------------------------------------------*/
614 static void
615 DecBuf(FlowBuf *buf)
616 {
617   if (buf == NULL)
618     return;
619   buf->fb_refs--;
620   if (buf->fb_refs == 0) {
621     free(buf->fb_buf);
622     free(buf);
623   }
624 }
625 /*-----------------------------------------------------------------*/
626 static void
627 ReallyCloseSocks(void)
628 {
629   int i;
630
631   memset(&socksToCloseVec, 0, sizeof(socksToCloseVec));
632
633   for (i = 0; i < numSocksToClose; i++) {
634     int fd = whichSocksToClose[i];
635     close(fd);
636     DecBuf(sockInfo[fd].si_readBuf);
637     DecBuf(sockInfo[fd].si_writeBuf);
638     ClearFd(fd, &masterReadSet);
639     ClearFd(fd, &masterWriteSet);
640     if (sockInfo[fd].si_needsHeaderSince) {
641       sockInfo[fd].si_needsHeaderSince = 0;
642       numNeedingHeaders--;
643     }
644     if (sockInfo[fd].si_whichService >= 0) {
645       SliceConnsDec(sockInfo[fd].si_whichService);
646       sockInfo[fd].si_whichService = -1;
647     }
648   }
649   numSocksToClose = 0;
650 }
651 /*-----------------------------------------------------------------*/
652 static void
653 SocketReadyToRead(int fd)
654 {
655   SockInfo *si = &sockInfo[fd];
656   int spaceLeft;
657   FlowBuf *fb;
658   int res;
659
660   /* if peer is closed, close ourselves */
661   if (si->si_peerFd < 0 && (!si->si_needsHeaderSince)) {
662     CloseSock(fd);
663     return;
664   }
665
666   if ((fb = si->si_readBuf) == NULL) {
667     fb = si->si_readBuf = calloc(1, sizeof(FlowBuf));
668     fb->fb_refs = 1;
669     if (si->si_peerFd >= 0) {
670       sockInfo[si->si_peerFd].si_writeBuf = fb;
671       fb->fb_refs = 2;
672     }
673   }
674
675   if (fb->fb_buf == NULL)
676     fb->fb_buf = malloc(FB_ALLOCSIZE);
677
678   /* determine read buffer size - if 0, then block reads and return */
679   if ((spaceLeft = FB_SIZE - fb->fb_used) < 0) {
680     if (si->si_needsHeaderSince) {
681       write(fd, err400BadRequest, strlen(err400BadRequest));
682       CloseSock(fd);
683       return;
684     }
685     else {
686       ClearFd(fd, &masterReadSet);
687       return;
688     }
689   }
690
691   /* read as much as allowed, and is available */
692   if ((res = read(fd, &fb->fb_buf[fb->fb_used], spaceLeft)) == 0) {
693     CloseSock(fd);
694     if (fb->fb_used == 0 && si->si_peerFd >= 0) {
695       CloseSock(si->si_peerFd);
696       si->si_peerFd = -1;
697     }
698     return;
699   }
700   if (res == -1) {
701     if (errno == EAGAIN)
702       return;
703     CloseSock(fd);
704     if (fb->fb_used == 0 && si->si_peerFd >= 0) {
705       CloseSock(si->si_peerFd);
706       si->si_peerFd = -1;
707     }
708     return;
709   }
710   fb->fb_used += res;
711   fb->fb_buf[fb->fb_used] = 0;  /* terminate it for convenience */
712   printf("sock %d, read %d, total %d\n", fd, res, fb->fb_used);
713
714   /* if we need header, check if we've gotten it. if so, do
715      modifications and continue. if not, check if we've read the
716      maximum, and if so, fail */
717   if (si->si_needsHeaderSince) {
718     int whichService;
719     SliceInfo *slice;
720
721 #define STATUS_REQ "GET /codemux/status.txt"
722     if (strncasecmp(fb->fb_buf, STATUS_REQ, sizeof(STATUS_REQ)-1) == 0) {
723       DumpStatus(fd);
724       CloseSock(fd);
725       return;
726     }
727
728     printf("trying to find service\n");
729     if (FindService(fb, &whichService, si->si_cliAddr) != SUCCESS)
730       return;
731     printf("found service %d\n", whichService);
732     slice = ServiceToSlice(whichService);
733
734     /* no service can have more than some absolute max number of
735        connections. Also, when we're too busy, start enforcing
736        fairness across the servers */
737     if (slice->si_numConns > SERVICE_MAX ||
738         (numTotalSliceConns > FAIRNESS_CUTOFF && 
739          slice->si_numConns > MAX_CONNS/numActiveSlices)) {
740       write(fd, err503TooBusy, strlen(err503TooBusy));
741       CloseSock(fd);
742       return;
743     }
744
745     if (slice->si_xid > 0) {
746       setsockopt(fd, SOL_SOCKET, SO_SETXID, 
747                  &slice->si_xid, sizeof(slice->si_xid));
748       fprintf(stderr, "setsockopt() with XID = %d name = %s\n", 
749               slice->si_xid, slice->si_sliceName);
750     }
751
752     si->si_needsHeaderSince = 0;
753     numNeedingHeaders--;
754     if (StartConnect(fd, whichService) != SUCCESS) {
755       write(fd, err503Unavailable, strlen(err503Unavailable));
756       CloseSock(fd);
757       return;
758     }
759     return;
760   }
761
762   /* write anything possible */
763   if (WriteAvailData(si->si_peerFd) != SUCCESS) {
764     /* assume the worst and close */
765     CloseSock(fd);
766     CloseSock(si->si_peerFd);
767     si->si_peerFd = -1;
768   }
769 }
770 /*-----------------------------------------------------------------*/
771 static void
772 SocketReadyToWrite(int fd)
773 {
774   SockInfo *si = &sockInfo[fd];
775
776   /* unblock it and read what it has */
777   si->si_blocked = FALSE;
778   ClearFd(fd, &masterWriteSet);
779   SetFd(fd, &masterReadSet);
780   
781   /* enable reading on peer just in case it was off */
782   if (si->si_peerFd >= 0)
783     SetFd(si->si_peerFd, &masterReadSet);
784     
785   /* if we have data, write it */
786   if (WriteAvailData(fd) != SUCCESS) {
787    /* assume the worst and close */
788     CloseSock(fd);
789     if (si->si_peerFd >= 0) {
790       CloseSock(si->si_peerFd);
791       si->si_peerFd = -1;
792     }
793     return;
794   }
795
796   /* if peer is closed and we're done writing, we should close */
797   if (si->si_peerFd < 0 && si->si_writeBuf->fb_used == 0)
798     CloseSock(fd);
799 }
800 /*-----------------------------------------------------------------*/
801 static void
802 CloseReqlessConns(void)
803 {
804   static int lastSweep;
805   int maxAge;
806   int i;
807
808   if (lastSweep == now)
809     return;
810   lastSweep = now;
811
812   if (numTotalSliceConns + numNeedingHeaders > MAX_CONNS ||
813       numNeedingHeaders > TARG_SETSIZE/20) {
814     /* second condition is probably an attack - close aggressively */
815     maxAge = 5;
816   }
817   else if (numTotalSliceConns + numNeedingHeaders > FAIRNESS_CUTOFF ||
818            numNeedingHeaders > TARG_SETSIZE/40) {
819     /* sweep a little aggressively */
820     maxAge = 10;
821   }
822   else if (numNeedingHeaders > TARG_SETSIZE/80) {
823     /* just sweep to close strays */
824     maxAge = 30;
825   }
826   else {
827     /* too little gained - not worth sweeping */
828     return;
829   }
830
831   /* if it's too old, close it */
832   for (i = 0; i < highestSetFd+1; i++) {
833     if (sockInfo[i].si_needsHeaderSince &&
834         (now - sockInfo[i].si_needsHeaderSince) > maxAge)
835       CloseSock(i);
836   }
837 }
838 /*-----------------------------------------------------------------*/
839 static void
840 MainLoop(int lisSock)
841 {
842   int i;
843   OurFDSet tempReadSet, tempWriteSet;
844   int res;
845   int lastConfCheck = 0;
846
847   signal(SIGPIPE, SIG_IGN);
848
849   while (1) {
850     int newSock;
851     int ceiling;
852     struct timeval timeout;
853
854     now = time(NULL);
855
856     if (now - lastConfCheck > 300) {
857       ReadConfFile();
858       GetSliceXids();           /* always call - in case new slices created */
859       lastConfCheck = now;
860     }
861
862     /* see if there's any activity */
863     tempReadSet = masterReadSet;
864     tempWriteSet = masterWriteSet;
865
866     /* trim it down if needed */
867     while (highestSetFd > 1 &&
868            (!FD_ISSET(highestSetFd, &tempReadSet)) &&
869            (!FD_ISSET(highestSetFd, &tempWriteSet)))
870       highestSetFd--;
871     timeout.tv_sec = 1;
872     timeout.tv_usec = 0;
873     res = select(highestSetFd+1, (fd_set *) &tempReadSet, 
874                  (fd_set *) &tempWriteSet, NULL, &timeout);
875     if (res < 0 && errno != EINTR) {
876       perror("select");
877       exit(-1);
878     }
879
880     now = time(NULL);
881
882     /* clear the bit for listen socket to avoid confusion */
883     ClearFd(lisSock, &tempReadSet);
884     
885     ceiling = highestSetFd+1;   /* copy it, since it changes during loop */
886     /* pass data back and forth as needed */
887     for (i = 0; i < ceiling; i++) {
888       if (FD_ISSET(i, &tempWriteSet))
889         SocketReadyToWrite(i);
890     }
891     for (i = 0; i < ceiling; i++) {
892       if (FD_ISSET(i, &tempReadSet))
893         SocketReadyToRead(i);
894     }
895
896     /* see if we need to close conns w/o requests */
897     CloseReqlessConns();
898     
899     /* do all closes */
900     ReallyCloseSocks();
901
902     /* try accepting new connections */
903     do {
904       struct sockaddr_in addr;
905       socklen_t lenAddr = sizeof(addr);
906       if ((newSock = accept(lisSock, (struct sockaddr *) &addr, 
907                             &lenAddr)) >= 0) {
908         memset(&sockInfo[newSock], 0, sizeof(SockInfo));
909         sockInfo[newSock].si_needsHeaderSince = now;
910         numNeedingHeaders++;
911         sockInfo[newSock].si_peerFd = -1;
912         sockInfo[newSock].si_cliAddr = addr.sin_addr;
913         sockInfo[newSock].si_whichService = -1;
914         SetFd(newSock, &masterReadSet);
915       }
916     } while (newSock >= 0);
917   }
918 }
919 /*-----------------------------------------------------------------*/
920 int
921 main(int argc, char *argv[])
922 {
923   int lisSock;
924
925   if ((lisSock = CreatePrivateAcceptSocket(DEMUX_PORT, TRUE)) < 0) {
926     fprintf(stderr, "failed creating accept socket\n");
927     exit(-1);
928   }
929   SetFd(lisSock, &masterReadSet);
930
931   while (1) {
932     numForks++;
933     if (fork()) {
934       /* this is the parent - just wait */
935       while (wait3(NULL, 0, NULL) < 1)
936         ;                       /* just keep waiting for a real pid */
937     }
938     else {
939       /* child process */
940       MainLoop(lisSock);
941       exit(-1);
942     }
943   }
944 }
945 /*-----------------------------------------------------------------*/