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>
41 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
44 int msock; /* master listening socket */
45 extern void *context_loop(int);
46 extern void *housekeeping_loop(void);
47 extern pthread_mutex_t MasterCritter;
49 const char *defaulthost = DEFAULT_HOST;
50 const char *defaultport = DEFAULT_PORT;
53 * This is a generic function to set up a master socket for listening on
54 * a TCP port. The server shuts down if the bind fails.
56 int ig_tcp_server(int port_number, int queue_len)
58 struct sockaddr_in sin;
61 memset(&sin, 0, sizeof(sin));
62 sin.sin_family = AF_INET;
63 sin.sin_addr.s_addr = INADDR_ANY;
65 if (port_number == 0) {
66 printf("webcit: Cannot start: no port number specified.\n");
69 sin.sin_port = htons((u_short) port_number);
71 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
73 printf("webcit: Can't create a socket: %s\n",
77 /* Set some socket options that make sense. */
79 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
81 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
82 printf("webcit: Can't bind: %s\n", strerror(errno));
85 if (listen(s, queue_len) < 0) {
86 printf("webcit: Can't listen: %s\n", strerror(errno));
94 * client_write() ... Send binary data to the client.
96 void client_write(int sock, char *buf, int nbytes)
98 int bytes_written = 0;
100 while (bytes_written < nbytes) {
101 retval = write(sock, &buf[bytes_written],
102 nbytes - bytes_written);
104 printf("client_write() failed: %s\n",
108 bytes_written = bytes_written + retval;
114 * cprintf() ... Send formatted printable data to the client.
116 void cprintf(int sock, const char *format,...)
121 va_start(arg_ptr, format);
122 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
123 buf[sizeof buf - 2] = '\n';
124 client_write(sock, buf, strlen(buf));
130 * Read data from the client socket.
132 * 1 Requested number of bytes has been read.
133 * 0 Request timed out.
134 * If the socket breaks, the session is immediately terminated.
136 int client_read_to(int sock, char *buf, int bytes, int timeout)
144 while (len < bytes) {
150 retval = select((sock) + 1,
151 &rfds, NULL, NULL, &tv);
152 if (FD_ISSET(sock, &rfds) == 0) {
155 rlen = read(sock, &buf[len], bytes - len);
157 printf("client_read() failed: %s\n",
167 * Read data from the client socket with default timeout.
168 * (This is implemented in terms of client_read_to() and could be
169 * justifiably moved out of sysdep.c)
171 int client_read(int sock, char *buf, int bytes)
173 return (client_read_to(sock, buf, bytes, SLEEPING));
178 * client_gets() ... Get a LF-terminated line of text from the client.
179 * (This is implemented in terms of client_read() and could be
180 * justifiably moved out of sysdep.c)
182 int client_gets(int sock, char *buf)
186 /* Read one character at a time.
189 retval = client_read(sock, &buf[i], 1);
190 if (retval != 1 || buf[i] == '\n' || i == 255)
194 /* If we got a long line, discard characters until the newline.
197 while (buf[i] != '\n' && retval == 1)
198 retval = client_read(sock, &buf[i], 1);
201 * Strip any trailing not-printable characters.
204 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
205 buf[strlen(buf) - 1] = 0;
212 * Start running as a daemon. Only close stdio if do_close_stdio is set.
214 void start_daemon(int do_close_stdio)
216 if (do_close_stdio) {
221 signal(SIGHUP, SIG_IGN);
222 signal(SIGINT, SIG_IGN);
223 signal(SIGQUIT, SIG_IGN);
229 * Here's where it all begins.
231 int main(int argc, char **argv)
233 struct sockaddr_in fsin; /* Data for master socket */
234 int alen; /* Data for master socket */
235 int ssock; /* Descriptor for master socket */
236 pthread_t SessThread; /* Thread descriptor */
237 pthread_attr_t attr; /* Thread attributes */
238 int a, i; /* General-purpose variables */
239 int port = PORT_NUM; /* Port to listen on */
240 char tracefile[PATH_MAX];
242 /* Parse command line */
243 while ((a = getopt(argc, argv, "hp:t:")) != EOF)
249 strcpy(tracefile, optarg);
250 freopen(tracefile, "w", stdout);
251 freopen(tracefile, "w", stderr);
252 freopen(tracefile, "r", stdin);
255 fprintf(stderr, "usage: webserver [-p localport] "
257 "[remotehost [remoteport]]\n");
262 defaulthost = argv[optind];
264 defaultport = argv[optind];
266 /* Tell 'em who's in da house */
267 printf("WebCit version 2.01\n");
268 printf("Copyright (C) 1996-1999. All rights reserved.\n\n");
270 if (chdir(WEBCITDIR) != 0)
274 * Bind the server to our favorite port.
275 * There is no need to check for errors, because ig_tcp_server()
276 * exits if it doesn't succeed.
278 printf("Attempting to bind to port %d...\n", port);
279 msock = ig_tcp_server(port, 5);
280 printf("Listening on socket %d\n", msock);
281 signal(SIGPIPE, SIG_IGN);
283 pthread_mutex_init(&MasterCritter, NULL);
288 * Start up the housekeeping thread
290 pthread_attr_init(&attr);
291 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
292 pthread_create(&SessThread, &attr,
293 (void *(*)(void *)) housekeeping_loop, NULL);
298 * Endless loop. Listen on the master socket. When a connection
299 * comes in, create a socket, a context, and a thread.
302 ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
303 printf("New connection on socket %d\n", ssock);
305 printf("webcit: accept() failed: %s\n",
308 /* Set the SO_REUSEADDR socket option */
310 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
313 /* set attributes for the new thread */
314 pthread_attr_init(&attr);
315 pthread_attr_setdetachstate(&attr,
316 PTHREAD_CREATE_DETACHED);
318 /* now create the thread */
319 if (pthread_create(&SessThread, &attr,
320 (void *(*)(void *)) context_loop,
323 printf("webcit: can't create thread: %s\n",