]> code.citadel.org Git - citadel.git/blob - webcit/webserver.c
7e350efafa4c850d66a30799c5ecb2e99f11b904
[citadel.git] / webcit / webserver.c
1 /*
2  * webserver.c
3  *
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.
7  *
8  */
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include "webcit.h"
28
29 int msock;                                      /* master listening socket */
30 extern void *context_loop(int);
31
32 /*
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.
35  */
36 int ig_tcp_server(int port_number, int queue_len)
37 {
38         struct sockaddr_in sin;
39         int s, i;
40         struct linger WebLinger = { 1, 9000 };
41
42         memset(&sin, 0, sizeof(sin));
43         sin.sin_family = AF_INET;
44         sin.sin_addr.s_addr = INADDR_ANY;
45
46         if (port_number == 0) {
47                 printf("webcit: Cannot start: no port number specified.\n");
48                 exit(1);
49                 }
50         
51         sin.sin_port = htons((u_short)port_number);
52
53         s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
54         if (s < 0) {
55                 printf("webcit: Can't create a socket: %s\n",
56                         strerror(errno));
57                 exit(errno);
58                 }
59
60         /* Set some socket options that make sense. */
61         i = 1;
62         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
63         setsockopt(s, SOL_SOCKET, SO_LINGER, &WebLinger, sizeof(WebLinger));
64
65         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
66                 printf("webcit: Can't bind: %s\n", strerror(errno));
67                 exit(errno);
68                 }
69
70         if (listen(s, queue_len) < 0) {
71                 printf("webcit: Can't listen: %s\n", strerror(errno));
72                 exit(errno);
73                 }
74
75         return(s);
76         }
77
78
79 /*
80  * client_write()   ...    Send binary data to the client.
81  */
82 void client_write(int sock, char *buf, int nbytes)
83 {
84         int bytes_written = 0;
85         int retval;
86         while (bytes_written < nbytes) {
87                 retval = write(sock, &buf[bytes_written],
88                         nbytes - bytes_written);
89                 if (retval < 1) {
90                         printf("client_write() failed: %s\n",
91                                 strerror(errno));
92                         pthread_exit(NULL);
93                         }
94                 bytes_written = bytes_written + retval;
95                 }
96         }
97
98
99 /*
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...
103  */
104 void cprintf(int sock, const char *format, ...) {   
105         va_list arg_ptr;   
106         char buf[256];   
107    
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)); 
112         va_end(arg_ptr);
113         }   
114
115
116 /*
117  * Read data from the client socket.
118  * Return values are:
119  *      1       Requested number of bytes has been read.
120  *      0       Request timed out.
121  * If the socket breaks, the session is immediately terminated.
122  */
123 int client_read_to(int sock, char *buf, int bytes, int timeout)
124 {
125         int len,rlen;
126         fd_set rfds;
127         struct timeval tv;
128         int retval;
129
130         len = 0;
131         while(len<bytes) {
132                 FD_ZERO(&rfds);
133                 FD_SET(sock, &rfds);
134                 tv.tv_sec = timeout;
135                 tv.tv_usec = 0;
136
137                 retval = select( (sock)+1, 
138                                         &rfds, NULL, NULL, &tv);
139                 if (FD_ISSET(sock, &rfds) == 0) {
140                         return(0);
141                         }
142
143                 rlen = read(sock, &buf[len], bytes-len);
144                 if (rlen<1) {
145                         printf("client_read() failed: %s\n",
146                                 strerror(errno));
147                         pthread_exit(NULL);
148                         }
149                 len = len + rlen;
150                 }
151         return(1);
152         }
153
154 /*
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)
158  */
159 int client_read(int sock, char *buf, int bytes)
160 {
161         return(client_read_to(sock, buf, bytes, SLEEPING));
162         }
163
164
165 /*
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)
169  */
170 int client_gets(int sock, char *buf)
171 {
172         int i, retval;
173
174         /* Read one character at a time.
175          */
176         for (i = 0;;i++) {
177                 retval = client_read(sock, &buf[i], 1);
178                 if (retval != 1 || buf[i] == '\n' || i == 255)
179                         break;
180                 }
181
182         /* If we got a long line, discard characters until the newline.
183          */
184         if (i == 255)
185                 while (buf[i] != '\n' && retval == 1)
186                         retval = client_read(sock, &buf[i], 1);
187
188         /*
189          * Strip any trailing not-printable characters.
190          */
191         buf[i] = 0;
192         while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1]))) {
193                 buf[strlen(buf)-1] = 0;
194                 }
195         return(retval);
196         }
197
198
199 /*
200  * Start running as a daemon.  Only close stdio if do_close_stdio is set.
201  */
202 void start_daemon(int do_close_stdio) {
203         if (do_close_stdio) {
204                 /* close(0); */
205                 close(1);
206                 close(2);
207                 }
208         signal(SIGHUP,SIG_IGN);
209         signal(SIGINT,SIG_IGN);
210         signal(SIGQUIT,SIG_IGN);
211         if (fork()!=0) exit(0);
212         }
213
214
215
216 /*
217  * Here's where it all begins.
218  */
219 int main(int argc, char **argv)
220 {
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 */
227         char convbuf[128];
228         
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");
233
234         /*
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.
238          */
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);
242
243         /* 
244          * Endless loop.  Listen on the master socket.  When a connection
245          * comes in, create a socket, a context, and a thread.
246          */     
247         while (1) {
248                 ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
249                 if (ssock < 0) {
250                         printf("webcit: accept() failed: %s\n",
251                                 strerror(errno));
252                         }
253                 else {
254                         /* Set the SO_REUSEADDR socket option */
255                         i = 1;
256                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
257                                 &i, sizeof(i));
258
259                         /* set attributes for the new thread */
260                         pthread_attr_init(&attr);
261                         pthread_attr_setdetachstate(&attr,
262                                 PTHREAD_CREATE_DETACHED);
263
264                         /* now create the thread */
265                         if (pthread_create(&SessThread, &attr,
266                                            (void* (*)(void*)) context_loop,
267                                            &ssock)
268                             != 0) {
269                                 printf("webcit: can't create thread: %s\n",
270                                         strerror(errno));
271                                 }
272
273                         }
274                 }
275         }
276