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>
40 int vsnprintf (char *buf, size_t max, const char *fmt, va_list argp);
43 int msock; /* master listening socket */
44 extern void *context_loop(int);
45 extern pthread_mutex_t MasterCritter;
48 * This is a generic function to set up a master socket for listening on
49 * a TCP port. The server shuts down if the bind fails.
51 int ig_tcp_server(int port_number, int queue_len)
53 struct sockaddr_in sin;
56 memset(&sin, 0, sizeof(sin));
57 sin.sin_family = AF_INET;
58 sin.sin_addr.s_addr = INADDR_ANY;
60 if (port_number == 0) {
61 printf("webcit: Cannot start: no port number specified.\n");
65 sin.sin_port = htons((u_short)port_number);
67 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
69 printf("webcit: Can't create a socket: %s\n",
74 /* Set some socket options that make sense. */
76 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
78 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
79 printf("webcit: Can't bind: %s\n", strerror(errno));
83 if (listen(s, queue_len) < 0) {
84 printf("webcit: Can't listen: %s\n", strerror(errno));
93 * client_write() ... Send binary data to the client.
95 void client_write(int sock, char *buf, int nbytes)
97 int bytes_written = 0;
99 while (bytes_written < nbytes) {
100 retval = write(sock, &buf[bytes_written],
101 nbytes - bytes_written);
103 printf("client_write() failed: %s\n",
107 bytes_written = bytes_written + retval;
113 * cprintf() ... Send formatted printable data to the client. It is
114 * implemented in terms of client_write() but remains in
115 * sysdep.c in case we port to somewhere without va_args...
117 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)
150 retval = select( (sock)+1,
151 &rfds, NULL, NULL, &tv);
152 if (FD_ISSET(sock, &rfds) == 0) {
156 rlen = read(sock, &buf[len], bytes-len);
158 printf("client_read() failed: %s\n",
168 * Read data from the client socket with default timeout.
169 * (This is implemented in terms of client_read_to() and could be
170 * justifiably moved out of sysdep.c)
172 int client_read(int sock, char *buf, int bytes)
174 return(client_read_to(sock, buf, bytes, SLEEPING));
179 * client_gets() ... Get a LF-terminated line of text from the client.
180 * (This is implemented in terms of client_read() and could be
181 * justifiably moved out of sysdep.c)
183 int client_gets(int sock, char *buf)
187 /* Read one character at a time.
190 retval = client_read(sock, &buf[i], 1);
191 if (retval != 1 || buf[i] == '\n' || i == 255)
195 /* If we got a long line, discard characters until the newline.
198 while (buf[i] != '\n' && retval == 1)
199 retval = client_read(sock, &buf[i], 1);
202 * Strip any trailing not-printable characters.
205 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
206 buf[strlen(buf)-1] = 0;
213 * Start running as a daemon. Only close stdio if do_close_stdio is set.
215 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);
224 if (fork()!=0) exit(0);
228 * Issue an HTTP Redirect header
229 * url must be a complete url (with http://)
231 void redirect(char *url) {
232 printf("Location: %s\n\n", url);
235 const char *defaulthost = DEFAULT_HOST;
236 const char *defaultport = DEFAULT_PORT;
239 * Here's where it all begins.
241 int main(int argc, char **argv)
243 struct sockaddr_in fsin; /* Data for master socket */
244 int alen; /* Data for master socket */
245 int ssock; /* Descriptor for master socket */
246 pthread_t SessThread; /* Thread descriptor */
247 pthread_attr_t attr; /* Thread attributes */
248 int a, i; /* General-purpose variables */
249 int port = PORT_NUM; /* Port to listen on */
250 /*struct linger WebLinger = { 1, 9000 };*/
252 /* Parse command line */
253 while ((a = getopt(argc, argv, "hp:")) != EOF)
259 fprintf(stderr, "usage: webserver [-p localport] "
260 "[remotehost [remoteport]]\n");
265 defaulthost = argv[optind];
267 defaultport = argv[optind];
270 /* Tell 'em who's in da house */
271 printf("WebCit v2 experimental\n");
272 printf("Copyright (C) 1996-1998 by Art Cancro. ");
273 printf("All rights reserved.\n\n");
276 * Bind the server to our favourite port.
277 * There is no need to check for errors, because ig_tcp_server()
278 * exits if it doesn't succeed.
280 printf("Attempting to bind to port %d...\n", port);
281 msock = ig_tcp_server(port, 5);
282 printf("Listening on socket %d\n", msock);
284 pthread_mutex_init(&MasterCritter, NULL);
287 * Endless loop. Listen on the master socket. When a connection
288 * comes in, create a socket, a context, and a thread.
291 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
292 printf("New connection on socket %d\n", ssock);
294 printf("webcit: accept() failed: %s\n",
298 /* Set the SO_REUSEADDR socket option */
300 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
302 /* setsockopt(ssock, SOL_SOCKET, SO_LINGER,
303 &WebLinger, sizeof(struct linger));*/
305 /* set attributes for the new thread */
306 pthread_attr_init(&attr);
307 pthread_attr_setdetachstate(&attr,
308 PTHREAD_CREATE_DETACHED);
310 /* now create the thread */
311 if (pthread_create(&SessThread, &attr,
312 (void* (*)(void*)) context_loop,
315 printf("webcit: can't create thread: %s\n",