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>
39 int msock; /* master listening socket */
40 extern void *context_loop(int);
41 extern pthread_mutex_t MasterCritter;
44 * This is a generic function to set up a master socket for listening on
45 * a TCP port. The server shuts down if the bind fails.
47 int ig_tcp_server(int port_number, int queue_len)
49 struct sockaddr_in sin;
51 struct linger WebLinger = { 1, 9000 };
53 memset(&sin, 0, sizeof(sin));
54 sin.sin_family = AF_INET;
55 sin.sin_addr.s_addr = INADDR_ANY;
57 if (port_number == 0) {
58 printf("webcit: Cannot start: no port number specified.\n");
62 sin.sin_port = htons((u_short)port_number);
64 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
66 printf("webcit: Can't create a socket: %s\n",
71 /* Set some socket options that make sense. */
73 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
74 setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
76 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
77 printf("webcit: Can't bind: %s\n", strerror(errno));
81 if (listen(s, queue_len) < 0) {
82 printf("webcit: Can't listen: %s\n", strerror(errno));
91 * client_write() ... Send binary data to the client.
93 void client_write(int sock, char *buf, int nbytes)
95 int bytes_written = 0;
97 while (bytes_written < nbytes) {
98 retval = write(sock, &buf[bytes_written],
99 nbytes - bytes_written);
101 printf("client_write() failed: %s\n",
105 bytes_written = bytes_written + retval;
111 * cprintf() ... Send formatted printable data to the client. It is
112 * implemented in terms of client_write() but remains in
113 * sysdep.c in case we port to somewhere without va_args...
115 void cprintf(int sock, const char *format, ...) {
119 va_start(arg_ptr, format);
120 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
121 buf[sizeof buf - 2] = '\n';
122 client_write(sock, buf, strlen(buf));
128 * Read data from the client socket.
130 * 1 Requested number of bytes has been read.
131 * 0 Request timed out.
132 * If the socket breaks, the session is immediately terminated.
134 int client_read_to(int sock, char *buf, int bytes, int timeout)
148 retval = select( (sock)+1,
149 &rfds, NULL, NULL, &tv);
150 if (FD_ISSET(sock, &rfds) == 0) {
154 rlen = read(sock, &buf[len], bytes-len);
156 printf("client_read() failed: %s\n",
166 * Read data from the client socket with default timeout.
167 * (This is implemented in terms of client_read_to() and could be
168 * justifiably moved out of sysdep.c)
170 int client_read(int sock, char *buf, int bytes)
172 return(client_read_to(sock, buf, bytes, SLEEPING));
177 * client_gets() ... Get a LF-terminated line of text from the client.
178 * (This is implemented in terms of client_read() and could be
179 * justifiably moved out of sysdep.c)
181 int client_gets(int sock, char *buf)
185 /* Read one character at a time.
188 retval = client_read(sock, &buf[i], 1);
189 if (retval != 1 || buf[i] == '\n' || i == 255)
193 /* If we got a long line, discard characters until the newline.
196 while (buf[i] != '\n' && retval == 1)
197 retval = client_read(sock, &buf[i], 1);
200 * Strip any trailing not-printable characters.
203 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
204 buf[strlen(buf)-1] = 0;
211 * Start running as a daemon. Only close stdio if do_close_stdio is set.
213 void start_daemon(int do_close_stdio) {
214 if (do_close_stdio) {
219 signal(SIGHUP,SIG_IGN);
220 signal(SIGINT,SIG_IGN);
221 signal(SIGQUIT,SIG_IGN);
222 if (fork()!=0) exit(0);
226 * Issue an HTTP Redirect header
227 * url must be a complete url (with http://)
229 void redirect(char *url) {
230 printf("Location: %s\n\n", url);
233 const char *defaulthost = DEFAULT_HOST;
234 const char *defaultport = DEFAULT_PORT;
237 * Here's where it all begins.
239 int main(int argc, char **argv)
241 struct sockaddr_in fsin; /* Data for master socket */
242 int alen; /* Data for master socket */
243 int ssock; /* Descriptor for master socket */
244 pthread_t SessThread; /* Thread descriptor */
245 pthread_attr_t attr; /* Thread attributes */
246 int a, i; /* General-purpose variables */
247 int port = PORT_NUM; /* Port to listen on */
249 /* Parse command line */
250 while ((a = getopt(argc, argv, "hp:")) != EOF)
256 fprintf(stderr, "usage: webserver [-p localport] "
257 "[remotehost [remoteport]]\n");
262 defaulthost = argv[optind];
264 defaultport = argv[optind];
267 /* Tell 'em who's in da house */
268 printf("WebCit v2 experimental\n");
269 printf("Copyright (C) 1996-1998 by Art Cancro. ");
270 printf("All rights reserved.\n\n");
273 * Bind the server to our favourite port.
274 * There is no need to check for errors, because ig_tcp_server()
275 * exits if it doesn't succeed.
277 printf("Attempting to bind to port %d...\n", port);
278 msock = ig_tcp_server(port, 5);
279 printf("Listening on socket %d\n", msock);
281 pthread_mutex_init(&MasterCritter, NULL);
284 * Endless loop. Listen on the master socket. When a connection
285 * comes in, create a socket, a context, and a thread.
288 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
289 printf("New connection on socket %d\n", ssock);
291 printf("webcit: accept() failed: %s\n",
295 /* Set the SO_REUSEADDR socket option */
297 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
300 /* set attributes for the new thread */
301 pthread_attr_init(&attr);
302 pthread_attr_setdetachstate(&attr,
303 PTHREAD_CREATE_DETACHED);
305 /* now create the thread */
306 if (pthread_create(&SessThread, &attr,
307 (void* (*)(void*)) context_loop,
310 printf("webcit: can't create thread: %s\n",