1 const char jcc_rcs[] = "$Id: jcc.c,v 1.9 2001/05/26 00:28:36 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.9 2001/05/26 00:28:36 jongfoster
37 * Automatic reloading of config file.
38 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
39 * Most of the global variables have been moved to a new
40 * struct configuration_spec, accessed through csp->config->globalname
41 * Most of the globals remaining are used by the Win32 GUI.
43 * Revision 1.8 2001/05/25 22:43:18 jongfoster
44 * Fixing minor memory leak and buffer overflow.
46 * Revision 1.7 2001/05/25 22:34:30 jongfoster
49 * Revision 1.6 2001/05/23 00:13:58 joergs
50 * AmigaOS support fixed.
52 * Revision 1.5 2001/05/22 18:46:04 oes
54 * - Enabled filtering banners by size rather than URL
55 * by adding patterns that replace all standard banner
56 * sizes with the "Junkbuster" gif to the re_filterfile
58 * - Enabled filtering WebBugs by providing a pattern
59 * which kills all 1x1 images
61 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
62 * which is selected by the (nonstandard and therefore
63 * capital) letter 'U' in the option string.
64 * It causes the quantifiers to be ungreedy by default.
65 * Appending a ? turns back to greedy (!).
67 * - Added a new interceptor ijb-send-banner, which
68 * sends back the "Junkbuster" gif. Without imagelist or
69 * MSIE detection support, or if tinygif = 1, or the
70 * URL isn't recognized as an imageurl, a lame HTML
71 * explanation is sent instead.
73 * - Added new feature, which permits blocking remote
74 * script redirects and firing back a local redirect
76 * The feature is conditionally compiled, i.e. it
77 * can be disabled with --disable-fast-redirects,
78 * plus it must be activated by a "fast-redirects"
79 * line in the config file, has its own log level
80 * and of course wants to be displayed by show-proxy-args
81 * Note: Boy, all the #ifdefs in 1001 locations and
82 * all the fumbling with configure.in and acconfig.h
83 * were *way* more work than the feature itself :-(
85 * - Because a generic redirect template was needed for
86 * this, tinygif = 3 now uses the same.
88 * - Moved GIFs, and other static HTTP response templates
93 * - Removed some >400 CRs again (Jon, you really worked
96 * Revision 1.4 2001/05/21 19:34:01 jongfoster
97 * Made failure to bind() a fatal error.
99 * Revision 1.3 2001/05/20 01:21:20 jongfoster
100 * Version 2.9.4 checkin.
101 * - Merged popupfile and cookiefile, and added control over PCRS
102 * filtering, in new "permissionsfile".
103 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
104 * file error you now get a message box (in the Win32 GUI) rather
105 * than the program exiting with no explanation.
106 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
108 * - Removed tabs from "config"
109 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
110 * - Bumped up version number.
112 * Revision 1.2 2001/05/17 22:34:44 oes
113 * - Added hint on GIF char array generation to jcc.c
114 * - Cleaned CRLF's from the sources and related files
115 * - Repaired logging for REF and FRC
117 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
118 * Initial import of version 2.9.3 source tree
121 *********************************************************************/
127 #include <sys/types.h>
136 # include <sys/timeb.h>
137 # include <windows.h>
139 # include <process.h>
142 # endif /* def TOGGLE */
145 # ifndef _WIN_CONSOLE
147 # endif /* ndef _WIN_CONSOLE */
149 #else /* ifndef _WIN32 */
152 # include <sys/time.h>
153 # include <sys/wait.h>
154 # include <sys/stat.h>
158 # include <socket.h> /* BeOS has select() for sockets only. */
159 # include <OS.h> /* declarations for threads and stuff. */
172 #include "showargs.h"
174 #include "killpopup.h"
175 #include "miscutil.h"
177 #include "jbsockets.h"
180 const char jcc_h_rcs[] = JCC_H_VERSION;
181 const char project_h_rcs[] = PROJECT_H_VERSION;
183 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
185 struct client_state clients[1];
186 struct file_list files[1];
189 int urls_read = 0; /* total nr of urls read inc rejected */
190 int urls_rejected = 0; /* total nr of urls rejected */
191 #endif /* def STATISTICS */
194 static void listen_loop(void);
195 static void chat(struct client_state *csp);
197 void serve(struct client_state *csp);
198 #else /* ifndef AMIGA */
199 static void serve(struct client_state *csp);
200 #endif /* def AMIGA */
203 static int32 server_thread(void *data);
204 #endif /* def __BEOS__ */
207 #define sleep(N) Sleep(((N) * 1000))
211 /*********************************************************************
215 * Description : Once a connection to the client has been accepted,
216 * this function is called (via serve()) to handle the
217 * main business of the communication. When this
218 * function returns, the caller must close the client
222 * 1 : csp = Current client state (buffers, headers, etc...)
224 * Returns : On success, the number of bytes written are returned (zero
225 * indicates nothing was written). On error, -1 is returned,
226 * and errno is set appropriately. If count is zero and the
227 * file descriptor refers to a regular file, 0 will be
228 * returned without causing any other effect. For a special
229 * file, the results are not portable.
231 *********************************************************************/
232 static void chat(struct client_state *csp)
234 /* This next line is a little ugly, but it simplifies the if statement below. */
235 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
237 # define IS_TOGGLED_ON csp->toggled_on &&
238 #else /* ifndef TOGGLE */
239 # define IS_TOGGLED_ON
240 #endif /* ndef TOGGLE */
242 /* This next line is a little ugly, but it simplifies the if statement below. */
243 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
245 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
246 #else /* ifndef TRUST_FILES */
247 # define IS_TRUSTED_URL
248 #endif /* ndef TRUST_FILES */
250 char buf[BUFSIZ], *hdr, *p, *req;
254 int n, maxfd, server_body, ms_iis5_hack = 0;
255 const struct gateway *gw;
256 struct http_request *http;
258 int block_popups; /* bool, 1==will block popups */
259 int block_popups_now = 0; /* bool, 1==currently blocking popups */
260 #endif /* def KILLPOPUPS */
262 int pcrs_filter; /* bool, 1==will filter through pcrs */
263 int filtering = 0; /* bool, 1==currently filtering through pcrs */
264 #endif /* def PCRS */
269 * Read the client's request. Note that since we're not using select() we
270 * could get blocked here if a client connected, then didn't say anything!
275 n = read_socket(csp->cfd, buf, sizeof(buf));
277 if (n <= 0) break; /* error! */
279 add_to_iob(csp, buf, n);
281 req = get_header(csp);
285 break; /* no HTTP request! */
290 continue; /* more to come! */
294 /* If this request contains the FORCE_PREFIX,
295 * better get rid of it now and set the force flag --oes
298 if (strstr(req, FORCE_PREFIX))
300 strclean(req, FORCE_PREFIX);
301 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
308 #endif /* def FORCE_LOAD */
310 parse_http_request(req, http, csp);
315 if (http->cmd == NULL)
317 strcpy(buf, CHEADER);
318 write_socket(csp->cfd, buf, strlen(buf));
322 /* decide how to route the HTTP request */
324 if ((gw = forward_url(http, csp)) == NULL)
326 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
327 /* Never get here - LOG_LEVEL_FATAL causes program exit */
330 /* build the http request to send to the server
331 * we have to do one of the following:
333 * create = use the original HTTP request to create a new
334 * HTTP request that has only the path component
335 * without the http://domainspec
336 * pass = pass the original HTTP request unchanged
338 * drop = drop the HTTP request
343 * +--------+--------+
345 * 0 | create | drop |
347 * Forwarding +--------+--------+
351 * +--------+--------+
355 if (gw->forward_host)
357 /* if forwarding, just pass the request as is */
358 enlist(csp->headers, http->cmd);
364 /* otherwise elide the host information from the url */
366 p = strsav(p, http->gpc);
368 p = strsav(p, http->path);
370 p = strsav(p, http->ver);
371 enlist(csp->headers, p);
376 /* decide what we're to do with cookies */
379 if (!csp->toggled_on)
381 /* Most compatible set of permissions */
382 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
386 csp->permissions = url_permissions(http, csp);
388 #else /* ifndef TOGGLE */
389 csp->permissions = url_permissions(http, csp);
390 #endif /* ndef TOGGLE */
393 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
394 #endif /* def KILLPOPUPS */
396 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
397 ((csp->permissions & PERMIT_RE_FILTER) != 0);
398 #endif /* def PCRS */
401 /* grab the rest of the client's headers */
405 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
407 n = read_socket(csp->cfd, buf, sizeof(buf));
410 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
413 add_to_iob(csp, buf, n);
417 if (p == NULL) break;
419 enlist(csp->headers, p);
423 /* filter it as required */
425 hdr = sed(client_patterns, add_client_headers, csp);
427 destroy_list(csp->headers);
429 /* Check the request against all rules, unless
430 * we're toggled off or in force mode.
436 #endif /* def FORCE_LOAD */
437 ( (p = intercept_url(http, csp)) ||
439 (p = block_url(http, csp))
440 #ifdef FAST_REDIRECTS
441 || (csp->config->fast_redirects && (p = redirect_url(http, csp)))
442 #endif /* def FAST_REDIRECTS */
447 #endif /* def STATISTICS */
449 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
451 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
452 /* Block as image? */
453 if ( (csp->config->tinygif > 0) && block_imageurl(http, csp) )
455 /* Send "blocked" image */
456 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
457 http->hostport, http->path);
459 if ((csp->config->tinygif == 2) || strstr(http->path, "ijb-send-banner"))
461 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
463 else if (csp->config->tinygif == 1)
465 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
467 else if ((csp->config->tinygif == 3) && (csp->config->tinygifurl))
470 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE)
471 + strlen(csp->config->tinygifurl));
472 sprintf(p, HTTP_REDIRECT_TEMPLATE, csp->config->tinygifurl);
473 write_socket(csp->cfd, p, strlen(p));
477 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
481 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
484 /* Send HTML "blocked" message, interception, or redirection result */
485 write_socket(csp->cfd, p, strlen(p));
488 log_error(LOG_LEVEL_LOG, "%s", p);
495 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
497 if (gw->forward_host)
499 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
500 gw->forward_host, gw->forward_port, http->hostport);
504 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
507 /* here we connect to the server, gateway, or the forwarder */
509 csp->sfd = (gw->conn)(gw, http, csp);
513 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
518 err = zalloc(strlen(CNXDOM) + strlen(http->host));
519 sprintf(err, CNXDOM, http->host);
523 eno = safe_strerror(errno);
524 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
525 sprintf(err, CFAIL, http->hostport, eno);
528 write_socket(csp->cfd, err, strlen(err));
530 log_error(LOG_LEVEL_LOG, err);
537 log_error(LOG_LEVEL_CONNECT, "OK");
539 if (gw->forward_host || (http->ssl == 0))
541 /* write the client's (modified) header to the server
542 * (along with anything else that may be in the buffer)
547 if ((write_socket(csp->sfd, hdr, n) != n)
548 || (flush_socket(csp->sfd, csp ) < 0))
550 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
553 eno = safe_strerror(errno);
554 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
555 sprintf(err, CFAIL, http->hostport, eno);
556 write_socket(csp->cfd, err, strlen(err));
566 * We're running an SSL tunnel and we're not forwarding,
567 * so just send the "connect succeeded" message to the
568 * client, flush the rest, and get out of the way.
570 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
578 /* we're finished with the client's header */
581 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
583 /* pass data between the client and server
584 * until one or the other shuts down the connection.
593 FD_SET(csp->cfd, &rfds);
594 FD_SET(csp->sfd, &rfds);
596 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
600 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
604 /* this is the body of the browser's request
605 * just read it and write it.
608 if (FD_ISSET(csp->cfd, &rfds))
610 n = read_socket(csp->cfd, buf, sizeof(buf));
612 if (n <= 0) break; /* "game over, man" */
614 if (write_socket(csp->sfd, buf, n) != n)
616 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
623 * The server wants to talk. It could be the header or the body.
624 * If `hdr' is null, then it's the header otherwise it's the body.
625 * FIXME: Does `hdr' really mean `host'?
629 if (FD_ISSET(csp->sfd, &rfds))
632 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
636 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
638 eno = safe_strerror(errno);
639 sprintf(buf, CFAIL, http->hostport, eno);
641 write_socket(csp->cfd, buf, strlen(buf));
645 /* Add a trailing zero. This lets filter_popups
646 * use string operations.
651 /* Filter the popups on this read. */
652 if (block_popups_now)
654 filter_popups(buf, n);
656 #endif /* def KILLPOPUPS */
658 /* Normally, this would indicate that we've read
659 * as much as the server has sent us and we can
660 * close the client connection. However, Microsoft
661 * in its wisdom has released IIS/5 with a bug that
662 * prevents it from sending the trailing \r\n in
663 * a 302 redirect header (and possibly other headers).
664 * To work around this if we've haven't parsed
665 * a full header we'll append a trailing \r\n
666 * and see if this now generates a valid one.
668 * This hack shouldn't have any impacts. If we've
669 * already transmitted the header or if this is a
670 * SSL connection, then we won't bother with this
671 * hack. So we only work on partially received
672 * headers. If we append a \r\n and this still
673 * doesn't generate a valid header, then we won't
674 * transmit anything to the client.
678 /* This hack must only be enforced for headers. */
679 if (server_body || http->ssl)
684 re_process_buffer(csp);
686 #endif /* def PCRS */
687 break; /* "game over, man" */
690 /* Let's pretend the server just sent us a blank line. */
691 n = sprintf(buf, "\r\n");
694 * Now, let the normal header parsing algorithm below do its
695 * job. If it fails, we'll exit instead of continuing.
702 * If this is an SSL connection or we're in the body
703 * of the server document, just write it to the client.
706 if (server_body || http->ssl)
711 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
714 #endif /* def PCRS */
717 if (write_socket(csp->cfd, buf, n) != n)
719 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
727 /* we're still looking for the end of the
728 * server's header ... (does that make header
729 * parsing an "out of body experience" ?
732 /* buffer up the data we just read */
733 add_to_iob(csp, buf, n);
735 /* get header lines from the iob */
737 while ((p = get_header(csp)))
741 /* see following note */
744 enlist(csp->headers, p);
748 /* NOTE: there are no "empty" headers so
749 * if the pointer `p' is not NULL we must
750 * assume that we reached the end of the
751 * buffer before we hit the end of the header.
758 /* Well, we tried our MS IIS/5
759 * hack and it didn't work.
760 * The header is incomplete
761 * and there isn't anything
762 * we can do about it.
768 /* Since we have to wait for
769 * more from the server before
770 * we can parse the headers
771 * we just continue here.
777 /* we have now received the entire header.
778 * filter it and send the result to the client
781 hdr = sed(server_patterns, add_server_headers, csp);
784 /* write the server's (modified) header to
785 * the client (along with anything else that
786 * may be in the buffer)
790 /* Start blocking popups if appropriate. */
792 if (csp->is_text && /* It's a text / * MIME-Type */
793 !http->ssl && /* We talk plaintext */
796 block_popups_now = 1;
799 #endif /* def KILLPOPUPS */
802 /* Start re_filtering this if appropriate. */
804 if (csp->is_text && /* It's a text / * MIME-Type */
805 !http->ssl && /* We talk plaintext */
806 pcrs_filter) /* Policy allows */
811 /* This next line is a little ugly, but it simplifies the if statement below. */
812 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
813 #define NOT_FILTERING_AND !filtering &&
815 #else /* not def PCRS */
817 #define NOT_FILTERING_AND
819 #endif /* def PCRS */
822 if ((write_socket(csp->cfd, hdr, n) != n)
823 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
825 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
827 /* the write failed, so don't bother
828 * mentioning it to the client...
829 * it probably can't hear us anyway.
835 /* we're finished with the server's header */
840 /* If this was a MS IIS/5 hack then it means
841 * the server has already closed the
842 * connection. Nothing more to read. Time
853 return; /* huh? we should never get here */
860 /*********************************************************************
864 * Description : This is little more than chat. We only "serve" to
865 * to close any socket that chat may have opened.
868 * 1 : csp = Current client state (buffers, headers, etc...)
872 *********************************************************************/
874 void serve(struct client_state *csp)
875 #else /* ifndef AMIGA */
876 static void serve(struct client_state *csp)
877 #endif /* def AMIGA */
880 close_socket(csp->cfd);
884 close_socket(csp->sfd);
894 /*********************************************************************
896 * Function : server_thread
898 * Description : We only exist to call `serve' in a threaded environment.
901 * 1 : data = Current client state (buffers, headers, etc...)
903 * Returns : Always 0.
905 *********************************************************************/
906 static int32 server_thread(void *data)
908 serve((struct client_state *) data);
916 /*********************************************************************
920 * Description : Load the config file and start the listen loop.
921 * This function is a lot more *sane* with the `load_config'
922 * and `listen_loop' functions; although it stills does
923 * a *little* too much for my taste.
926 * 1 : argc = Number of parameters (including $0).
927 * 2 : argv = Array of (char *)'s to the parameters.
929 * Returns : 1 if : can't open config file, unrecognized directive,
930 * stats requested in multi-thread mode, can't open the
931 * log file, can't open the jar file, listen port is invalid,
932 * any load fails, and can't bind port.
934 * Else main never returns, the process must be signaled
935 * to terminate execution. Or, on Windows, use the
936 * "File", "Exit" menu option.
938 *********************************************************************/
940 int _main(int argc, const char *argv[])
942 int main(int argc, const char *argv[])
947 "AmiTCP:db/junkbuster.config"
948 #elif !defined(_WIN32)
955 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
956 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
958 printf("JunkBuster proxy version " VERSION ".\n\n"
959 "Usage: %s [configfile]\n\n"
960 "See " HOME_PAGE_URL " for details.\n"
961 "This program is distributed under the GNU GPL, version 2 or later.\n",
965 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
967 printf(VERSION "\n");
970 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
981 configfile = argv[1];
992 signal(SIGPIPE, SIG_IGN);
993 signal(SIGCHLD, SIG_IGN);
995 #else /* ifdef _WIN32 */
998 * We *are* in a windows console app.
999 * Print a verbose messages about FAQ's and such
1001 printf(win32_blurb);
1002 # endif /* def _WIN_CONSOLE */
1003 #endif /* def _WIN32 */
1014 /*********************************************************************
1016 * Function : listen_loop
1018 * Description : bind the listen port and enter a "FOREVER" listening loop.
1024 *********************************************************************/
1025 static void listen_loop(void)
1027 struct client_state *csp = NULL;
1029 struct configuration_spec * config;
1031 config = load_config();
1033 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1034 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1036 bfd = bind_port(config->haddr, config->hport);
1040 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1041 "- There may be another junkbuster or some other "
1042 "proxy running on port %d",
1043 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1044 config->hport, config->hport
1046 /* shouldn't get here */
1050 config->need_bind = 0;
1055 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1056 while (waitpid(-1, NULL, WNOHANG) > 0)
1058 /* zombie children */
1060 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1063 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1065 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1069 memset(csp, '\0', sizeof(*csp));
1074 csp->config = config = load_config();
1076 if ( config->need_bind )
1079 * Since we were listening to the "old port", we will not see
1080 * a "listen" param change until the next IJB request. So, at
1081 * least 1 more request must be made for us to find the new
1082 * setting. I am simply closing the old socket and binding the
1085 * Which-ever is correct, we will serve 1 more page via the
1086 * old settings. This should probably be a "show-proxy-args"
1087 * request. This should not be a so common of an operation
1088 * that this will hurt people's feelings.
1093 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1094 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1095 bfd = bind_port(config->haddr, config->hport);
1099 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1100 "- There may be another junkbuster or some other "
1101 "proxy running on port %d",
1102 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1103 config->hport, config->hport
1105 /* shouldn't get here */
1109 config->need_bind = 0;
1112 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1114 if (!accept_connection(csp, bfd))
1116 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1129 log_error(LOG_LEVEL_CONNECT, "OK");
1133 /* by haroon - most of credit to srt19170 */
1134 csp->toggled_on = g_bToggleIJB;
1137 if (run_loader(csp))
1139 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1140 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1143 if (block_acl(NULL,csp))
\r
1145 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
\r
1146 close_socket(csp->cfd);
\r
1151 /* add it to the list of clients */
\r
1152 csp->next = clients->next;
\r
1153 clients->next = csp;
\r
1155 if (config->multi_threaded)
1159 /* this is a switch () statment in the C preprocessor - ugh */
1160 #undef SELECTED_ONE_OPTION
1162 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1163 #define SELECTED_ONE_OPTION
1164 child_id = _beginthread(
1170 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1171 #define SELECTED_ONE_OPTION
1173 thread_id tid = spawn_thread
1174 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1176 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1178 child_id = (int) tid;
1187 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1188 #define SELECTED_ONE_OPTION
1189 csp->cfd = ReleaseSocket(csp->cfd, -1);
1190 if((child_id = (int)CreateNewProcTags(
1191 NP_Entry, (ULONG)server_thread,
1192 NP_Output, Output(),
1193 NP_CloseOutput, FALSE,
1194 NP_Name, (ULONG)"junkbuster child",
1195 NP_StackSize, 20*1024,
1199 ((struct Task *)child_id)->tc_UserData = csp;
1200 Signal((struct Task *)child_id, SIGF_SINGLE);
1205 #if !defined(SELECTED_ONE_OPTION)
1209 #undef SELECTED_ONE_OPTION
1210 /* end of cpp switch () */
1212 if (child_id < 0) /* failed */
1216 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1218 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1220 write_socket(csp->cfd, buf, strlen(buf));
1221 close_socket(csp->cfd);
1227 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1228 /* This block is only needed when using fork().
1229 * When using threads, the server thread was
1230 * created and run by the call to _beginthread().
1232 if (child_id == 0) /* child */
1240 /* in a fork()'d environment, the parent's
1241 * copy of the client socket and the CSP
1245 #if !defined(_WIN32) && defined(__CYGWIN__)
1247 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1248 close_socket(csp->cfd);
1251 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */