1 const char jcc_rcs[] = "$Id: jcc.c,v 1.10 2001/05/26 15:26:15 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $
6 * Purpose : Main file. Contains main() method, main loop, and
7 * the main connection-handling function.
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.10 2001/05/26 15:26:15 jongfoster
37 * ACL feature now provides more security by immediately dropping
38 * connections from untrusted hosts.
40 * Revision 1.9 2001/05/26 00:28:36 jongfoster
41 * Automatic reloading of config file.
42 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
43 * Most of the global variables have been moved to a new
44 * struct configuration_spec, accessed through csp->config->globalname
45 * Most of the globals remaining are used by the Win32 GUI.
47 * Revision 1.8 2001/05/25 22:43:18 jongfoster
48 * Fixing minor memory leak and buffer overflow.
50 * Revision 1.7 2001/05/25 22:34:30 jongfoster
53 * Revision 1.6 2001/05/23 00:13:58 joergs
54 * AmigaOS support fixed.
56 * Revision 1.5 2001/05/22 18:46:04 oes
58 * - Enabled filtering banners by size rather than URL
59 * by adding patterns that replace all standard banner
60 * sizes with the "Junkbuster" gif to the re_filterfile
62 * - Enabled filtering WebBugs by providing a pattern
63 * which kills all 1x1 images
65 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
66 * which is selected by the (nonstandard and therefore
67 * capital) letter 'U' in the option string.
68 * It causes the quantifiers to be ungreedy by default.
69 * Appending a ? turns back to greedy (!).
71 * - Added a new interceptor ijb-send-banner, which
72 * sends back the "Junkbuster" gif. Without imagelist or
73 * MSIE detection support, or if tinygif = 1, or the
74 * URL isn't recognized as an imageurl, a lame HTML
75 * explanation is sent instead.
77 * - Added new feature, which permits blocking remote
78 * script redirects and firing back a local redirect
80 * The feature is conditionally compiled, i.e. it
81 * can be disabled with --disable-fast-redirects,
82 * plus it must be activated by a "fast-redirects"
83 * line in the config file, has its own log level
84 * and of course wants to be displayed by show-proxy-args
85 * Note: Boy, all the #ifdefs in 1001 locations and
86 * all the fumbling with configure.in and acconfig.h
87 * were *way* more work than the feature itself :-(
89 * - Because a generic redirect template was needed for
90 * this, tinygif = 3 now uses the same.
92 * - Moved GIFs, and other static HTTP response templates
97 * - Removed some >400 CRs again (Jon, you really worked
100 * Revision 1.4 2001/05/21 19:34:01 jongfoster
101 * Made failure to bind() a fatal error.
103 * Revision 1.3 2001/05/20 01:21:20 jongfoster
104 * Version 2.9.4 checkin.
105 * - Merged popupfile and cookiefile, and added control over PCRS
106 * filtering, in new "permissionsfile".
107 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
108 * file error you now get a message box (in the Win32 GUI) rather
109 * than the program exiting with no explanation.
110 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
112 * - Removed tabs from "config"
113 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
114 * - Bumped up version number.
116 * Revision 1.2 2001/05/17 22:34:44 oes
117 * - Added hint on GIF char array generation to jcc.c
118 * - Cleaned CRLF's from the sources and related files
119 * - Repaired logging for REF and FRC
121 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
122 * Initial import of version 2.9.3 source tree
125 *********************************************************************/
131 #include <sys/types.h>
140 # include <sys/timeb.h>
141 # include <windows.h>
143 # include <process.h>
146 # endif /* def TOGGLE */
149 # ifndef _WIN_CONSOLE
151 # endif /* ndef _WIN_CONSOLE */
153 #else /* ifndef _WIN32 */
156 # include <sys/time.h>
157 # include <sys/wait.h>
158 # include <sys/stat.h>
162 # include <socket.h> /* BeOS has select() for sockets only. */
163 # include <OS.h> /* declarations for threads and stuff. */
176 #include "showargs.h"
178 #include "killpopup.h"
179 #include "miscutil.h"
181 #include "jbsockets.h"
184 const char jcc_h_rcs[] = JCC_H_VERSION;
185 const char project_h_rcs[] = PROJECT_H_VERSION;
187 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
189 struct client_state clients[1];
190 struct file_list files[1];
193 int urls_read = 0; /* total nr of urls read inc rejected */
194 int urls_rejected = 0; /* total nr of urls rejected */
195 #endif /* def STATISTICS */
198 static void listen_loop(void);
199 static void chat(struct client_state *csp);
201 void serve(struct client_state *csp);
202 #else /* ifndef AMIGA */
203 static void serve(struct client_state *csp);
204 #endif /* def AMIGA */
207 static int32 server_thread(void *data);
208 #endif /* def __BEOS__ */
211 #define sleep(N) Sleep(((N) * 1000))
215 /*********************************************************************
219 * Description : Once a connection to the client has been accepted,
220 * this function is called (via serve()) to handle the
221 * main business of the communication. When this
222 * function returns, the caller must close the client
226 * 1 : csp = Current client state (buffers, headers, etc...)
228 * Returns : On success, the number of bytes written are returned (zero
229 * indicates nothing was written). On error, -1 is returned,
230 * and errno is set appropriately. If count is zero and the
231 * file descriptor refers to a regular file, 0 will be
232 * returned without causing any other effect. For a special
233 * file, the results are not portable.
235 *********************************************************************/
236 static void chat(struct client_state *csp)
238 /* This next line is a little ugly, but it simplifies the if statement below. */
239 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
241 # define IS_TOGGLED_ON csp->toggled_on &&
242 #else /* ifndef TOGGLE */
243 # define IS_TOGGLED_ON
244 #endif /* ndef TOGGLE */
246 /* This next line is a little ugly, but it simplifies the if statement below. */
247 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
249 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
250 #else /* ifndef TRUST_FILES */
251 # define IS_TRUSTED_URL
252 #endif /* ndef TRUST_FILES */
254 char buf[BUFSIZ], *hdr, *p, *req;
258 int n, maxfd, server_body;
259 int ms_iis5_hack = 0;
261 const struct gateway *gw;
262 struct http_request *http;
264 int block_popups; /* bool, 1==will block popups */
265 int block_popups_now = 0; /* bool, 1==currently blocking popups */
266 #endif /* def KILLPOPUPS */
268 int pcrs_filter; /* bool, 1==will filter through pcrs */
269 int filtering = 0; /* bool, 1==currently filtering through pcrs */
270 #endif /* def PCRS */
275 * Read the client's request. Note that since we're not using select() we
276 * could get blocked here if a client connected, then didn't say anything!
281 n = read_socket(csp->cfd, buf, sizeof(buf));
283 if (n <= 0) break; /* error! */
285 add_to_iob(csp, buf, n);
287 req = get_header(csp);
291 break; /* no HTTP request! */
296 continue; /* more to come! */
300 /* If this request contains the FORCE_PREFIX,
301 * better get rid of it now and set the force flag --oes
304 if (strstr(req, FORCE_PREFIX))
306 strclean(req, FORCE_PREFIX);
307 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
314 #endif /* def FORCE_LOAD */
316 parse_http_request(req, http, csp);
321 if (http->cmd == NULL)
323 strcpy(buf, CHEADER);
324 write_socket(csp->cfd, buf, strlen(buf));
326 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
331 /* decide how to route the HTTP request */
333 if ((gw = forward_url(http, csp)) == NULL)
335 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
336 /* Never get here - LOG_LEVEL_FATAL causes program exit */
339 /* build the http request to send to the server
340 * we have to do one of the following:
342 * create = use the original HTTP request to create a new
343 * HTTP request that has only the path component
344 * without the http://domainspec
345 * pass = pass the original HTTP request unchanged
347 * drop = drop the HTTP request
352 * +--------+--------+
354 * 0 | create | drop |
356 * Forwarding +--------+--------+
360 * +--------+--------+
364 if (gw->forward_host)
366 /* if forwarding, just pass the request as is */
367 enlist(csp->headers, http->cmd);
373 /* otherwise elide the host information from the url */
375 p = strsav(p, http->gpc);
377 p = strsav(p, http->path);
379 p = strsav(p, http->ver);
380 enlist(csp->headers, p);
385 /* decide what we're to do with cookies */
388 if (!csp->toggled_on)
390 /* Most compatible set of permissions */
391 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
395 csp->permissions = url_permissions(http, csp);
397 #else /* ifndef TOGGLE */
398 csp->permissions = url_permissions(http, csp);
399 #endif /* ndef TOGGLE */
402 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
403 #endif /* def KILLPOPUPS */
405 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
406 ((csp->permissions & PERMIT_RE_FILTER) != 0);
407 #endif /* def PCRS */
410 /* grab the rest of the client's headers */
414 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
416 n = read_socket(csp->cfd, buf, sizeof(buf));
419 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
422 add_to_iob(csp, buf, n);
426 if (p == NULL) break;
428 enlist(csp->headers, p);
432 /* filter it as required */
434 hdr = sed(client_patterns, add_client_headers, csp);
436 destroy_list(csp->headers);
438 /* Check the request against all rules, unless
439 * we're toggled off or in force mode.
445 #endif /* def FORCE_LOAD */
446 ( (p = intercept_url(http, csp)) ||
448 (p = block_url(http, csp))
449 #ifdef FAST_REDIRECTS
450 || (csp->config->fast_redirects && (p = redirect_url(http, csp)))
451 #endif /* def FAST_REDIRECTS */
456 #endif /* def STATISTICS */
458 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
460 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 1",
461 csp->ip_addr_str, http->cmd);
463 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
464 /* Block as image? */
465 if ( (csp->config->tinygif > 0) && block_imageurl(http, csp) )
467 /* Send "blocked" image */
468 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
469 http->hostport, http->path);
471 if ((csp->config->tinygif == 2) || strstr(http->path, "ijb-send-banner"))
473 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
475 else if (csp->config->tinygif == 1)
477 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
479 else if ((csp->config->tinygif == 3) && (csp->config->tinygifurl))
482 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE)
483 + strlen(csp->config->tinygifurl));
484 sprintf(p, HTTP_REDIRECT_TEMPLATE, csp->config->tinygifurl);
485 write_socket(csp->cfd, p, strlen(p));
489 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
493 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
496 /* Send HTML "blocked" message, interception, or redirection result */
497 write_socket(csp->cfd, p, strlen(p));
505 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
507 if (gw->forward_host)
509 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
510 gw->forward_host, gw->forward_port, http->hostport);
514 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
517 /* here we connect to the server, gateway, or the forwarder */
519 csp->sfd = (gw->conn)(gw, http, csp);
523 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
528 err = zalloc(strlen(CNXDOM) + strlen(http->host));
529 sprintf(err, CNXDOM, http->host);
531 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
532 csp->ip_addr_str, http->cmd);
536 eno = safe_strerror(errno);
537 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
538 sprintf(err, CFAIL, http->hostport, eno);
540 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
541 csp->ip_addr_str, http->cmd);
544 write_socket(csp->cfd, err, strlen(err));
551 log_error(LOG_LEVEL_CONNECT, "OK");
553 if (gw->forward_host || (http->ssl == 0))
555 /* write the client's (modified) header to the server
556 * (along with anything else that may be in the buffer)
561 if ((write_socket(csp->sfd, hdr, n) != n)
562 || (flush_socket(csp->sfd, csp ) < 0))
564 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
567 eno = safe_strerror(errno);
568 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
569 sprintf(err, CFAIL, http->hostport, eno);
570 write_socket(csp->cfd, err, strlen(err));
572 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
573 csp->ip_addr_str, http->cmd);
583 * We're running an SSL tunnel and we're not forwarding,
584 * so just send the "connect succeeded" message to the
585 * client, flush the rest, and get out of the way.
587 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
588 csp->ip_addr_str, http->cmd);
590 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
598 /* we're finished with the client's header */
601 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
603 /* pass data between the client and server
604 * until one or the other shuts down the connection.
613 FD_SET(csp->cfd, &rfds);
614 FD_SET(csp->sfd, &rfds);
616 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
620 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
624 /* this is the body of the browser's request
625 * just read it and write it.
628 if (FD_ISSET(csp->cfd, &rfds))
630 n = read_socket(csp->cfd, buf, sizeof(buf));
634 break; /* "game over, man" */
637 if (write_socket(csp->sfd, buf, n) != n)
639 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
646 * The server wants to talk. It could be the header or the body.
647 * If `hdr' is null, then it's the header otherwise it's the body.
648 * FIXME: Does `hdr' really mean `host'?
652 if (FD_ISSET(csp->sfd, &rfds))
655 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
659 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
661 eno = safe_strerror(errno);
662 sprintf(buf, CFAIL, http->hostport, eno);
665 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
666 csp->ip_addr_str, http->cmd);
668 write_socket(csp->cfd, buf, strlen(buf));
672 /* Add a trailing zero. This lets filter_popups
673 * use string operations.
678 /* Filter the popups on this read. */
679 if (block_popups_now)
681 filter_popups(buf, n);
683 #endif /* def KILLPOPUPS */
685 /* Normally, this would indicate that we've read
686 * as much as the server has sent us and we can
687 * close the client connection. However, Microsoft
688 * in its wisdom has released IIS/5 with a bug that
689 * prevents it from sending the trailing \r\n in
690 * a 302 redirect header (and possibly other headers).
691 * To work around this if we've haven't parsed
692 * a full header we'll append a trailing \r\n
693 * and see if this now generates a valid one.
695 * This hack shouldn't have any impacts. If we've
696 * already transmitted the header or if this is a
697 * SSL connection, then we won't bother with this
698 * hack. So we only work on partially received
699 * headers. If we append a \r\n and this still
700 * doesn't generate a valid header, then we won't
701 * transmit anything to the client.
705 /* This hack must only be enforced for headers. */
706 if (server_body || http->ssl)
711 re_process_buffer(csp);
713 #endif /* def PCRS */
714 break; /* "game over, man" */
717 /* Let's pretend the server just sent us a blank line. */
718 n = sprintf(buf, "\r\n");
721 * Now, let the normal header parsing algorithm below do its
722 * job. If it fails, we'll exit instead of continuing.
729 * If this is an SSL connection or we're in the body
730 * of the server document, just write it to the client.
733 if (server_body || http->ssl)
738 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
741 #endif /* def PCRS */
744 if (write_socket(csp->cfd, buf, n) != n)
746 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
755 /* we're still looking for the end of the
756 * server's header ... (does that make header
757 * parsing an "out of body experience" ?
760 /* buffer up the data we just read */
761 add_to_iob(csp, buf, n);
763 /* get header lines from the iob */
765 while ((p = get_header(csp)))
769 /* see following note */
772 enlist(csp->headers, p);
776 /* NOTE: there are no "empty" headers so
777 * if the pointer `p' is not NULL we must
778 * assume that we reached the end of the
779 * buffer before we hit the end of the header.
786 /* Well, we tried our MS IIS/5
787 * hack and it didn't work.
788 * The header is incomplete
789 * and there isn't anything
790 * we can do about it.
796 /* Since we have to wait for
797 * more from the server before
798 * we can parse the headers
799 * we just continue here.
805 /* we have now received the entire header.
806 * filter it and send the result to the client
809 hdr = sed(server_patterns, add_server_headers, csp);
812 /* write the server's (modified) header to
813 * the client (along with anything else that
814 * may be in the buffer)
818 /* Start blocking popups if appropriate. */
820 if (csp->is_text && /* It's a text / * MIME-Type */
821 !http->ssl && /* We talk plaintext */
824 block_popups_now = 1;
827 #endif /* def KILLPOPUPS */
830 /* Start re_filtering this if appropriate. */
832 if (csp->is_text && /* It's a text / * MIME-Type */
833 !http->ssl && /* We talk plaintext */
834 pcrs_filter) /* Policy allows */
839 /* This next line is a little ugly, but it simplifies the if statement below. */
840 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
841 #define NOT_FILTERING_AND !filtering &&
843 #else /* not def PCRS */
845 #define NOT_FILTERING_AND
847 #endif /* def PCRS */
849 if ((write_socket(csp->cfd, hdr, n) != n)
850 || (NOT_FILTERING_AND (n = flush_socket(csp->cfd, csp) < 0)))
852 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
854 /* the write failed, so don't bother
855 * mentioning it to the client...
856 * it probably can't hear us anyway.
862 NOT_FILTERING_AND (byte_count += n);
864 /* we're finished with the server's header */
869 /* If this was a MS IIS/5 hack then it means
870 * the server has already closed the
871 * connection. Nothing more to read. Time
882 return; /* huh? we should never get here */
885 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
886 csp->ip_addr_str, http->cmd, byte_count);
890 /*********************************************************************
894 * Description : This is little more than chat. We only "serve" to
895 * to close any socket that chat may have opened.
898 * 1 : csp = Current client state (buffers, headers, etc...)
902 *********************************************************************/
904 void serve(struct client_state *csp)
905 #else /* ifndef AMIGA */
906 static void serve(struct client_state *csp)
907 #endif /* def AMIGA */
910 close_socket(csp->cfd);
914 close_socket(csp->sfd);
924 /*********************************************************************
926 * Function : server_thread
928 * Description : We only exist to call `serve' in a threaded environment.
931 * 1 : data = Current client state (buffers, headers, etc...)
933 * Returns : Always 0.
935 *********************************************************************/
936 static int32 server_thread(void *data)
938 serve((struct client_state *) data);
946 /*********************************************************************
950 * Description : Load the config file and start the listen loop.
951 * This function is a lot more *sane* with the `load_config'
952 * and `listen_loop' functions; although it stills does
953 * a *little* too much for my taste.
956 * 1 : argc = Number of parameters (including $0).
957 * 2 : argv = Array of (char *)'s to the parameters.
959 * Returns : 1 if : can't open config file, unrecognized directive,
960 * stats requested in multi-thread mode, can't open the
961 * log file, can't open the jar file, listen port is invalid,
962 * any load fails, and can't bind port.
964 * Else main never returns, the process must be signaled
965 * to terminate execution. Or, on Windows, use the
966 * "File", "Exit" menu option.
968 *********************************************************************/
970 int _main(int argc, const char *argv[])
972 int main(int argc, const char *argv[])
977 "AmiTCP:db/junkbuster.config"
978 #elif !defined(_WIN32)
985 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
986 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
988 printf("JunkBuster proxy version " VERSION ".\n\n"
989 "Usage: %s [configfile]\n\n"
990 "See " HOME_PAGE_URL " for details.\n"
991 "This program is distributed under the GNU GPL, version 2 or later.\n",
995 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
997 printf(VERSION "\n");
1000 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1011 configfile = argv[1];
1022 signal(SIGPIPE, SIG_IGN);
1023 signal(SIGCHLD, SIG_IGN);
1025 #else /* ifdef _WIN32 */
1026 # ifdef _WIN_CONSOLE
1028 * We *are* in a windows console app.
1029 * Print a verbose messages about FAQ's and such
1031 printf(win32_blurb);
1032 # endif /* def _WIN_CONSOLE */
1033 #endif /* def _WIN32 */
1044 /*********************************************************************
1046 * Function : listen_loop
1048 * Description : bind the listen port and enter a "FOREVER" listening loop.
1054 *********************************************************************/
1055 static void listen_loop(void)
1057 struct client_state *csp = NULL;
1059 struct configuration_spec * config;
1061 config = load_config();
1063 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1064 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1066 bfd = bind_port(config->haddr, config->hport);
1070 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1071 "- There may be another junkbuster or some other "
1072 "proxy running on port %d",
1073 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1074 config->hport, config->hport
1076 /* shouldn't get here */
1080 config->need_bind = 0;
1085 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1086 while (waitpid(-1, NULL, WNOHANG) > 0)
1088 /* zombie children */
1090 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1093 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1095 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1099 memset(csp, '\0', sizeof(*csp));
1104 csp->config = config = load_config();
1106 if ( config->need_bind )
1109 * Since we were listening to the "old port", we will not see
1110 * a "listen" param change until the next IJB request. So, at
1111 * least 1 more request must be made for us to find the new
1112 * setting. I am simply closing the old socket and binding the
1115 * Which-ever is correct, we will serve 1 more page via the
1116 * old settings. This should probably be a "show-proxy-args"
1117 * request. This should not be a so common of an operation
1118 * that this will hurt people's feelings.
1123 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1124 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1125 bfd = bind_port(config->haddr, config->hport);
1129 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1130 "- There may be another junkbuster or some other "
1131 "proxy running on port %d",
1132 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1133 config->hport, config->hport
1135 /* shouldn't get here */
1139 config->need_bind = 0;
1142 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1144 if (!accept_connection(csp, bfd))
1146 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1159 log_error(LOG_LEVEL_CONNECT, "OK");
1163 /* by haroon - most of credit to srt19170 */
1164 csp->toggled_on = g_bToggleIJB;
1167 if (run_loader(csp))
1169 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1170 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1173 if (block_acl(NULL,csp))
1175 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1176 close_socket(csp->cfd);
1181 /* add it to the list of clients */
1182 csp->next = clients->next;
1183 clients->next = csp;
1185 if (config->multi_threaded)
1189 /* this is a switch () statment in the C preprocessor - ugh */
1190 #undef SELECTED_ONE_OPTION
1192 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1193 #define SELECTED_ONE_OPTION
1194 child_id = _beginthread(
1200 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1201 #define SELECTED_ONE_OPTION
1203 thread_id tid = spawn_thread
1204 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1206 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1208 child_id = (int) tid;
1217 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1218 #define SELECTED_ONE_OPTION
1219 csp->cfd = ReleaseSocket(csp->cfd, -1);
1220 if((child_id = (int)CreateNewProcTags(
1221 NP_Entry, (ULONG)server_thread,
1222 NP_Output, Output(),
1223 NP_CloseOutput, FALSE,
1224 NP_Name, (ULONG)"junkbuster child",
1225 NP_StackSize, 20*1024,
1229 ((struct Task *)child_id)->tc_UserData = csp;
1230 Signal((struct Task *)child_id, SIGF_SINGLE);
1235 #if !defined(SELECTED_ONE_OPTION)
1239 #undef SELECTED_ONE_OPTION
1240 /* end of cpp switch () */
1242 if (child_id < 0) /* failed */
1246 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1248 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1250 write_socket(csp->cfd, buf, strlen(buf));
1251 close_socket(csp->cfd);
1257 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1258 /* This block is only needed when using fork().
1259 * When using threads, the server thread was
1260 * created and run by the call to _beginthread().
1262 if (child_id == 0) /* child */
1270 /* in a fork()'d environment, the parent's
1271 * copy of the client socket and the CSP
1275 #if !defined(_WIN32) && defined(__CYGWIN__)
1277 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1278 close_socket(csp->cfd);
1281 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */