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");
70 sin.sin_port = htons((u_short)port_number);
72 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
74 printf("webcit: Can't create a socket: %s\n",
79 /* Set some socket options that make sense. */
81 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
83 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
84 printf("webcit: Can't bind: %s\n", strerror(errno));
88 if (listen(s, queue_len) < 0) {
89 printf("webcit: Can't listen: %s\n", strerror(errno));
98 * client_write() ... Send binary data to the client.
100 void client_write(int sock, char *buf, int nbytes)
102 int bytes_written = 0;
104 while (bytes_written < nbytes) {
105 retval = write(sock, &buf[bytes_written],
106 nbytes - bytes_written);
108 printf("client_write() failed: %s\n",
112 bytes_written = bytes_written + retval;
118 * cprintf() ... Send formatted printable data to the client.
120 void cprintf(int sock, const char *format, ...) {
124 va_start(arg_ptr, format);
125 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
126 buf[sizeof buf - 2] = '\n';
127 client_write(sock, buf, strlen(buf));
133 * Read data from the client socket.
135 * 1 Requested number of bytes has been read.
136 * 0 Request timed out.
137 * If the socket breaks, the session is immediately terminated.
139 int client_read_to(int sock, char *buf, int bytes, int timeout)
153 retval = select( (sock)+1,
154 &rfds, NULL, NULL, &tv);
155 if (FD_ISSET(sock, &rfds) == 0) {
159 rlen = read(sock, &buf[len], bytes-len);
161 printf("client_read() failed: %s\n",
171 * Read data from the client socket with default timeout.
172 * (This is implemented in terms of client_read_to() and could be
173 * justifiably moved out of sysdep.c)
175 int client_read(int sock, char *buf, int bytes)
177 return(client_read_to(sock, buf, bytes, SLEEPING));
182 * client_gets() ... Get a LF-terminated line of text from the client.
183 * (This is implemented in terms of client_read() and could be
184 * justifiably moved out of sysdep.c)
186 int client_gets(int sock, char *buf)
190 /* Read one character at a time.
193 retval = client_read(sock, &buf[i], 1);
194 if (retval != 1 || buf[i] == '\n' || i == 255)
198 /* If we got a long line, discard characters until the newline.
201 while (buf[i] != '\n' && retval == 1)
202 retval = client_read(sock, &buf[i], 1);
205 * Strip any trailing not-printable characters.
208 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
209 buf[strlen(buf)-1] = 0;
216 * Start running as a daemon. Only close stdio if do_close_stdio is set.
218 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);
227 if (fork()!=0) exit(0);
231 * Here's where it all begins.
233 int main(int argc, char **argv)
235 struct sockaddr_in fsin; /* Data for master socket */
236 int alen; /* Data for master socket */
237 int ssock; /* Descriptor for master socket */
238 pthread_t SessThread; /* Thread descriptor */
239 pthread_attr_t attr; /* Thread attributes */
240 int a, i; /* General-purpose variables */
241 int port = PORT_NUM; /* Port to listen on */
243 /* Parse command line */
244 while ((a = getopt(argc, argv, "hp:")) != EOF)
250 fprintf(stderr, "usage: webserver [-p localport] "
251 "[remotehost [remoteport]]\n");
256 defaulthost = argv[optind];
258 defaultport = argv[optind];
261 /* Tell 'em who's in da house */
262 printf("WebCit v2 experimental\n");
263 printf("Copyright (C) 1996-1998 by Art Cancro. ");
264 printf("All rights reserved.\n\n");
266 if (chdir(WEBCITDIR)!=0) perror("chdir");
269 * Bind the server to our favourite port.
270 * There is no need to check for errors, because ig_tcp_server()
271 * exits if it doesn't succeed.
273 printf("Attempting to bind to port %d...\n", port);
274 msock = ig_tcp_server(port, 5);
275 printf("Listening on socket %d\n", msock);
276 signal(SIGPIPE, SIG_IGN);
278 pthread_mutex_init(&MasterCritter, NULL);
283 * Start up the housekeeping thread
285 pthread_attr_init(&attr);
286 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
287 pthread_create(&SessThread, &attr,
288 (void* (*)(void*)) housekeeping_loop, NULL);
293 * Endless loop. Listen on the master socket. When a connection
294 * comes in, create a socket, a context, and a thread.
297 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
298 printf("New connection on socket %d\n", ssock);
300 printf("webcit: accept() failed: %s\n",
304 /* Set the SO_REUSEADDR socket option */
306 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
309 /* set attributes for the new thread */
310 pthread_attr_init(&attr);
311 pthread_attr_setdetachstate(&attr,
312 PTHREAD_CREATE_DETACHED);
314 /* now create the thread */
315 if (pthread_create(&SessThread, &attr,
316 (void* (*)(void*)) context_loop,
319 printf("webcit: can't create thread: %s\n",