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",
249 /* free up the attributes */
250 pthread_attr_destroy(&attr);
254 * Here's where it all begins.
256 int main(int argc, char **argv)
258 pthread_t SessThread; /* Thread descriptor */
259 pthread_attr_t attr; /* Thread attributes */
260 int a, i; /* General-purpose variables */
261 int port = PORT_NUM; /* Port to listen on */
262 char tracefile[PATH_MAX];
265 /* Parse command line */
267 while ((a = getopt(argc, argv, "hi:p:t:cs")) != EOF)
269 while ((a = getopt(argc, argv, "hi:p:t:c")) != EOF)
273 strcpy(ip_addr, optarg);
279 strcpy(tracefile, optarg);
280 freopen(tracefile, "w", stdout);
281 freopen(tracefile, "w", stderr);
282 freopen(tracefile, "r", stdin);
285 verbosity = atoi(optarg);
288 server_cookie = malloc(SIZ);
289 if (server_cookie != NULL) {
290 strcpy(server_cookie, "Set-cookie: wcserver=");
292 &server_cookie[strlen(server_cookie)],
294 lprintf(2, "gethostname: %s\n",
304 fprintf(stderr, "usage: webserver "
305 "[-i ip_addr] [-p http_port] "
306 "[-t tracefile] [-c] "
310 "[remotehost [remoteport]]\n");
315 ctdlhost = argv[optind];
317 ctdlport = argv[optind];
319 /* Tell 'em who's in da house */
320 lprintf(1, SERVER "\n"
321 "Copyright (C) 1996-2004 by the Citadel/UX development team.\n"
322 "This software is distributed under the terms of the GNU General Public\n"
323 "License. If you paid for this software, someone is ripping you off.\n\n");
325 if (chdir(WEBCITDIR) != 0)
329 * Set up a place to put thread-specific data.
330 * We only need a single pointer per thread - it points to the
331 * wcsession struct to which the thread is currently bound.
333 if (pthread_key_create(&MyConKey, NULL) != 0) {
334 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
338 * Set up a place to put thread-specific SSL data.
339 * We don't stick this in the wcsession struct because SSL starts
340 * up before the session is bound, and it gets torn down between
344 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
345 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
350 * Bind the server to our favorite port.
351 * There is no need to check for errors, because ig_tcp_server()
352 * exits if it doesn't succeed.
354 lprintf(2, "Attempting to bind to port %d...\n", port);
355 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
356 lprintf(2, "Listening on socket %d\n", msock);
357 signal(SIGPIPE, SIG_IGN);
359 pthread_mutex_init(&SessionListMutex, NULL);
362 * Start up the housekeeping thread
364 pthread_attr_init(&attr);
365 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
366 pthread_create(&SessThread, &attr,
367 (void *(*)(void *)) housekeeping_loop, NULL);
371 * If this is an HTTPS server, fire up SSL
379 /* Start a few initial worker threads */
380 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
381 spawn_another_worker_thread();
384 /* now the original thread becomes another worker */
391 * Entry point for worker threads
393 void worker_entry(void) {
397 int fail_this_transaction = 0;
400 /* Only one thread can accept at a time */
401 fail_this_transaction = 0;
402 ssock = accept(msock, NULL, 0);
404 lprintf(2, "accept() failed: %s\n", strerror(errno));
406 /* Set the SO_REUSEADDR socket option */
408 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
411 /* If we are an HTTPS server, go crypto now. */
414 if (starttls(ssock) != 0) {
415 fail_this_transaction = 1;
421 if (fail_this_transaction == 0) {
422 /* Perform an HTTP transaction... */
424 /* ...and close the socket. */
425 lingering_close(ssock);
430 } while (!time_to_die);
436 int lprintf(int loglevel, const char *format, ...)
441 va_start(ap, format);
442 vsprintf(buf, format, ap);
445 if (loglevel <= verbosity) {
450 gettimeofday(&tv, NULL);
451 tim = localtime((time_t *)&(tv.tv_sec));
453 if (WC) if (WC->wc_session) sess = 1;
456 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld:%d] %s",
457 tim->tm_year + 1900, tim->tm_mon + 1,
458 tim->tm_mday, tim->tm_hour, tim->tm_min,
459 tim->tm_sec, (long)tv.tv_usec / 1000,
460 (long)pthread_self(),
461 WC->wc_session, buf);
464 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld] %s",
465 tim->tm_year + 1900, tim->tm_mon + 1,
466 tim->tm_mday, tim->tm_hour, tim->tm_min,
467 tim->tm_sec, (long)tv.tv_usec / 1000,
468 (long)pthread_self(),