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) {
161 client_write_ssl((char *)buf, count);
165 /* write(2, buf, count); FIXME */
166 return(write(WC->http_sock, buf, count));
171 * Read data from the client socket with default timeout.
172 * (This is implemented in terms of client_read_to() and could be
173 * justifiably moved out of sysdep.c)
175 int client_read(int sock, char *buf, int bytes)
177 return (client_read_to(sock, buf, bytes, SLEEPING));
182 * client_gets() ... Get a LF-terminated line of text from the client.
183 * (This is implemented in terms of client_read() and could be
184 * justifiably moved out of sysdep.c)
186 int client_gets(int sock, char *buf)
190 /* Read one character at a time.
193 retval = client_read(sock, &buf[i], 1);
194 if (retval != 1 || buf[i] == '\n' || i == 255)
198 /* If we got a long line, discard characters until the newline.
201 while (buf[i] != '\n' && retval == 1)
202 retval = client_read(sock, &buf[i], 1);
205 * Strip any trailing non-printable characters.
208 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
209 buf[strlen(buf) - 1] = 0;
216 * Start running as a daemon. Only close stdio if do_close_stdio is set.
218 void start_daemon(int do_close_stdio)
220 if (do_close_stdio) {
225 signal(SIGHUP, SIG_IGN);
226 signal(SIGINT, SIG_IGN);
227 signal(SIGQUIT, SIG_IGN);
232 void spawn_another_worker_thread() {
233 pthread_t SessThread; /* Thread descriptor */
234 pthread_attr_t attr; /* Thread attributes */
237 lprintf(3, "Creating a new thread\n");
239 /* set attributes for the new thread */
240 pthread_attr_init(&attr);
241 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
243 /* Our per-thread stacks need to be bigger than the default size, otherwise
244 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
247 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
248 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
249 pthread_attr_destroy(&attr);
252 /* now create the thread */
253 if (pthread_create(&SessThread, &attr,
254 (void *(*)(void *)) worker_entry, NULL)
256 lprintf(1, "Can't create thread: %s\n",
260 /* free up the attributes */
261 pthread_attr_destroy(&attr);
265 * Here's where it all begins.
267 int main(int argc, char **argv)
269 pthread_t SessThread; /* Thread descriptor */
270 pthread_attr_t attr; /* Thread attributes */
271 int a, i; /* General-purpose variables */
272 int port = PORT_NUM; /* Port to listen on */
273 char tracefile[PATH_MAX];
276 /* Parse command line */
278 while ((a = getopt(argc, argv, "hi:p:t:cs")) != EOF)
280 while ((a = getopt(argc, argv, "hi:p:t:c")) != EOF)
284 strcpy(ip_addr, optarg);
290 strcpy(tracefile, optarg);
291 freopen(tracefile, "w", stdout);
292 freopen(tracefile, "w", stderr);
293 freopen(tracefile, "r", stdin);
296 verbosity = atoi(optarg);
299 server_cookie = malloc(SIZ);
300 if (server_cookie != NULL) {
301 strcpy(server_cookie, "Set-cookie: wcserver=");
303 &server_cookie[strlen(server_cookie)],
305 lprintf(2, "gethostname: %s\n",
315 fprintf(stderr, "usage: webserver "
316 "[-i ip_addr] [-p http_port] "
317 "[-t tracefile] [-c] "
321 "[remotehost [remoteport]]\n");
326 ctdlhost = argv[optind];
328 ctdlport = argv[optind];
330 /* Tell 'em who's in da house */
331 lprintf(1, SERVER "\n"
332 "Copyright (C) 1996-2005 by the Citadel/UX development team.\n"
333 "This software is distributed under the terms of the GNU General Public\n"
334 "License. If you paid for this software, someone is ripping you off.\n\n");
336 if (chdir(WEBCITDIR) != 0)
340 * Set up a place to put thread-specific data.
341 * We only need a single pointer per thread - it points to the
342 * wcsession struct to which the thread is currently bound.
344 if (pthread_key_create(&MyConKey, NULL) != 0) {
345 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
349 * Set up a place to put thread-specific SSL data.
350 * We don't stick this in the wcsession struct because SSL starts
351 * up before the session is bound, and it gets torn down between
355 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
356 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
361 * Bind the server to our favorite port.
362 * There is no need to check for errors, because ig_tcp_server()
363 * exits if it doesn't succeed.
365 lprintf(2, "Attempting to bind to port %d...\n", port);
366 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
367 lprintf(2, "Listening on socket %d\n", msock);
368 signal(SIGPIPE, SIG_IGN);
370 pthread_mutex_init(&SessionListMutex, NULL);
373 * Start up the housekeeping thread
375 pthread_attr_init(&attr);
376 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
377 pthread_create(&SessThread, &attr,
378 (void *(*)(void *)) housekeeping_loop, NULL);
382 * If this is an HTTPS server, fire up SSL
390 /* Start a few initial worker threads */
391 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
392 spawn_another_worker_thread();
395 /* now the original thread becomes another worker */
402 * Entry point for worker threads
404 void worker_entry(void) {
408 int fail_this_transaction = 0;
411 /* Only one thread can accept at a time */
412 fail_this_transaction = 0;
413 ssock = accept(msock, NULL, 0);
415 lprintf(2, "accept() failed: %s\n", strerror(errno));
417 /* Set the SO_REUSEADDR socket option */
419 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
422 /* If we are an HTTPS server, go crypto now. */
425 if (starttls(ssock) != 0) {
426 fail_this_transaction = 1;
432 if (fail_this_transaction == 0) {
433 /* Perform an HTTP transaction... */
435 /* ...and close the socket. */
436 lingering_close(ssock);
441 } while (!time_to_die);
447 int lprintf(int loglevel, const char *format, ...)
451 if (loglevel <= verbosity) {
452 va_start(ap, format);
453 vfprintf(stderr, format, ap);