3 * Copyright (C) 2005 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Elementary WWW server (port 80)
24 * Author: Simone Piccardi
27 * Usage: wwwd -h give all info
31 ****************************************************************/
33 * Include needed headers
36 #include <string.h> /* error strings */
37 #include <sys/types.h> /* predefined types */
38 #include <unistd.h> /* include unix standard library */
39 #include <arpa/inet.h> /* IP addresses conversion utiliites */
40 #include <sys/socket.h> /* socket library */
41 #include <stdio.h> /* include standard I/O library */
43 #include <syslog.h> /* syslog system functions */
44 #include <signal.h> /* signal functions */
45 #include <errno.h> /* error code */
52 int demonize = 1; /* daemon use option: default is daemon */
53 int debugging = 0; /* debug info printing option: default is no debug */
55 /* Subroutines declaration */
57 void ServPage(int sockfd);
58 void PrintErr(char * error);
59 void print_headers(FILE *file);
61 /* Program beginning */
62 int main(int argc, char *argv[])
65 * Variables definition
71 struct sockaddr_in cli_add;
73 char debug[MAXLINE], ipaddr[20];
75 * Input section: decode parameters passed in the calling
79 opterr = 0; /* don't want writing to stderr */
80 while ( (i = getopt(argc, argv, "hdicw:")) != -1) {
86 printf("Wrong -h option use\n");
100 waiting = strtol(optarg, NULL, 10);
102 case '?': /* unrecognized options */
103 printf("Unrecognized options -%c\n",optopt);
105 default: /* should not reached */
109 /* ***********************************************************
111 * Options processing completed
113 * Main code beginning
115 * ***********************************************************/
116 /* Main code begin here */
117 if (compat) { /* install signal handler */
118 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
120 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
122 /* create and bind socket */
123 if ( (list_fd = sockbind2(argv[optind], "www", 6, SOCK_STREAM)) < 0) {
126 /* release privileges and go daemon */
127 if (setgid(65534) !=0) { /* first give away group privileges */
128 perror("cannot give away group privileges");
131 if (setuid(65534) !=0) { /* and only after user ... */
132 perror("cannot give away user privileges");
135 if (demonize) { /* go daemon */
136 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
137 if (daemon(0, 0) != 0) {
138 perror("cannot start as daemon");
143 if (listen(list_fd, BACKLOG) < 0 ) {
144 PrintErr("listen error");
147 if (waiting) sleep(waiting);
148 /* handle echo to client */
150 /* accept connection */
151 len = sizeof(cli_add);
152 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
153 < 0) && (errno == EINTR));
155 PrintErr("accept error");
159 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
160 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
162 syslog(LOG_DEBUG, debug);
167 /* fork to handle connection */
168 if ( (pid = fork()) < 0 ){
169 PrintErr("fork error");
172 if (pid == 0) { /* child */
173 close(list_fd); /* close listening socket */
174 ServPage(conn_fd); /* handle echo */
176 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
178 syslog(LOG_DEBUG, debug);
184 } else { /* parent */
185 close(conn_fd); /* close connected socket */
188 /* normal exit, never reached */
192 * routine to print usage info and exit
196 printf("Elementary echo server\n");
198 printf(" echod [-h] \n");
199 printf(" -h print this help\n");
200 printf(" -d write debug info\n");
201 printf(" -i use interactively\n");
202 printf(" -c disable BSD semantics\n");
203 printf(" -w N wait N sec. before calling accept\n");
207 * routine to handle echo for connection
209 void ServPage(int sockfd)
211 char buffer[MAXLINE];
214 char *line, *copy, *method, *ptr, *filename, *version;
215 char *methods[] = { "GET", "PUT", NULL };
224 sock = fdopen(sockfd, "w+");
225 /* main loop, reading 0 char means client close connection */
226 line = fgets(buffer, MAXLINE, sock);
227 copy = strndupa(line, MAXLINE);
230 PrintErr("Errore in lettura");
233 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
234 PrintErr("Non ho trovato il metodo");
237 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
238 PrintErr("Non ho trovato il file");
241 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
242 PrintErr("Non ho trovato la versione");
245 printf("metodo %s -- file %s -- versione %s\n", method, filename, version);
247 while ( (ptr = methods[i]) != NULL) {
248 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
254 printf("No method %s found\n", method);
258 while (strcmp(line,"\r\n")) {
259 line = fgets(buffer, MAXLINE, sock);
260 printf("letto: %s\n", line);
263 if ( (file = fopen(filename, "r")) == NULL) {
264 printf("file %s", filename);
265 perror("Error opening");
266 fprintf(sock, "HTTP/1.0 %s\n", codes[1]);
270 fprintf(sock, "HTTP/1.0 %s\n", codes[0]);
275 while (!feof(file)) {
276 printf("Loop %d\n", j++);
277 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
279 printf("Errore in lettura");
283 printf("Loop %d rimasti %d\n", j, nleft);
284 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
286 printf("Errore in scrittura");
291 printf("Ok fin qui!");
292 // line = fgets(buffer, MAXLINE, sock);
298 * routine to print error on stout or syslog
300 void PrintErr(char * error)
302 if (demonize) { /* daemon mode */
303 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
310 void print_headers(FILE *file)
315 fprintf(file, "Date: %s", ctime(&tempo));
316 fprintf(file, "Server: WWWd test server\n");
317 fprintf(file, "Connection: close\n");
318 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
320 printf("Date: %s", ctime(&tempo));
321 printf("Server: WWWd test server\n");
322 printf("Connection: close\n");
323 printf("Content-Type: text/html; charset=iso-8859-1\n");