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",
158 ssize_t client_write(const void *buf, size_t count) {
161 client_write_ssl((char *)buf, count);
165 return(write(WC->http_sock, buf, count));
170 * Read data from the client socket with default timeout.
171 * (This is implemented in terms of client_read_to() and could be
172 * justifiably moved out of sysdep.c)
174 int client_read(int sock, char *buf, int bytes)
176 return (client_read_to(sock, buf, bytes, SLEEPING));
181 * client_gets() ... Get a LF-terminated line of text from the client.
182 * (This is implemented in terms of client_read() and could be
183 * justifiably moved out of sysdep.c)
185 int client_gets(int sock, char *buf)
189 /* Read one character at a time.
192 retval = client_read(sock, &buf[i], 1);
193 if (retval != 1 || buf[i] == '\n' || i == 255)
197 /* If we got a long line, discard characters until the newline.
200 while (buf[i] != '\n' && retval == 1)
201 retval = client_read(sock, &buf[i], 1);
204 * Strip any trailing non-printable characters.
207 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
208 buf[strlen(buf) - 1] = 0;
215 * Start running as a daemon. Only close stdio if do_close_stdio is set.
217 void start_daemon(int do_close_stdio)
219 if (do_close_stdio) {
224 signal(SIGHUP, SIG_IGN);
225 signal(SIGINT, SIG_IGN);
226 signal(SIGQUIT, SIG_IGN);
231 void spawn_another_worker_thread() {
232 pthread_t SessThread; /* Thread descriptor */
233 pthread_attr_t attr; /* Thread attributes */
235 lprintf(3, "Creating a new thread\n");
237 /* set attributes for the new thread */
238 pthread_attr_init(&attr);
239 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
241 /* now create the thread */
242 if (pthread_create(&SessThread, &attr,
243 (void *(*)(void *)) worker_entry, NULL)
245 lprintf(1, "Can't create thread: %s\n",
251 * Here's where it all begins.
253 int main(int argc, char **argv)
255 pthread_t SessThread; /* Thread descriptor */
256 pthread_attr_t attr; /* Thread attributes */
257 int a, i; /* General-purpose variables */
258 int port = PORT_NUM; /* Port to listen on */
259 char tracefile[PATH_MAX];
262 /* Parse command line */
264 while ((a = getopt(argc, argv, "hi:p:t:cs")) != EOF)
266 while ((a = getopt(argc, argv, "hi:p:t:c")) != EOF)
270 strcpy(ip_addr, optarg);
276 strcpy(tracefile, optarg);
277 freopen(tracefile, "w", stdout);
278 freopen(tracefile, "w", stderr);
279 freopen(tracefile, "r", stdin);
282 verbosity = atoi(optarg);
285 server_cookie = malloc(SIZ);
286 if (server_cookie != NULL) {
287 strcpy(server_cookie, "Set-cookie: wcserver=");
289 &server_cookie[strlen(server_cookie)],
291 lprintf(2, "gethostname: %s\n",
301 fprintf(stderr, "usage: webserver "
302 "[-i ip_addr] [-p http_port] "
303 "[-t tracefile] [-c] "
307 "[remotehost [remoteport]]\n");
312 ctdlhost = argv[optind];
314 ctdlport = argv[optind];
316 /* Tell 'em who's in da house */
317 lprintf(1, SERVER "\n"
318 "Copyright (C) 1996-2004 by the Citadel/UX development team.\n"
319 "This software is distributed under the terms of the GNU General Public\n"
320 "License. If you paid for this software, someone is ripping you off.\n\n");
322 if (chdir(WEBCITDIR) != 0)
326 * Set up a place to put thread-specific data.
327 * We only need a single pointer per thread - it points to the
328 * wcsession struct to which the thread is currently bound.
330 if (pthread_key_create(&MyConKey, NULL) != 0) {
331 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
335 * Set up a place to put thread-specific SSL data.
336 * We don't stick this in the wcsession struct because SSL starts
337 * up before the session is bound, and it gets torn down between
341 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
342 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
347 * Bind the server to our favorite port.
348 * There is no need to check for errors, because ig_tcp_server()
349 * exits if it doesn't succeed.
351 lprintf(2, "Attempting to bind to port %d...\n", port);
352 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
353 lprintf(2, "Listening on socket %d\n", msock);
354 signal(SIGPIPE, SIG_IGN);
356 pthread_mutex_init(&SessionListMutex, NULL);
359 * Start up the housekeeping thread
361 pthread_attr_init(&attr);
362 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
363 pthread_create(&SessThread, &attr,
364 (void *(*)(void *)) housekeeping_loop, NULL);
368 * If this is an HTTPS server, fire up SSL
376 /* Start a few initial worker threads */
377 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
378 spawn_another_worker_thread();
381 /* now the original thread becomes another worker */
388 * Entry point for worker threads
390 void worker_entry(void) {
394 int fail_this_transaction = 0;
397 /* Only one thread can accept at a time */
398 fail_this_transaction = 0;
399 ssock = accept(msock, NULL, 0);
401 lprintf(2, "accept() failed: %s\n", strerror(errno));
403 /* Set the SO_REUSEADDR socket option */
405 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
408 /* If we are an HTTPS server, go crypto now. */
411 if (starttls(ssock) != 0) {
412 fail_this_transaction = 1;
418 if (fail_this_transaction == 0) {
419 /* Perform an HTTP transaction... */
421 /* ...and close the socket. */
422 lingering_close(ssock);
427 } while (!time_to_die);
433 int lprintf(int loglevel, const char *format, ...)
438 va_start(ap, format);
439 vsprintf(buf, format, ap);
442 if (loglevel <= verbosity) {
446 gettimeofday(&tv, NULL);
447 tim = localtime((time_t *)&(tv.tv_sec));
449 if (WC && WC->wc_session) {
451 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld:%d] %s",
452 tim->tm_year + 1900, tim->tm_mon + 1,
453 tim->tm_mday, tim->tm_hour, tim->tm_min,
454 tim->tm_sec, (long)tv.tv_usec / 1000,
455 (long)pthread_self(),
456 WC->wc_session, buf);
459 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld] %s",
460 tim->tm_year + 1900, tim->tm_mon + 1,
461 tim->tm_mday, tim->tm_hour, tim->tm_min,
462 tim->tm_sec, (long)tv.tv_usec / 1000,
463 (long)pthread_self(),