1 const char jcc_rcs[] = "$Id: jcc.c,v 1.6 2001/05/23 00:13:58 joergs 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.6 2001/05/23 00:13:58 joergs
37 * AmigaOS support fixed.
39 * Revision 1.5 2001/05/22 18:46:04 oes
41 * - Enabled filtering banners by size rather than URL
42 * by adding patterns that replace all standard banner
43 * sizes with the "Junkbuster" gif to the re_filterfile
45 * - Enabled filtering WebBugs by providing a pattern
46 * which kills all 1x1 images
48 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
49 * which is selected by the (nonstandard and therefore
50 * capital) letter 'U' in the option string.
51 * It causes the quantifiers to be ungreedy by default.
52 * Appending a ? turns back to greedy (!).
54 * - Added a new interceptor ijb-send-banner, which
55 * sends back the "Junkbuster" gif. Without imagelist or
56 * MSIE detection support, or if tinygif = 1, or the
57 * URL isn't recognized as an imageurl, a lame HTML
58 * explanation is sent instead.
60 * - Added new feature, which permits blocking remote
61 * script redirects and firing back a local redirect
63 * The feature is conditionally compiled, i.e. it
64 * can be disabled with --disable-fast-redirects,
65 * plus it must be activated by a "fast-redirects"
66 * line in the config file, has its own log level
67 * and of course wants to be displayed by show-proxy-args
68 * Note: Boy, all the #ifdefs in 1001 locations and
69 * all the fumbling with configure.in and acconfig.h
70 * were *way* more work than the feature itself :-(
72 * - Because a generic redirect template was needed for
73 * this, tinygif = 3 now uses the same.
75 * - Moved GIFs, and other static HTTP response templates
80 * - Removed some >400 CRs again (Jon, you really worked
83 * Revision 1.4 2001/05/21 19:34:01 jongfoster
84 * Made failure to bind() a fatal error.
86 * Revision 1.3 2001/05/20 01:21:20 jongfoster
87 * Version 2.9.4 checkin.
88 * - Merged popupfile and cookiefile, and added control over PCRS
89 * filtering, in new "permissionsfile".
90 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
91 * file error you now get a message box (in the Win32 GUI) rather
92 * than the program exiting with no explanation.
93 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
95 * - Removed tabs from "config"
96 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
97 * - Bumped up version number.
99 * Revision 1.2 2001/05/17 22:34:44 oes
100 * - Added hint on GIF char array generation to jcc.c
101 * - Cleaned CRLF's from the sources and related files
102 * - Repaired logging for REF and FRC
104 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
105 * Initial import of version 2.9.3 source tree
108 *********************************************************************/
114 #include <sys/types.h>
123 # include <sys/timeb.h>
124 # include <windows.h>
126 # include <process.h>
129 # endif /* def TOGGLE */
132 # ifndef _WIN_CONSOLE
134 # endif /* ndef _WIN_CONSOLE */
136 #else /* ifndef _WIN32 */
139 # include <sys/time.h>
140 # include <sys/wait.h>
141 # include <sys/stat.h>
145 # include <socket.h> /* BeOS has select() for sockets only. */
146 # include <OS.h> /* declarations for threads and stuff. */
159 #include "showargs.h"
161 #include "killpopup.h"
162 #include "miscutil.h"
164 #include "jbsockets.h"
167 const char jcc_h_rcs[] = JCC_H_VERSION;
168 const char project_h_rcs[] = PROJECT_H_VERSION;
170 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
172 struct client_state clients[1];
173 struct file_list files[1];
176 int urls_read = 0; /* total nr of urls read inc rejected */
177 int urls_rejected = 0; /* total nr of urls rejected */
178 #endif /* def STATISTICS */
181 static void listen_loop(void);
182 static void chat(struct client_state *csp);
184 void serve(struct client_state *csp);
185 #else /* ifndef AMIGA */
186 static void serve(struct client_state *csp);
187 #endif /* def AMIGA */
190 static int32 server_thread(void *data);
191 #endif /* def __BEOS__ */
194 #define sleep(N) Sleep(((N) * 1000))
198 /*********************************************************************
202 * Description : Once a connection to the client has been accepted,
203 * this function is called (via serve()) to handle the
204 * main business of the communication. When this
205 * function returns, the caller must close the client
209 * 1 : csp = Current client state (buffers, headers, etc...)
211 * Returns : On success, the number of bytes written are returned (zero
212 * indicates nothing was written). On error, -1 is returned,
213 * and errno is set appropriately. If count is zero and the
214 * file descriptor refers to a regular file, 0 will be
215 * returned without causing any other effect. For a special
216 * file, the results are not portable.
218 *********************************************************************/
219 static void chat(struct client_state *csp)
221 /* This next line is a little ugly, but it simplifies the if statement below. */
222 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
224 # define IS_TOGGLED_ON csp->toggled_on &&
225 #else /* ifndef TOGGLE */
226 # define IS_TOGGLED_ON
227 #endif /* ndef TOGGLE */
229 /* This next line is a little ugly, but it simplifies the if statement below. */
230 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
232 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
233 #else /* ifndef TRUST_FILES */
234 # define IS_TRUSTED_URL
235 #endif /* ndef TRUST_FILES */
237 char buf[BUFSIZ], *hdr, *p, *req;
241 int n, maxfd, server_body, ms_iis5_hack = 0;
242 const struct gateway *gw;
243 struct http_request *http;
245 int block_popups; /* bool, 1==will block popups */
246 int block_popups_now = 0; /* bool, 1==currently blocking popups */
247 #endif /* def KILLPOPUPS */
249 int pcrs_filter; /* bool, 1==will filter through pcrs */
250 int filtering = 0; /* bool, 1==currently filtering through pcrs */
251 #endif /* def PCRS */
256 * Read the client's request. Note that since we're not using select() we
257 * could get blocked here if a client connected, then didn't say anything!
262 n = read_socket(csp->cfd, buf, sizeof(buf));
264 if (n <= 0) break; /* error! */
266 add_to_iob(csp, buf, n);
268 req = get_header(csp);
272 break; /* no HTTP request! */
277 continue; /* more to come! */
281 /* If this request contains the FORCE_PREFIX,
282 * better get rid of it now and set the force flag --oes
285 if (strstr(req, FORCE_PREFIX))
287 strclean(req, FORCE_PREFIX);
288 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
295 #endif /* def FORCE_LOAD */
297 parse_http_request(req, http, csp);
302 if (http->cmd == NULL)
304 strcpy(buf, CHEADER);
305 write_socket(csp->cfd, buf, strlen(buf));
309 /* decide how to route the HTTP request */
311 if ((gw = forward_url(http, csp)) == NULL)
313 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
314 /* Never get here - LOG_LEVEL_FATAL causes program exit */
317 /* build the http request to send to the server
318 * we have to do one of the following:
320 * create = use the original HTTP request to create a new
321 * HTTP request that has only the path component
322 * without the http://domainspec
323 * pass = pass the original HTTP request unchanged
325 * drop = drop the HTTP request
330 * +--------+--------+
332 * 0 | create | drop |
334 * Forwarding +--------+--------+
338 * +--------+--------+
342 if (gw->forward_host)
344 /* if forwarding, just pass the request as is */
345 enlist(csp->headers, http->cmd);
351 /* otherwise elide the host information from the url */
353 p = strsav(p, http->gpc);
355 p = strsav(p, http->path);
357 p = strsav(p, http->ver);
358 enlist(csp->headers, p);
363 /* decide what we're to do with cookies */
366 if (!csp->toggled_on)
368 /* Most compatible set of permissions */
369 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
373 csp->permissions = url_permissions(http, csp);
375 #else /* ifndef TOGGLE */
376 csp->permissions = url_permissions(http, csp);
377 #endif /* ndef TOGGLE */
380 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
381 #endif /* def KILLPOPUPS */
383 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
384 ((csp->permissions & PERMIT_RE_FILTER) != 0);
385 #endif /* def PCRS */
388 /* grab the rest of the client's headers */
392 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
394 n = read_socket(csp->cfd, buf, sizeof(buf));
397 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
400 add_to_iob(csp, buf, n);
404 if (p == NULL) break;
406 enlist(csp->headers, p);
410 /* filter it as required */
412 hdr = sed(client_patterns, add_client_headers, csp);
414 destroy_list(csp->headers);
416 /* Check the request against all rules, unless
417 * we're toggled off or in force mode.
423 #endif /* def FORCE_LOAD */
424 ( (p = intercept_url(http, csp)) ||
426 (p = block_url(http, csp))
427 #ifdef FAST_REDIRECTS
428 || (fast_redirects && (p = redirect_url(http, csp)))
429 #endif /* def FAST_REDIRECTS */
434 #endif /* def STATISTICS */
436 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
438 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
439 /* Block as image? */
440 if ( (tinygif > 0) && block_imageurl(http, csp) )
442 /* Send "blocked" image */
443 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
444 http->hostport, http->path);
446 if ((tinygif == 2) || strstr(http->path, "ijb-send-banner"))
448 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
452 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
454 else if ((tinygif == 3) && (tinygifurl))
456 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(tinygifurl));
457 sprintf(p, HTTP_REDIRECT_TEMPLATE, tinygifurl);
458 write_socket(csp->cfd, p, strlen(p));
462 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
466 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
469 /* Send HTML "blocked" message, interception, or redirection result */
470 write_socket(csp->cfd, p, strlen(p));
473 log_error(LOG_LEVEL_LOG, "%s", p);
480 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
482 if (gw->forward_host)
484 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
485 gw->forward_host, gw->forward_port, http->hostport);
489 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
492 /* here we connect to the server, gateway, or the forwarder */
494 csp->sfd = (gw->conn)(gw, http, csp);
498 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
503 err = zalloc(strlen(CNXDOM) + strlen(http->host));
504 sprintf(err, CNXDOM, http->host);
508 eno = safe_strerror(errno);
509 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
510 sprintf(err, CFAIL, http->hostport, eno);
513 write_socket(csp->cfd, err, strlen(err));
515 log_error(LOG_LEVEL_LOG, err);
522 log_error(LOG_LEVEL_CONNECT, "OK");
524 if (gw->forward_host || (http->ssl == 0))
526 /* write the client's (modified) header to the server
527 * (along with anything else that may be in the buffer)
532 if ((write_socket(csp->sfd, hdr, n) != n)
533 || (flush_socket(csp->sfd, csp ) < 0))
535 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
538 eno = safe_strerror(errno);
539 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
540 sprintf(err, CFAIL, http->hostport, eno);
541 write_socket(csp->cfd, err, strlen(err));
551 * We're running an SSL tunnel and we're not forwarding,
552 * so just send the "connect succeeded" message to the
553 * client, flush the rest, and get out of the way.
555 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
563 /* we're finished with the client's header */
566 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
568 /* pass data between the client and server
569 * until one or the other shuts down the connection.
578 FD_SET(csp->cfd, &rfds);
579 FD_SET(csp->sfd, &rfds);
581 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
585 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
589 /* this is the body of the browser's request
590 * just read it and write it.
593 if (FD_ISSET(csp->cfd, &rfds))
595 n = read_socket(csp->cfd, buf, sizeof(buf));
597 if (n <= 0) break; /* "game over, man" */
599 if (write_socket(csp->sfd, buf, n) != n)
601 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
608 * The server wants to talk. It could be the header or the body.
609 * If `hdr' is null, then it's the header otherwise it's the body.
610 * FIXME: Does `hdr' really mean `host'?
614 if (FD_ISSET(csp->sfd, &rfds))
617 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
621 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
623 eno = safe_strerror(errno);
624 sprintf(buf, CFAIL, http->hostport, eno);
626 write_socket(csp->cfd, buf, strlen(buf));
630 /* Add a trailing zero. This lets filter_popups
631 * use string operations.
636 /* Filter the popups on this read. */
637 if (block_popups_now)
639 filter_popups(buf, n);
641 #endif /* def KILLPOPUPS */
643 /* Normally, this would indicate that we've read
644 * as much as the server has sent us and we can
645 * close the client connection. However, Microsoft
646 * in its wisdom has released IIS/5 with a bug that
647 * prevents it from sending the trailing \r\n in
648 * a 302 redirect header (and possibly other headers).
649 * To work around this if we've haven't parsed
650 * a full header we'll append a trailing \r\n
651 * and see if this now generates a valid one.
653 * This hack shouldn't have any impacts. If we've
654 * already transmitted the header or if this is a
655 * SSL connection, then we won't bother with this
656 * hack. So we only work on partially received
657 * headers. If we append a \r\n and this still
658 * doesn't generate a valid header, then we won't
659 * transmit anything to the client.
663 /* This hack must only be enforced for headers. */
664 if (server_body || http->ssl)
669 re_process_buffer(csp);
671 #endif /* def PCRS */
672 break; /* "game over, man" */
675 /* Let's pretend the server just sent us a blank line. */
676 n = sprintf(buf, "\r\n");
679 * Now, let the normal header parsing algorithm below do its
680 * job. If it fails, we'll exit instead of continuing.
687 * If this is an SSL connection or we're in the body
688 * of the server document, just write it to the client.
691 if (server_body || http->ssl)
696 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
699 #endif /* def PCRS */
702 if (write_socket(csp->cfd, buf, n) != n)
704 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
712 /* we're still looking for the end of the
713 * server's header ... (does that make header
714 * parsing an "out of body experience" ?
717 /* buffer up the data we just read */
718 add_to_iob(csp, buf, n);
720 /* get header lines from the iob */
722 while ((p = get_header(csp)))
726 /* see following note */
729 enlist(csp->headers, p);
733 /* NOTE: there are no "empty" headers so
734 * if the pointer `p' is not NULL we must
735 * assume that we reached the end of the
736 * buffer before we hit the end of the header.
743 /* Well, we tried our MS IIS/5
744 * hack and it didn't work.
745 * The header is incomplete
746 * and there isn't anything
747 * we can do about it.
753 /* Since we have to wait for
754 * more from the server before
755 * we can parse the headers
756 * we just continue here.
762 /* we have now received the entire header.
763 * filter it and send the result to the client
766 hdr = sed(server_patterns, add_server_headers, csp);
769 /* write the server's (modified) header to
770 * the client (along with anything else that
771 * may be in the buffer)
775 /* Start blocking popups if appropriate. */
777 if (csp->is_text && /* It's a text / * MIME-Type */
778 !http->ssl && /* We talk plaintext */
781 block_popups_now = 1;
784 #endif /* def KILLPOPUPS */
787 /* Start re_filtering this if appropriate. */
789 if (csp->is_text && /* It's a text / * MIME-Type */
790 !http->ssl && /* We talk plaintext */
791 pcrs_filter) /* Policy allows */
796 /* This next line is a little ugly, but it simplifies the if statement below. */
797 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
798 #define NOT_FILTERING_AND !filtering &&
800 #else /* not def PCRS */
802 #define NOT_FILTERING_AND
804 #endif /* def PCRS */
807 if ((write_socket(csp->cfd, hdr, n) != n)
808 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
810 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
812 /* the write failed, so don't bother
813 * mentioning it to the client...
814 * it probably can't hear us anyway.
820 /* we're finished with the server's header */
825 /* If this was a MS IIS/5 hack then it means
826 * the server has already closed the
827 * connection. Nothing more to read. Time
838 return; /* huh? we should never get here */
845 /*********************************************************************
849 * Description : This is little more than chat. We only "serve" to
850 * to close any socket that chat may have opened.
853 * 1 : csp = Current client state (buffers, headers, etc...)
857 *********************************************************************/
859 void serve(struct client_state *csp)
860 #else /* ifndef AMIGA */
861 static void serve(struct client_state *csp)
862 #endif /* def AMIGA */
865 close_socket(csp->cfd);
869 close_socket(csp->sfd);
879 /*********************************************************************
881 * Function : server_thread
883 * Description : We only exist to call `serve' in a threaded environment.
886 * 1 : data = Current client state (buffers, headers, etc...)
888 * Returns : Always 0.
890 *********************************************************************/
891 static int32 server_thread(void *data)
893 serve((struct client_state *) data);
901 /*********************************************************************
905 * Description : Load the config file and start the listen loop.
906 * This function is a lot more *sane* with the `load_config'
907 * and `listen_loop' functions; although it stills does
908 * a *little* too much for my taste.
911 * 1 : argc = Number of parameters (including $0).
912 * 2 : argv = Array of (char *)'s to the parameters.
914 * Returns : 1 if : can't open config file, unrecognized directive,
915 * stats requested in multi-thread mode, can't open the
916 * log file, can't open the jar file, listen port is invalid,
917 * any load fails, and can't bind port.
919 * Else main never returns, the process must be signaled
920 * to terminate execution. Or, on Windows, use the
921 * "File", "Exit" menu option.
923 *********************************************************************/
925 int _main(int argc, const char *argv[])
927 int main(int argc, const char *argv[])
932 "AmiTCP:db/junkbuster.config"
933 #elif !defined(_WIN32)
940 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
941 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
943 printf("JunkBuster proxy version " VERSION ".\n\n"
944 "Usage: %s [configfile]\n\n"
945 "See " HOME_PAGE_URL " for details.\n"
946 "This program is distributed under the GNU GPL, version 2 or later.\n",
950 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
952 printf(VERSION "\n");
955 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
966 configfile = argv[1];
969 remove_all_loaders();
970 memset( proxy_args, 0, sizeof( proxy_args ) );
976 * Since load_config acts as a signal handler too, it returns
977 * its status in configret. Check it for an error in loading.
979 if ( 0 != configret )
981 /* load config failed! Exit with error. */
991 signal(SIGPIPE, SIG_IGN);
992 signal(SIGCHLD, SIG_IGN);
993 signal(SIGHUP, load_config);
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;
1030 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1031 haddr ? haddr : "INADDR_ANY", hport);
1033 bfd = bind_port(haddr, hport);
1038 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1039 "- There may be another junkbuster or some other "
1040 "proxy running on port %d",
1041 (NULL != haddr) ? haddr : "INADDR_ANY", hport, hport
1043 /* shouldn't get here */
1050 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1051 while (waitpid(-1, NULL, WNOHANG) > 0)
1053 /* zombie children */
1055 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1058 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1060 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1064 memset(csp, '\0', sizeof(*csp));
1069 if ( config_changed )
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.
1085 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1086 haddr ? haddr : "INADDR_ANY", hport);
1087 bfd = bind_port(haddr, hport);
1092 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1094 if (!accept_connection(csp, bfd))
1096 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1108 log_error(LOG_LEVEL_CONNECT, "OK");
1112 /* by haroon - most of credit to srt19170 */
1113 csp->toggled_on = g_bToggleIJB;
1116 /* add it to the list of clients */
1117 csp->next = clients->next;
1118 clients->next = csp;
1120 if (run_loader(csp))
1122 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1123 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1130 /* this is a switch () statment in the C preprocessor - ugh */
1131 #undef SELECTED_ONE_OPTION
1133 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1134 #define SELECTED_ONE_OPTION
1135 child_id = _beginthread(
1141 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1142 #define SELECTED_ONE_OPTION
1144 thread_id tid = spawn_thread
1145 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1147 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1149 child_id = (int) tid;
1158 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1159 #define SELECTED_ONE_OPTION
1160 csp->cfd = ReleaseSocket(csp->cfd, -1);
1161 if((child_id = (int)CreateNewProcTags(
1162 NP_Entry, (ULONG)server_thread,
1163 NP_Output, Output(),
1164 NP_CloseOutput, FALSE,
1165 NP_Name, (ULONG)"junkbuster child",
1166 NP_StackSize, 20*1024,
1170 ((struct Task *)child_id)->tc_UserData = csp;
1171 Signal((struct Task *)child_id, SIGF_SINGLE);
1176 #if !defined(SELECTED_ONE_OPTION)
1180 #undef SELECTED_ONE_OPTION
1181 /* end of cpp switch () */
1183 if (child_id < 0) /* failed */
1187 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1189 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1191 write_socket(csp->cfd, buf, strlen(buf));
1192 close_socket(csp->cfd);
1198 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1199 /* This block is only needed when using fork().
1200 * When using threads, the server thread was
1201 * created and run by the call to _beginthread().
1203 if (child_id == 0) /* child */
1211 /* in a fork()'d environment, the parent's
1212 * copy of the client socket and the CSP
1216 #if !defined(_WIN32) && defined(__CYGWIN__)
1218 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1219 close_socket(csp->cfd);
1222 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */