1 const char jcc_rcs[] = "$Id: jcc.c,v 1.2 2001/05/17 22:34:44 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.2 2001/05/17 22:34:44 oes
37 * - Added hint on GIF char array generation to jcc.c
38 * - Cleaned CRLF's from the sources and related files
39 * - Repaired logging for REF and FRC
41 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
42 * Initial import of version 2.9.3 source tree
45 *********************************************************************/
51 #include <sys/types.h>
60 # include <sys/timeb.h>
66 # endif /* def TOGGLE */
71 # endif /* ndef _WIN_CONSOLE */
73 #else /* ifndef _WIN32 */
76 # include <sys/time.h>
77 # include <sys/wait.h>
78 # include <sys/stat.h>
82 # include <socket.h> /* BeOS has select() for sockets only. */
83 # include <OS.h> /* declarations for threads and stuff. */
98 #include "killpopup.h"
101 #include "jbsockets.h"
104 const char jcc_h_rcs[] = JCC_H_VERSION;
105 const char project_h_rcs[] = PROJECT_H_VERSION;
107 const char DEFAULT_USER_AGENT[] ="User-Agent: Mozilla (X11; I; Linux 2.0.32 i586)";
109 struct client_state clients[1];
110 struct file_list files[1];
113 int urls_read = 0; /* total nr of urls read inc rejected */
114 int urls_rejected = 0; /* total nr of urls rejected */
115 #endif /* def STATISTICS */
118 static void listen_loop(void);
119 static void chat(struct client_state *csp);
120 static void serve(struct client_state *csp);
122 static int32 server_thread(void *data);
123 #endif /* def __BEOS__ */
126 #define BODY "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
128 static const char CFAIL[] =
129 "HTTP/1.0 503 Connect failed\n"
130 "Content-Type: text/html\n\n"
133 "<title>Internet Junkbuster: Connect failed</title>\n"
139 "TCP connection to '%s' failed: %s.\n<br>"
143 static const char CNXDOM[] =
144 "HTTP/1.0 404 Non-existent domain\n"
145 "Content-Type: text/html\n\n"
148 "<title>Internet Junkbuster: Non-existent domain</title>\n"
154 "No such domain: %s\n"
158 static const char CSUCCEED[] =
159 "HTTP/1.0 200 Connection established\n"
160 "Proxy-Agent: IJ/" VERSION "\n\n";
162 static const char CHEADER[] =
163 "HTTP/1.0 400 Invalid header received from browser\n\n";
165 static const char SHEADER[] =
166 "HTTP/1.0 502 Invalid header received from server\n\n";
168 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
171 * Hint: You can encode your own GIFs like that:
172 * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
175 static const char BLANKGIF[] =
176 "HTTP/1.0 200 OK\r\n"
177 "Pragma: no-cache\r\n"
178 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
179 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
180 "Content-type: image/gif\r\n\r\n"
181 "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
182 "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
183 "\000\001\000\000\002\002D\001\000;";
185 static const char JBGIF[] =
186 "HTTP/1.0 200 OK\r\n"
187 "Pragma: no-cache\r\n"
188 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
189 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
190 "Content-type: image/gif\r\n\r\n"
191 "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
192 "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
193 "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
194 "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
195 "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
196 "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
197 "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
200 static const char FWGIF[] =
201 "HTTP/1.0 302 Blocked Advert\r\n"
202 "Pragma: no-cache\r\n"
203 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
204 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
207 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
210 #define sleep(N) Sleep(((N) * 1000))
214 /*********************************************************************
218 * Description : Once a connection to the client has been accepted,
219 * this function is called (via serve()) to handle the
220 * main business of the communication. When this
221 * function returns, the caller must close the client
225 * 1 : csp = Current client state (buffers, headers, etc...)
227 * Returns : On success, the number of bytes written are returned (zero
228 * indicates nothing was written). On error, -1 is returned,
229 * and errno is set appropriately. If count is zero and the
230 * file descriptor refers to a regular file, 0 will be
231 * returned without causing any other effect. For a special
232 * file, the results are not portable.
234 *********************************************************************/
235 static void chat(struct client_state *csp)
237 /* This next line is a little ugly, but it simplifies the if statement below. */
\r
238 /* Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't */
\r
240 # define IS_TOGGLED_ON csp->toggled_on &&
\r
241 #else /* ifndef TOGGLE */
\r
242 # define IS_TOGGLED_ON
\r
243 #endif /* ndef TOGGLE */
\r
245 /* This next line is a little ugly, but it simplifies the if statement below. */
\r
246 /* Basically if TRUST_FILES, then we want the if to call "trust_url", else we don't */
\r
248 # define IS_TRUSTED_URL (p = trust_url(http, csp)) ||
\r
249 #else /* ifndef TRUST_FILES */
\r
250 # define IS_TRUSTED_URL
\r
251 #endif /* ndef TRUST_FILES */
\r
254 char buf[BUFSIZ], *hdr, *p, *req;
258 int n, maxfd, server_body, ms_iis5_hack = 0;
259 const struct gateway *gw;
260 struct http_request *http;
\r
262 int block_popups; /* bool, 1==will block popups */
\r
263 int block_popups_now = 0; /* bool, 1==currently blocking popups */
\r
264 #endif /* def KILLPOPUPS */
\r
266 int pcrs_filter; /* bool, 1==will filter through pcrs */
\r
267 int filtering = 0; /* bool, 1==currently filtering through pcrs */
268 #endif /* def PCRS */
273 * Read the client's request. Note that since we're not using select() we
274 * could get blocked here if a client connected, then didn't say anything!
279 n = read_socket(csp->cfd, buf, sizeof(buf));
281 if (n <= 0) break; /* error! */
283 add_to_iob(csp, buf, n);
285 req = get_header(csp);
289 break; /* no HTTP request! */
294 continue; /* more to come! */
298 /* If this request contains the FORCE_PREFIX,
299 * better get rid of it now and set the force flag --oes
302 if(strstr(req, FORCE_PREFIX))
304 strclean(req, FORCE_PREFIX);
305 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
312 #endif /* def FORCE_LOAD */
314 parse_http_request(req, http, csp);
319 if (http->cmd == NULL)
321 strcpy(buf, CHEADER);
322 write_socket(csp->cfd, buf, strlen(buf));
326 /* decide how to route the HTTP request */
328 if ((gw = forward_url(http, csp)) == NULL)
330 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
331 /* Never get here - LOG_LEVEL_FATAL causes program exit */
\r
334 /* build the http request to send to the server
335 * we have to do one of the following:
337 * create = use the original HTTP request to create a new
338 * HTTP request that has only the path component
339 * without the http://domainspec
340 * pass = pass the original HTTP request unchanged
342 * drop = drop the HTTP request
347 * +--------+--------+
349 * 0 | create | drop |
351 * Forwarding +--------+--------+
355 * +--------+--------+
359 if (gw->forward_host)
361 /* if forwarding, just pass the request as is */
362 enlist(csp->headers, http->cmd);
368 /* otherwise elide the host information from the url */
370 p = strsav(p, http->gpc);
372 p = strsav(p, http->path);
374 p = strsav(p, http->ver);
375 enlist(csp->headers, p);
380 /* decide what we're to do with cookies */
383 if (!csp->toggled_on)
\r
385 /* Most compatible set of permissions */
\r
386 csp->permissions = PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | PERMIT_POPUPS;
\r
390 csp->permissions = url_permissions(http, csp);
\r
392 #else /* ifndef TOGGLE */
\r
393 csp->permissions = url_permissions(http, csp);
\r
394 #endif /* ndef TOGGLE */
\r
397 block_popups = ((csp->permissions & PERMIT_POPUPS) == 0);
\r
398 #endif /* def KILLPOPUPS */
\r
400 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
\r
401 ((csp->permissions & PERMIT_RE_FILTER) != 0);
402 #endif /* def PCRS */
\r
405 /* grab the rest of the client's headers */
409 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
411 n = read_socket(csp->cfd, buf, sizeof(buf));
414 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
417 add_to_iob(csp, buf, n);
421 if (p == NULL) break;
423 enlist(csp->headers, p);
427 /* filter it as required */
429 hdr = sed(client_patterns, add_client_headers, csp);
431 destroy_list(csp->headers);
435 * by haroon - most of credit to srt19170
\r
436 * if toggled_on flag is true then IJB is enabled, do the usual
\r
437 * otherwise avoid crunching
\r
440 /* Check the request against all rules, unless
441 * we're disabled or in force mode.
447 #endif /* def FORCE_LOAD */
448 ( (p = intercept_url(http, csp)) ||
450 (p = block_url(http, csp)) ))
454 #endif /* def STATISTICS */
456 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
458 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
459 /* now use block_imageurl */
460 if ( (tinygif > 0) && block_imageurl(http, csp) )
462 /* Send "blocked" image */
463 log_error(LOG_LEVEL_GPC, "%s%s image crunch!",
464 http->hostport, http->path);
468 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
470 else if ((tinygif == 3) && (tinygifurl))
472 write_socket(csp->cfd, FWGIF, sizeof(FWGIF)-1);
473 write_socket(csp->cfd, tinygifurl, strlen(tinygifurl));
477 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
481 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
483 /* Send HTML "blocked" message */
484 write_socket(csp->cfd, p, strlen(p));
487 log_error(LOG_LEVEL_LOG, "%s", p);
494 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
496 if (gw->forward_host)
498 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
499 gw->forward_host, gw->forward_port, http->hostport);
503 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
506 /* here we connect to the server, gateway, or the forwarder */
508 csp->sfd = (gw->conn)(gw, http, csp);
512 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
517 err = zalloc(strlen(CNXDOM) + strlen(http->host));
518 sprintf(err, CNXDOM, http->host);
522 eno = safe_strerror(errno);
523 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
524 sprintf(err, CFAIL, http->hostport, eno);
527 write_socket(csp->cfd, err, strlen(err));
529 log_error(LOG_LEVEL_LOG, err);
536 log_error(LOG_LEVEL_CONNECT, "OK");
538 if (gw->forward_host || (http->ssl == 0))
540 /* write the client's (modified) header to the server
541 * (along with anything else that may be in the buffer)
546 if ((write_socket(csp->sfd, hdr, n) != n)
547 || (flush_socket(csp->sfd, csp ) < 0))
549 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
552 eno = safe_strerror(errno);
553 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
554 sprintf(err, CFAIL, http->hostport, eno);
555 write_socket(csp->cfd, err, strlen(err));
565 * We're running an SSL tunnel and we're not forwarding,
566 * so just send the "connect succeeded" message to the
567 * client, flush the rest, and get out of the way.
569 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
577 /* we're finished with the client's header */
580 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
582 /* pass data between the client and server
583 * until one or the other shuts down the connection.
592 FD_SET(csp->cfd, &rfds);
593 FD_SET(csp->sfd, &rfds);
595 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
599 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
603 /* this is the body of the browser's request
604 * just read it and write it.
607 if (FD_ISSET(csp->cfd, &rfds))
609 n = read_socket(csp->cfd, buf, sizeof(buf));
611 if (n <= 0) break; /* "game over, man" */
613 if (write_socket(csp->sfd, buf, n) != n)
615 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
622 * The server wants to talk. It could be the header or the body.
623 * If `hdr' is null, then it's the header otherwise it's the body.
624 * FIXME: Does `hdr' really mean `host'?
628 if (FD_ISSET(csp->sfd, &rfds))
631 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
635 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
637 eno = safe_strerror(errno);
638 sprintf(buf, CFAIL, http->hostport, eno);
640 write_socket(csp->cfd, buf, strlen(buf));
644 /* Add a trailing zero. This lets filter_popups
645 * use string operations.
650 /* Filter the popups on this read. */
651 if (block_popups_now)
653 filter_popups(buf, n);
655 #endif /* def KILLPOPUPS */
657 /* Normally, this would indicate that we've read
658 * as much as the server has sent us and we can
659 * close the client connection. However, Microsoft
660 * in its wisdom has released IIS/5 with a bug that
661 * prevents it from sending the trailing \r\n in
662 * a 302 redirect header (and possibly other headers).
663 * To work around this if we've haven't parsed
664 * a full header we'll append a trailing \r\n
665 * and see if this now generates a valid one.
667 * This hack shouldn't have any impacts. If we've
668 * already transmitted the header or if this is a
669 * SSL connection, then we won't bother with this
670 * hack. So we only work on partially received
671 * headers. If we append a \r\n and this still
672 * doesn't generate a valid header, then we won't
673 * transmit anything to the client.
677 /* This hack must only be enforced for headers. */
678 if (server_body || http->ssl)
683 re_process_buffer(csp);
685 #endif /* def PCRS */
686 break; /* "game over, man" */
689 /* Let's pretend the server just sent us a blank line. */
690 n = sprintf(buf, "\r\n");
693 * Now, let the normal header parsing algorithm below do its
694 * job. If it fails, we'll exit instead of continuing.
701 * If this is an SSL connection or we're in the body
702 * of the server document, just write it to the client.
705 if (server_body || http->ssl)
710 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
713 #endif /* def PCRS */
716 if (write_socket(csp->cfd, buf, n) != n)
718 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
726 /* we're still looking for the end of the
727 * server's header ... (does that make header
728 * parsing an "out of body experience" ?
731 /* buffer up the data we just read */
732 add_to_iob(csp, buf, n);
734 /* get header lines from the iob */
736 while ((p = get_header(csp)))
740 /* see following note */
743 enlist(csp->headers, p);
747 /* NOTE: there are no "empty" headers so
748 * if the pointer `p' is not NULL we must
749 * assume that we reached the end of the
750 * buffer before we hit the end of the header.
757 /* Well, we tried our MS IIS/5
758 * hack and it didn't work.
759 * The header is incomplete
760 * and there isn't anything
761 * we can do about it.
767 /* Since we have to wait for
768 * more from the server before
769 * we can parse the headers
770 * we just continue here.
776 /* we have now received the entire header.
777 * filter it and send the result to the client
780 hdr = sed(server_patterns, add_server_headers, csp);
783 /* write the server's (modified) header to
784 * the client (along with anything else that
785 * may be in the buffer)
789 /* Start blocking popups if appropriate. */
\r
791 if (csp->is_text && /* It's a text / * MIME-Type */
\r
792 !http->ssl && /* We talk plaintext */
\r
795 block_popups_now = 1;
\r
798 #endif /* def KILLPOPUPS */
\r
801 /* Start re_filtering this if appropriate. */
803 if (csp->is_text && /* It's a text / * MIME-Type */
804 !http->ssl && /* We talk plaintext */
\r
805 pcrs_filter) /* Policy allows */
810 /* This next line is a little ugly, but it simplifies the if statement below. */
811 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
812 #define NOT_FILTERING_AND !filtering &&
814 #else /* not def PCRS */
816 #define NOT_FILTERING_AND
818 #endif /* def PCRS */
821 if ((write_socket(csp->cfd, hdr, n) != n)
822 || (NOT_FILTERING_AND (flush_socket(csp->cfd, csp) < 0)))
824 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
826 /* the write failed, so don't bother
827 * mentioning it to the client...
828 * it probably can't hear us anyway.
834 /* we're finished with the server's header */
839 /* If this was a MS IIS/5 hack then it means
840 * the server has already closed the
841 * connection. Nothing more to read. Time
852 return; /* huh? we should never get here */
859 /*********************************************************************
863 * Description : This is little more than chat. We only "serve" to
864 * to close any socket that chat may have opened.
867 * 1 : csp = Current client state (buffers, headers, etc...)
871 *********************************************************************/
872 static void serve(struct client_state *csp)
875 close_socket(csp->cfd);
879 close_socket(csp->sfd);
889 /*********************************************************************
891 * Function : server_thread
893 * Description : We only exist to call `serve' in a threaded environment.
896 * 1 : data = Current client state (buffers, headers, etc...)
898 * Returns : Always 0.
900 *********************************************************************/
901 static int32 server_thread(void *data)
903 serve((struct client_state *) data);
911 /*********************************************************************
915 * Description : Load the config file and start the listen loop.
916 * This function is a lot more *sane* with the `load_config'
917 * and `listen_loop' functions; although it stills does
918 * a *little* too much for my taste.
921 * 1 : argc = Number of parameters (including $0).
922 * 2 : argv = Array of (char *)'s to the parameters.
924 * Returns : 1 if : can't open config file, unrecognized directive,
925 * stats requested in multi-thread mode, can't open the
926 * log file, can't open the jar file, listen port is invalid,
927 * any load fails, and can't bind port.
929 * Else main never returns, the process must be signaled
930 * to terminate execution. Or, on Windows, use the
931 * "File", "Exit" menu option.
933 *********************************************************************/
935 int _main(int argc, const char *argv[])
937 int main(int argc, const char *argv[])
948 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
949 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
951 printf("JunkBuster proxy version " VERSION ".\n\n"
952 "Usage: %s [configfile]\n\n"
953 "See " HOME_PAGE_URL " for details.\n"
954 "This program is distributed under the GNU GPL, version 2 or later.\n",
958 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
960 printf(VERSION "\n");
963 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
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_ERROR, "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
1053 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1054 while (waitpid(-1, NULL, WNOHANG) > 0)
1056 /* zombie children */
1058 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1061 if ( NULL == (csp = (struct client_state *) malloc(sizeof(*csp))) )
1063 log_error(LOG_LEVEL_ERROR, "malloc(%d) for csp failed: %E", sizeof(*csp));
1067 memset(csp, '\0', sizeof(*csp));
1072 if ( config_changed )
1075 * Since we were listening to the "old port", we will not see
1076 * a "listen" param change until the next IJB request. So, at
1077 * least 1 more request must be made for us to find the new
1078 * setting. I am simply closing the old socket and binding the
1081 * Which-ever is correct, we will serve 1 more page via the
1082 * old settings. This should probably be a "show-proxy-args"
1083 * request. This should not be a so common of an operation
1084 * that this will hurt people's feelings.
1088 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1089 haddr ? haddr : "INADDR_ANY", hport);
1090 bfd = bind_port(haddr, hport);
1095 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1097 if (!accept_connection(csp, bfd))
1099 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1111 log_error(LOG_LEVEL_CONNECT, "OK");
1115 /* by haroon - most of credit to srt19170 */
1116 csp->toggled_on = g_bToggleIJB;
1119 /* add it to the list of clients */
1120 csp->next = clients->next;
1121 clients->next = csp;
1123 if (run_loader(csp))
1125 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
\r
1126 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1133 /* this is a switch () statment in the C preprocessor - ugh */
1134 #undef SELECTED_ONE_OPTION
1136 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1137 #define SELECTED_ONE_OPTION
1138 child_id = _beginthread(
1144 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1145 #define SELECTED_ONE_OPTION
1147 thread_id tid = spawn_thread
1148 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1150 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1152 child_id = (int) tid;
1161 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1162 #define SELECTED_ONE_OPTION
1163 csp->cfd = ReleaseSocket(csp->cfd, -1);
1164 if((child_id = (int)CreateNewProcTags(
1165 NP_Entry, (ULONG)server_thread,
1166 NP_Output, Output(),
1167 NP_CloseOutput, FALSE,
1168 NP_Name, (ULONG)"junkbuster child",
1169 NP_StackSize, 20*1024,
1173 ((struct Task *)child_id)->tc_UserData = csp;
1174 Signal((struct Task *)child_id, SIGF_SINGLE);
1179 #if !defined(SELECTED_ONE_OPTION)
1183 #undef SELECTED_ONE_OPTION
1184 /* end of cpp switch () */
1186 if (child_id < 0) /* failed */
1190 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1192 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1194 write_socket(csp->cfd, buf, strlen(buf));
1195 close_socket(csp->cfd);
1201 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1202 /* This block is only needed when using fork().
1203 * When using threads, the server thread was
1204 * created and run by the call to _beginthread().
1206 if (child_id == 0) /* child */
1214 /* in a fork()'d environment, the parent's
1215 * copy of the client socket and the CSP
1219 #if !defined(_WIN32) && defined(__CYGWIN__)
1221 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1222 close_socket(csp->cfd);
1225 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */