3 * Copyright (C) 2007 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 /****************************************************************
21 * Program countd: Elementary TCP server for teaching purpose, count
22 * number of request to the server itself, use POSIX semaphores and
25 * Author: Simone Piccardi
28 * Usage: countd -h give all info
30 ****************************************************************/
32 * Include needed headers
34 #include <sys/types.h> /* predefined types */
35 #include <unistd.h> /* include unix standard library */
36 #include <arpa/inet.h> /* IP addresses conversion utiliites */
37 #include <sys/socket.h> /* socket library */
38 #include <stdio.h> /* include standard I/O library */
41 #include <semaphore.h>
42 #include <syslog.h> /* syslog system functions */
43 #include <signal.h> /* signal functions */
44 #include <errno.h> /* error code */
45 #include <string.h> /* error strings */
52 int demonize = 1; /* daemon use option: default is daemon */
53 int debugging = 0; /* debug info printing option: default is no debug */
54 /* Subroutines declaration */
56 void ServEcho(int sockfd);
57 void PrintErr(char * error);
58 /* Program beginning */
59 int main(int argc, char *argv[])
62 * Variables definition
70 struct sockaddr_in cli_add;
72 char debug[MAXLINE], ipaddr[20];
74 * Input section: decode parameters passed in the calling
78 opterr = 0; /* don't want writing to stderr */
79 while ( (i = getopt(argc, argv, "hkrdicw:")) != -1) {
85 printf("Wrong -h option use\n");
105 waiting = strtol(optarg, NULL, 10);
107 case '?': /* unrecognized options */
108 printf("Unrecognized options -%c\n",optopt);
110 default: /* should not reached */
114 /* ***********************************************************
116 * Options processing completed
118 * Main code beginning
120 * ***********************************************************/
121 /* Main code begin here */
122 if (compat) { /* install signal handler */
123 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
125 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
127 /* create and bind socket */
128 if ( (list_fd = sockbindopt(argv[optind], "echo", 6,
129 SOCK_STREAM, reuse)) < 0) {
132 /* release privileges and go daemon */
133 if (setgid(65534) !=0) { /* first give away group privileges */
134 perror("cannot give away group privileges");
137 if (setuid(65534) !=0) { /* and only after user ... */
138 perror("cannot give away user privileges");
141 if (demonize) { /* go daemon */
142 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
143 if (daemon(0, 0) != 0) {
144 perror("cannot start as daemon");
149 if (listen(list_fd, BACKLOG) < 0 ) {
150 PrintErr("listen error");
153 if (waiting) sleep(waiting);
154 /* handle echo to client */
156 /* accept connection */
157 len = sizeof(cli_add);
158 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
159 < 0) && (errno == EINTR));
161 PrintErr("accept error");
165 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
166 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
168 syslog(LOG_DEBUG, debug);
173 /* fork to handle connection */
174 if ( (pid = fork()) < 0 ){
175 PrintErr("fork error");
178 if (pid == 0) { /* child */
179 close(list_fd); /* close listening socket */
180 if (keepalive) { /* enable keepalive ? */
181 setsockopt(conn_fd, SOL_SOCKET, SO_KEEPALIVE,
182 &keepalive, sizeof(keepalive));
184 ServEcho(conn_fd); /* handle echo */
186 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
188 syslog(LOG_DEBUG, debug);
194 } else { /* parent */
195 close(conn_fd); /* close connected socket */
198 /* normal exit, never reached */
202 * routine to print usage info and exit
205 printf("A counter server\n");
207 printf(" countd [-h] \n");
208 printf(" -h print this help\n");
209 printf(" -d write debug info\n");
210 printf(" -k enable SO_KEEPALIVE\n");
211 printf(" -r enable SO_REUSEADDR\n");
212 printf(" -i use interactively\n");
213 printf(" -c disable BSD semantics\n");
214 printf(" -w N wait N sec. before calling accept\n");
218 * routine to handle echo for connection
220 void ServEcho(int sockfd) {
221 char buffer[MAXLINE];
223 char debug[MAXLINE+20];
224 /* main loop, reading 0 char means client close connection */
225 while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) {
227 PrintErr("Errore in lettura");
230 nwrite = FullWrite(sockfd, buffer, nread);
232 PrintErr("Errore in scrittura");
237 snprintf(debug, MAXLINE+20, "Letti %d byte, %s", nread, buffer);
238 if (demonize) { /* daemon mode */
239 syslog(LOG_DEBUG, debug);
248 * routine to print error on stout or syslog
250 void PrintErr(char * error) {
251 if (demonize) { /* daemon mode */
252 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
264 int main(int argc, char *argv[])
267 * Variables definition
269 int list_fd, conn_fd;
271 struct sockaddr_in serv_add;
272 char buffer[MAXLINE];
275 * Input section: decode parameters passed in the calling
276 * Use getopt function
278 opterr = 0; /* don't want writing to stderr */
279 while ( (i = getopt(argc, argv, "h")) != -1) {
285 printf("Wrong -h option use\n");
289 case '?': /* unrecognized options */
290 printf("Unrecognized options -%c\n",optopt);
292 default: /* should not reached */
296 /* ***********************************************************
298 * Options processing completed
300 * Main code beginning
302 * ***********************************************************/
304 if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
305 perror("Socket creation error");
308 /* initialize address */
309 memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */
310 serv_add.sin_family = AF_INET; /* address type is INET */
311 serv_add.sin_port = htons(13); /* daytime port is 13 */
312 serv_add.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */
314 if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
315 perror("bind error");
318 /* listen on socket */
319 if (listen(list_fd, BACKLOG) < 0 ) {
320 perror("listen error");
323 /* write daytime to client */
325 if ( (conn_fd = accept(list_fd, (struct sockaddr *) NULL, NULL)) <0 ) {
326 perror("accept error");
329 timeval = time(NULL);
330 snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
331 if ( (write(conn_fd, buffer, strlen(buffer))) < 0 ) {
332 perror("write error");
342 * routine to print usage info and exit
345 printf("Simple daytime server\n");
347 printf(" daytimed [-h] \n");
348 printf(" -h print this help\n");