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.
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #ifdef HAVE_SYS_TIME_H
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
40 #include "webserver.h"
43 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
46 int verbosity = 9; /* Logging level */
47 int msock; /* master listening socket */
48 int is_https = 0; /* Nonzero if I am an HTTPS service */
49 extern void *context_loop(int);
50 extern void *housekeeping_loop(void);
51 extern pthread_mutex_t SessionListMutex;
52 extern pthread_key_t MyConKey;
55 char *server_cookie = NULL;
58 char *ctdlhost = DEFAULT_HOST;
59 char *ctdlport = DEFAULT_PORT;
62 * This is a generic function to set up a master socket for listening on
63 * a TCP port. The server shuts down if the bind fails.
65 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
67 struct sockaddr_in sin;
70 memset(&sin, 0, sizeof(sin));
71 sin.sin_family = AF_INET;
72 if (ip_addr == NULL) {
73 sin.sin_addr.s_addr = INADDR_ANY;
76 sin.sin_addr.s_addr = inet_addr(ip_addr);
79 if (sin.sin_addr.s_addr == INADDR_NONE) {
80 sin.sin_addr.s_addr = INADDR_ANY;
83 if (port_number == 0) {
84 lprintf(1, "Cannot start: no port number specified.\n");
87 sin.sin_port = htons((u_short) port_number);
89 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
91 lprintf(1, "Can't create a socket: %s\n",
95 /* Set some socket options that make sense. */
97 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
99 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
100 lprintf(1, "Can't bind: %s\n", strerror(errno));
103 if (listen(s, queue_len) < 0) {
104 lprintf(1, "Can't listen: %s\n", strerror(errno));
112 * Read data from the client socket.
114 * 1 Requested number of bytes has been read.
115 * 0 Request timed out.
116 * -1 Connection is broken, or other error.
118 int client_read_to(int sock, char *buf, int bytes, int timeout)
128 return(client_read_ssl(buf, bytes, timeout));
133 while (len < bytes) {
139 retval = select((sock) + 1,
140 &rfds, NULL, NULL, &tv);
141 if (FD_ISSET(sock, &rfds) == 0) {
145 rlen = read(sock, &buf[len], bytes - len);
148 lprintf(2, "client_read() failed: %s\n",
154 /*write(2, buf, bytes); FIXME */
159 ssize_t client_write(const void *buf, size_t count) {
162 client_write_ssl((char *)buf, count);
166 /* write(2, buf, count); FIXME */
167 return(write(WC->http_sock, buf, count));
172 * Read data from the client socket with default timeout.
173 * (This is implemented in terms of client_read_to() and could be
174 * justifiably moved out of sysdep.c)
176 int client_read(int sock, char *buf, int bytes)
178 return (client_read_to(sock, buf, bytes, SLEEPING));
183 * client_gets() ... Get a LF-terminated line of text from the client.
184 * (This is implemented in terms of client_read() and could be
185 * justifiably moved out of sysdep.c)
187 int client_gets(int sock, char *buf)
191 /* Read one character at a time.
194 retval = client_read(sock, &buf[i], 1);
195 if (retval != 1 || buf[i] == '\n' || i == 255)
199 /* If we got a long line, discard characters until the newline.
202 while (buf[i] != '\n' && retval == 1)
203 retval = client_read(sock, &buf[i], 1);
206 * Strip any trailing non-printable characters.
209 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
210 buf[strlen(buf) - 1] = 0;
217 * Start running as a daemon. Only close stdio if do_close_stdio is set.
219 void start_daemon(int do_close_stdio)
221 if (do_close_stdio) {
226 signal(SIGHUP, SIG_IGN);
227 signal(SIGINT, SIG_IGN);
228 signal(SIGQUIT, SIG_IGN);
233 void spawn_another_worker_thread() {
234 pthread_t SessThread; /* Thread descriptor */
235 pthread_attr_t attr; /* Thread attributes */
238 lprintf(3, "Creating a new thread\n");
240 /* set attributes for the new thread */
241 pthread_attr_init(&attr);
242 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
244 /* Our per-thread stacks need to be bigger than the default size, otherwise
245 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
248 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
249 lprintf(1, "pthread_attr_setstacksize: %s\n", strerror(ret));
250 pthread_attr_destroy(&attr);
253 /* now create the thread */
254 if (pthread_create(&SessThread, &attr,
255 (void *(*)(void *)) worker_entry, NULL)
257 lprintf(1, "Can't create thread: %s\n",
261 /* free up the attributes */
262 pthread_attr_destroy(&attr);
266 * Here's where it all begins.
268 int main(int argc, char **argv)
270 pthread_t SessThread; /* Thread descriptor */
271 pthread_attr_t attr; /* Thread attributes */
272 int a, i; /* General-purpose variables */
273 int port = PORT_NUM; /* Port to listen on */
274 char tracefile[PATH_MAX];
277 /* Parse command line */
279 while ((a = getopt(argc, argv, "hi:p:t:cs")) != EOF)
281 while ((a = getopt(argc, argv, "hi:p:t:c")) != EOF)
285 strcpy(ip_addr, optarg);
291 strcpy(tracefile, optarg);
292 freopen(tracefile, "w", stdout);
293 freopen(tracefile, "w", stderr);
294 freopen(tracefile, "r", stdin);
297 verbosity = atoi(optarg);
300 server_cookie = malloc(SIZ);
301 if (server_cookie != NULL) {
302 strcpy(server_cookie, "Set-cookie: wcserver=");
304 &server_cookie[strlen(server_cookie)],
306 lprintf(2, "gethostname: %s\n",
316 fprintf(stderr, "usage: webserver "
317 "[-i ip_addr] [-p http_port] "
318 "[-t tracefile] [-c] "
322 "[remotehost [remoteport]]\n");
327 ctdlhost = argv[optind];
329 ctdlport = argv[optind];
331 /* Tell 'em who's in da house */
332 lprintf(1, SERVER "\n"
333 "Copyright (C) 1996-2005 by the Citadel/UX development team.\n"
334 "This software is distributed under the terms of the GNU General Public\n"
335 "License. If you paid for this software, someone is ripping you off.\n\n");
337 if (chdir(WEBCITDIR) != 0)
341 * Set up a place to put thread-specific data.
342 * We only need a single pointer per thread - it points to the
343 * wcsession struct to which the thread is currently bound.
345 if (pthread_key_create(&MyConKey, NULL) != 0) {
346 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
350 * Set up a place to put thread-specific SSL data.
351 * We don't stick this in the wcsession struct because SSL starts
352 * up before the session is bound, and it gets torn down between
356 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
357 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
362 * Bind the server to our favorite port.
363 * There is no need to check for errors, because ig_tcp_server()
364 * exits if it doesn't succeed.
366 lprintf(2, "Attempting to bind to port %d...\n", port);
367 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
368 lprintf(2, "Listening on socket %d\n", msock);
369 signal(SIGPIPE, SIG_IGN);
371 pthread_mutex_init(&SessionListMutex, NULL);
374 * Start up the housekeeping thread
376 pthread_attr_init(&attr);
377 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
378 pthread_create(&SessThread, &attr,
379 (void *(*)(void *)) housekeeping_loop, NULL);
383 * If this is an HTTPS server, fire up SSL
391 /* Start a few initial worker threads */
392 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
393 spawn_another_worker_thread();
396 /* now the original thread becomes another worker */
403 * Entry point for worker threads
405 void worker_entry(void) {
409 int fail_this_transaction = 0;
412 /* Only one thread can accept at a time */
413 fail_this_transaction = 0;
414 ssock = accept(msock, NULL, 0);
416 lprintf(2, "accept() failed: %s\n", strerror(errno));
418 /* Set the SO_REUSEADDR socket option */
420 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
423 /* If we are an HTTPS server, go crypto now. */
426 if (starttls(ssock) != 0) {
427 fail_this_transaction = 1;
433 if (fail_this_transaction == 0) {
434 /* Perform an HTTP transaction... */
436 /* ...and close the socket. */
437 lingering_close(ssock);
442 } while (!time_to_die);
448 int lprintf(int loglevel, const char *format, ...)
452 if (loglevel <= verbosity) {
453 va_start(ap, format);
454 vfprintf(stderr, format, ap);