1 const char jcc_rcs[] = "$Id: jcc.c,v 1.7 2001/05/25 22:34:30 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.7 2001/05/25 22:34:30 jongfoster
39 * Revision 1.6 2001/05/23 00:13:58 joergs
40 * AmigaOS support fixed.
42 * Revision 1.5 2001/05/22 18:46:04 oes
44 * - Enabled filtering banners by size rather than URL
45 * by adding patterns that replace all standard banner
46 * sizes with the "Junkbuster" gif to the re_filterfile
48 * - Enabled filtering WebBugs by providing a pattern
49 * which kills all 1x1 images
51 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
52 * which is selected by the (nonstandard and therefore
53 * capital) letter 'U' in the option string.
54 * It causes the quantifiers to be ungreedy by default.
55 * Appending a ? turns back to greedy (!).
57 * - Added a new interceptor ijb-send-banner, which
58 * sends back the "Junkbuster" gif. Without imagelist or
59 * MSIE detection support, or if tinygif = 1, or the
60 * URL isn't recognized as an imageurl, a lame HTML
61 * explanation is sent instead.
63 * - Added new feature, which permits blocking remote
64 * script redirects and firing back a local redirect
66 * The feature is conditionally compiled, i.e. it
67 * can be disabled with --disable-fast-redirects,
68 * plus it must be activated by a "fast-redirects"
69 * line in the config file, has its own log level
70 * and of course wants to be displayed by show-proxy-args
71 * Note: Boy, all the #ifdefs in 1001 locations and
72 * all the fumbling with configure.in and acconfig.h
73 * were *way* more work than the feature itself :-(
75 * - Because a generic redirect template was needed for
76 * this, tinygif = 3 now uses the same.
78 * - Moved GIFs, and other static HTTP response templates
83 * - Removed some >400 CRs again (Jon, you really worked
86 * Revision 1.4 2001/05/21 19:34:01 jongfoster
87 * Made failure to bind() a fatal error.
89 * Revision 1.3 2001/05/20 01:21:20 jongfoster
90 * Version 2.9.4 checkin.
91 * - Merged popupfile and cookiefile, and added control over PCRS
92 * filtering, in new "permissionsfile".
93 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
94 * file error you now get a message box (in the Win32 GUI) rather
95 * than the program exiting with no explanation.
96 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
98 * - Removed tabs from "config"
99 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
100 * - Bumped up version number.
102 * Revision 1.2 2001/05/17 22:34:44 oes
103 * - Added hint on GIF char array generation to jcc.c
104 * - Cleaned CRLF's from the sources and related files
105 * - Repaired logging for REF and FRC
107 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
108 * Initial import of version 2.9.3 source tree
111 *********************************************************************/
117 #include <sys/types.h>
126 # include <sys/timeb.h>
127 # include <windows.h>
129 # include <process.h>
132 # endif /* def TOGGLE */
135 # ifndef _WIN_CONSOLE
137 # endif /* ndef _WIN_CONSOLE */
139 #else /* ifndef _WIN32 */
142 # include <sys/time.h>
143 # include <sys/wait.h>
144 # include <sys/stat.h>
148 # include <socket.h> /* BeOS has select() for sockets only. */
149 # include <OS.h> /* declarations for threads and stuff. */
162 #include "showargs.h"
164 #include "killpopup.h"
165 #include "miscutil.h"
167 #include "jbsockets.h"
170 const char jcc_h_rcs[] = JCC_H_VERSION;
171 const char project_h_rcs[] = PROJECT_H_VERSION;
173 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
175 struct client_state clients[1];
176 struct file_list files[1];
179 int urls_read = 0; /* total nr of urls read inc rejected */
180 int urls_rejected = 0; /* total nr of urls rejected */
181 #endif /* def STATISTICS */
184 static void listen_loop(void);
185 static void chat(struct client_state *csp);
187 void serve(struct client_state *csp);
188 #else /* ifndef AMIGA */
189 static void serve(struct client_state *csp);
190 #endif /* def AMIGA */
193 static int32 server_thread(void *data);
194 #endif /* def __BEOS__ */
197 #define sleep(N) Sleep(((N) * 1000))
201 /*********************************************************************
205 * Description : Once a connection to the client has been accepted,
206 * this function is called (via serve()) to handle the
207 * main business of the communication. When this
208 * function returns, the caller must close the client
212 * 1 : csp = Current client state (buffers, headers, etc...)
214 * Returns : On success, the number of bytes written are returned (zero
215 * indicates nothing was written). On error, -1 is returned,
216 * and errno is set appropriately. If count is zero and the
217 * file descriptor refers to a regular file, 0 will be
218 * returned without causing any other effect. For a special
219 * file, the results are not portable.
221 *********************************************************************/
222 static void chat(struct client_state *csp)
224 /* This next line is a little ugly, but it simplifies the if statement below. */
225 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
227 # define IS_TOGGLED_ON csp->toggled_on &&
228 #else /* ifndef TOGGLE */
229 # define IS_TOGGLED_ON
230 #endif /* ndef TOGGLE */
232 /* This next line is a little ugly, but it simplifies the if statement below. */
233 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
235 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
236 #else /* ifndef TRUST_FILES */
237 # define IS_TRUSTED_URL
238 #endif /* ndef TRUST_FILES */
240 char buf[BUFSIZ], *hdr, *p, *req;
244 int n, maxfd, server_body, ms_iis5_hack = 0;
245 const struct gateway *gw;
246 struct http_request *http;
248 int block_popups; /* bool, 1==will block popups */
249 int block_popups_now = 0; /* bool, 1==currently blocking popups */
250 #endif /* def KILLPOPUPS */
252 int pcrs_filter; /* bool, 1==will filter through pcrs */
253 int filtering = 0; /* bool, 1==currently filtering through pcrs */
254 #endif /* def PCRS */
259 * Read the client's request. Note that since we're not using select() we
260 * could get blocked here if a client connected, then didn't say anything!
265 n = read_socket(csp->cfd, buf, sizeof(buf));
267 if (n <= 0) break; /* error! */
269 add_to_iob(csp, buf, n);
271 req = get_header(csp);
275 break; /* no HTTP request! */
280 continue; /* more to come! */
284 /* If this request contains the FORCE_PREFIX,
285 * better get rid of it now and set the force flag --oes
288 if (strstr(req, FORCE_PREFIX))
290 strclean(req, FORCE_PREFIX);
291 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
298 #endif /* def FORCE_LOAD */
300 parse_http_request(req, http, csp);
305 if (http->cmd == NULL)
307 strcpy(buf, CHEADER);
308 write_socket(csp->cfd, buf, strlen(buf));
312 /* decide how to route the HTTP request */
314 if ((gw = forward_url(http, csp)) == NULL)
316 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
317 /* Never get here - LOG_LEVEL_FATAL causes program exit */
320 /* build the http request to send to the server
321 * we have to do one of the following:
323 * create = use the original HTTP request to create a new
324 * HTTP request that has only the path component
325 * without the http://domainspec
326 * pass = pass the original HTTP request unchanged
328 * drop = drop the HTTP request
333 * +--------+--------+
335 * 0 | create | drop |
337 * Forwarding +--------+--------+
341 * +--------+--------+
345 if (gw->forward_host)
347 /* if forwarding, just pass the request as is */
348 enlist(csp->headers, http->cmd);
354 /* otherwise elide the host information from the url */
356 p = strsav(p, http->gpc);
358 p = strsav(p, http->path);
360 p = strsav(p, http->ver);
361 enlist(csp->headers, p);
366 /* decide what we're to do with cookies */
369 if (!csp->toggled_on)
371 /* Most compatible set of permissions */
372 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
376 csp->permissions = url_permissions(http, csp);
378 #else /* ifndef TOGGLE */
379 csp->permissions = url_permissions(http, csp);
380 #endif /* ndef TOGGLE */
383 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
384 #endif /* def KILLPOPUPS */
386 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
387 ((csp->permissions & PERMIT_RE_FILTER) != 0);
388 #endif /* def PCRS */
391 /* grab the rest of the client's headers */
395 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
397 n = read_socket(csp->cfd, buf, sizeof(buf));
400 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
403 add_to_iob(csp, buf, n);
407 if (p == NULL) break;
409 enlist(csp->headers, p);
413 /* filter it as required */
415 hdr = sed(client_patterns, add_client_headers, csp);
417 destroy_list(csp->headers);
419 /* Check the request against all rules, unless
420 * we're toggled off or in force mode.
426 #endif /* def FORCE_LOAD */
427 ( (p = intercept_url(http, csp)) ||
429 (p = block_url(http, csp))
430 #ifdef FAST_REDIRECTS
431 || (fast_redirects && (p = redirect_url(http, csp)))
432 #endif /* def FAST_REDIRECTS */
437 #endif /* def STATISTICS */
439 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
441 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
442 /* Block as image? */
443 if ( (tinygif > 0) && block_imageurl(http, csp) )
445 /* Send "blocked" image */
446 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
447 http->hostport, http->path);
449 if ((tinygif == 2) || strstr(http->path, "ijb-send-banner"))
451 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
453 else if (tinygif == 1)
455 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
457 else if ((tinygif == 3) && (tinygifurl))
460 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE) + strlen(tinygifurl));
461 sprintf(p, HTTP_REDIRECT_TEMPLATE, tinygifurl);
462 write_socket(csp->cfd, p, strlen(p));
466 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
470 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
473 /* Send HTML "blocked" message, interception, or redirection result */
474 write_socket(csp->cfd, p, strlen(p));
477 log_error(LOG_LEVEL_LOG, "%s", p);
484 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
486 if (gw->forward_host)
488 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
489 gw->forward_host, gw->forward_port, http->hostport);
493 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
496 /* here we connect to the server, gateway, or the forwarder */
498 csp->sfd = (gw->conn)(gw, http, csp);
502 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
507 err = zalloc(strlen(CNXDOM) + strlen(http->host));
508 sprintf(err, CNXDOM, http->host);
512 eno = safe_strerror(errno);
513 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
514 sprintf(err, CFAIL, http->hostport, eno);
517 write_socket(csp->cfd, err, strlen(err));
519 log_error(LOG_LEVEL_LOG, err);
526 log_error(LOG_LEVEL_CONNECT, "OK");
528 if (gw->forward_host || (http->ssl == 0))
530 /* write the client's (modified) header to the server
531 * (along with anything else that may be in the buffer)
536 if ((write_socket(csp->sfd, hdr, n) != n)
537 || (flush_socket(csp->sfd, csp ) < 0))
539 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
542 eno = safe_strerror(errno);
543 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
544 sprintf(err, CFAIL, http->hostport, eno);
545 write_socket(csp->cfd, err, strlen(err));
555 * We're running an SSL tunnel and we're not forwarding,
556 * so just send the "connect succeeded" message to the
557 * client, flush the rest, and get out of the way.
559 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
567 /* we're finished with the client's header */
570 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
572 /* pass data between the client and server
573 * until one or the other shuts down the connection.
582 FD_SET(csp->cfd, &rfds);
583 FD_SET(csp->sfd, &rfds);
585 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
589 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
593 /* this is the body of the browser's request
594 * just read it and write it.
597 if (FD_ISSET(csp->cfd, &rfds))
599 n = read_socket(csp->cfd, buf, sizeof(buf));
601 if (n <= 0) break; /* "game over, man" */
603 if (write_socket(csp->sfd, buf, n) != n)
605 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
612 * The server wants to talk. It could be the header or the body.
613 * If `hdr' is null, then it's the header otherwise it's the body.
614 * FIXME: Does `hdr' really mean `host'?
618 if (FD_ISSET(csp->sfd, &rfds))
621 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
625 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
627 eno = safe_strerror(errno);
628 sprintf(buf, CFAIL, http->hostport, eno);
630 write_socket(csp->cfd, buf, strlen(buf));
634 /* Add a trailing zero. This lets filter_popups
635 * use string operations.
640 /* Filter the popups on this read. */
641 if (block_popups_now)
643 filter_popups(buf, n);
645 #endif /* def KILLPOPUPS */
647 /* Normally, this would indicate that we've read
648 * as much as the server has sent us and we can
649 * close the client connection. However, Microsoft
650 * in its wisdom has released IIS/5 with a bug that
651 * prevents it from sending the trailing \r\n in
652 * a 302 redirect header (and possibly other headers).
653 * To work around this if we've haven't parsed
654 * a full header we'll append a trailing \r\n
655 * and see if this now generates a valid one.
657 * This hack shouldn't have any impacts. If we've
658 * already transmitted the header or if this is a
659 * SSL connection, then we won't bother with this
660 * hack. So we only work on partially received
661 * headers. If we append a \r\n and this still
662 * doesn't generate a valid header, then we won't
663 * transmit anything to the client.
667 /* This hack must only be enforced for headers. */
668 if (server_body || http->ssl)
673 re_process_buffer(csp);
675 #endif /* def PCRS */
676 break; /* "game over, man" */
679 /* Let's pretend the server just sent us a blank line. */
680 n = sprintf(buf, "\r\n");
683 * Now, let the normal header parsing algorithm below do its
684 * job. If it fails, we'll exit instead of continuing.
691 * If this is an SSL connection or we're in the body
692 * of the server document, just write it to the client.
695 if (server_body || http->ssl)
700 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
703 #endif /* def PCRS */
706 if (write_socket(csp->cfd, buf, n) != n)
708 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
716 /* we're still looking for the end of the
717 * server's header ... (does that make header
718 * parsing an "out of body experience" ?
721 /* buffer up the data we just read */
722 add_to_iob(csp, buf, n);
724 /* get header lines from the iob */
726 while ((p = get_header(csp)))
730 /* see following note */
733 enlist(csp->headers, p);
737 /* NOTE: there are no "empty" headers so
738 * if the pointer `p' is not NULL we must
739 * assume that we reached the end of the
740 * buffer before we hit the end of the header.
747 /* Well, we tried our MS IIS/5
748 * hack and it didn't work.
749 * The header is incomplete
750 * and there isn't anything
751 * we can do about it.
757 /* Since we have to wait for
758 * more from the server before
759 * we can parse the headers
760 * we just continue here.
766 /* we have now received the entire header.
767 * filter it and send the result to the client
770 hdr = sed(server_patterns, add_server_headers, csp);
773 /* write the server's (modified) header to
774 * the client (along with anything else that
775 * may be in the buffer)
779 /* Start blocking popups if appropriate. */
781 if (csp->is_text && /* It's a text / * MIME-Type */
782 !http->ssl && /* We talk plaintext */
785 block_popups_now = 1;
788 #endif /* def KILLPOPUPS */
791 /* Start re_filtering this if appropriate. */
793 if (csp->is_text && /* It's a text / * MIME-Type */
794 !http->ssl && /* We talk plaintext */
795 pcrs_filter) /* Policy allows */
800 /* This next line is a little ugly, but it simplifies the if statement below. */
801 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
802 #define NOT_FILTERING_AND !filtering &&
804 #else /* not def PCRS */
806 #define NOT_FILTERING_AND
808 #endif /* def PCRS */
811 if ((write_socket(csp->cfd, hdr, n) != n)
812 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
814 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
816 /* the write failed, so don't bother
817 * mentioning it to the client...
818 * it probably can't hear us anyway.
824 /* we're finished with the server's header */
829 /* If this was a MS IIS/5 hack then it means
830 * the server has already closed the
831 * connection. Nothing more to read. Time
842 return; /* huh? we should never get here */
849 /*********************************************************************
853 * Description : This is little more than chat. We only "serve" to
854 * to close any socket that chat may have opened.
857 * 1 : csp = Current client state (buffers, headers, etc...)
861 *********************************************************************/
863 void serve(struct client_state *csp)
864 #else /* ifndef AMIGA */
865 static void serve(struct client_state *csp)
866 #endif /* def AMIGA */
869 close_socket(csp->cfd);
873 close_socket(csp->sfd);
883 /*********************************************************************
885 * Function : server_thread
887 * Description : We only exist to call `serve' in a threaded environment.
890 * 1 : data = Current client state (buffers, headers, etc...)
892 * Returns : Always 0.
894 *********************************************************************/
895 static int32 server_thread(void *data)
897 serve((struct client_state *) data);
905 /*********************************************************************
909 * Description : Load the config file and start the listen loop.
910 * This function is a lot more *sane* with the `load_config'
911 * and `listen_loop' functions; although it stills does
912 * a *little* too much for my taste.
915 * 1 : argc = Number of parameters (including $0).
916 * 2 : argv = Array of (char *)'s to the parameters.
918 * Returns : 1 if : can't open config file, unrecognized directive,
919 * stats requested in multi-thread mode, can't open the
920 * log file, can't open the jar file, listen port is invalid,
921 * any load fails, and can't bind port.
923 * Else main never returns, the process must be signaled
924 * to terminate execution. Or, on Windows, use the
925 * "File", "Exit" menu option.
927 *********************************************************************/
929 int _main(int argc, const char *argv[])
931 int main(int argc, const char *argv[])
936 "AmiTCP:db/junkbuster.config"
937 #elif !defined(_WIN32)
944 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
945 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
947 printf("JunkBuster proxy version " VERSION ".\n\n"
948 "Usage: %s [configfile]\n\n"
949 "See " HOME_PAGE_URL " for details.\n"
950 "This program is distributed under the GNU GPL, version 2 or later.\n",
954 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
956 printf(VERSION "\n");
959 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
970 configfile = argv[1];
973 remove_all_loaders();
974 memset( proxy_args, 0, sizeof( proxy_args ) );
980 * Since load_config acts as a signal handler too, it returns
981 * its status in configret. Check it for an error in loading.
983 if ( 0 != configret )
985 /* load config failed! Exit with error. */
995 signal(SIGPIPE, SIG_IGN);
996 signal(SIGCHLD, SIG_IGN);
997 signal(SIGHUP, load_config);
999 #else /* ifdef _WIN32 */
1000 # ifdef _WIN_CONSOLE
1002 * We *are* in a windows console app.
1003 * Print a verbose messages about FAQ's and such
1005 printf(win32_blurb);
1006 # endif /* def _WIN_CONSOLE */
1007 #endif /* def _WIN32 */
1018 /*********************************************************************
1020 * Function : listen_loop
1022 * Description : bind the listen port and enter a "FOREVER" listening loop.
1028 *********************************************************************/
1029 static void listen_loop(void)
1031 struct client_state *csp = NULL;
1034 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1035 haddr ? haddr : "INADDR_ANY", hport);
1037 bfd = bind_port(haddr, hport);
1042 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1043 "- There may be another junkbuster or some other "
1044 "proxy running on port %d",
1045 (NULL != haddr) ? haddr : "INADDR_ANY", hport, hport
1047 /* shouldn't get here */
1054 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1055 while (waitpid(-1, NULL, WNOHANG) > 0)
1057 /* zombie children */
1059 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1062 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1064 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1068 memset(csp, '\0', sizeof(*csp));
1073 if ( config_changed )
1076 * Since we were listening to the "old port", we will not see
1077 * a "listen" param change until the next IJB request. So, at
1078 * least 1 more request must be made for us to find the new
1079 * setting. I am simply closing the old socket and binding the
1082 * Which-ever is correct, we will serve 1 more page via the
1083 * old settings. This should probably be a "show-proxy-args"
1084 * request. This should not be a so common of an operation
1085 * that this will hurt people's feelings.
1089 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1090 haddr ? haddr : "INADDR_ANY", hport);
1091 bfd = bind_port(haddr, hport);
1096 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1098 if (!accept_connection(csp, bfd))
1100 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1112 log_error(LOG_LEVEL_CONNECT, "OK");
1116 /* by haroon - most of credit to srt19170 */
1117 csp->toggled_on = g_bToggleIJB;
1120 /* add it to the list of clients */
1121 csp->next = clients->next;
1122 clients->next = csp;
1124 if (run_loader(csp))
1126 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1127 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1134 /* this is a switch () statment in the C preprocessor - ugh */
1135 #undef SELECTED_ONE_OPTION
1137 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1138 #define SELECTED_ONE_OPTION
1139 child_id = _beginthread(
1145 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1146 #define SELECTED_ONE_OPTION
1148 thread_id tid = spawn_thread
1149 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1151 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1153 child_id = (int) tid;
1162 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1163 #define SELECTED_ONE_OPTION
1164 csp->cfd = ReleaseSocket(csp->cfd, -1);
1165 if((child_id = (int)CreateNewProcTags(
1166 NP_Entry, (ULONG)server_thread,
1167 NP_Output, Output(),
1168 NP_CloseOutput, FALSE,
1169 NP_Name, (ULONG)"junkbuster child",
1170 NP_StackSize, 20*1024,
1174 ((struct Task *)child_id)->tc_UserData = csp;
1175 Signal((struct Task *)child_id, SIGF_SINGLE);
1180 #if !defined(SELECTED_ONE_OPTION)
1184 #undef SELECTED_ONE_OPTION
1185 /* end of cpp switch () */
1187 if (child_id < 0) /* failed */
1191 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1193 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1195 write_socket(csp->cfd, buf, strlen(buf));
1196 close_socket(csp->cfd);
1202 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1203 /* This block is only needed when using fork().
1204 * When using threads, the server thread was
1205 * created and run by the call to _beginthread().
1207 if (child_id == 0) /* child */
1215 /* in a fork()'d environment, the parent's
1216 * copy of the client socket and the CSP
1220 #if !defined(_WIN32) && defined(__CYGWIN__)
1222 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1223 close_socket(csp->cfd);
1226 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */