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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
31 int msock; /* master listening socket */
32 extern void *context_loop(int);
33 extern pthread_mutex_t MasterCritter;
36 * This is a generic function to set up a master socket for listening on
37 * a TCP port. The server shuts down if the bind fails.
39 int ig_tcp_server(int port_number, int queue_len)
41 struct sockaddr_in sin;
43 struct linger WebLinger = { 1, 9000 };
45 memset(&sin, 0, sizeof(sin));
46 sin.sin_family = AF_INET;
47 sin.sin_addr.s_addr = INADDR_ANY;
49 if (port_number == 0) {
50 printf("webcit: Cannot start: no port number specified.\n");
54 sin.sin_port = htons((u_short)port_number);
56 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
58 printf("webcit: Can't create a socket: %s\n",
63 /* Set some socket options that make sense. */
65 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
66 setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
68 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
69 printf("webcit: Can't bind: %s\n", strerror(errno));
73 if (listen(s, queue_len) < 0) {
74 printf("webcit: Can't listen: %s\n", strerror(errno));
83 * client_write() ... Send binary data to the client.
85 void client_write(int sock, char *buf, int nbytes)
87 int bytes_written = 0;
89 while (bytes_written < nbytes) {
90 retval = write(sock, &buf[bytes_written],
91 nbytes - bytes_written);
93 printf("client_write() failed: %s\n",
97 bytes_written = bytes_written + retval;
103 * cprintf() ... Send formatted printable data to the client. It is
104 * implemented in terms of client_write() but remains in
105 * sysdep.c in case we port to somewhere without va_args...
107 void cprintf(int sock, const char *format, ...) {
111 va_start(arg_ptr, format);
112 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
113 buf[sizeof buf - 2] = '\n';
114 client_write(sock, buf, strlen(buf));
120 * Read data from the client socket.
122 * 1 Requested number of bytes has been read.
123 * 0 Request timed out.
124 * If the socket breaks, the session is immediately terminated.
126 int client_read_to(int sock, char *buf, int bytes, int timeout)
140 retval = select( (sock)+1,
141 &rfds, NULL, NULL, &tv);
142 if (FD_ISSET(sock, &rfds) == 0) {
146 rlen = read(sock, &buf[len], bytes-len);
148 printf("client_read() failed: %s\n",
158 * Read data from the client socket with default timeout.
159 * (This is implemented in terms of client_read_to() and could be
160 * justifiably moved out of sysdep.c)
162 int client_read(int sock, char *buf, int bytes)
164 return(client_read_to(sock, buf, bytes, SLEEPING));
169 * client_gets() ... Get a LF-terminated line of text from the client.
170 * (This is implemented in terms of client_read() and could be
171 * justifiably moved out of sysdep.c)
173 int client_gets(int sock, char *buf)
177 /* Read one character at a time.
180 retval = client_read(sock, &buf[i], 1);
181 if (retval != 1 || buf[i] == '\n' || i == 255)
185 /* If we got a long line, discard characters until the newline.
188 while (buf[i] != '\n' && retval == 1)
189 retval = client_read(sock, &buf[i], 1);
192 * Strip any trailing not-printable characters.
195 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
196 buf[strlen(buf)-1] = 0;
203 * Start running as a daemon. Only close stdio if do_close_stdio is set.
205 void start_daemon(int do_close_stdio) {
206 if (do_close_stdio) {
211 signal(SIGHUP,SIG_IGN);
212 signal(SIGINT,SIG_IGN);
213 signal(SIGQUIT,SIG_IGN);
214 if (fork()!=0) exit(0);
218 * Issue an HTTP Redirect header
219 * url must be a complete url (with http://)
221 void redirect(char *url) {
222 printf("Location: %s\n\n", url);
225 const char *defaulthost = DEFAULT_HOST;
226 const char *defaultport = DEFAULT_PORT;
229 * Here's where it all begins.
231 int main(int argc, char **argv)
233 struct sockaddr_in fsin; /* Data for master socket */
234 int alen; /* Data for master socket */
235 int ssock; /* Descriptor for master socket */
236 pthread_t SessThread; /* Thread descriptor */
237 pthread_attr_t attr; /* Thread attributes */
238 int a, i; /* General-purpose variables */
239 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",