"Fossies" - the Fresh Open Source Software Archive

Member "libping-1.15/src/main.c" (26 Oct 2002, 7180 Bytes) of package /linux/privat/old/libping-1.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.c" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * DING
    3  *
    4  * Copyright (C) 2001 Jeffrey Fulmer <jdfulmer@armstrong.com>
    5  * This file is part of LIBPING
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; either version 2 of the License, or
   10  * (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20  *
   21  */ 
   22 #include <stdio.h>
   23 #include <stdlib.h>
   24 #include <ping.h>
   25 #include <getopt.h>
   26 
   27 #ifndef  EXIT_SUCCESS
   28 # define EXIT_SUCCESS 0
   29 #endif /*EXIT_SUCCESS*/
   30 
   31 #ifndef  EXIT_FAILURE
   32 # define EXIT_FAILURE 1
   33 #endif /*EXIT_FAILURE*/
   34 
   35 #define  TRUE         1
   36 #define  FALSE        0
   37 
   38 typedef enum
   39 {
   40   ECHO,
   41   SMTP,
   42   POP3,
   43   HTTP,
   44   PING
   45 } pingtype;
   46 
   47 extern char *version_string; 
   48 
   49 char *hostname;
   50 int nhosts;
   51 int bytes    = 2560;
   52 int script   = 0;
   53 int timeout  = 0;
   54 int RRT      = 0;
   55 pingtype pt  = PING;
   56 
   57 /**
   58  * long options, std options struct
   59  */
   60 static struct option long_options[] =
   61 {
   62   { "version",    no_argument,       NULL, 'V' },
   63   { "version",    no_argument,       NULL, 'v' },
   64   { "help",       no_argument,       NULL, 'h' },
   65   { "verbose",    no_argument,       NULL, 'v' },
   66   { "echo",       required_argument, NULL, 'e' },
   67   { "rtt",        no_argument,       NULL, 'r' },
   68   { "timeout",    required_argument, NULL, 't' },
   69   { "script",     no_argument,       NULL, 's' }, 
   70   { "mail",       no_argument,       NULL, 'm' }, 
   71   { "pop",        no_argument,       NULL, 'p' }, 
   72   { "www",        no_argument,       NULL, 'w' } 
   73 };
   74 
   75 void
   76 display_version( int i )
   77 {
   78   /**
   79    * version_string is defined in version.c
   80    * adding it to a separate file allows us
   81    * to parse it in configure.
   82    */
   83   printf( "ding %s\n", version_string );
   84   /**
   85    * if TRUE, exit
   86    */
   87   if( i == 1 ){ exit( EXIT_SUCCESS ); }
   88 }  /* end of display version */
   89 
   90 void
   91 display_help()
   92 {
   93   /**
   94    * call display_version, but do not exit
   95    */
   96   display_version( FALSE );
   97   printf("Usage: ding [options] hostname\n");
   98   printf("       ding [options] url (for -w/--www option)\n");
   99   printf("Options:\n"                    );
  100   puts("  -V, --version      VERSION, prints version number to screen.");
  101   puts("  -h, --help         HELP, prints this section.");
  102   puts("  -t, --timeout=NUM  TIMEOUT, seconds of inactivity before the connection" );
  103   puts("                     times out. Default timeout value is 5." );
  104   puts("  -r, --rtt          ROUND TRIP TIME, returns the rtt in milliseconds." );
  105   puts("  -e, --echo=NUM     ECHO, tcp echo NUM bytes to a network device;");
  106   puts("                     returns the round trip time in milliseconds." );
  107   puts("  -m, --mail         MAIL, ping a smtp mail server");
  108   puts("  -p, --pop          POP, ping a pop3 mail server");
  109   puts("  -w, --www          WWW, ping a http/https web server");
  110   puts("  -s, --script       BOOLEAN, prints the  output in \'script friendly\' format,");
  111   puts("                     i.e., 1 = host is alive, 0 = host is unreachable.  This" );
  112   puts("                     option does NOT print a newline \'\\n\' character so there" );
  113   puts("                     is no need to chomp it." );
  114   /**
  115    * our work is done, exit nicely
  116    */
  117   exit( EXIT_SUCCESS );
  118 }
  119 
  120 /**
  121  * parses command line arguments and assigns
  122  * values to run time variables. relies on GNU
  123  * getopts included with this distribution.
  124  */
  125 void
  126 parse_cmdline( int argc, char *argv[] )
  127 {
  128   int c = 0;
  129   while ((c = getopt_long (argc, argv, "Vvhe:srt:mpw", long_options, (int *)0)) != EOF){
  130   switch( c ){
  131       case 'V':
  132         display_version( TRUE );
  133         break;
  134       case 'v':
  135         display_version( TRUE );
  136         break;
  137       case 'h':
  138         display_help();
  139         exit( EXIT_SUCCESS );
  140       case 's':
  141         script = 1;
  142         break;
  143       case 't':
  144         timeout  = atoi( optarg );
  145         break;
  146       case 'r':
  147         RRT = 1;
  148         break;       
  149       case 'e':
  150         pt = ECHO;
  151         bytes = atoi( optarg );
  152         break;
  153       case 'm':
  154         pt = SMTP;
  155         break;
  156       case 'p':
  157         pt = POP3;
  158         break;
  159       case 'w':
  160         pt = HTTP;
  161         break;
  162     } /** end of switch( c )           **/
  163   }   /** end of while c = getopt_long **/
  164   nhosts   = argc - optind;
  165   hostname = argv[argc-1];
  166   if( !nhosts ){
  167     display_help();
  168   }
  169 }     /** end of parse_cmdline         **/
  170 
  171 int
  172 main( int argc, char *argv[] )
  173 {
  174   int test = 0;
  175   parse_cmdline( argc, argv );
  176 
  177   switch( pt ){
  178   case PING:
  179     if( timeout ){
  180       if( RRT )
  181         test = tpingthost( hostname, timeout ); 
  182       else
  183         test = pingthost( hostname, timeout );
  184     }
  185     else{
  186       if( RRT )
  187         test = tpinghost( hostname );
  188       else
  189         test = pinghost( hostname ); 
  190     }
  191     break;
  192   case ECHO:
  193     if( timeout ){
  194       if( RRT )
  195         test = techothost( hostname, bytes, timeout );
  196       else
  197         test = echothost ( hostname, bytes, timeout );
  198     }
  199     else{
  200       if( RRT )
  201         test = techohost( hostname, bytes );
  202       else
  203         test = echohost ( hostname, bytes );
  204     }
  205     break;
  206   case SMTP:
  207     if( timeout ){
  208       if( RRT )
  209         test = tpingtsmtp( hostname, timeout );
  210       else
  211         test = pingtsmtp ( hostname, timeout );
  212     }
  213     else{
  214       if( RRT )
  215         test = tpingsmtp( hostname );
  216       else
  217         test = pingsmtp ( hostname );
  218     }
  219     break;
  220   case POP3:
  221     if( timeout ){
  222       if( RRT )
  223         test = tpingtpop3( hostname, timeout );
  224       else
  225         test = pingtpop3 ( hostname, timeout );
  226     }
  227     else{
  228       if( RRT )
  229         test = tpingpop3( hostname );
  230       else
  231         test = pingpop3 ( hostname );
  232     }
  233     break; 
  234   case HTTP:
  235     if( timeout ){
  236       if( RRT )
  237         test = tpingthttp( hostname, timeout );
  238       else
  239         test = pingthttp ( hostname, timeout );
  240     }
  241     else{
  242       if( RRT )
  243         test = tpinghttp( hostname );
  244       else
  245         test = pinghttp ( hostname );
  246     }
  247     break; 
  248   default:
  249     break;
  250   }
  251 
  252   if( test  > 1 ){
  253     if( script ) 
  254       printf( "%d", test );
  255     else
  256       printf( "round trip time: %d milliseconds\n", test );
  257   }
  258   else if( test == 1 ){ 
  259     if( script )
  260       printf( "1" );
  261     else
  262       printf( "%s is alive.\n", hostname );
  263   }
  264   else if ( test == 0 ){
  265     if( script ) 
  266       printf( "0" );
  267     else
  268       printf( "0 connection timed out.\n" );
  269   }
  270   else if( test == -1 ){
  271     if( script ) 
  272       printf( "-1" );
  273     else
  274       printf( "-1 host or service is unreachable.\n" );
  275   }
  276   else if( test == -2 ){
  277     if( script ) 
  278       printf( "-2" );
  279     else
  280       printf( "-2 socket error, do you have permission?\n" );
  281   }
  282   else if( test == -3 ){
  283     if( script )
  284       printf( "-3" );
  285     else
  286       printf( "-3 connection refused\n" );
  287   } 
  288   
  289   exit( 0 );
  290 }