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;
50 * This is a generic function to set up a master socket for listening on
51 * a TCP port. The server shuts down if the bind fails.
53 int ig_tcp_server(int port_number, int queue_len)
55 struct sockaddr_in sin;
58 memset(&sin, 0, sizeof(sin));
59 sin.sin_family = AF_INET;
60 sin.sin_addr.s_addr = INADDR_ANY;
62 if (port_number == 0) {
63 printf("webcit: Cannot start: no port number specified.\n");
67 sin.sin_port = htons((u_short)port_number);
69 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
71 printf("webcit: Can't create a socket: %s\n",
76 /* Set some socket options that make sense. */
78 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
80 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
81 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));
95 * client_write() ... Send binary data to the client.
97 void client_write(int sock, char *buf, int nbytes)
99 int bytes_written = 0;
101 while (bytes_written < nbytes) {
102 retval = write(sock, &buf[bytes_written],
103 nbytes - bytes_written);
105 printf("client_write() failed: %s\n",
109 bytes_written = bytes_written + retval;
115 * cprintf() ... Send formatted printable data to the client. It is
116 * implemented in terms of client_write() but remains in
117 * sysdep.c in case we port to somewhere without va_args...
119 void cprintf(int sock, const char *format, ...) {
123 va_start(arg_ptr, format);
124 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
125 buf[sizeof buf - 2] = '\n';
126 client_write(sock, buf, strlen(buf));
132 * Read data from the client socket.
134 * 1 Requested number of bytes has been read.
135 * 0 Request timed out.
136 * If the socket breaks, the session is immediately terminated.
138 int client_read_to(int sock, char *buf, int bytes, int timeout)
152 retval = select( (sock)+1,
153 &rfds, NULL, NULL, &tv);
154 if (FD_ISSET(sock, &rfds) == 0) {
158 rlen = read(sock, &buf[len], bytes-len);
160 printf("client_read() failed: %s\n",
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 not-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) {
218 if (do_close_stdio) {
223 signal(SIGHUP,SIG_IGN);
224 signal(SIGINT,SIG_IGN);
225 signal(SIGQUIT,SIG_IGN);
226 if (fork()!=0) exit(0);
230 * Issue an HTTP Redirect header
231 * url must be a complete url (with http://)
233 void redirect(char *url) {
234 printf("Location: %s\n\n", url);
237 const char *defaulthost = DEFAULT_HOST;
238 const char *defaultport = DEFAULT_PORT;
241 * Here's where it all begins.
243 int main(int argc, char **argv)
245 struct sockaddr_in fsin; /* Data for master socket */
246 int alen; /* Data for master socket */
247 int ssock; /* Descriptor for master socket */
248 pthread_t SessThread; /* Thread descriptor */
249 pthread_attr_t attr; /* Thread attributes */
250 int a, i; /* General-purpose variables */
251 int port = PORT_NUM; /* Port to listen on */
253 /* Parse command line */
254 while ((a = getopt(argc, argv, "hp:")) != EOF)
260 fprintf(stderr, "usage: webserver [-p localport] "
261 "[remotehost [remoteport]]\n");
266 defaulthost = argv[optind];
268 defaultport = argv[optind];
271 /* Tell 'em who's in da house */
272 printf("WebCit v2 experimental\n");
273 printf("Copyright (C) 1996-1998 by Art Cancro. ");
274 printf("All rights reserved.\n\n");
277 * Bind the server to our favourite port.
278 * There is no need to check for errors, because ig_tcp_server()
279 * exits if it doesn't succeed.
281 printf("Attempting to bind to port %d...\n", port);
282 msock = ig_tcp_server(port, 5);
283 printf("Listening on socket %d\n", msock);
284 signal(SIGPIPE, SIG_IGN);
286 pthread_mutex_init(&MasterCritter, NULL);
291 * Start up the housekeeping thread
293 pthread_attr_init(&attr);
294 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
295 pthread_create(&SessThread, &attr,
296 (void* (*)(void*)) housekeeping_loop, NULL);
301 * Endless loop. Listen on the master socket. When a connection
302 * comes in, create a socket, a context, and a thread.
305 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
306 printf("New connection on socket %d\n", ssock);
308 printf("webcit: accept() failed: %s\n",
312 /* Set the SO_REUSEADDR socket option */
314 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
317 /* set attributes for the new thread */
318 pthread_attr_init(&attr);
319 pthread_attr_setdetachstate(&attr,
320 PTHREAD_CREATE_DETACHED);
322 /* now create the thread */
323 if (pthread_create(&SessThread, &attr,
324 (void* (*)(void*)) context_loop,
327 printf("webcit: can't create thread: %s\n",