4 * This contains a simple multithreaded TCP server manager. It sits around
5 * waiting on the specified port for incoming HTTP connections. When a
6 * connection is established, it calls context_loop() from context_loop.c.
20 #include <sys/types.h>
22 #include <sys/socket.h>
23 #ifdef HAVE_SYS_TIME_H
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
39 #include "webserver.h"
42 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
45 int verbosity = 9; /* Logging level */
46 int msock; /* master listening socket */
47 int is_https = 0; /* Nonzero if I am an HTTPS service */
48 extern void *context_loop(int);
49 extern void *housekeeping_loop(void);
50 extern pthread_mutex_t SessionListMutex;
51 extern pthread_key_t MyConKey;
54 char *server_cookie = NULL;
57 char *ctdlhost = DEFAULT_HOST;
58 char *ctdlport = DEFAULT_PORT;
61 * This is a generic function to set up a master socket for listening on
62 * a TCP port. The server shuts down if the bind fails.
64 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
66 struct sockaddr_in sin;
69 memset(&sin, 0, sizeof(sin));
70 sin.sin_family = AF_INET;
71 if (ip_addr == NULL) {
72 sin.sin_addr.s_addr = INADDR_ANY;
75 sin.sin_addr.s_addr = inet_addr(ip_addr);
78 if (sin.sin_addr.s_addr == INADDR_NONE) {
79 sin.sin_addr.s_addr = INADDR_ANY;
82 if (port_number == 0) {
83 lprintf(1, "Cannot start: no port number specified.\n");
86 sin.sin_port = htons((u_short) port_number);
88 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
90 lprintf(1, "Can't create a socket: %s\n",
94 /* Set some socket options that make sense. */
96 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
98 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
99 lprintf(1, "Can't bind: %s\n", strerror(errno));
102 if (listen(s, queue_len) < 0) {
103 lprintf(1, "Can't listen: %s\n", strerror(errno));
111 * Read data from the client socket.
113 * 1 Requested number of bytes has been read.
114 * 0 Request timed out.
115 * -1 Connection is broken, or other error.
117 int client_read_to(int sock, char *buf, int bytes, int timeout)
127 return(client_read_ssl(buf, bytes, timeout));
132 while (len < bytes) {
138 retval = select((sock) + 1,
139 &rfds, NULL, NULL, &tv);
140 if (FD_ISSET(sock, &rfds) == 0) {
144 rlen = read(sock, &buf[len], bytes - len);
147 lprintf(2, "client_read() failed: %s\n",
153 /* write(2, buf, bytes); FIXME */
158 ssize_t client_write(const void *buf, size_t count) {
160 if (WC->burst != NULL) {
161 WC->burst = realloc(WC->burst, (WC->burst_len + count + 2));
162 memcpy(&WC->burst[WC->burst_len], buf, count);
163 WC->burst_len += count;
169 client_write_ssl((char *)buf, count);
173 /* write(2, buf, count); FIXME */
174 return(write(WC->http_sock, buf, count));
178 void begin_burst(void) {
179 if (WC->burst != NULL) {
184 WC->burst = malloc(SIZ);
187 void end_burst(void) {
191 the_len = WC->burst_len;
192 the_data = WC->burst;
197 wprintf("Content-length: %d\r\n\r\n", the_len);
198 client_write(the_data, the_len);
205 * Read data from the client socket with default timeout.
206 * (This is implemented in terms of client_read_to() and could be
207 * justifiably moved out of sysdep.c)
209 int client_read(int sock, char *buf, int bytes)
211 return (client_read_to(sock, buf, bytes, SLEEPING));
216 * client_gets() ... Get a LF-terminated line of text from the client.
217 * (This is implemented in terms of client_read() and could be
218 * justifiably moved out of sysdep.c)
220 int client_gets(int sock, char *buf)
224 /* Read one character at a time.
227 retval = client_read(sock, &buf[i], 1);
228 if (retval != 1 || buf[i] == '\n' || i == 255)
232 /* If we got a long line, discard characters until the newline.
235 while (buf[i] != '\n' && retval == 1)
236 retval = client_read(sock, &buf[i], 1);
239 * Strip any trailing non-printable characters.
242 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
243 buf[strlen(buf) - 1] = 0;
250 * Start running as a daemon. Only close stdio if do_close_stdio is set.
252 void start_daemon(int do_close_stdio)
254 if (do_close_stdio) {
259 signal(SIGHUP, SIG_IGN);
260 signal(SIGINT, SIG_IGN);
261 signal(SIGQUIT, SIG_IGN);
266 void spawn_another_worker_thread() {
267 pthread_t SessThread; /* Thread descriptor */
268 pthread_attr_t attr; /* Thread attributes */
271 lprintf(3, "Creating a new thread\n");
273 /* set attributes for the new thread */
274 pthread_attr_init(&attr);
275 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
277 /* Our per-thread stacks need to be bigger than the default size, otherwise
278 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
281 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
282 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
283 pthread_attr_destroy(&attr);
286 /* now create the thread */
287 if (pthread_create(&SessThread, &attr,
288 (void *(*)(void *)) worker_entry, NULL)
290 lprintf(1, "Can't create thread: %s\n",
294 /* free up the attributes */
295 pthread_attr_destroy(&attr);
299 * Here's where it all begins.
301 int main(int argc, char **argv)
303 pthread_t SessThread; /* Thread descriptor */
304 pthread_attr_t attr; /* Thread attributes */
305 int a, i; /* General-purpose variables */
306 int port = PORT_NUM; /* Port to listen on */
307 char tracefile[PATH_MAX];
310 /* Parse command line */
312 while ((a = getopt(argc, argv, "hi:p:t:cs")) != EOF)
314 while ((a = getopt(argc, argv, "hi:p:t:c")) != EOF)
318 strcpy(ip_addr, optarg);
324 strcpy(tracefile, optarg);
325 freopen(tracefile, "w", stdout);
326 freopen(tracefile, "w", stderr);
327 freopen(tracefile, "r", stdin);
330 verbosity = atoi(optarg);
333 server_cookie = malloc(SIZ);
334 if (server_cookie != NULL) {
335 strcpy(server_cookie, "Set-cookie: wcserver=");
337 &server_cookie[strlen(server_cookie)],
339 lprintf(2, "gethostname: %s\n",
349 fprintf(stderr, "usage: webserver "
350 "[-i ip_addr] [-p http_port] "
351 "[-t tracefile] [-c] "
355 "[remotehost [remoteport]]\n");
360 ctdlhost = argv[optind];
362 ctdlport = argv[optind];
364 /* Tell 'em who's in da house */
365 lprintf(1, SERVER "\n"
366 "Copyright (C) 1996-2005 by the Citadel/UX development team.\n"
367 "This software is distributed under the terms of the GNU General Public\n"
368 "License. If you paid for this software, someone is ripping you off.\n\n");
370 if (chdir(WEBCITDIR) != 0)
374 * Set up a place to put thread-specific data.
375 * We only need a single pointer per thread - it points to the
376 * wcsession struct to which the thread is currently bound.
378 if (pthread_key_create(&MyConKey, NULL) != 0) {
379 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
383 * Set up a place to put thread-specific SSL data.
384 * We don't stick this in the wcsession struct because SSL starts
385 * up before the session is bound, and it gets torn down between
389 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
390 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
395 * Bind the server to our favorite port.
396 * There is no need to check for errors, because ig_tcp_server()
397 * exits if it doesn't succeed.
399 lprintf(2, "Attempting to bind to port %d...\n", port);
400 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
401 lprintf(2, "Listening on socket %d\n", msock);
402 signal(SIGPIPE, SIG_IGN);
404 pthread_mutex_init(&SessionListMutex, NULL);
407 * Start up the housekeeping thread
409 pthread_attr_init(&attr);
410 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
411 pthread_create(&SessThread, &attr,
412 (void *(*)(void *)) housekeeping_loop, NULL);
416 * If this is an HTTPS server, fire up SSL
424 /* Start a few initial worker threads */
425 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
426 spawn_another_worker_thread();
429 /* now the original thread becomes another worker */
436 * Entry point for worker threads
438 void worker_entry(void) {
442 int fail_this_transaction = 0;
445 /* Only one thread can accept at a time */
446 fail_this_transaction = 0;
447 ssock = accept(msock, NULL, 0);
449 lprintf(2, "accept() failed: %s\n", strerror(errno));
451 /* Set the SO_REUSEADDR socket option */
453 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
456 /* If we are an HTTPS server, go crypto now. */
459 if (starttls(ssock) != 0) {
460 fail_this_transaction = 1;
466 if (fail_this_transaction == 0) {
467 /* Perform an HTTP transaction... */
469 /* ...and close the socket. */
470 lingering_close(ssock);
475 } while (!time_to_die);
481 int lprintf(int loglevel, const char *format, ...)
485 if (loglevel <= verbosity) {
486 va_start(ap, format);
487 vfprintf(stderr, format, ap);