1 const char jcc_rcs[] = "$Id: jcc.c,v 1.11 2001/05/26 17:27:53 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.11 2001/05/26 17:27:53 jongfoster
37 * Added support for CLF and fixed LOG_LEVEL_LOG.
38 * Also did CRLF->LF fix of my previous patch.
40 * Revision 1.10 2001/05/26 15:26:15 jongfoster
41 * ACL feature now provides more security by immediately dropping
42 * connections from untrusted hosts.
44 * Revision 1.9 2001/05/26 00:28:36 jongfoster
45 * Automatic reloading of config file.
46 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
47 * Most of the global variables have been moved to a new
48 * struct configuration_spec, accessed through csp->config->globalname
49 * Most of the globals remaining are used by the Win32 GUI.
51 * Revision 1.8 2001/05/25 22:43:18 jongfoster
52 * Fixing minor memory leak and buffer overflow.
54 * Revision 1.7 2001/05/25 22:34:30 jongfoster
57 * Revision 1.6 2001/05/23 00:13:58 joergs
58 * AmigaOS support fixed.
60 * Revision 1.5 2001/05/22 18:46:04 oes
62 * - Enabled filtering banners by size rather than URL
63 * by adding patterns that replace all standard banner
64 * sizes with the "Junkbuster" gif to the re_filterfile
66 * - Enabled filtering WebBugs by providing a pattern
67 * which kills all 1x1 images
69 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
70 * which is selected by the (nonstandard and therefore
71 * capital) letter 'U' in the option string.
72 * It causes the quantifiers to be ungreedy by default.
73 * Appending a ? turns back to greedy (!).
75 * - Added a new interceptor ijb-send-banner, which
76 * sends back the "Junkbuster" gif. Without imagelist or
77 * MSIE detection support, or if tinygif = 1, or the
78 * URL isn't recognized as an imageurl, a lame HTML
79 * explanation is sent instead.
81 * - Added new feature, which permits blocking remote
82 * script redirects and firing back a local redirect
84 * The feature is conditionally compiled, i.e. it
85 * can be disabled with --disable-fast-redirects,
86 * plus it must be activated by a "fast-redirects"
87 * line in the config file, has its own log level
88 * and of course wants to be displayed by show-proxy-args
89 * Note: Boy, all the #ifdefs in 1001 locations and
90 * all the fumbling with configure.in and acconfig.h
91 * were *way* more work than the feature itself :-(
93 * - Because a generic redirect template was needed for
94 * this, tinygif = 3 now uses the same.
96 * - Moved GIFs, and other static HTTP response templates
101 * - Removed some >400 CRs again (Jon, you really worked
104 * Revision 1.4 2001/05/21 19:34:01 jongfoster
105 * Made failure to bind() a fatal error.
107 * Revision 1.3 2001/05/20 01:21:20 jongfoster
108 * Version 2.9.4 checkin.
109 * - Merged popupfile and cookiefile, and added control over PCRS
110 * filtering, in new "permissionsfile".
111 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
112 * file error you now get a message box (in the Win32 GUI) rather
113 * than the program exiting with no explanation.
114 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
116 * - Removed tabs from "config"
117 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
118 * - Bumped up version number.
120 * Revision 1.2 2001/05/17 22:34:44 oes
121 * - Added hint on GIF char array generation to jcc.c
122 * - Cleaned CRLF's from the sources and related files
123 * - Repaired logging for REF and FRC
125 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
126 * Initial import of version 2.9.3 source tree
129 *********************************************************************/
135 #include <sys/types.h>
144 # include <sys/timeb.h>
145 # include <windows.h>
147 # include <process.h>
150 # endif /* def TOGGLE */
153 # ifndef _WIN_CONSOLE
155 # endif /* ndef _WIN_CONSOLE */
157 #else /* ifndef _WIN32 */
160 # include <sys/time.h>
161 # include <sys/wait.h>
162 # include <sys/stat.h>
166 # include <socket.h> /* BeOS has select() for sockets only. */
167 # include <OS.h> /* declarations for threads and stuff. */
180 #include "showargs.h"
182 #include "killpopup.h"
183 #include "miscutil.h"
185 #include "jbsockets.h"
188 const char jcc_h_rcs[] = JCC_H_VERSION;
189 const char project_h_rcs[] = PROJECT_H_VERSION;
191 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
193 struct client_state clients[1];
194 struct file_list files[1];
197 int urls_read = 0; /* total nr of urls read inc rejected */
198 int urls_rejected = 0; /* total nr of urls rejected */
199 #endif /* def STATISTICS */
202 static void listen_loop(void);
203 static void chat(struct client_state *csp);
205 void serve(struct client_state *csp);
206 #else /* ifndef AMIGA */
207 static void serve(struct client_state *csp);
208 #endif /* def AMIGA */
211 static int32 server_thread(void *data);
212 #endif /* def __BEOS__ */
215 #define sleep(N) Sleep(((N) * 1000))
219 /*********************************************************************
223 * Description : Once a connection to the client has been accepted,
224 * this function is called (via serve()) to handle the
225 * main business of the communication. When this
226 * function returns, the caller must close the client
230 * 1 : csp = Current client state (buffers, headers, etc...)
232 * Returns : On success, the number of bytes written are returned (zero
233 * indicates nothing was written). On error, -1 is returned,
234 * and errno is set appropriately. If count is zero and the
235 * file descriptor refers to a regular file, 0 will be
236 * returned without causing any other effect. For a special
237 * file, the results are not portable.
239 *********************************************************************/
240 static void chat(struct client_state *csp)
242 /* This next line is a little ugly, but it simplifies the if statement below. */
243 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
245 # define IS_TOGGLED_ON csp->toggled_on &&
246 #else /* ifndef TOGGLE */
247 # define IS_TOGGLED_ON
248 #endif /* ndef TOGGLE */
250 /* This next line is a little ugly, but it simplifies the if statement below. */
251 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
253 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
254 #else /* ifndef TRUST_FILES */
255 # define IS_TRUSTED_URL
256 #endif /* ndef TRUST_FILES */
258 char buf[BUFSIZ], *hdr, *p, *req;
262 int n, maxfd, server_body;
263 int ms_iis5_hack = 0;
265 const struct gateway *gw;
266 struct http_request *http;
268 int block_popups; /* bool, 1==will block popups */
269 int block_popups_now = 0; /* bool, 1==currently blocking popups */
270 #endif /* def KILLPOPUPS */
272 int pcrs_filter; /* bool, 1==will filter through pcrs */
273 int filtering = 0; /* bool, 1==currently filtering through pcrs */
274 #endif /* def PCRS */
279 * Read the client's request. Note that since we're not using select() we
280 * could get blocked here if a client connected, then didn't say anything!
285 n = read_socket(csp->cfd, buf, sizeof(buf));
287 if (n <= 0) break; /* error! */
289 add_to_iob(csp, buf, n);
291 req = get_header(csp);
295 break; /* no HTTP request! */
300 continue; /* more to come! */
304 /* If this request contains the FORCE_PREFIX,
305 * better get rid of it now and set the force flag --oes
308 if (strstr(req, FORCE_PREFIX))
310 strclean(req, FORCE_PREFIX);
311 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
318 #endif /* def FORCE_LOAD */
320 parse_http_request(req, http, csp);
325 if (http->cmd == NULL)
327 strcpy(buf, CHEADER);
328 write_socket(csp->cfd, buf, strlen(buf));
330 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
335 /* decide how to route the HTTP request */
337 if ((gw = forward_url(http, csp)) == NULL)
339 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
340 /* Never get here - LOG_LEVEL_FATAL causes program exit */
343 /* build the http request to send to the server
344 * we have to do one of the following:
346 * create = use the original HTTP request to create a new
347 * HTTP request that has only the path component
348 * without the http://domainspec
349 * pass = pass the original HTTP request unchanged
351 * drop = drop the HTTP request
356 * +--------+--------+
358 * 0 | create | drop |
360 * Forwarding +--------+--------+
364 * +--------+--------+
368 if (gw->forward_host)
370 /* if forwarding, just pass the request as is */
371 enlist(csp->headers, http->cmd);
377 /* otherwise elide the host information from the url */
379 p = strsav(p, http->gpc);
381 p = strsav(p, http->path);
383 p = strsav(p, http->ver);
384 enlist(csp->headers, p);
389 /* decide what we're to do with cookies */
392 if (!csp->toggled_on)
394 /* Most compatible set of permissions */
395 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
399 csp->permissions = url_permissions(http, csp);
401 #else /* ifndef TOGGLE */
402 csp->permissions = url_permissions(http, csp);
403 #endif /* ndef TOGGLE */
406 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
407 #endif /* def KILLPOPUPS */
409 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
410 ((csp->permissions & PERMIT_RE_FILTER) != 0);
411 #endif /* def PCRS */
414 /* grab the rest of the client's headers */
418 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
420 n = read_socket(csp->cfd, buf, sizeof(buf));
423 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
426 add_to_iob(csp, buf, n);
430 if (p == NULL) break;
432 enlist(csp->headers, p);
436 /* filter it as required */
438 hdr = sed(client_patterns, add_client_headers, csp);
440 destroy_list(csp->headers);
442 /* Check the request against all rules, unless
443 * we're toggled off or in force mode.
449 #endif /* def FORCE_LOAD */
450 ( (p = intercept_url(http, csp)) ||
452 (p = block_url(http, csp))
453 #ifdef FAST_REDIRECTS
454 || (csp->config->fast_redirects && (p = redirect_url(http, csp)))
455 #endif /* def FAST_REDIRECTS */
460 #endif /* def STATISTICS */
462 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
464 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 1",
465 csp->ip_addr_str, http->cmd);
467 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
468 /* Block as image? */
469 if ( (csp->config->tinygif > 0) && block_imageurl(http, csp) )
471 /* Send "blocked" image */
472 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
473 http->hostport, http->path);
475 if ((csp->config->tinygif == 2) || strstr(http->path, "ijb-send-banner"))
477 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
479 else if (csp->config->tinygif == 1)
481 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
483 else if ((csp->config->tinygif == 3) && (csp->config->tinygifurl))
486 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE)
487 + strlen(csp->config->tinygifurl));
488 sprintf(p, HTTP_REDIRECT_TEMPLATE, csp->config->tinygifurl);
489 write_socket(csp->cfd, p, strlen(p));
493 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
497 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
500 /* Send HTML "blocked" message, interception, or redirection result */
501 write_socket(csp->cfd, p, strlen(p));
509 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
511 if (gw->forward_host)
513 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
514 gw->forward_host, gw->forward_port, http->hostport);
518 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
521 /* here we connect to the server, gateway, or the forwarder */
523 csp->sfd = (gw->conn)(gw, http, csp);
527 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
532 err = zalloc(strlen(CNXDOM) + strlen(http->host));
533 sprintf(err, CNXDOM, http->host);
535 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
536 csp->ip_addr_str, http->cmd);
540 eno = safe_strerror(errno);
541 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
542 sprintf(err, CFAIL, http->hostport, eno);
544 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
545 csp->ip_addr_str, http->cmd);
548 write_socket(csp->cfd, err, strlen(err));
555 log_error(LOG_LEVEL_CONNECT, "OK");
557 if (gw->forward_host || (http->ssl == 0))
559 /* write the client's (modified) header to the server
560 * (along with anything else that may be in the buffer)
565 if ((write_socket(csp->sfd, hdr, n) != n)
566 || (flush_socket(csp->sfd, csp ) < 0))
568 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
571 eno = safe_strerror(errno);
572 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
573 sprintf(err, CFAIL, http->hostport, eno);
574 write_socket(csp->cfd, err, strlen(err));
576 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
577 csp->ip_addr_str, http->cmd);
587 * We're running an SSL tunnel and we're not forwarding,
588 * so just send the "connect succeeded" message to the
589 * client, flush the rest, and get out of the way.
591 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
592 csp->ip_addr_str, http->cmd);
594 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
602 /* we're finished with the client's header */
605 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
607 /* pass data between the client and server
608 * until one or the other shuts down the connection.
617 FD_SET(csp->cfd, &rfds);
618 FD_SET(csp->sfd, &rfds);
620 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
624 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
628 /* this is the body of the browser's request
629 * just read it and write it.
632 if (FD_ISSET(csp->cfd, &rfds))
634 n = read_socket(csp->cfd, buf, sizeof(buf));
638 break; /* "game over, man" */
641 if (write_socket(csp->sfd, buf, n) != n)
643 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
650 * The server wants to talk. It could be the header or the body.
651 * If `hdr' is null, then it's the header otherwise it's the body.
652 * FIXME: Does `hdr' really mean `host'?
656 if (FD_ISSET(csp->sfd, &rfds))
659 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
663 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
665 eno = safe_strerror(errno);
666 sprintf(buf, CFAIL, http->hostport, eno);
669 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
670 csp->ip_addr_str, http->cmd);
672 write_socket(csp->cfd, buf, strlen(buf));
676 /* Add a trailing zero. This lets filter_popups
677 * use string operations.
682 /* Filter the popups on this read. */
683 if (block_popups_now)
685 filter_popups(buf, n);
687 #endif /* def KILLPOPUPS */
689 /* Normally, this would indicate that we've read
690 * as much as the server has sent us and we can
691 * close the client connection. However, Microsoft
692 * in its wisdom has released IIS/5 with a bug that
693 * prevents it from sending the trailing \r\n in
694 * a 302 redirect header (and possibly other headers).
695 * To work around this if we've haven't parsed
696 * a full header we'll append a trailing \r\n
697 * and see if this now generates a valid one.
699 * This hack shouldn't have any impacts. If we've
700 * already transmitted the header or if this is a
701 * SSL connection, then we won't bother with this
702 * hack. So we only work on partially received
703 * headers. If we append a \r\n and this still
704 * doesn't generate a valid header, then we won't
705 * transmit anything to the client.
709 /* This hack must only be enforced for headers. */
710 if (server_body || http->ssl)
715 p = re_process_buffer(csp);
716 hdr = sed(server_patterns, add_server_headers, csp);
718 if ((write_socket(csp->cfd, hdr, n) != n)
719 || (write_socket(csp->cfd, p, csp->content_length) != csp->content_length))
721 log_error(LOG_LEVEL_CONNECT, "write modified content to client failed: %E");
727 #endif /* def PCRS */
728 break; /* "game over, man" */
731 /* Let's pretend the server just sent us a blank line. */
732 n = sprintf(buf, "\r\n");
735 * Now, let the normal header parsing algorithm below do its
736 * job. If it fails, we'll exit instead of continuing.
743 * If this is an SSL connection or we're in the body
744 * of the server document, just write it to the client.
747 if (server_body || http->ssl)
752 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
755 #endif /* def PCRS */
758 if (write_socket(csp->cfd, buf, n) != n)
760 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
769 /* we're still looking for the end of the
770 * server's header ... (does that make header
771 * parsing an "out of body experience" ?
774 /* buffer up the data we just read */
775 add_to_iob(csp, buf, n);
777 /* get header lines from the iob */
779 while ((p = get_header(csp)))
783 /* see following note */
786 enlist(csp->headers, p);
790 /* NOTE: there are no "empty" headers so
791 * if the pointer `p' is not NULL we must
792 * assume that we reached the end of the
793 * buffer before we hit the end of the header.
800 /* Well, we tried our MS IIS/5
801 * hack and it didn't work.
802 * The header is incomplete
803 * and there isn't anything
804 * we can do about it.
810 /* Since we have to wait for
811 * more from the server before
812 * we can parse the headers
813 * we just continue here.
819 /* we have now received the entire header.
820 * filter it and send the result to the client
823 hdr = sed(server_patterns, add_server_headers, csp);
826 /* write the server's (modified) header to
827 * the client (along with anything else that
828 * may be in the buffer)
832 /* Start blocking popups if appropriate. */
834 if (csp->is_text && /* It's a text / * MIME-Type */
835 !http->ssl && /* We talk plaintext */
836 block_popups) /* Policy allows */
838 block_popups_now = 1;
841 #endif /* def KILLPOPUPS */
844 /* Start re_filtering this if appropriate. */
846 if (csp->is_text && /* It's a text / * MIME-Type */
847 !http->ssl && /* We talk plaintext */
848 pcrs_filter) /* Policy allows */
853 /* This next line is a little ugly, but it simplifies the if statement below. */
854 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
855 #define NOT_FILTERING_AND !filtering &&
857 #else /* not def PCRS */
859 #define NOT_FILTERING_AND
861 #endif /* def PCRS */
863 if (NOT_FILTERING_AND ((write_socket(csp->cfd, hdr, n) != n)
864 || (n = flush_socket(csp->cfd, csp) < 0)))
866 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
868 /* the write failed, so don't bother
869 * mentioning it to the client...
870 * it probably can't hear us anyway.
876 NOT_FILTERING_AND (byte_count += n);
878 /* we're finished with the server's header */
883 /* If this was a MS IIS/5 hack then it means
884 * the server has already closed the
885 * connection. Nothing more to read. Time
896 return; /* huh? we should never get here */
899 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
900 csp->ip_addr_str, http->cmd, byte_count);
904 /*********************************************************************
908 * Description : This is little more than chat. We only "serve" to
909 * to close any socket that chat may have opened.
912 * 1 : csp = Current client state (buffers, headers, etc...)
916 *********************************************************************/
918 void serve(struct client_state *csp)
919 #else /* ifndef AMIGA */
920 static void serve(struct client_state *csp)
921 #endif /* def AMIGA */
924 close_socket(csp->cfd);
928 close_socket(csp->sfd);
938 /*********************************************************************
940 * Function : server_thread
942 * Description : We only exist to call `serve' in a threaded environment.
945 * 1 : data = Current client state (buffers, headers, etc...)
947 * Returns : Always 0.
949 *********************************************************************/
950 static int32 server_thread(void *data)
952 serve((struct client_state *) data);
960 /*********************************************************************
964 * Description : Load the config file and start the listen loop.
965 * This function is a lot more *sane* with the `load_config'
966 * and `listen_loop' functions; although it stills does
967 * a *little* too much for my taste.
970 * 1 : argc = Number of parameters (including $0).
971 * 2 : argv = Array of (char *)'s to the parameters.
973 * Returns : 1 if : can't open config file, unrecognized directive,
974 * stats requested in multi-thread mode, can't open the
975 * log file, can't open the jar file, listen port is invalid,
976 * any load fails, and can't bind port.
978 * Else main never returns, the process must be signaled
979 * to terminate execution. Or, on Windows, use the
980 * "File", "Exit" menu option.
982 *********************************************************************/
984 int _main(int argc, const char *argv[])
986 int main(int argc, const char *argv[])
991 "AmiTCP:db/junkbuster.config"
992 #elif !defined(_WIN32)
999 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1000 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
1002 printf("JunkBuster proxy version " VERSION ".\n\n"
1003 "Usage: %s [configfile]\n\n"
1004 "See " HOME_PAGE_URL " for details.\n"
1005 "This program is distributed under the GNU GPL, version 2 or later.\n",
1009 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
1011 printf(VERSION "\n");
1014 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1025 configfile = argv[1];
1036 signal(SIGPIPE, SIG_IGN);
1037 signal(SIGCHLD, SIG_IGN);
1039 #else /* ifdef _WIN32 */
1040 # ifdef _WIN_CONSOLE
1042 * We *are* in a windows console app.
1043 * Print a verbose messages about FAQ's and such
1045 printf(win32_blurb);
1046 # endif /* def _WIN_CONSOLE */
1047 #endif /* def _WIN32 */
1058 /*********************************************************************
1060 * Function : listen_loop
1062 * Description : bind the listen port and enter a "FOREVER" listening loop.
1068 *********************************************************************/
1069 static void listen_loop(void)
1071 struct client_state *csp = NULL;
1073 struct configuration_spec * config;
1075 config = load_config();
1077 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1078 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1080 bfd = bind_port(config->haddr, config->hport);
1084 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1085 "- There may be another junkbuster or some other "
1086 "proxy running on port %d",
1087 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1088 config->hport, config->hport
1090 /* shouldn't get here */
1094 config->need_bind = 0;
1099 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1100 while (waitpid(-1, NULL, WNOHANG) > 0)
1102 /* zombie children */
1104 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1107 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1109 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1113 memset(csp, '\0', sizeof(*csp));
1118 csp->config = config = load_config();
1120 if ( config->need_bind )
1123 * Since we were listening to the "old port", we will not see
1124 * a "listen" param change until the next IJB request. So, at
1125 * least 1 more request must be made for us to find the new
1126 * setting. I am simply closing the old socket and binding the
1129 * Which-ever is correct, we will serve 1 more page via the
1130 * old settings. This should probably be a "show-proxy-args"
1131 * request. This should not be a so common of an operation
1132 * that this will hurt people's feelings.
1137 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1138 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1139 bfd = bind_port(config->haddr, config->hport);
1143 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1144 "- There may be another junkbuster or some other "
1145 "proxy running on port %d",
1146 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1147 config->hport, config->hport
1149 /* shouldn't get here */
1153 config->need_bind = 0;
1156 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1158 if (!accept_connection(csp, bfd))
1160 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1173 log_error(LOG_LEVEL_CONNECT, "OK");
1177 /* by haroon - most of credit to srt19170 */
1178 csp->toggled_on = g_bToggleIJB;
1181 if (run_loader(csp))
1183 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1184 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1187 if (block_acl(NULL,csp))
1189 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1190 close_socket(csp->cfd);
1195 /* add it to the list of clients */
1196 csp->next = clients->next;
1197 clients->next = csp;
1199 if (config->multi_threaded)
1203 /* this is a switch () statment in the C preprocessor - ugh */
1204 #undef SELECTED_ONE_OPTION
1206 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1207 #define SELECTED_ONE_OPTION
1208 child_id = _beginthread(
1214 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1215 #define SELECTED_ONE_OPTION
1217 thread_id tid = spawn_thread
1218 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1220 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1222 child_id = (int) tid;
1231 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1232 #define SELECTED_ONE_OPTION
1233 csp->cfd = ReleaseSocket(csp->cfd, -1);
1234 if((child_id = (int)CreateNewProcTags(
1235 NP_Entry, (ULONG)server_thread,
1236 NP_Output, Output(),
1237 NP_CloseOutput, FALSE,
1238 NP_Name, (ULONG)"junkbuster child",
1239 NP_StackSize, 20*1024,
1243 ((struct Task *)child_id)->tc_UserData = csp;
1244 Signal((struct Task *)child_id, SIGF_SINGLE);
1249 #if !defined(SELECTED_ONE_OPTION)
1253 #undef SELECTED_ONE_OPTION
1254 /* end of cpp switch () */
1256 if (child_id < 0) /* failed */
1260 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1262 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1264 write_socket(csp->cfd, buf, strlen(buf));
1265 close_socket(csp->cfd);
1271 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1272 /* This block is only needed when using fork().
1273 * When using threads, the server thread was
1274 * created and run by the call to _beginthread().
1276 if (child_id == 0) /* child */
1284 /* in a fork()'d environment, the parent's
1285 * copy of the client socket and the CSP
1289 #if !defined(_WIN32) && defined(__CYGWIN__)
1291 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1292 close_socket(csp->cfd);
1295 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */