1 const char jcc_rcs[] = "$Id: jcc.c,v 1.5 2001/05/22 18:46:04 oes 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.5 2001/05/22 18:46:04 oes
38 * - Enabled filtering banners by size rather than URL
39 * by adding patterns that replace all standard banner
40 * sizes with the "Junkbuster" gif to the re_filterfile
42 * - Enabled filtering WebBugs by providing a pattern
43 * which kills all 1x1 images
45 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
46 * which is selected by the (nonstandard and therefore
47 * capital) letter 'U' in the option string.
48 * It causes the quantifiers to be ungreedy by default.
49 * Appending a ? turns back to greedy (!).
51 * - Added a new interceptor ijb-send-banner, which
52 * sends back the "Junkbuster" gif. Without imagelist or
53 * MSIE detection support, or if tinygif = 1, or the
54 * URL isn't recognized as an imageurl, a lame HTML
55 * explanation is sent instead.
57 * - Added new feature, which permits blocking remote
58 * script redirects and firing back a local redirect
60 * The feature is conditionally compiled, i.e. it
61 * can be disabled with --disable-fast-redirects,
62 * plus it must be activated by a "fast-redirects"
63 * line in the config file, has its own log level
64 * and of course wants to be displayed by show-proxy-args
65 * Note: Boy, all the #ifdefs in 1001 locations and
66 * all the fumbling with configure.in and acconfig.h
67 * were *way* more work than the feature itself :-(
69 * - Because a generic redirect template was needed for
70 * this, tinygif = 3 now uses the same.
72 * - Moved GIFs, and other static HTTP response templates
77 * - Removed some >400 CRs again (Jon, you really worked
80 * Revision 1.4 2001/05/21 19:34:01 jongfoster
81 * Made failure to bind() a fatal error.
83 * Revision 1.3 2001/05/20 01:21:20 jongfoster
84 * Version 2.9.4 checkin.
85 * - Merged popupfile and cookiefile, and added control over PCRS
86 * filtering, in new "permissionsfile".
87 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
88 * file error you now get a message box (in the Win32 GUI) rather
89 * than the program exiting with no explanation.
90 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
92 * - Removed tabs from "config"
93 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
94 * - Bumped up version number.
96 * Revision 1.2 2001/05/17 22:34:44 oes
97 * - Added hint on GIF char array generation to jcc.c
98 * - Cleaned CRLF's from the sources and related files
99 * - Repaired logging for REF and FRC
101 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
102 * Initial import of version 2.9.3 source tree
105 *********************************************************************/
111 #include <sys/types.h>
120 # include <sys/timeb.h>
121 # include <windows.h>
123 # include <process.h>
126 # endif /* def TOGGLE */
129 # ifndef _WIN_CONSOLE
131 # endif /* ndef _WIN_CONSOLE */
133 #else /* ifndef _WIN32 */
136 # include <sys/time.h>
137 # include <sys/wait.h>
138 # include <sys/stat.h>
142 # include <socket.h> /* BeOS has select() for sockets only. */
143 # include <OS.h> /* declarations for threads and stuff. */
156 #include "showargs.h"
158 #include "killpopup.h"
159 #include "miscutil.h"
161 #include "jbsockets.h"
164 const char jcc_h_rcs[] = JCC_H_VERSION;
165 const char project_h_rcs[] = PROJECT_H_VERSION;
167 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
169 struct client_state clients[1];
170 struct file_list files[1];
173 int urls_read = 0; /* total nr of urls read inc rejected */
174 int urls_rejected = 0; /* total nr of urls rejected */
175 #endif /* def STATISTICS */
178 static void listen_loop(void);
179 static void chat(struct client_state *csp);
181 void serve(struct client_state *csp);
182 #else /* ifndef AMIGA */
183 static void serve(struct client_state *csp);
184 #endif /* def AMIGA */
187 static int32 server_thread(void *data);
188 #endif /* def __BEOS__ */
191 #define sleep(N) Sleep(((N) * 1000))
195 /*********************************************************************
199 * Description : Once a connection to the client has been accepted,
200 * this function is called (via serve()) to handle the
201 * main business of the communication. When this
202 * function returns, the caller must close the client
206 * 1 : csp = Current client state (buffers, headers, etc...)
208 * Returns : On success, the number of bytes written are returned (zero
209 * indicates nothing was written). On error, -1 is returned,
210 * and errno is set appropriately. If count is zero and the
211 * file descriptor refers to a regular file, 0 will be
212 * returned without causing any other effect. For a special
213 * file, the results are not portable.
215 *********************************************************************/
216 static void chat(struct client_state *csp)
218 /* This next line is a little ugly, but it simplifies the if statement below. */
219 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
221 # define IS_TOGGLED_ON csp->toggled_on &&
222 #else /* ifndef TOGGLE */
223 # define IS_TOGGLED_ON
224 #endif /* ndef TOGGLE */
226 /* This next line is a little ugly, but it simplifies the if statement below. */
227 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
229 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
230 #else /* ifndef TRUST_FILES */
231 # define IS_TRUSTED_URL
232 #endif /* ndef TRUST_FILES */
234 char buf[BUFSIZ], *hdr, *p, *req;
238 int n, maxfd, server_body, ms_iis5_hack = 0;
239 const struct gateway *gw;
240 struct http_request *http;
242 int block_popups; /* bool, 1==will block popups */
243 int block_popups_now = 0; /* bool, 1==currently blocking popups */
244 #endif /* def KILLPOPUPS */
246 int pcrs_filter; /* bool, 1==will filter through pcrs */
247 int filtering = 0; /* bool, 1==currently filtering through pcrs */
248 #endif /* def PCRS */
253 * Read the client's request. Note that since we're not using select() we
254 * could get blocked here if a client connected, then didn't say anything!
259 n = read_socket(csp->cfd, buf, sizeof(buf));
261 if (n <= 0) break; /* error! */
263 add_to_iob(csp, buf, n);
265 req = get_header(csp);
269 break; /* no HTTP request! */
274 continue; /* more to come! */
278 /* If this request contains the FORCE_PREFIX,
279 * better get rid of it now and set the force flag --oes
282 if(strstr(req, FORCE_PREFIX))
284 strclean(req, FORCE_PREFIX);
285 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
292 #endif /* def FORCE_LOAD */
294 parse_http_request(req, http, csp);
299 if (http->cmd == NULL)
301 strcpy(buf, CHEADER);
302 write_socket(csp->cfd, buf, strlen(buf));
306 /* decide how to route the HTTP request */
308 if ((gw = forward_url(http, csp)) == NULL)
310 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
311 /* Never get here - LOG_LEVEL_FATAL causes program exit */
314 /* build the http request to send to the server
315 * we have to do one of the following:
317 * create = use the original HTTP request to create a new
318 * HTTP request that has only the path component
319 * without the http://domainspec
320 * pass = pass the original HTTP request unchanged
322 * drop = drop the HTTP request
327 * +--------+--------+
329 * 0 | create | drop |
331 * Forwarding +--------+--------+
335 * +--------+--------+
339 if (gw->forward_host)
341 /* if forwarding, just pass the request as is */
342 enlist(csp->headers, http->cmd);
348 /* otherwise elide the host information from the url */
350 p = strsav(p, http->gpc);
352 p = strsav(p, http->path);
354 p = strsav(p, http->ver);
355 enlist(csp->headers, p);
360 /* decide what we're to do with cookies */
363 if (!csp->toggled_on)
365 /* Most compatible set of permissions */
366 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
370 csp->permissions = url_permissions(http, csp);
372 #else /* ifndef TOGGLE */
373 csp->permissions = url_permissions(http, csp);
374 #endif /* ndef TOGGLE */
377 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
378 #endif /* def KILLPOPUPS */
380 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
381 ((csp->permissions & PERMIT_RE_FILTER) != 0);
382 #endif /* def PCRS */
385 /* grab the rest of the client's headers */
389 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
391 n = read_socket(csp->cfd, buf, sizeof(buf));
394 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
397 add_to_iob(csp, buf, n);
401 if (p == NULL) break;
403 enlist(csp->headers, p);
407 /* filter it as required */
409 hdr = sed(client_patterns, add_client_headers, csp);
411 destroy_list(csp->headers);
413 /* Check the request against all rules, unless
414 * we're toggled off or in force mode.
420 #endif /* def FORCE_LOAD */
421 ( (p = intercept_url(http, csp)) ||
423 (p = block_url(http, csp))
424 #ifdef FAST_REDIRECTS
425 || (fast_redirects && (p = redirect_url(http, csp)))
426 #endif /* def FAST_REDIRECTS */
431 #endif /* def STATISTICS */
433 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
435 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
436 /* Block as image? */
437 if ( (tinygif > 0) && block_imageurl(http, csp) )
439 /* Send "blocked" image */
440 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
441 http->hostport, http->path);
443 if ((tinygif == 2) || strstr(http->path, "ijb-send-banner"))
445 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
449 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
451 else if ((tinygif == 3) && (tinygifurl))
453 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(tinygifurl));
454 sprintf(p, HTTP_REDIRECT_TEMPLATE, tinygifurl);
455 write_socket(csp->cfd, p, strlen(p));
459 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
463 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
466 /* Send HTML "blocked" message, interception, or redirection result */
467 write_socket(csp->cfd, p, strlen(p));
470 log_error(LOG_LEVEL_LOG, "%s", p);
477 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
479 if (gw->forward_host)
481 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
482 gw->forward_host, gw->forward_port, http->hostport);
486 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
489 /* here we connect to the server, gateway, or the forwarder */
491 csp->sfd = (gw->conn)(gw, http, csp);
495 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
500 err = zalloc(strlen(CNXDOM) + strlen(http->host));
501 sprintf(err, CNXDOM, http->host);
505 eno = safe_strerror(errno);
506 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
507 sprintf(err, CFAIL, http->hostport, eno);
510 write_socket(csp->cfd, err, strlen(err));
512 log_error(LOG_LEVEL_LOG, err);
519 log_error(LOG_LEVEL_CONNECT, "OK");
521 if (gw->forward_host || (http->ssl == 0))
523 /* write the client's (modified) header to the server
524 * (along with anything else that may be in the buffer)
529 if ((write_socket(csp->sfd, hdr, n) != n)
530 || (flush_socket(csp->sfd, csp ) < 0))
532 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
535 eno = safe_strerror(errno);
536 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
537 sprintf(err, CFAIL, http->hostport, eno);
538 write_socket(csp->cfd, err, strlen(err));
548 * We're running an SSL tunnel and we're not forwarding,
549 * so just send the "connect succeeded" message to the
550 * client, flush the rest, and get out of the way.
552 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
560 /* we're finished with the client's header */
563 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
565 /* pass data between the client and server
566 * until one or the other shuts down the connection.
575 FD_SET(csp->cfd, &rfds);
576 FD_SET(csp->sfd, &rfds);
578 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
582 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
586 /* this is the body of the browser's request
587 * just read it and write it.
590 if (FD_ISSET(csp->cfd, &rfds))
592 n = read_socket(csp->cfd, buf, sizeof(buf));
594 if (n <= 0) break; /* "game over, man" */
596 if (write_socket(csp->sfd, buf, n) != n)
598 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
605 * The server wants to talk. It could be the header or the body.
606 * If `hdr' is null, then it's the header otherwise it's the body.
607 * FIXME: Does `hdr' really mean `host'?
611 if (FD_ISSET(csp->sfd, &rfds))
614 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
618 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
620 eno = safe_strerror(errno);
621 sprintf(buf, CFAIL, http->hostport, eno);
623 write_socket(csp->cfd, buf, strlen(buf));
627 /* Add a trailing zero. This lets filter_popups
628 * use string operations.
633 /* Filter the popups on this read. */
634 if (block_popups_now)
636 filter_popups(buf, n);
638 #endif /* def KILLPOPUPS */
640 /* Normally, this would indicate that we've read
641 * as much as the server has sent us and we can
642 * close the client connection. However, Microsoft
643 * in its wisdom has released IIS/5 with a bug that
644 * prevents it from sending the trailing \r\n in
645 * a 302 redirect header (and possibly other headers).
646 * To work around this if we've haven't parsed
647 * a full header we'll append a trailing \r\n
648 * and see if this now generates a valid one.
650 * This hack shouldn't have any impacts. If we've
651 * already transmitted the header or if this is a
652 * SSL connection, then we won't bother with this
653 * hack. So we only work on partially received
654 * headers. If we append a \r\n and this still
655 * doesn't generate a valid header, then we won't
656 * transmit anything to the client.
660 /* This hack must only be enforced for headers. */
661 if (server_body || http->ssl)
666 re_process_buffer(csp);
668 #endif /* def PCRS */
669 break; /* "game over, man" */
672 /* Let's pretend the server just sent us a blank line. */
673 n = sprintf(buf, "\r\n");
676 * Now, let the normal header parsing algorithm below do its
677 * job. If it fails, we'll exit instead of continuing.
684 * If this is an SSL connection or we're in the body
685 * of the server document, just write it to the client.
688 if (server_body || http->ssl)
693 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
696 #endif /* def PCRS */
699 if (write_socket(csp->cfd, buf, n) != n)
701 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
709 /* we're still looking for the end of the
710 * server's header ... (does that make header
711 * parsing an "out of body experience" ?
714 /* buffer up the data we just read */
715 add_to_iob(csp, buf, n);
717 /* get header lines from the iob */
719 while ((p = get_header(csp)))
723 /* see following note */
726 enlist(csp->headers, p);
730 /* NOTE: there are no "empty" headers so
731 * if the pointer `p' is not NULL we must
732 * assume that we reached the end of the
733 * buffer before we hit the end of the header.
740 /* Well, we tried our MS IIS/5
741 * hack and it didn't work.
742 * The header is incomplete
743 * and there isn't anything
744 * we can do about it.
750 /* Since we have to wait for
751 * more from the server before
752 * we can parse the headers
753 * we just continue here.
759 /* we have now received the entire header.
760 * filter it and send the result to the client
763 hdr = sed(server_patterns, add_server_headers, csp);
766 /* write the server's (modified) header to
767 * the client (along with anything else that
768 * may be in the buffer)
772 /* Start blocking popups if appropriate. */
774 if (csp->is_text && /* It's a text / * MIME-Type */
775 !http->ssl && /* We talk plaintext */
778 block_popups_now = 1;
781 #endif /* def KILLPOPUPS */
784 /* Start re_filtering this if appropriate. */
786 if (csp->is_text && /* It's a text / * MIME-Type */
787 !http->ssl && /* We talk plaintext */
788 pcrs_filter) /* Policy allows */
793 /* This next line is a little ugly, but it simplifies the if statement below. */
794 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
795 #define NOT_FILTERING_AND !filtering &&
797 #else /* not def PCRS */
799 #define NOT_FILTERING_AND
801 #endif /* def PCRS */
804 if ((write_socket(csp->cfd, hdr, n) != n)
805 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
807 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
809 /* the write failed, so don't bother
810 * mentioning it to the client...
811 * it probably can't hear us anyway.
817 /* we're finished with the server's header */
822 /* If this was a MS IIS/5 hack then it means
823 * the server has already closed the
824 * connection. Nothing more to read. Time
835 return; /* huh? we should never get here */
842 /*********************************************************************
846 * Description : This is little more than chat. We only "serve" to
847 * to close any socket that chat may have opened.
850 * 1 : csp = Current client state (buffers, headers, etc...)
854 *********************************************************************/
856 void serve(struct client_state *csp)
857 #else /* ifndef AMIGA */
858 static void serve(struct client_state *csp)
859 #endif /* def AMIGA */
862 close_socket(csp->cfd);
866 close_socket(csp->sfd);
876 /*********************************************************************
878 * Function : server_thread
880 * Description : We only exist to call `serve' in a threaded environment.
883 * 1 : data = Current client state (buffers, headers, etc...)
885 * Returns : Always 0.
887 *********************************************************************/
888 static int32 server_thread(void *data)
890 serve((struct client_state *) data);
898 /*********************************************************************
902 * Description : Load the config file and start the listen loop.
903 * This function is a lot more *sane* with the `load_config'
904 * and `listen_loop' functions; although it stills does
905 * a *little* too much for my taste.
908 * 1 : argc = Number of parameters (including $0).
909 * 2 : argv = Array of (char *)'s to the parameters.
911 * Returns : 1 if : can't open config file, unrecognized directive,
912 * stats requested in multi-thread mode, can't open the
913 * log file, can't open the jar file, listen port is invalid,
914 * any load fails, and can't bind port.
916 * Else main never returns, the process must be signaled
917 * to terminate execution. Or, on Windows, use the
918 * "File", "Exit" menu option.
920 *********************************************************************/
922 int _main(int argc, const char *argv[])
924 int main(int argc, const char *argv[])
929 "AmiTCP:db/junkbuster.config"
930 #elif !defined(_WIN32)
937 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
938 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
940 printf("JunkBuster proxy version " VERSION ".\n\n"
941 "Usage: %s [configfile]\n\n"
942 "See " HOME_PAGE_URL " for details.\n"
943 "This program is distributed under the GNU GPL, version 2 or later.\n",
947 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
949 printf(VERSION "\n");
952 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
963 configfile = argv[1];
966 remove_all_loaders();
967 memset( proxy_args, 0, sizeof( proxy_args ) );
973 * Since load_config acts as a signal handler too, it returns
974 * its status in configret. Check it for an error in loading.
976 if ( 0 != configret )
978 /* load config failed! Exit with error. */
988 signal(SIGPIPE, SIG_IGN);
989 signal(SIGCHLD, SIG_IGN);
990 signal(SIGHUP, load_config);
992 #else /* ifdef _WIN32 */
995 * We *are* in a windows console app.
996 * Print a verbose messages about FAQ's and such
999 # endif /* def _WIN_CONSOLE */
1000 #endif /* def _WIN32 */
1011 /*********************************************************************
1013 * Function : listen_loop
1015 * Description : bind the listen port and enter a "FOREVER" listening loop.
1021 *********************************************************************/
1022 static void listen_loop(void)
1024 struct client_state *csp = NULL;
1027 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1028 haddr ? haddr : "INADDR_ANY", hport);
1030 bfd = bind_port(haddr, hport);
1035 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1036 "- There may be another junkbuster or some other "
1037 "proxy running on port %d",
1038 (NULL != haddr) ? haddr : "INADDR_ANY", hport, hport
1040 /* shouldn't get here */
1047 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1048 while (waitpid(-1, NULL, WNOHANG) > 0)
1050 /* zombie children */
1052 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1055 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1057 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1061 memset(csp, '\0', sizeof(*csp));
1066 if ( config_changed )
1069 * Since we were listening to the "old port", we will not see
1070 * a "listen" param change until the next IJB request. So, at
1071 * least 1 more request must be made for us to find the new
1072 * setting. I am simply closing the old socket and binding the
1075 * Which-ever is correct, we will serve 1 more page via the
1076 * old settings. This should probably be a "show-proxy-args"
1077 * request. This should not be a so common of an operation
1078 * that this will hurt people's feelings.
1082 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1083 haddr ? haddr : "INADDR_ANY", hport);
1084 bfd = bind_port(haddr, hport);
1089 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1091 if (!accept_connection(csp, bfd))
1093 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1105 log_error(LOG_LEVEL_CONNECT, "OK");
1109 /* by haroon - most of credit to srt19170 */
1110 csp->toggled_on = g_bToggleIJB;
1113 /* add it to the list of clients */
1114 csp->next = clients->next;
1115 clients->next = csp;
1117 if (run_loader(csp))
1119 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1120 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1127 /* this is a switch () statment in the C preprocessor - ugh */
1128 #undef SELECTED_ONE_OPTION
1130 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1131 #define SELECTED_ONE_OPTION
1132 child_id = _beginthread(
1138 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1139 #define SELECTED_ONE_OPTION
1141 thread_id tid = spawn_thread
1142 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1144 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1146 child_id = (int) tid;
1155 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1156 #define SELECTED_ONE_OPTION
1157 csp->cfd = ReleaseSocket(csp->cfd, -1);
1158 if((child_id = (int)CreateNewProcTags(
1159 NP_Entry, (ULONG)server_thread,
1160 NP_Output, Output(),
1161 NP_CloseOutput, FALSE,
1162 NP_Name, (ULONG)"junkbuster child",
1163 NP_StackSize, 20*1024,
1167 ((struct Task *)child_id)->tc_UserData = csp;
1168 Signal((struct Task *)child_id, SIGF_SINGLE);
1173 #if !defined(SELECTED_ONE_OPTION)
1177 #undef SELECTED_ONE_OPTION
1178 /* end of cpp switch () */
1180 if (child_id < 0) /* failed */
1184 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1186 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1188 write_socket(csp->cfd, buf, strlen(buf));
1189 close_socket(csp->cfd);
1195 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1196 /* This block is only needed when using fork().
1197 * When using threads, the server thread was
1198 * created and run by the call to _beginthread().
1200 if (child_id == 0) /* child */
1208 /* in a fork()'d environment, the parent's
1209 * copy of the client socket and the CSP
1213 #if !defined(_WIN32) && defined(__CYGWIN__)
1215 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1216 close_socket(csp->cfd);
1219 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */