6b806ac944032fd763cfe948e959b8e1582c8f18
[gapil.git] / sources / TCP_echo.c
1 /* TCP_echo.c
2  * 
3  * Copyright (C) 2001-2003 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_echo.c
22  * Simple TCP client for echo service (port 7)
23  *
24  * Author: Simone Piccardi
25  * Jun. 2001
26  *
27  * Usage: echo -h give all info's
28  *
29  * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 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 <errno.h>       /* include error codes */
41 #include <string.h>      /* include erroro strings definitions */
42 #include <stdlib.h>
43
44 #include "Gapil.h"
45 #include "macros.h"
46
47 #define MAXLINE 256
48 void usage(void);
49 void ClientEcho(FILE * filein, int socket);
50 void SigTERM_hand(int sig);
51
52 /* Program begin */
53 int main(int argc, char *argv[])
54 {
55 /* 
56  * Variables definition  
57  */
58     int sock, i;
59     int reset = 0;
60     struct linger ling;
61     /*
62      * Input section: decode parameters passed in the calling 
63      * Use getopt function
64      */
65     opterr = 0;  /* don't want writing to stderr */
66     while ( (i = getopt(argc, argv, "hr")) != -1) {
67         switch (i) {
68         /* 
69          * Handling options 
70          */ 
71         case 'h':  
72             printf("Wrong -h option use\n");
73             usage();
74             return(1);
75             break;
76         case 'r':
77             reset = 1;
78             break;
79         case '?':   /* unrecognized options */
80             printf("Unrecognized options -%c\n",optopt);
81             usage();
82         default:    /* should not reached */
83             usage();
84         }
85     }
86     /* ***********************************************************
87      * 
88      *           Options processing completed
89      *
90      *                Main code beginning
91      * 
92      * ***********************************************************/
93     /* call sockaddr to get a connected socket */
94     if ( (sock = sockconn(argv[optind], "echo", 6, SOCK_STREAM)) < 0) {
95         if (errno) perror("Socket creation error");
96         return 1;
97     }
98     /* check if resetting on close is required */
99     if (reset) {
100         printf("Setting reset on close \n");
101         ling.l_onoff = 1;
102         ling.l_linger = 0;      
103         if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))) {
104             perror("Cannot set linger");
105             exit(1);
106         }
107     }
108     /* do read/write operations */
109     ClientEcho(stdin, sock);
110     /* normal exit */
111     return 0;
112 }
113 /*
114  * routine to print usage info and exit 
115  */
116 void usage(void) {
117     printf("Take daytime from a remote host \n");
118     printf("Usage:\n");
119     printf("  daytime [-h] [-v] [host in dotted decimal form] \n");
120 //    printf("  -v         set verbosity on\n");
121     printf("  -r           require reset on closing\n");
122     printf("  -h           print this help\n");
123     exit(1);
124 }
125
126 void ClientEcho(FILE * filein, int socket) 
127 {
128     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
129     int nread, nwrite; 
130     int maxfd;
131     fd_set fset;
132     int eof = 0;
133     /* initialize file descriptor set */
134     FD_ZERO(&fset);
135     maxfd = max(fileno(filein), socket) + 1;
136     while (1) {
137         FD_SET(socket, &fset);         /* set for the socket */
138         if (eof == 0) {
139             FD_SET(fileno(filein), &fset); /* set for the standard input */
140         }
141         select(maxfd, &fset, NULL, NULL, NULL); /* wait for read ready */
142         if (FD_ISSET(fileno(filein), &fset)) {  /* if ready on stdin */
143             if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */
144                 eof = 1;               /* EOF on input */
145                 shutdown(socket, SHUT_WR);      /* close write half */
146                 FD_CLR(fileno(filein), &fset);  /* no more interest on stdin */
147             } else {                   /* else we have to write to socket */
148                 nwrite = FullWrite(socket, sendbuff, strlen(sendbuff)); 
149                 if (nwrite < 0) {      /* on error stop */
150                     printf("Errore in scrittura: %s", strerror(errno));
151                     return;
152                 }
153             }
154         }
155         if (FD_ISSET(socket, &fset)) { /* if ready on socket */ 
156             nread = read(socket, recvbuff, strlen(sendbuff)); /* do read */
157             if (nread < 0) {  /* error condition, stop client */
158                 printf("Errore in lettura: %s\n", strerror(errno));
159                 return;
160             }
161             if (nread == 0) { /* server closed connection, stop */
162                 if (eof == 1) {
163                     return;
164                 } else {
165                     printf("EOF prematuro sul socket\n");
166                     return;
167                 }
168             }
169             recvbuff[nread] = 0;   /* else read is ok, write on stdout */
170             if (fputs(recvbuff, stdout) == EOF) {
171                 perror("Errore in scrittura su terminale");
172                 return;
173             }
174         }
175     }
176 }