1 const char jcc_rcs[] = "$Id: jcc.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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.
37 *********************************************************************/
43 #include <sys/types.h>
52 # include <sys/timeb.h>
58 # endif /* def TOGGLE */
63 # endif /* ndef _WIN_CONSOLE */
65 #else /* ifndef _WIN32 */
68 # include <sys/time.h>
69 # include <sys/wait.h>
70 # include <sys/stat.h>
74 # include <socket.h> /* BeOS has select() for sockets only. */
75 # include <OS.h> /* declarations for threads and stuff. */
90 #include "killpopup.h"
93 #include "jbsockets.h"
96 const char jcc_h_rcs[] = JCC_H_VERSION;
97 const char project_h_rcs[] = PROJECT_H_VERSION;
99 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
101 struct client_state clients[1];
102 struct file_list files[1];
105 int urls_read = 0; /* total nr of urls read inc rejected */
106 int urls_rejected = 0; /* total nr of urls rejected */
107 #endif /* def STATISTICS */
110 static void listen_loop(void);
111 static void chat(struct client_state *csp);
112 static void serve(struct client_state *csp);
114 static int32 server_thread(void *data);
115 #endif /* def __BEOS__ */
118 #define BODY "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
120 static const char CFAIL[] =
121 "HTTP/1.0 503 Connect failed\n"
122 "Content-Type: text/html\n\n"
125 "<title>Internet Junkbuster: Connect failed</title>\n"
131 "TCP connection to '%s' failed: %s.\n<br>"
135 static const char CNXDOM[] =
136 "HTTP/1.0 404 Non-existent domain\n"
137 "Content-Type: text/html\n\n"
140 "<title>Internet Junkbuster: Non-existent domain</title>\n"
146 "No such domain: %s\n"
150 static const char CSUCCEED[] =
151 "HTTP/1.0 200 Connection established\n"
152 "Proxy-Agent: IJ/" VERSION "\n\n";
154 static const char CHEADER[] =
155 "HTTP/1.0 400 Invalid header received from browser\n\n";
157 static const char SHEADER[] =
158 "HTTP/1.0 502 Invalid header received from server\n\n";
160 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
161 static const char BLANKGIF[] =
162 "HTTP/1.0 200 OK\r\n"
163 "Pragma: no-cache\r\n"
164 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
165 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
166 "Content-type: image/gif\r\n\r\n"
167 "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
168 "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
169 "\000\001\000\000\002\002D\001\000;";
171 static const char JBGIF[] =
172 "HTTP/1.0 200 OK\r\n"
173 "Pragma: no-cache\r\n"
174 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
175 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
176 "Content-type: image/gif\r\n\r\n"
177 "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
178 "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
179 "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
180 "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
181 "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
182 "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
183 "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
186 static const char FWGIF[] =
\r
187 "HTTP/1.0 302 Blocked Advert\r\n"
\r
188 "Pragma: no-cache\r\n"
\r
189 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
190 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
\r
193 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
196 #define sleep(N) Sleep(((N) * 1000))
200 /*********************************************************************
204 * Description : Once a connection to the client has been accepted,
205 * this function is called (via serve()) to handle the
206 * main business of the communication. When this
207 * function returns, the caller must close the client
211 * 1 : csp = Current client state (buffers, headers, etc...)
213 * Returns : On success, the number of bytes written are returned (zero
214 * indicates nothing was written). On error, -1 is returned,
215 * and errno is set appropriately. If count is zero and the
216 * file descriptor refers to a regular file, 0 will be
217 * returned without causing any other effect. For a special
218 * file, the results are not portable.
220 *********************************************************************/
221 static void chat(struct client_state *csp)
223 char buf[BUFSIZ], *hdr, *p, *req;
227 int n, maxfd, server_body, ms_iis5_hack = 0;
228 struct cookie_spec *cs;
229 const struct gateway *gw;
230 struct http_request *http;
233 #endif /* def PCRS */
238 * Read the client's request. Note that since we're not using select() we
239 * could get blocked here if a client connected, then didn't say anything!
244 n = read_socket(csp->cfd, buf, sizeof(buf));
246 if (n <= 0) break; /* error! */
248 add_to_iob(csp, buf, n);
250 req = get_header(csp);
254 break; /* no HTTP request! */
\r
259 continue; /* more to come! */
\r
263 /* If this request contains the FORCE_PREFIX,
264 * better get rid of it now and set the force flag --oes
267 if(strstr(req, FORCE_PREFIX))
269 strclean(req, FORCE_PREFIX);
270 /* if DEBUG(FRC) fprintf(logfp, "%s: Enforcing request \"%s\".\n", prog, req); */
277 #endif /* def FORCE_LOAD */
279 parse_http_request(req, http, csp);
284 if (http->cmd == NULL)
286 strcpy(buf, CHEADER);
287 write_socket(csp->cfd, buf, strlen(buf));
291 /* decide how to route the HTTP request */
293 if ((gw = forward_url(http, csp)) == NULL)
295 log_error(LOG_LEVEL_ERROR, "gateway spec is NULL!?!? This can't happen!");
299 /* build the http request to send to the server
300 * we have to do one of the following:
302 * create = use the original HTTP request to create a new
303 * HTTP request that has only the path component
304 * without the http://domainspec
305 * pass = pass the original HTTP request unchanged
307 * drop = drop the HTTP request
312 * +--------+--------+
314 * 0 | create | drop |
316 * Forwarding +--------+--------+
320 * +--------+--------+
324 if (gw->forward_host)
326 /* if forwarding, just pass the request as is */
327 enlist(csp->headers, http->cmd);
333 /* otherwise elide the host information from the url */
335 p = strsav(p, http->gpc);
337 p = strsav(p, http->path);
339 p = strsav(p, http->ver);
340 enlist(csp->headers, p);
345 /* decide what we're to do with cookies */
349 * by haroon - most of credit to srt19170
350 * if toggled_on flag is false IJB is disabled, pass cookies thru
352 if (!csp->toggled_on)
354 csp->accept_server_cookie = 1;
355 csp->send_user_cookie = 1;
360 if ((cs = cookie_url(http, csp)))
362 csp->accept_server_cookie = cs->accept_server_cookie;
363 csp->send_user_cookie = cs->send_user_cookie;
367 csp->accept_server_cookie = 0;
368 csp->send_user_cookie = 0;
371 /* grab the rest of the client's headers */
375 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
377 n = read_socket(csp->cfd, buf, sizeof(buf));
380 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
383 add_to_iob(csp, buf, n);
387 if (p == NULL) break;
389 enlist(csp->headers, p);
393 /* filter it as required */
395 hdr = sed(client_patterns, add_client_headers, csp);
397 destroy_list(csp->headers);
401 * by haroon - most of credit to srt19170
402 * if toggled_on flag is true then IJB is enabled, do the usual
403 * otherwise avoid crunching
406 /* This next line is a little ugly, but it simplifies the if statement below. */
407 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
408 #define IS_TOGGLED_ON csp->toggled_on &&
410 #else /* ifndef TOGGLE */
412 /* We don't have TOGGLE, so we don't care about toggling. */
413 #define IS_TOGGLED_ON
415 #endif /* ndef TOGGLE */
419 /* This next line is a little ugly, but it simplifies the if statement below. */
420 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
421 #define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
423 #else /* ifndef TRUST_FILES */
425 /* We don't have TRUST_FILES, so we don't care about trusted URL's. */
426 #define IS_TRUSTED_URL
428 #endif /* ndef TRUST_FILES */
431 /* Check the request against all rules, unless
432 * we're disabled or in force mode.
438 #endif /* def FORCE_LOAD */
439 ( (p = intercept_url(http, csp)) ||
441 (p = block_url(http, csp)) ))
445 #endif /* def STATISTICS */
447 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
449 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
450 /* now use block_imageurl */
451 if ( (tinygif > 0) && block_imageurl(http, csp) )
453 /* Send "blocked" image */
454 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
455 http->hostport, http->path);
459 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
461 else if ((tinygif == 3) && (tinygifurl))
\r
463 write_socket(csp->cfd, FWGIF, sizeof(FWGIF)-1);
\r
464 write_socket(csp->cfd, tinygifurl, strlen(tinygifurl));
\r
468 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
472 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
474 /* Send HTML "blocked" message */
475 write_socket(csp->cfd, p, strlen(p));
478 log_error(LOG_LEVEL_LOG, "%s", p);
485 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
487 if (gw->forward_host)
489 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
490 gw->forward_host, gw->forward_port, http->hostport);
494 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
497 /* here we connect to the server, gateway, or the forwarder */
499 csp->sfd = (gw->conn)(gw, http, csp);
503 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
508 err = zalloc(strlen(CNXDOM) + strlen(http->host));
509 sprintf(err, CNXDOM, http->host);
513 eno = safe_strerror(errno);
514 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
515 sprintf(err, CFAIL, http->hostport, eno);
518 write_socket(csp->cfd, err, strlen(err));
520 log_error(LOG_LEVEL_LOG, err);
527 log_error(LOG_LEVEL_CONNECT, "OK");
529 if (gw->forward_host || (http->ssl == 0))
531 /* write the client's (modified) header to the server
532 * (along with anything else that may be in the buffer)
537 if ((write_socket(csp->sfd, hdr, n) != n)
538 || (flush_socket(csp->sfd, csp ) < 0))
540 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
543 eno = safe_strerror(errno);
544 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
545 sprintf(err, CFAIL, http->hostport, eno);
546 write_socket(csp->cfd, err, strlen(err));
556 * We're running an SSL tunnel and we're not forwarding,
557 * so just send the "connect succeeded" message to the
558 * client, flush the rest, and get out of the way.
560 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
568 /* we're finished with the client's header */
571 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
573 /* pass data between the client and server
574 * until one or the other shuts down the connection.
583 FD_SET(csp->cfd, &rfds);
584 FD_SET(csp->sfd, &rfds);
586 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
590 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
594 /* this is the body of the browser's request
595 * just read it and write it.
598 if (FD_ISSET(csp->cfd, &rfds))
600 n = read_socket(csp->cfd, buf, sizeof(buf));
602 if (n <= 0) break; /* "game over, man" */
604 if (write_socket(csp->sfd, buf, n) != n)
606 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
613 * The server wants to talk. It could be the header or the body.
614 * If `hdr' is null, then it's the header otherwise it's the body.
615 * FIXME: Does `hdr' really mean `host'?
619 if (FD_ISSET(csp->sfd, &rfds))
622 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
626 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
628 eno = safe_strerror(errno);
629 sprintf(buf, CFAIL, http->hostport, eno);
631 write_socket(csp->cfd, buf, strlen(buf));
635 /* Add a trailing zero. This lets filter_popups
636 * use string operations.
641 /* Filter the popups on this read. */
644 ( ( http->host != NULL ) && ( popupfile != NULL ) ) ) )
646 filter_popups(csp, http->host, buf, n);
648 #endif /* def KILLPOPUPS */
650 /* Normally, this would indicate that we've read
651 * as much as the server has sent us and we can
652 * close the client connection. However, Microsoft
653 * in its wisdom has released IIS/5 with a bug that
654 * prevents it from sending the trailing \r\n in
655 * a 302 redirect header (and possibly other headers).
656 * To work around this if we've haven't parsed
657 * a full header we'll append a trailing \r\n
658 * and see if this now generates a valid one.
660 * This hack shouldn't have any impacts. If we've
661 * already transmitted the header or if this is a
662 * SSL connection, then we won't bother with this
663 * hack. So we only work on partially received
664 * headers. If we append a \r\n and this still
665 * doesn't generate a valid header, then we won't
666 * transmit anything to the client.
670 /* This hack must only be enforced for headers. */
671 if (server_body || http->ssl)
676 re_process_buffer(csp);
678 #endif /* def PCRS */
679 break; /* "game over, man" */
682 /* Let's pretend the server just sent us a blank line. */
683 n = sprintf(buf, "\r\n");
686 * Now, let the normal header parsing algorithm below do its
687 * job. If it fails, we'll exit instead of continuing.
694 * If this is an SSL connection or we're in the body
695 * of the server document, just write it to the client.
698 if (server_body || http->ssl)
703 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
706 #endif /* def PCRS */
709 if (write_socket(csp->cfd, buf, n) != n)
711 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
719 /* we're still looking for the end of the
720 * server's header ... (does that make header
721 * parsing an "out of body experience" ?
724 /* buffer up the data we just read */
725 add_to_iob(csp, buf, n);
727 /* get header lines from the iob */
729 while ((p = get_header(csp)))
733 /* see following note */
736 enlist(csp->headers, p);
740 /* NOTE: there are no "empty" headers so
741 * if the pointer `p' is not NULL we must
742 * assume that we reached the end of the
743 * buffer before we hit the end of the header.
750 /* Well, we tried our MS IIS/5
751 * hack and it didn't work.
752 * The header is incomplete
753 * and there isn't anything
754 * we can do about it.
760 /* Since we have to wait for
761 * more from the server before
762 * we can parse the headers
763 * we just continue here.
769 /* we have now received the entire header.
770 * filter it and send the result to the client
773 hdr = sed(server_patterns, add_server_headers, csp);
776 /* write the server's (modified) header to
777 * the client (along with anything else that
778 * may be in the buffer)
782 /* Decide if we want to re_filter this. */
784 if (IS_TOGGLED_ON /* Only filter if toggle is "on" */
785 csp->is_text && /* It's a text / * MIME-Type */
786 re_filterfile && /* There are expressions to be used */
787 !http->ssl && /* We talk plaintext */
788 (re_filter_all || !csp->send_user_cookie)) /* 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 *********************************************************************/
855 static void serve(struct client_state *csp)
858 close_socket(csp->cfd);
862 close_socket(csp->sfd);
872 /*********************************************************************
874 * Function : server_thread
876 * Description : We only exist to call `serve' in a threaded environment.
879 * 1 : data = Current client state (buffers, headers, etc...)
881 * Returns : Always 0.
883 *********************************************************************/
884 static int32 server_thread(void *data)
886 serve((struct client_state *) data);
894 /*********************************************************************
898 * Description : Load the config file and start the listen loop.
899 * This function is a lot more *sane* with the `load_config'
900 * and `listen_loop' functions; although it stills does
901 * a *little* too much for my taste.
904 * 1 : argc = Number of parameters (including $0).
905 * 2 : argv = Array of (char *)'s to the parameters.
907 * Returns : 1 if : can't open config file, unrecognized directive,
908 * stats requested in multi-thread mode, can't open the
909 * log file, can't open the jar file, listen port is invalid,
910 * any load fails, and can't bind port.
912 * Else main never returns, the process must be signaled
913 * to terminate execution. Or, on Windows, use the
914 * "File", "Exit" menu option.
916 *********************************************************************/
918 int _main(int argc, const char *argv[])
920 int main(int argc, const char *argv[])
931 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
932 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
934 printf("JunkBuster proxy version " VERSION ".\n\n"
935 "Usage: %s [configfile]\n\n"
936 "See " HOME_PAGE_URL " for details.\n"
937 "This program is distributed under the GNU GPL, version 2 or later.\n",
941 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
943 printf(VERSION "\n");
946 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
953 configfile = argv[1];
956 remove_all_loaders();
957 memset( proxy_args, 0, sizeof( proxy_args ) );
963 * Since load_config acts as a signal handler too, it returns
964 * its status in configret. Check it for an error in loading.
966 if ( 0 != configret )
968 /* load config failed! Exit with error. */
978 signal(SIGPIPE, SIG_IGN);
979 signal(SIGCHLD, SIG_IGN);
980 signal(SIGHUP, load_config);
982 #else /* ifdef _WIN32 */
985 * We *are* in a windows console app.
986 * Print a verbose messages about FAQ's and such
989 # endif /* def _WIN_CONSOLE */
990 #endif /* def _WIN32 */
1001 /*********************************************************************
1003 * Function : listen_loop
1005 * Description : bind the listen port and enter a "FOREVER" listening loop.
1011 *********************************************************************/
1012 static void listen_loop(void)
1014 struct client_state *csp = NULL;
1017 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1018 haddr ? haddr : "INADDR_ANY", hport);
1020 bfd = bind_port(haddr, hport);
1025 log_error(LOG_LEVEL_ERROR, "can't bind %s:%d: %E "
1026 "- There may be another junkbuster or some other "
1027 "proxy running on port %d",
1028 (NULL != haddr) ? haddr : "INADDR_ANY", hport, hport
1036 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1037 while (waitpid(-1, NULL, WNOHANG) > 0)
1039 /* zombie children */
1041 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1044 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1046 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1050 memset(csp, '\0', sizeof(*csp));
1055 if ( config_changed )
1058 * Since we were listening to the "old port", we will not see
1059 * a "listen" param change until the next IJB request. So, at
1060 * least 1 more request must be made for us to find the new
1061 * setting. I am simply closing the old socket and binding the
1064 * Which-ever is correct, we will serve 1 more page via the
1065 * old settings. This should probably be a "show-proxy-args"
1066 * request. This should not be a so common of an operation
1067 * that this will hurt people's feelings.
1071 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1072 haddr ? haddr : "INADDR_ANY", hport);
1073 bfd = bind_port(haddr, hport);
1078 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1080 if (!accept_connection(csp, bfd))
1082 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1094 log_error(LOG_LEVEL_CONNECT, "OK");
1098 /* by haroon - most of credit to srt19170 */
1099 csp->toggled_on = g_bToggleIJB;
1102 /* add it to the list of clients */
1103 csp->next = clients->next;
1104 clients->next = csp;
1106 if (run_loader(csp))
1108 log_error(LOG_LEVEL_ERROR, "a loader failed - must exit");
1116 /* this is a switch () statment in the C preprocessor - ugh */
1117 #undef SELECTED_ONE_OPTION
1119 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1120 #define SELECTED_ONE_OPTION
1121 child_id = _beginthread(
1127 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1128 #define SELECTED_ONE_OPTION
1130 thread_id tid = spawn_thread
1131 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1133 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1135 child_id = (int) tid;
1144 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
\r
1145 #define SELECTED_ONE_OPTION
\r
1146 csp->cfd = ReleaseSocket(csp->cfd, -1);
\r
1147 if((child_id = (int)CreateNewProcTags(
\r
1148 NP_Entry, (ULONG)server_thread,
\r
1149 NP_Output, Output(),
\r
1150 NP_CloseOutput, FALSE,
\r
1151 NP_Name, (ULONG)"junkbuster child",
\r
1152 NP_StackSize, 20*1024,
\r
1156 ((struct Task *)child_id)->tc_UserData = csp;
\r
1157 Signal((struct Task *)child_id, SIGF_SINGLE);
\r
1158 Wait(SIGF_SINGLE);
\r
1162 #if !defined(SELECTED_ONE_OPTION)
1166 #undef SELECTED_ONE_OPTION
1167 /* end of cpp switch () */
1169 if (child_id < 0) /* failed */
1173 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1175 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1177 write_socket(csp->cfd, buf, strlen(buf));
1178 close_socket(csp->cfd);
1184 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1185 /* This block is only needed when using fork().
1186 * When using threads, the server thread was
1187 * created and run by the call to _beginthread().
1189 if (child_id == 0) /* child */
1197 /* in a fork()'d environment, the parent's
1198 * copy of the client socket and the CSP
1202 #if !defined(_WIN32) && defined(__CYGWIN__)
1204 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1205 close_socket(csp->cfd);
1208 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */