Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / TCP_echo.c
index f60c518233c4d2e399d9fcc7357c74319a5b571b..2d41e936f247cece2222a1795570be13b1bfd7d3 100644 (file)
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 piccardi Exp $
- *
  ****************************************************************/
 /* 
  * Include needed headers
  */
-#include <sys/types.h>   /* predefined types */
-#include <unistd.h>      /* include unix standard library */
-#include <arpa/inet.h>   /* IP addresses conversion utiliites */
-#include <sys/socket.h>  /* socket library */
-#include <stdio.h>      /* include standard I/O library */
-#include <errno.h>      /* include error codes */
-#include <string.h>     /* include erroro strings definitions */
+#include <sys/types.h>   /* primitive system data types */
+#include <unistd.h>      /* unix standard library */
+#include <arpa/inet.h>   /* IP addresses conversion utilities */
+#include <sys/socket.h>  /* socket constants, types and functions */
+#include <stdio.h>      /* standard I/O library */
+#include <errno.h>      /* error definitions and routines */
+#include <string.h>     /* C strings library */
+#include <stdlib.h>      /* C standard library */
+#include <netinet/tcp.h> /* TCP constants and types */
+
+/* still not defined in some include, because too new ... */
+#ifndef TCP_CONGESTION
+#define TCP_CONGESTION 13
+#endif
 
 #include "Gapil.h"
 #include "macros.h"
@@ -55,14 +60,17 @@ int main(int argc, char *argv[])
  * Variables definition  
  */
     int sock, i;
+    socklen_t len;
     int reset = 0;
+    int verbosity = 0;
+    char buffer[MAXLINE];
     struct linger ling;
     /*
      * Input section: decode parameters passed in the calling 
      * Use getopt function
      */
     opterr = 0;         /* don't want writing to stderr */
-    while ( (i = getopt(argc, argv, "hr")) != -1) {
+    while ( (i = getopt(argc, argv, "hrv")) != -1) {
        switch (i) {
        /* 
         * Handling options 
@@ -72,6 +80,9 @@ int main(int argc, char *argv[])
            usage();
            return(1);
            break;
+       case 'v':
+           verbosity = 1;
+           break;
        case 'r':
            reset = 1;
            break;
@@ -89,11 +100,21 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
-    /* call sockaddr to get a connected socket */
+    /* call sockconn to get a connected socket */
     if ( (sock = sockconn(argv[optind], "echo", 6, SOCK_STREAM)) < 0) {
        if (errno) perror("Socket creation error");
        return 1;
     }
+    /* print some info about the socket, used to test some TCP_* options */
+    if (verbosity) {
+       len = sizeof(buffer);
+       if (getsockopt(sock, SOL_TCP, TCP_CONGESTION, buffer, &len) < 0) {
+           perror("Cannot read congestion algorithm");
+       } else {
+           buffer[len]=0;
+           printf("Congestion algorithm %s\n", buffer);
+       }
+    }
     /* check if resetting on close is required */
     if (reset) {
        printf("Setting reset on close \n");