Corretti i commenti ai listati in una forma piu' leggibile (spero).
[gapil.git] / sources / TCP_cunc_daytimed.c
1 /* TCP_cunc_daytimed.c
2  * 
3  * Copyright (C) 2001 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 TCP_cunc_daytimed.c
22  * Elementary TCP cuncurrent server for daytime service (port 13)
23  *
24  * Author: Simone Piccardi
25  * May. 2001
26  *
27  * Usage: daytimed -h give all info
28  *
29  * $Id: TCP_cunc_daytimed.c,v 1.1 2003/04/29 15:33:39 piccardi Exp $
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
42 #define MAXLINE 80
43 #define BACKLOG 10
44 /* Program begin */
45 void usage(void);
46 int main(int argc, char *argv[])
47 {
48 /* 
49  * Variables definition  
50  */
51     int list_fd, conn_fd;
52     int i;
53     struct sockaddr_in serv_add, client;
54     char buffer[MAXLINE];
55     socklen_t len;
56     time_t timeval;
57     pid_t pid;
58     int logging=0;
59     /*
60      * Input section: decode parameters passed in the calling 
61      * Use getopt function
62      */
63     opterr = 0;  /* don't want writing to stderr */
64     while ( (i = getopt(argc, argv, "hv")) != -1) {
65         switch (i) {
66         /* 
67          * Handling options 
68          */ 
69         case 'h':  
70             printf("Wrong -h option use\n");
71             usage();
72             return(0);
73             break;
74         case '?':   /* unrecognized options */
75             printf("Unrecognized options -%c\n",optopt);
76             usage();
77             return(0);
78             break;
79         case 'v':
80             logging = 1;
81             break;
82         default:    /* should not reached */
83             usage();
84             return(0);
85         }
86     }
87     /* ***********************************************************
88      * 
89      *           Options processing completed
90      *
91      *                Main code beginning
92      * 
93      * ***********************************************************/
94     /* create socket */
95     if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
96         perror("Socket creation error");
97         exit(-1);
98     }
99     /* initialize address */
100     memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */
101     serv_add.sin_family = AF_INET;                  /* address type is INET */
102     serv_add.sin_port = htons(13);                  /* daytime port is 13 */
103     serv_add.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
104     /* bind socket */
105     if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
106         perror("bind error");
107         exit(-1);
108     }
109     /* listen on socket */
110     if (listen(list_fd, BACKLOG) < 0 ) {
111         perror("listen error");
112         exit(-1);
113     }
114     /* write daytime to client */
115     while (1) {
116         if ( (conn_fd = accept(list_fd, (struct sockaddr *)&client, &len)) 
117              <0 ) {
118             perror("accept error");
119             exit(-1);
120         }
121         /* fork to handle connection */
122         if ( (pid = fork()) < 0 ){
123             perror("fork error");
124             exit(-1);
125         }
126         if (pid == 0) {                 /* child */
127             close(list_fd);
128             timeval = time(NULL);
129             snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
130             if ( (write(conn_fd, buffer, strlen(buffer))) < 0 ) {
131                 perror("write error");
132                 exit(-1);
133             }
134             if (logging) {
135                 inet_ntop(AF_INET, &client.sin_addr, buffer, sizeof(buffer));
136                 printf("Request from host %s, port %d\n", buffer,
137                        ntohs(client.sin_port));
138             }
139             close(conn_fd);
140             exit(0);
141         } else {                        /* parent */
142             close(conn_fd);
143         }
144     }
145     /* normal exit, never reached */
146     exit(0);
147 }
148 /*
149  * routine to print usage info and exit
150  */
151 void usage(void) {
152     printf("Simple daytime server\n");
153     printf("Usage:\n");
154     printf("  daytimed [-hv] \n");
155     printf("  -h           print this help\n");
156     printf("  -v           print request source on stdout\n");
157     exit(1);
158 }
159