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);
31 extern pthread_mutex_t MasterCritter;
34 * This is a generic function to set up a master socket for listening on
35 * a TCP port. The server shuts down if the bind fails.
37 int ig_tcp_server(int port_number, int queue_len)
39 struct sockaddr_in sin;
41 struct linger WebLinger = { 1, 9000 };
43 memset(&sin, 0, sizeof(sin));
44 sin.sin_family = AF_INET;
45 sin.sin_addr.s_addr = INADDR_ANY;
47 if (port_number == 0) {
48 printf("webcit: Cannot start: no port number specified.\n");
52 sin.sin_port = htons((u_short)port_number);
54 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
56 printf("webcit: Can't create a socket: %s\n",
61 /* Set some socket options that make sense. */
63 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
64 setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
66 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
67 printf("webcit: Can't bind: %s\n", strerror(errno));
71 if (listen(s, queue_len) < 0) {
72 printf("webcit: Can't listen: %s\n", strerror(errno));
81 * client_write() ... Send binary data to the client.
83 void client_write(int sock, char *buf, int nbytes)
85 int bytes_written = 0;
87 while (bytes_written < nbytes) {
88 retval = write(sock, &buf[bytes_written],
89 nbytes - bytes_written);
91 printf("client_write() failed: %s\n",
95 bytes_written = bytes_written + retval;
101 * cprintf() ... Send formatted printable data to the client. It is
102 * implemented in terms of client_write() but remains in
103 * sysdep.c in case we port to somewhere without va_args...
105 void cprintf(int sock, const char *format, ...) {
109 va_start(arg_ptr, format);
110 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
111 buf[sizeof buf - 2] = '\n';
112 client_write(sock, buf, strlen(buf));
118 * Read data from the client socket.
120 * 1 Requested number of bytes has been read.
121 * 0 Request timed out.
122 * If the socket breaks, the session is immediately terminated.
124 int client_read_to(int sock, char *buf, int bytes, int timeout)
138 retval = select( (sock)+1,
139 &rfds, NULL, NULL, &tv);
140 if (FD_ISSET(sock, &rfds) == 0) {
144 rlen = read(sock, &buf[len], bytes-len);
146 printf("client_read() failed: %s\n",
156 * Read data from the client socket with default timeout.
157 * (This is implemented in terms of client_read_to() and could be
158 * justifiably moved out of sysdep.c)
160 int client_read(int sock, char *buf, int bytes)
162 return(client_read_to(sock, buf, bytes, SLEEPING));
167 * client_gets() ... Get a LF-terminated line of text from the client.
168 * (This is implemented in terms of client_read() and could be
169 * justifiably moved out of sysdep.c)
171 int client_gets(int sock, char *buf)
175 /* Read one character at a time.
178 retval = client_read(sock, &buf[i], 1);
179 if (retval != 1 || buf[i] == '\n' || i == 255)
183 /* If we got a long line, discard characters until the newline.
186 while (buf[i] != '\n' && retval == 1)
187 retval = client_read(sock, &buf[i], 1);
190 * Strip any trailing not-printable characters.
193 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
194 buf[strlen(buf)-1] = 0;
201 * Start running as a daemon. Only close stdio if do_close_stdio is set.
203 void start_daemon(int do_close_stdio) {
204 if (do_close_stdio) {
209 signal(SIGHUP,SIG_IGN);
210 signal(SIGINT,SIG_IGN);
211 signal(SIGQUIT,SIG_IGN);
212 if (fork()!=0) exit(0);
216 * Issue an HTTP Redirect header
217 * url must be a complete url (with http://)
219 void redirect(char *url) {
220 printf("Location: %s\n\n", url);
225 * Here's where it all begins.
227 int main(int argc, char **argv)
229 struct sockaddr_in fsin; /* Data for master socket */
230 int alen; /* Data for master socket */
231 int ssock; /* Descriptor for master socket */
232 pthread_t SessThread; /* Thread descriptor */
233 pthread_attr_t attr; /* Thread attributes */
234 int a, i; /* General-purpose variables */
237 /* Tell 'em who's in da house */
238 printf("WebCit v2 experimental\n");
239 printf("Copyright (C) 1996-1998 by Art Cancro. ");
240 printf("All rights reserved.\n\n");
243 * Bind the server to our favourite port.
244 * There is no need to check for errors, because ig_tcp_server()
245 * exits if it doesn't succeed.
247 printf("Attempting to bind to port %d...\n", PORT_NUM);
248 msock = ig_tcp_server(PORT_NUM, 5);
249 printf("Listening on socket %d\n", msock);
251 pthread_mutex_init(&MasterCritter, NULL);
254 * Endless loop. Listen on the master socket. When a connection
255 * comes in, create a socket, a context, and a thread.
258 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
259 printf("New connection on socket %d\n", ssock);
261 printf("webcit: accept() failed: %s\n",
265 /* Set the SO_REUSEADDR socket option */
267 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
270 /* set attributes for the new thread */
271 pthread_attr_init(&attr);
272 pthread_attr_setdetachstate(&attr,
273 PTHREAD_CREATE_DETACHED);
275 /* now create the thread */
276 if (pthread_create(&SessThread, &attr,
277 (void* (*)(void*)) context_loop,
280 printf("webcit: can't create thread: %s\n",