1 const char jcc_rcs[] = "$Id: jcc.c,v 1.8 2001/05/25 22:43:18 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.8 2001/05/25 22:43:18 jongfoster
37 * Fixing minor memory leak and buffer overflow.
39 * Revision 1.7 2001/05/25 22:34:30 jongfoster
42 * Revision 1.6 2001/05/23 00:13:58 joergs
43 * AmigaOS support fixed.
45 * Revision 1.5 2001/05/22 18:46:04 oes
47 * - Enabled filtering banners by size rather than URL
48 * by adding patterns that replace all standard banner
49 * sizes with the "Junkbuster" gif to the re_filterfile
51 * - Enabled filtering WebBugs by providing a pattern
52 * which kills all 1x1 images
54 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
55 * which is selected by the (nonstandard and therefore
56 * capital) letter 'U' in the option string.
57 * It causes the quantifiers to be ungreedy by default.
58 * Appending a ? turns back to greedy (!).
60 * - Added a new interceptor ijb-send-banner, which
61 * sends back the "Junkbuster" gif. Without imagelist or
62 * MSIE detection support, or if tinygif = 1, or the
63 * URL isn't recognized as an imageurl, a lame HTML
64 * explanation is sent instead.
66 * - Added new feature, which permits blocking remote
67 * script redirects and firing back a local redirect
69 * The feature is conditionally compiled, i.e. it
70 * can be disabled with --disable-fast-redirects,
71 * plus it must be activated by a "fast-redirects"
72 * line in the config file, has its own log level
73 * and of course wants to be displayed by show-proxy-args
74 * Note: Boy, all the #ifdefs in 1001 locations and
75 * all the fumbling with configure.in and acconfig.h
76 * were *way* more work than the feature itself :-(
78 * - Because a generic redirect template was needed for
79 * this, tinygif = 3 now uses the same.
81 * - Moved GIFs, and other static HTTP response templates
86 * - Removed some >400 CRs again (Jon, you really worked
89 * Revision 1.4 2001/05/21 19:34:01 jongfoster
90 * Made failure to bind() a fatal error.
92 * Revision 1.3 2001/05/20 01:21:20 jongfoster
93 * Version 2.9.4 checkin.
94 * - Merged popupfile and cookiefile, and added control over PCRS
95 * filtering, in new "permissionsfile".
96 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
97 * file error you now get a message box (in the Win32 GUI) rather
98 * than the program exiting with no explanation.
99 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
101 * - Removed tabs from "config"
102 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
103 * - Bumped up version number.
105 * Revision 1.2 2001/05/17 22:34:44 oes
106 * - Added hint on GIF char array generation to jcc.c
107 * - Cleaned CRLF's from the sources and related files
108 * - Repaired logging for REF and FRC
110 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
111 * Initial import of version 2.9.3 source tree
114 *********************************************************************/
120 #include <sys/types.h>
129 # include <sys/timeb.h>
130 # include <windows.h>
132 # include <process.h>
135 # endif /* def TOGGLE */
138 # ifndef _WIN_CONSOLE
140 # endif /* ndef _WIN_CONSOLE */
142 #else /* ifndef _WIN32 */
145 # include <sys/time.h>
146 # include <sys/wait.h>
147 # include <sys/stat.h>
151 # include <socket.h> /* BeOS has select() for sockets only. */
152 # include <OS.h> /* declarations for threads and stuff. */
165 #include "showargs.h"
167 #include "killpopup.h"
168 #include "miscutil.h"
170 #include "jbsockets.h"
173 const char jcc_h_rcs[] = JCC_H_VERSION;
174 const char project_h_rcs[] = PROJECT_H_VERSION;
176 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
178 struct client_state clients[1];
179 struct file_list files[1];
182 int urls_read = 0; /* total nr of urls read inc rejected */
183 int urls_rejected = 0; /* total nr of urls rejected */
184 #endif /* def STATISTICS */
187 static void listen_loop(void);
188 static void chat(struct client_state *csp);
190 void serve(struct client_state *csp);
191 #else /* ifndef AMIGA */
192 static void serve(struct client_state *csp);
193 #endif /* def AMIGA */
196 static int32 server_thread(void *data);
197 #endif /* def __BEOS__ */
200 #define sleep(N) Sleep(((N) * 1000))
204 /*********************************************************************
208 * Description : Once a connection to the client has been accepted,
209 * this function is called (via serve()) to handle the
210 * main business of the communication. When this
211 * function returns, the caller must close the client
215 * 1 : csp = Current client state (buffers, headers, etc...)
217 * Returns : On success, the number of bytes written are returned (zero
218 * indicates nothing was written). On error, -1 is returned,
219 * and errno is set appropriately. If count is zero and the
220 * file descriptor refers to a regular file, 0 will be
221 * returned without causing any other effect. For a special
222 * file, the results are not portable.
224 *********************************************************************/
225 static void chat(struct client_state *csp)
227 /* This next line is a little ugly, but it simplifies the if statement below. */
228 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
230 # define IS_TOGGLED_ON csp->toggled_on &&
231 #else /* ifndef TOGGLE */
232 # define IS_TOGGLED_ON
233 #endif /* ndef TOGGLE */
235 /* This next line is a little ugly, but it simplifies the if statement below. */
236 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
238 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
239 #else /* ifndef TRUST_FILES */
240 # define IS_TRUSTED_URL
241 #endif /* ndef TRUST_FILES */
243 char buf[BUFSIZ], *hdr, *p, *req;
247 int n, maxfd, server_body, ms_iis5_hack = 0;
248 const struct gateway *gw;
249 struct http_request *http;
251 int block_popups; /* bool, 1==will block popups */
252 int block_popups_now = 0; /* bool, 1==currently blocking popups */
253 #endif /* def KILLPOPUPS */
255 int pcrs_filter; /* bool, 1==will filter through pcrs */
256 int filtering = 0; /* bool, 1==currently filtering through pcrs */
257 #endif /* def PCRS */
262 * Read the client's request. Note that since we're not using select() we
263 * could get blocked here if a client connected, then didn't say anything!
268 n = read_socket(csp->cfd, buf, sizeof(buf));
270 if (n <= 0) break; /* error! */
272 add_to_iob(csp, buf, n);
274 req = get_header(csp);
278 break; /* no HTTP request! */
283 continue; /* more to come! */
287 /* If this request contains the FORCE_PREFIX,
288 * better get rid of it now and set the force flag --oes
291 if (strstr(req, FORCE_PREFIX))
293 strclean(req, FORCE_PREFIX);
294 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
301 #endif /* def FORCE_LOAD */
303 parse_http_request(req, http, csp);
308 if (http->cmd == NULL)
310 strcpy(buf, CHEADER);
311 write_socket(csp->cfd, buf, strlen(buf));
315 /* decide how to route the HTTP request */
317 if ((gw = forward_url(http, csp)) == NULL)
319 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
320 /* Never get here - LOG_LEVEL_FATAL causes program exit */
323 /* build the http request to send to the server
324 * we have to do one of the following:
326 * create = use the original HTTP request to create a new
327 * HTTP request that has only the path component
328 * without the http://domainspec
329 * pass = pass the original HTTP request unchanged
331 * drop = drop the HTTP request
336 * +--------+--------+
338 * 0 | create | drop |
340 * Forwarding +--------+--------+
344 * +--------+--------+
348 if (gw->forward_host)
350 /* if forwarding, just pass the request as is */
351 enlist(csp->headers, http->cmd);
357 /* otherwise elide the host information from the url */
359 p = strsav(p, http->gpc);
361 p = strsav(p, http->path);
363 p = strsav(p, http->ver);
364 enlist(csp->headers, p);
369 /* decide what we're to do with cookies */
372 if (!csp->toggled_on)
374 /* Most compatible set of permissions */
375 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
379 csp->permissions = url_permissions(http, csp);
381 #else /* ifndef TOGGLE */
382 csp->permissions = url_permissions(http, csp);
383 #endif /* ndef TOGGLE */
386 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
387 #endif /* def KILLPOPUPS */
389 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
390 ((csp->permissions & PERMIT_RE_FILTER) != 0);
391 #endif /* def PCRS */
394 /* grab the rest of the client's headers */
398 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
400 n = read_socket(csp->cfd, buf, sizeof(buf));
403 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
406 add_to_iob(csp, buf, n);
410 if (p == NULL) break;
412 enlist(csp->headers, p);
416 /* filter it as required */
418 hdr = sed(client_patterns, add_client_headers, csp);
420 destroy_list(csp->headers);
422 /* Check the request against all rules, unless
423 * we're toggled off or in force mode.
429 #endif /* def FORCE_LOAD */
430 ( (p = intercept_url(http, csp)) ||
432 (p = block_url(http, csp))
433 #ifdef FAST_REDIRECTS
434 || (csp->config->fast_redirects && (p = redirect_url(http, csp)))
435 #endif /* def FAST_REDIRECTS */
440 #endif /* def STATISTICS */
442 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
444 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
445 /* Block as image? */
446 if ( (csp->config->tinygif > 0) && block_imageurl(http, csp) )
448 /* Send "blocked" image */
449 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
450 http->hostport, http->path);
452 if ((csp->config->tinygif == 2) || strstr(http->path, "ijb-send-banner"))
454 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
456 else if (csp->config->tinygif == 1)
458 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
460 else if ((csp->config->tinygif == 3) && (csp->config->tinygifurl))
463 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE)
464 + strlen(csp->config->tinygifurl));
465 sprintf(p, HTTP_REDIRECT_TEMPLATE, csp->config->tinygifurl);
466 write_socket(csp->cfd, p, strlen(p));
470 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
474 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
477 /* Send HTML "blocked" message, interception, or redirection result */
478 write_socket(csp->cfd, p, strlen(p));
481 log_error(LOG_LEVEL_LOG, "%s", p);
488 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
490 if (gw->forward_host)
492 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
493 gw->forward_host, gw->forward_port, http->hostport);
497 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
500 /* here we connect to the server, gateway, or the forwarder */
502 csp->sfd = (gw->conn)(gw, http, csp);
506 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
511 err = zalloc(strlen(CNXDOM) + strlen(http->host));
512 sprintf(err, CNXDOM, http->host);
516 eno = safe_strerror(errno);
517 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
518 sprintf(err, CFAIL, http->hostport, eno);
521 write_socket(csp->cfd, err, strlen(err));
523 log_error(LOG_LEVEL_LOG, err);
530 log_error(LOG_LEVEL_CONNECT, "OK");
532 if (gw->forward_host || (http->ssl == 0))
534 /* write the client's (modified) header to the server
535 * (along with anything else that may be in the buffer)
540 if ((write_socket(csp->sfd, hdr, n) != n)
541 || (flush_socket(csp->sfd, csp ) < 0))
543 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
546 eno = safe_strerror(errno);
547 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
548 sprintf(err, CFAIL, http->hostport, eno);
549 write_socket(csp->cfd, err, strlen(err));
559 * We're running an SSL tunnel and we're not forwarding,
560 * so just send the "connect succeeded" message to the
561 * client, flush the rest, and get out of the way.
563 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
571 /* we're finished with the client's header */
574 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
576 /* pass data between the client and server
577 * until one or the other shuts down the connection.
586 FD_SET(csp->cfd, &rfds);
587 FD_SET(csp->sfd, &rfds);
589 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
593 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
597 /* this is the body of the browser's request
598 * just read it and write it.
601 if (FD_ISSET(csp->cfd, &rfds))
603 n = read_socket(csp->cfd, buf, sizeof(buf));
605 if (n <= 0) break; /* "game over, man" */
607 if (write_socket(csp->sfd, buf, n) != n)
609 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
616 * The server wants to talk. It could be the header or the body.
617 * If `hdr' is null, then it's the header otherwise it's the body.
618 * FIXME: Does `hdr' really mean `host'?
622 if (FD_ISSET(csp->sfd, &rfds))
625 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
629 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
631 eno = safe_strerror(errno);
632 sprintf(buf, CFAIL, http->hostport, eno);
634 write_socket(csp->cfd, buf, strlen(buf));
638 /* Add a trailing zero. This lets filter_popups
639 * use string operations.
644 /* Filter the popups on this read. */
645 if (block_popups_now)
647 filter_popups(buf, n);
649 #endif /* def KILLPOPUPS */
651 /* Normally, this would indicate that we've read
652 * as much as the server has sent us and we can
653 * close the client connection. However, Microsoft
654 * in its wisdom has released IIS/5 with a bug that
655 * prevents it from sending the trailing \r\n in
656 * a 302 redirect header (and possibly other headers).
657 * To work around this if we've haven't parsed
658 * a full header we'll append a trailing \r\n
659 * and see if this now generates a valid one.
661 * This hack shouldn't have any impacts. If we've
662 * already transmitted the header or if this is a
663 * SSL connection, then we won't bother with this
664 * hack. So we only work on partially received
665 * headers. If we append a \r\n and this still
666 * doesn't generate a valid header, then we won't
667 * transmit anything to the client.
671 /* This hack must only be enforced for headers. */
672 if (server_body || http->ssl)
677 re_process_buffer(csp);
679 #endif /* def PCRS */
680 break; /* "game over, man" */
683 /* Let's pretend the server just sent us a blank line. */
684 n = sprintf(buf, "\r\n");
687 * Now, let the normal header parsing algorithm below do its
688 * job. If it fails, we'll exit instead of continuing.
695 * If this is an SSL connection or we're in the body
696 * of the server document, just write it to the client.
699 if (server_body || http->ssl)
704 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
707 #endif /* def PCRS */
710 if (write_socket(csp->cfd, buf, n) != n)
712 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
720 /* we're still looking for the end of the
721 * server's header ... (does that make header
722 * parsing an "out of body experience" ?
725 /* buffer up the data we just read */
726 add_to_iob(csp, buf, n);
728 /* get header lines from the iob */
730 while ((p = get_header(csp)))
734 /* see following note */
737 enlist(csp->headers, p);
741 /* NOTE: there are no "empty" headers so
742 * if the pointer `p' is not NULL we must
743 * assume that we reached the end of the
744 * buffer before we hit the end of the header.
751 /* Well, we tried our MS IIS/5
752 * hack and it didn't work.
753 * The header is incomplete
754 * and there isn't anything
755 * we can do about it.
761 /* Since we have to wait for
762 * more from the server before
763 * we can parse the headers
764 * we just continue here.
770 /* we have now received the entire header.
771 * filter it and send the result to the client
774 hdr = sed(server_patterns, add_server_headers, csp);
777 /* write the server's (modified) header to
778 * the client (along with anything else that
779 * may be in the buffer)
783 /* Start blocking popups if appropriate. */
785 if (csp->is_text && /* It's a text / * MIME-Type */
786 !http->ssl && /* We talk plaintext */
789 block_popups_now = 1;
792 #endif /* def KILLPOPUPS */
795 /* Start re_filtering this if appropriate. */
797 if (csp->is_text && /* It's a text / * MIME-Type */
798 !http->ssl && /* We talk plaintext */
799 pcrs_filter) /* Policy allows */
804 /* This next line is a little ugly, but it simplifies the if statement below. */
805 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
806 #define NOT_FILTERING_AND !filtering &&
808 #else /* not def PCRS */
810 #define NOT_FILTERING_AND
812 #endif /* def PCRS */
815 if ((write_socket(csp->cfd, hdr, n) != n)
816 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
818 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
820 /* the write failed, so don't bother
821 * mentioning it to the client...
822 * it probably can't hear us anyway.
828 /* we're finished with the server's header */
833 /* If this was a MS IIS/5 hack then it means
834 * the server has already closed the
835 * connection. Nothing more to read. Time
846 return; /* huh? we should never get here */
853 /*********************************************************************
857 * Description : This is little more than chat. We only "serve" to
858 * to close any socket that chat may have opened.
861 * 1 : csp = Current client state (buffers, headers, etc...)
865 *********************************************************************/
867 void serve(struct client_state *csp)
868 #else /* ifndef AMIGA */
869 static void serve(struct client_state *csp)
870 #endif /* def AMIGA */
873 close_socket(csp->cfd);
877 close_socket(csp->sfd);
887 /*********************************************************************
889 * Function : server_thread
891 * Description : We only exist to call `serve' in a threaded environment.
894 * 1 : data = Current client state (buffers, headers, etc...)
896 * Returns : Always 0.
898 *********************************************************************/
899 static int32 server_thread(void *data)
901 serve((struct client_state *) data);
909 /*********************************************************************
913 * Description : Load the config file and start the listen loop.
914 * This function is a lot more *sane* with the `load_config'
915 * and `listen_loop' functions; although it stills does
916 * a *little* too much for my taste.
919 * 1 : argc = Number of parameters (including $0).
920 * 2 : argv = Array of (char *)'s to the parameters.
922 * Returns : 1 if : can't open config file, unrecognized directive,
923 * stats requested in multi-thread mode, can't open the
924 * log file, can't open the jar file, listen port is invalid,
925 * any load fails, and can't bind port.
927 * Else main never returns, the process must be signaled
928 * to terminate execution. Or, on Windows, use the
929 * "File", "Exit" menu option.
931 *********************************************************************/
933 int _main(int argc, const char *argv[])
935 int main(int argc, const char *argv[])
940 "AmiTCP:db/junkbuster.config"
941 #elif !defined(_WIN32)
948 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
949 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
951 printf("JunkBuster proxy version " VERSION ".\n\n"
952 "Usage: %s [configfile]\n\n"
953 "See " HOME_PAGE_URL " for details.\n"
954 "This program is distributed under the GNU GPL, version 2 or later.\n",
958 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
960 printf(VERSION "\n");
963 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
974 configfile = argv[1];
985 signal(SIGPIPE, SIG_IGN);
986 signal(SIGCHLD, SIG_IGN);
988 #else /* ifdef _WIN32 */
991 * We *are* in a windows console app.
992 * Print a verbose messages about FAQ's and such
995 # endif /* def _WIN_CONSOLE */
996 #endif /* def _WIN32 */
1007 /*********************************************************************
1009 * Function : listen_loop
1011 * Description : bind the listen port and enter a "FOREVER" listening loop.
1017 *********************************************************************/
1018 static void listen_loop(void)
1020 struct client_state *csp = NULL;
1022 struct configuration_spec * config;
1024 config = load_config();
1026 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1027 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1029 bfd = bind_port(config->haddr, config->hport);
1033 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1034 "- There may be another junkbuster or some other "
1035 "proxy running on port %d",
1036 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1037 config->hport, config->hport
1039 /* shouldn't get here */
1043 config->need_bind = 0;
1048 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1049 while (waitpid(-1, NULL, WNOHANG) > 0)
1051 /* zombie children */
1053 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1056 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1058 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1062 memset(csp, '\0', sizeof(*csp));
1067 csp->config = config = load_config();
1069 if ( config->need_bind )
1072 * Since we were listening to the "old port", we will not see
1073 * a "listen" param change until the next IJB request. So, at
1074 * least 1 more request must be made for us to find the new
1075 * setting. I am simply closing the old socket and binding the
1078 * Which-ever is correct, we will serve 1 more page via the
1079 * old settings. This should probably be a "show-proxy-args"
1080 * request. This should not be a so common of an operation
1081 * that this will hurt people's feelings.
1086 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1087 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1088 bfd = bind_port(config->haddr, config->hport);
1092 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1093 "- There may be another junkbuster or some other "
1094 "proxy running on port %d",
1095 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1096 config->hport, config->hport
1098 /* shouldn't get here */
1102 config->need_bind = 0;
1105 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1107 if (!accept_connection(csp, bfd))
1109 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1121 log_error(LOG_LEVEL_CONNECT, "OK");
1125 /* by haroon - most of credit to srt19170 */
1126 csp->toggled_on = g_bToggleIJB;
1129 /* add it to the list of clients */
1130 csp->next = clients->next;
1131 clients->next = csp;
1133 if (run_loader(csp))
1135 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1136 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1139 if (config->multi_threaded)
1143 /* this is a switch () statment in the C preprocessor - ugh */
1144 #undef SELECTED_ONE_OPTION
1146 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1147 #define SELECTED_ONE_OPTION
1148 child_id = _beginthread(
1154 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1155 #define SELECTED_ONE_OPTION
1157 thread_id tid = spawn_thread
1158 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1160 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1162 child_id = (int) tid;
1171 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1172 #define SELECTED_ONE_OPTION
1173 csp->cfd = ReleaseSocket(csp->cfd, -1);
1174 if((child_id = (int)CreateNewProcTags(
1175 NP_Entry, (ULONG)server_thread,
1176 NP_Output, Output(),
1177 NP_CloseOutput, FALSE,
1178 NP_Name, (ULONG)"junkbuster child",
1179 NP_StackSize, 20*1024,
1183 ((struct Task *)child_id)->tc_UserData = csp;
1184 Signal((struct Task *)child_id, SIGF_SINGLE);
1189 #if !defined(SELECTED_ONE_OPTION)
1193 #undef SELECTED_ONE_OPTION
1194 /* end of cpp switch () */
1196 if (child_id < 0) /* failed */
1200 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1202 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1204 write_socket(csp->cfd, buf, strlen(buf));
1205 close_socket(csp->cfd);
1211 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1212 /* This block is only needed when using fork().
1213 * When using threads, the server thread was
1214 * created and run by the call to _beginthread().
1216 if (child_id == 0) /* child */
1224 /* in a fork()'d environment, the parent's
1225 * copy of the client socket and the CSP
1229 #if !defined(_WIN32) && defined(__CYGWIN__)
1231 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1232 close_socket(csp->cfd);
1235 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */