Programma di esempio delle funzioni delle capabilities, e un po' di
[gapil.git] / sources / TCP_echod.c
1 /* TCP_echod.c
2  * 
3  * Copyright (C) 2001-2004 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program echod 
22  * Elementary TCP server for echo service (port 7)
23  *
24  * Author: Simone Piccardi
25  * Jun. 2001
26  *
27  * Usage: echod -h give all info
28  *
29  * $Id$ 
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <sys/types.h>   /* predefined types */
36 #include <unistd.h>      /* include unix standard library */
37 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
38 #include <sys/socket.h>  /* socket library */
39 #include <stdio.h>       /* include standard I/O library */
40 #include <time.h>
41 #include <syslog.h>      /* syslog system functions */
42 #include <signal.h>      /* signal functions */
43 #include <errno.h>       /* error code */
44 #include <string.h>      /* error strings */
45 #include <stdlib.h>
46
47 #include "Gapil.h"
48
49 #define BACKLOG 10
50 #define MAXLINE 256
51 int demonize  = 1;  /* daemon use option: default is daemon */
52 int debugging = 0;  /* debug info printing option: default is no debug */
53 /* Subroutines declaration */
54 void usage(void);
55 void ServEcho(int sockfd);
56 void PrintErr(char * error);
57 /* Program beginning */
58 int main(int argc, char *argv[])
59 {
60 /* 
61  * Variables definition  
62  */
63     int list_fd, conn_fd;
64     int waiting = 0;
65     int keepalive = 0; 
66     int reuse = 0;
67     int compat = 0;
68     pid_t pid;
69     struct sockaddr_in cli_add;
70     socklen_t len;
71     char debug[MAXLINE], ipaddr[20];
72     /*
73      * Input section: decode parameters passed in the calling 
74      * Use getopt function
75      */
76     int i;
77     opterr = 0;  /* don't want writing to stderr */
78     while ( (i = getopt(argc, argv, "hkrdicw:")) != -1) {
79         switch (i) {
80         /* 
81          * Handling options 
82          */ 
83         case 'h':  
84             printf("Wrong -h option use\n");
85             usage();
86             return(0);
87             break;
88         case 'i':
89             demonize = 0;
90             break;
91         case 'k':
92             keepalive = 1;
93             break;
94         case 'r':
95             reuse = 1;
96             break;
97         case 'c':
98             compat = 1;
99             break;
100         case 'd':
101             debugging = 1;
102             break;
103         case 'w':
104             waiting = strtol(optarg, NULL, 10);
105             break;
106         case '?':   /* unrecognized options */
107             printf("Unrecognized options -%c\n",optopt);
108             usage();
109         default:    /* should not reached */
110             usage();
111         }
112     }
113     /* ***********************************************************
114      * 
115      *           Options processing completed
116      *
117      *                Main code beginning
118      * 
119      * ***********************************************************/
120     /* Main code begin here */
121     if (compat) {                             /* install signal handler */
122         Signal(SIGCHLD, HandSigCHLD);         /* non restarting handler */
123     } else {
124         SignalRestart(SIGCHLD, HandSigCHLD);  /* restarting handler */
125     }
126     /* create and bind socket */
127     if ( (list_fd = sockbindopt(argv[optind], "echo", 6, 
128                                 SOCK_STREAM, reuse)) < 0) {
129         return 1;
130     }   
131     /* release privileges and go daemon */
132     if (setgid(65534) !=0) { /* first give away group privileges */
133         perror("cannot give away group privileges");
134         exit(1);
135     }
136     if (setuid(65534) !=0) { /* and only after user ... */
137         perror("cannot give away user privileges");
138         exit(1);
139     }
140     if (demonize) {          /* go daemon */
141         openlog(argv[0], 0, LOG_DAEMON); /* open logging */
142         if (daemon(0, 0) != 0) {
143             perror("cannot start as daemon");
144             exit(1);
145         }
146     }
147     /* main body */
148     if (listen(list_fd, BACKLOG) < 0 ) {
149         PrintErr("listen error");
150         exit(1);
151     }
152     if (waiting) sleep(waiting);
153     /* handle echo to client */
154     while (1) {
155         /* accept connection */
156         len = sizeof(cli_add);
157         while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) 
158                 < 0) && (errno == EINTR)); 
159         if (conn_fd < 0) {
160             PrintErr("accept error");
161             exit(1);
162         }
163         if (debugging) {
164             inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
165             snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
166             if (demonize) {
167                 syslog(LOG_DEBUG, debug);
168             } else {
169                 printf("%s", debug);
170             }
171         }
172         /* fork to handle connection */
173         if ( (pid = fork()) < 0 ){
174             PrintErr("fork error");
175             exit(1);
176         }
177         if (pid == 0) {      /* child */
178             close(list_fd);          /* close listening socket */   
179             if (keepalive) {         /* enable keepalive ? */
180                 setsockopt(conn_fd, SOL_SOCKET, SO_KEEPALIVE, 
181                            &keepalive, sizeof(keepalive));
182             }
183             ServEcho(conn_fd);       /* handle echo */
184             if (debugging) {
185                 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
186                 if (demonize) {
187                     syslog(LOG_DEBUG, debug);
188                 } else {
189                     printf("%s", debug);
190                 }
191             }
192             exit(0);
193         } else {             /* parent */
194             close(conn_fd);          /* close connected socket */
195         }
196     }
197     /* normal exit, never reached */
198     exit(0);
199 }
200 /*
201  * routine to print usage info and exit
202  */
203 void usage(void) {
204     printf("Elementary echo server\n");
205     printf("Usage:\n");
206     printf("  echod [-h] \n");
207     printf("  -h           print this help\n");
208     printf("  -d           write debug info\n");
209     printf("  -k           enable SO_KEEPALIVE\n");
210     printf("  -r           enable SO_REUSEADDR\n");
211     printf("  -i           use interactively\n");
212     printf("  -c           disable BSD semantics\n");
213     printf("  -w N         wait N sec. before calling accept\n");
214     exit(1);
215 }
216 /*
217  * routine to handle echo for connection
218  */
219 void ServEcho(int sockfd) {
220     char buffer[MAXLINE];
221     int nread, nwrite;
222     char debug[MAXLINE+20];
223     /* main loop, reading 0 char means client close connection */
224     while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) {
225         if (nread < 0) {
226             PrintErr("Errore in lettura");
227             return;
228         }
229         nwrite = FullWrite(sockfd, buffer, nread);
230         if (nwrite) {
231             PrintErr("Errore in scrittura");
232             return;
233         }
234         if (debugging) {
235             buffer[nread] = 0;
236             snprintf(debug, MAXLINE+20, "Letti %d byte, %s", nread, buffer);
237             if (demonize) {          /* daemon mode */
238                 syslog(LOG_DEBUG, debug);
239             } else {
240                 printf("%s", debug);
241             }
242         }
243     }
244     return;
245 }
246 /*
247  * routine to print error on stout or syslog
248  */
249 void PrintErr(char * error) {
250     if (demonize) {                       /* daemon mode */
251         syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
252     } else {
253         perror(error);
254     }
255     return;
256 }