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.
15 #include <sys/types.h>
17 #include <sys/socket.h>
20 #include <netinet/in.h>
29 int msock; /* master listening socket */
30 extern void *context_loop(int);
33 * This is a generic function to set up a master socket for listening on
34 * a TCP port. The server shuts down if the bind fails.
36 int ig_tcp_server(int port_number, int queue_len)
38 struct sockaddr_in sin;
40 struct linger WebLinger = { 1, 9000 };
42 memset(&sin, 0, sizeof(sin));
43 sin.sin_family = AF_INET;
44 sin.sin_addr.s_addr = INADDR_ANY;
46 if (port_number == 0) {
47 printf("webcit: Cannot start: no port number specified.\n");
51 sin.sin_port = htons((u_short)port_number);
53 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
55 printf("webcit: Can't create a socket: %s\n",
60 /* Set some socket options that make sense. */
62 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
63 setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
65 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
66 printf("webcit: Can't bind: %s\n", strerror(errno));
70 if (listen(s, queue_len) < 0) {
71 printf("webcit: Can't listen: %s\n", strerror(errno));
80 * client_write() ... Send binary data to the client.
82 void client_write(int sock, char *buf, int nbytes)
84 int bytes_written = 0;
86 while (bytes_written < nbytes) {
87 retval = write(sock, &buf[bytes_written],
88 nbytes - bytes_written);
90 printf("client_write() failed: %s\n",
94 bytes_written = bytes_written + retval;
100 * cprintf() ... Send formatted printable data to the client. It is
101 * implemented in terms of client_write() but remains in
102 * sysdep.c in case we port to somewhere without va_args...
104 void cprintf(int sock, const char *format, ...) {
108 va_start(arg_ptr, format);
109 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
110 buf[sizeof buf - 2] = '\n';
111 client_write(sock, buf, strlen(buf));
117 * Read data from the client socket.
119 * 1 Requested number of bytes has been read.
120 * 0 Request timed out.
121 * If the socket breaks, the session is immediately terminated.
123 int client_read_to(int sock, char *buf, int bytes, int timeout)
137 retval = select( (sock)+1,
138 &rfds, NULL, NULL, &tv);
139 if (FD_ISSET(sock, &rfds) == 0) {
143 rlen = read(sock, &buf[len], bytes-len);
145 printf("client_read() failed: %s\n",
155 * Read data from the client socket with default timeout.
156 * (This is implemented in terms of client_read_to() and could be
157 * justifiably moved out of sysdep.c)
159 int client_read(int sock, char *buf, int bytes)
161 return(client_read_to(sock, buf, bytes, SLEEPING));
166 * client_gets() ... Get a LF-terminated line of text from the client.
167 * (This is implemented in terms of client_read() and could be
168 * justifiably moved out of sysdep.c)
170 int client_gets(int sock, char *buf)
174 /* Read one character at a time.
177 retval = client_read(sock, &buf[i], 1);
178 if (retval != 1 || buf[i] == '\n' || i == 255)
182 /* If we got a long line, discard characters until the newline.
185 while (buf[i] != '\n' && retval == 1)
186 retval = client_read(sock, &buf[i], 1);
189 * Strip any trailing not-printable characters.
192 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
193 buf[strlen(buf)-1] = 0;
200 * Start running as a daemon. Only close stdio if do_close_stdio is set.
202 void start_daemon(int do_close_stdio) {
203 if (do_close_stdio) {
208 signal(SIGHUP,SIG_IGN);
209 signal(SIGINT,SIG_IGN);
210 signal(SIGQUIT,SIG_IGN);
211 if (fork()!=0) exit(0);
217 * Here's where it all begins.
219 int main(int argc, char **argv)
221 struct sockaddr_in fsin; /* Data for master socket */
222 int alen; /* Data for master socket */
223 int ssock; /* Descriptor for master socket */
224 pthread_t SessThread; /* Thread descriptor */
225 pthread_attr_t attr; /* Thread attributes */
226 int a, i; /* General-purpose variables */
229 /* Tell 'em who's in da house */
230 printf("WebCit v2 experimental\n");
231 printf("Copyright (C) 1996-1998 by Art Cancro. ");
232 printf("All rights reserved.\n\n");
235 * Bind the server to our favourite port.
236 * There is no need to check for errors, because ig_tcp_server()
237 * exits if it doesn't succeed.
239 printf("Attempting to bind to port %d...\n", PORT_NUM);
240 msock = ig_tcp_server(PORT_NUM, 5);
241 printf("Listening on socket %d\n", msock);
244 * Endless loop. Listen on the master socket. When a connection
245 * comes in, create a socket, a context, and a thread.
248 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
250 printf("webcit: accept() failed: %s\n",
254 /* Set the SO_REUSEADDR socket option */
256 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
259 /* set attributes for the new thread */
260 pthread_attr_init(&attr);
261 pthread_attr_setdetachstate(&attr,
262 PTHREAD_CREATE_DETACHED);
264 /* now create the thread */
265 if (pthread_create(&SessThread, &attr,
266 (void* (*)(void*)) context_loop,
269 printf("webcit: can't create thread: %s\n",