Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/app/ping/ping.c

    r3be62bc raadf01e  
    3939#include <task.h>
    4040#include <time.h>
    41 #include <ipc/ipc.h>
    4241#include <ipc/services.h>
    4342
     
    6867/** Prints the application help.
    6968 */
    70 void ping_print_help(void);
     69void print_help(void);
     70
     71/** Translates the character string to the address family number.
     72 *  @param[in] name The address family name.
     73 *  @returns The corresponding address family number.
     74 *  @returns EAFNOSUPPORTED if the address family is not supported.
     75 */
     76int parse_address_family(const char * name);
     77
     78void print_help(void){
     79        printf(
     80                "Network Ping aplication\n" \
     81                "Usage: ping [options] numeric_address\n" \
     82                "Where options are:\n" \
     83                "\n" \
     84                "-c request_count | --count=request_count\n" \
     85                "\tThe number of packets the application sends. The default is three (3).\n" \
     86                "\n" \
     87                "--dont_fragment\n" \
     88                "\tDisable packet fragmentation.\n"
     89                "\n" \
     90                "-f address_family | --family=address_family\n" \
     91                "\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
     92                "\n" \
     93                "-h | --help\n" \
     94                "\tShow this application help.\n"
     95                "\n" \
     96                "-s packet_size | --size=packet_size\n" \
     97                "\tThe packet data size the application sends. The default is 38 bytes.\n" \
     98                "\n" \
     99                "-t timeout | --timeout=timeout\n" \
     100                "\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \
     101                "\n" \
     102                "--tos=tos\n" \
     103                "\tThe type of service to be used.\n" \
     104                "\n" \
     105                "--ttl=ttl\n" \
     106                "\tThe time to live to be used.\n" \
     107                "\n" \
     108                "-v | --verbose\n" \
     109                "\tShow all output messages.\n"
     110        );
     111}
     112
     113int parse_address_family(const char * name){
     114        if(str_lcmp(name, "AF_INET", 7) == 0){
     115                return AF_INET;
     116        }else if(str_lcmp(name, "AF_INET6", 8) == 0){
     117                return AF_INET6;
     118        }
     119        return EAFNOSUPPORT;
     120}
    71121
    72122int main(int argc, char * argv[]){
     
    76126        int verbose                     = 0;
    77127        int dont_fragment       = 0;
    78         ip_ttl_t ttl            = 0;
    79         ip_tos_t tos            = 0;
     128        ip_ttl_t ttl                            = 0;
     129        ip_tos_t tos                            = 0;
    80130        int count                       = 3;
    81         suseconds_t timeout     = 3000;
     131        suseconds_t timeout                     = 3000;
    82132        int family                      = AF_INET;
    83133
    84         socklen_t max_length                            = sizeof(struct sockaddr_in6);
     134        socklen_t max_length            = sizeof(struct sockaddr_in6);
    85135        uint8_t address_data[max_length];
    86         struct sockaddr * address                       = (struct sockaddr *) address_data;
     136        struct sockaddr * address               = (struct sockaddr *) address_data;
    87137        struct sockaddr_in * address_in         = (struct sockaddr_in *) address;
    88138        struct sockaddr_in6 * address_in6       = (struct sockaddr_in6 *) address;
     
    97147        int index;
    98148
    99         // print the program label
    100149        printf("Task %d - ", task_get_id());
    101150        printf("%s\n", NAME);
    102151
    103         // parse the command line arguments
    104         // stop before the last argument if it does not start with the minus sign ('-')
    105         for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){
    106                 // options should start with the minus sign ('-')
     152        if(argc <= 1){
     153                print_help();
     154                return EINVAL;
     155        }
     156
     157        for(index = 1; (index < argc - 1) || ((index == argc) && (argv[index][0] == '-')); ++ index){
    107158                if(argv[index][0] == '-'){
    108159                        switch(argv[index][1]){
    109                                 // short options with only one letter
    110160                                case 'c':
    111161                                        ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "count", 0));
     
    115165                                        break;
    116166                                case 'h':
    117                                         ping_print_help();
     167                                        print_help();
    118168                                        return EOK;
    119169                                        break;
     
    129179                                        verbose = 1;
    130180                                        break;
    131                                 // long options with the double minus sign ('-')
    132181                                case '-':
    133182                                        if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
     
    138187                                                ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "address family", 9, parse_address_family));
    139188                                        }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
    140                                                 ping_print_help();
     189                                                print_help();
    141190                                                return EOK;
    142191                                        }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
     
    156205                                        }else{
    157206                                                print_unrecognized(index, argv[index] + 2);
    158                                                 ping_print_help();
     207                                                print_help();
    159208                                                return EINVAL;
    160209                                        }
     
    162211                                default:
    163212                                        print_unrecognized(index, argv[index] + 1);
    164                                         ping_print_help();
     213                                        print_help();
    165214                                        return EINVAL;
    166215                        }
    167216                }else{
    168217                        print_unrecognized(index, argv[index]);
    169                         ping_print_help();
     218                        print_help();
    170219                        return EINVAL;
    171220                }
    172221        }
    173222
    174         // if not before the last argument containing the address
    175         if(index >= argc){
    176                 printf("Command line error: missing address\n");
    177                 ping_print_help();
    178                 return EINVAL;
    179         }
    180 
    181         // prepare the address buffer
    182223        bzero(address_data, max_length);
    183224        switch(family){
     
    197238        }
    198239
    199         // parse the last argument which should contain the address
    200240        if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){
    201241                fprintf(stderr, "Address parse error %d\n", ERROR_CODE);
     
    203243        }
    204244
    205         // connect to the ICMP module
    206245        icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
    207246        if(icmp_phone < 0){
     
    210249        }
    211250
    212         // print the ping header
    213251        printf("PING %d bytes of data\n", size);
    214252        if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
     
    218256        }
    219257
    220         // do count times
    221258        while(count > 0){
    222 
    223                 // get the starting time
    224259                if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){
    225260                        fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    226                         // release the ICMP phone
    227                         ipc_hangup(icmp_phone);
    228261                        return ERROR_CODE;
    229262                }
    230 
    231                 // request the ping
    232263                result = icmp_echo_msg(icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen);
    233 
    234                 // get the ending time
    235264                if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){
    236265                        fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    237                         // release the ICMP phone
    238                         ipc_hangup(icmp_phone);
    239266                        return ERROR_CODE;
    240267                }
    241 
    242                 // print the result
    243268                switch(result){
    244269                        case ICMP_ECHO:
     
    258283        }
    259284
    260         // release the ICMP phone
    261         ipc_hangup(icmp_phone);
    262 
    263285        return EOK;
    264286}
    265287
    266 void ping_print_help(void){
    267         printf(
    268                 "Network Ping aplication\n" \
    269                 "Usage: ping [options] numeric_address\n" \
    270                 "Where options are:\n" \
    271                 "\n" \
    272                 "-c request_count | --count=request_count\n" \
    273                 "\tThe number of packets the application sends. The default is three (3).\n" \
    274                 "\n" \
    275                 "--dont_fragment\n" \
    276                 "\tDisable packet fragmentation.\n"
    277                 "\n" \
    278                 "-f address_family | --family=address_family\n" \
    279                 "\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
    280                 "\n" \
    281                 "-h | --help\n" \
    282                 "\tShow this application help.\n"
    283                 "\n" \
    284                 "-s packet_size | --size=packet_size\n" \
    285                 "\tThe packet data size the application sends. The default is 38 bytes.\n" \
    286                 "\n" \
    287                 "-t timeout | --timeout=timeout\n" \
    288                 "\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \
    289                 "\n" \
    290                 "--tos=tos\n" \
    291                 "\tThe type of service to be used.\n" \
    292                 "\n" \
    293                 "--ttl=ttl\n" \
    294                 "\tThe time to live to be used.\n" \
    295                 "\n" \
    296                 "-v | --verbose\n" \
    297                 "\tShow all output messages.\n"
    298         );
    299 }
    300 
    301288/** @}
    302289 */
Note: See TracChangeset for help on using the changeset viewer.