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;
41 memset(&sin, 0, sizeof(sin));
42 sin.sin_family = AF_INET;
43 sin.sin_addr.s_addr = INADDR_ANY;
45 if (port_number == 0) {
46 printf("webcit: Cannot start: no port number specified.\n");
50 sin.sin_port = htons((u_short)port_number);
52 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
54 printf("webcit: Can't create a socket: %s\n",
59 /* Set the SO_REUSEADDR socket option, because it makes sense. */
61 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
63 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
64 printf("webcit: Can't bind: %s\n", strerror(errno));
68 if (listen(s, queue_len) < 0) {
69 printf("webcit: Can't listen: %s\n", strerror(errno));
78 * client_write() ... Send binary data to the client.
80 void client_write(int sock, char *buf, int nbytes)
82 int bytes_written = 0;
84 while (bytes_written < nbytes) {
85 retval = write(sock, &buf[bytes_written],
86 nbytes - bytes_written);
88 printf("client_write() failed: %s\n",
92 bytes_written = bytes_written + retval;
98 * cprintf() ... Send formatted printable data to the client. It is
99 * implemented in terms of client_write() but remains in
100 * sysdep.c in case we port to somewhere without va_args...
102 void cprintf(int sock, const char *format, ...) {
106 va_start(arg_ptr, format);
107 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
108 buf[sizeof buf - 2] = '\n';
109 client_write(sock, buf, strlen(buf));
115 * Read data from the client socket.
117 * 1 Requested number of bytes has been read.
118 * 0 Request timed out.
119 * If the socket breaks, the session is immediately terminated.
121 int client_read_to(int sock, char *buf, int bytes, int timeout)
135 retval = select( (sock)+1,
136 &rfds, NULL, NULL, &tv);
137 if (FD_ISSET(sock, &rfds) == 0) {
141 rlen = read(sock, &buf[len], bytes-len);
143 printf("client_read() failed: %s\n",
153 * Read data from the client socket with default timeout.
154 * (This is implemented in terms of client_read_to() and could be
155 * justifiably moved out of sysdep.c)
157 int client_read(int sock, char *buf, int bytes)
159 return(client_read_to(sock, buf, bytes, SLEEPING));
164 * client_gets() ... Get a LF-terminated line of text from the client.
165 * (This is implemented in terms of client_read() and could be
166 * justifiably moved out of sysdep.c)
168 int client_gets(int sock, char *buf)
172 /* Read one character at a time.
175 retval = client_read(sock, &buf[i], 1);
176 if (retval != 1 || buf[i] == '\n' || i == 255)
180 /* If we got a long line, discard characters until the newline.
183 while (buf[i] != '\n' && retval == 1)
184 retval = client_read(sock, &buf[i], 1);
187 * Strip any trailing not-printable characters.
190 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
191 buf[strlen(buf)-1] = 0;
198 * Start running as a daemon. Only close stdio if do_close_stdio is set.
200 void start_daemon(int do_close_stdio) {
201 if (do_close_stdio) {
206 signal(SIGHUP,SIG_IGN);
207 signal(SIGINT,SIG_IGN);
208 signal(SIGQUIT,SIG_IGN);
209 if (fork()!=0) exit(0);
215 * Here's where it all begins.
217 int main(int argc, char **argv)
219 struct sockaddr_in fsin; /* Data for master socket */
220 int alen; /* Data for master socket */
221 int ssock; /* Descriptor for master socket */
222 pthread_t SessThread; /* Thread descriptor */
223 pthread_attr_t attr; /* Thread attributes */
224 int a, i; /* General-purpose variables */
227 /* Tell 'em who's in da house */
228 printf("WebCit v2 experimental\n");
229 printf("Copyright (C) 1996-1998 by Art Cancro. ");
230 printf("All rights reserved.\n\n");
233 * Bind the server to our favourite port.
234 * There is no need to check for errors, because ig_tcp_server()
235 * exits if it doesn't succeed.
237 printf("Attempting to bind to port %d...\n", PORT_NUM);
238 msock = ig_tcp_server(PORT_NUM, 5);
239 printf("Listening on socket %d\n", msock);
242 * Endless loop. Listen on the master socket. When a connection
243 * comes in, create a socket, a context, and a thread.
246 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
248 printf("webcit: accept() failed: %s\n",
252 /* Set the SO_REUSEADDR socket option */
254 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
257 /* set attributes for the new thread */
258 pthread_attr_init(&attr);
259 pthread_attr_setdetachstate(&attr,
260 PTHREAD_CREATE_DETACHED);
262 /* now create the thread */
263 if (pthread_create(&SessThread, &attr,
264 (void* (*)(void*)) context_loop,
267 printf("webcit: can't create thread: %s\n",