/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup ping
* @{
*/
/** @file
* Ping application.
*/
#include <stdio.h>
#include <string.h>
#include <task.h>
#include <time.h>
#include <ipc/services.h>
#include "../../include/icmp_api.h"
#include "../../include/in.h"
#include "../../include/inet.h"
#include "../../include/ip_codes.h"
#include "../../include/socket_codes.h"
#include "../../err.h"
#include "../parse.h"
/** Echo module name.
*/
#define NAME "Ping"
/** Module entry point.
* Reads command line parameters and pings.
* @param argc The number of command line parameters. Input parameter.
* @param argv The command line parameters. Input parameter.
* @returns EOK on success.
*/
int main( int argc, char * argv[] );
/** Prints the application help.
*/
void print_help( void );
/** Translates the character string to the address family number.
* @param name The address family name. Input parameter.
* @returns The corresponding address family number.
*/
int parse_address_family( const char * name );
void print_help( void ){
"Network Ping aplication\n" \
"Usage: ping [options] numeric_address\n" \
"Where options are:\n" \
"\n" \
"-c request_count | --count=request_count\n" \
"\tThe number of packets the application sends. The default is three (3).\n" \
"\n" \
"--dont_fragment\n" \
"\tDisable packet fragmentation.\n"
"\n" \
"-f address_family | --family=address_family\n" \
"\tThe given address family. Only the AF_INET is supported.\n"
"\n" \
"-h | --help\n" \
"\tShow this application help.\n"
"\n" \
"-s packet_size | --size=packet_size\n" \
"\tThe packet data size the application sends. The default is 38 bytes.\n" \
"\n" \
"-t timeout | --timeout=timeout\n" \
"\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \
"\n" \
"--tos=tos\n" \
"\tThe type of service to be used.\n" \
"\n" \
"--ttl=ttl\n" \
"\tThe time to live to be used.\n" \
"\n" \
"-v | --verbose\n" \
"\tShow all output messages.\n"
);
}
int parse_address_family( const char * name ){
if( str_lcmp( name, "AF_INET", 7 ) == 0 ){
return AF_INET;
}
return ENOENT;
}
int main( int argc, char * argv[] ){
ERROR_DECLARE;
size_t size = 38;
int verbose = 0;
int dont_fragment = 0;
ip_ttl_t ttl = 0;
ip_tos_t tos = 0;
int count = 3;
suseconds_t timeout = 3000;
struct sockaddr_in address = { .sin_family = AF_INET, .sin_port = 7 };
int icmp_phone;
struct timeval time_before;
struct timeval time_after;
int result;
int value;
int index;
printf( "Task %d - ", task_get_id
());
for( index = 1; index < argc - 1; ++ index ){
if( argv[ index ][ 0 ] == '-' ){
switch( argv[ index ][ 1 ] ){
case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
break;
case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "address family", 0, parse_address_family ));
address.sin_family = ( uint16_t ) value;
break;
case 'h': print_help();
return EOK;
break;
case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 ));
size = (value >= 0 ) ? ( size_t ) value : 0;
break;
case 't': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "timeout", 0 ));
timeout = (value >= 0 ) ? ( suseconds_t ) value : 0;
break;
case 'v': verbose = 1;
break;
case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
}else if( str_lcmp( argv[ index ] + 2, "dont_fragment", 13 ) == 0 ){
dont_fragment = 1;
}else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "address family", 9, parse_address_family ));
address.sin_family = ( uint16_t ) value;
}else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
print_help();
return EOK;
}else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 7 ));
size = (value >= 0 ) ? ( size_t ) value : 0;
}else if( str_lcmp( argv[ index ] + 2, "timeout=", 8 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "timeout", 7 ));
timeout = (value >= 0 ) ? ( suseconds_t ) value : 0;
}else if( str_lcmp( argv[ index ] + 2, "tos=", 4 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "type of service", 7 ));
tos = (value >= 0 ) ? ( ip_tos_t ) value : 0;
}else if( str_lcmp( argv[ index ] + 2, "ttl=", 4 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "time to live", 7 ));
ttl = (value >= 0 ) ? ( ip_ttl_t ) value : 0;
}else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
verbose = 1;
}else{
print_unrecognized( index, argv[ index ] + 2 );
print_help();
return EINVAL;
}
break;
default:
print_unrecognized( index, argv[ index ] + 1 );
print_help();
return EINVAL;
}
}else{
print_unrecognized( index, argv[ index ] );
print_help();
return EINVAL;
}
}
if( ERROR_OCCURRED( inet_pton( address.sin_family, argv[ argc - 1 ], ( uint8_t * ) & address.sin_addr ))){
fprintf( stderr
, "Address parse error %d\n", ERROR_CODE
);
return ERROR_CODE;
}
icmp_phone = icmp_connect_module( SERVICE_ICMP );
if( icmp_phone < 0 ){
fprintf( stderr
, "ICMP connect error %d\n", icmp_phone
);
}
printf( "PING %d bytes of data\n", size
);
while( count > 0 ){
if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){
fprintf( stderr
, "Get time of day error %d\n", ERROR_CODE
);
return ERROR_CODE;
}
result = icmp_echo_msg( icmp_phone, size, timeout, ttl, tos, dont_fragment, ( struct sockaddr * ) & address, sizeof( address ));
if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){
fprintf( stderr
, "Get time of day error %d\n", ERROR_CODE
);
return ERROR_CODE;
}
switch( result ){
case ICMP_ECHO:
printf( "Ping round trip time %d microseconds\n", tv_sub
( & time_after
, & time_before
));
break;
case ELIMIT:
break;
default:
printf( "Error %d.\n", result
);
}
-- count;
}
if( verbose
) printf( "Exiting\n" );
return EOK;
}
/** @}
*/