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.
16 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include <netinet/in.h>
30 int msock; /* master listening socket */
31 extern void *context_loop(int);
32 extern pthread_mutex_t MasterCritter;
35 * This is a generic function to set up a master socket for listening on
36 * a TCP port. The server shuts down if the bind fails.
38 int ig_tcp_server(int port_number, int queue_len)
40 struct sockaddr_in sin;
42 struct linger WebLinger = { 1, 9000 };
44 memset(&sin, 0, sizeof(sin));
45 sin.sin_family = AF_INET;
46 sin.sin_addr.s_addr = INADDR_ANY;
48 if (port_number == 0) {
49 printf("webcit: Cannot start: no port number specified.\n");
53 sin.sin_port = htons((u_short)port_number);
55 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
57 printf("webcit: Can't create a socket: %s\n",
62 /* Set some socket options that make sense. */
64 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
65 setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
67 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
68 printf("webcit: Can't bind: %s\n", strerror(errno));
72 if (listen(s, queue_len) < 0) {
73 printf("webcit: Can't listen: %s\n", strerror(errno));
82 * client_write() ... Send binary data to the client.
84 void client_write(int sock, char *buf, int nbytes)
86 int bytes_written = 0;
88 while (bytes_written < nbytes) {
89 retval = write(sock, &buf[bytes_written],
90 nbytes - bytes_written);
92 printf("client_write() failed: %s\n",
96 bytes_written = bytes_written + retval;
102 * cprintf() ... Send formatted printable data to the client. It is
103 * implemented in terms of client_write() but remains in
104 * sysdep.c in case we port to somewhere without va_args...
106 void cprintf(int sock, const char *format, ...) {
110 va_start(arg_ptr, format);
111 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
112 buf[sizeof buf - 2] = '\n';
113 client_write(sock, buf, strlen(buf));
119 * Read data from the client socket.
121 * 1 Requested number of bytes has been read.
122 * 0 Request timed out.
123 * If the socket breaks, the session is immediately terminated.
125 int client_read_to(int sock, char *buf, int bytes, int timeout)
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);
147 printf("client_read() failed: %s\n",
157 * Read data from the client socket with default timeout.
158 * (This is implemented in terms of client_read_to() and could be
159 * justifiably moved out of sysdep.c)
161 int client_read(int sock, char *buf, int bytes)
163 return(client_read_to(sock, buf, bytes, SLEEPING));
168 * client_gets() ... Get a LF-terminated line of text from the client.
169 * (This is implemented in terms of client_read() and could be
170 * justifiably moved out of sysdep.c)
172 int client_gets(int sock, char *buf)
176 /* Read one character at a time.
179 retval = client_read(sock, &buf[i], 1);
180 if (retval != 1 || buf[i] == '\n' || i == 255)
184 /* If we got a long line, discard characters until the newline.
187 while (buf[i] != '\n' && retval == 1)
188 retval = client_read(sock, &buf[i], 1);
191 * Strip any trailing not-printable characters.
194 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
195 buf[strlen(buf)-1] = 0;
202 * Start running as a daemon. Only close stdio if do_close_stdio is set.
204 void start_daemon(int do_close_stdio) {
205 if (do_close_stdio) {
210 signal(SIGHUP,SIG_IGN);
211 signal(SIGINT,SIG_IGN);
212 signal(SIGQUIT,SIG_IGN);
213 if (fork()!=0) exit(0);
217 * Issue an HTTP Redirect header
218 * url must be a complete url (with http://)
220 void redirect(char *url) {
221 printf("Location: %s\n\n", url);
224 const char *defaulthost = DEFAULT_HOST;
225 const char *defaultport = DEFAULT_PORT;
228 * Here's where it all begins.
230 int main(int argc, char **argv)
232 struct sockaddr_in fsin; /* Data for master socket */
233 int alen; /* Data for master socket */
234 int ssock; /* Descriptor for master socket */
235 pthread_t SessThread; /* Thread descriptor */
236 pthread_attr_t attr; /* Thread attributes */
237 int a, i; /* General-purpose variables */
238 int port = PORT_NUM; /* Port to listen on */
241 /* Parse command line */
242 while ((a = getopt(argc, argv, "hp:")) != EOF)
248 fprintf(stderr, "usage: webserver [-p localport] "
249 "[remotehost [remoteport]]\n");
254 defaulthost = argv[optind];
256 defaultport = argv[optind];
259 /* Tell 'em who's in da house */
260 printf("WebCit v2 experimental\n");
261 printf("Copyright (C) 1996-1998 by Art Cancro. ");
262 printf("All rights reserved.\n\n");
265 * Bind the server to our favourite port.
266 * There is no need to check for errors, because ig_tcp_server()
267 * exits if it doesn't succeed.
269 printf("Attempting to bind to port %d...\n", port);
270 msock = ig_tcp_server(port, 5);
271 printf("Listening on socket %d\n", msock);
273 pthread_mutex_init(&MasterCritter, NULL);
276 * Endless loop. Listen on the master socket. When a connection
277 * comes in, create a socket, a context, and a thread.
280 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
281 printf("New connection on socket %d\n", ssock);
283 printf("webcit: accept() failed: %s\n",
287 /* Set the SO_REUSEADDR socket option */
289 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
292 /* set attributes for the new thread */
293 pthread_attr_init(&attr);
294 pthread_attr_setdetachstate(&attr,
295 PTHREAD_CREATE_DETACHED);
297 /* now create the thread */
298 if (pthread_create(&SessThread, &attr,
299 (void* (*)(void*)) context_loop,
302 printf("webcit: can't create thread: %s\n",