Subversion Repositories HelenOS

Rev

Rev 4717 | Rev 4731 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4716 mejdrech 1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup ping
30
 *  @{
31
 */
32
 
33
/** @file
34
 *  Ping application.
35
 */
36
 
37
#include <stdio.h>
38
#include <string.h>
39
#include <task.h>
40
#include <time.h>
41
#include <ipc/services.h>
42
 
43
#include "../../include/icmp_api.h"
44
#include "../../include/in.h"
4720 mejdrech 45
#include "../../include/in6.h"
4716 mejdrech 46
#include "../../include/inet.h"
47
#include "../../include/ip_codes.h"
4720 mejdrech 48
#include "../../include/socket_errno.h"
4716 mejdrech 49
 
50
#include "../../err.h"
51
 
52
#include "../parse.h"
4720 mejdrech 53
#include "../print_error.h"
4716 mejdrech 54
 
55
/** Echo module name.
56
 */
57
#define NAME    "Ping"
58
 
59
/** Module entry point.
60
 *  Reads command line parameters and pings.
61
 *  @param argc The number of command line parameters. Input parameter.
62
 *  @param argv The command line parameters. Input parameter.
63
 *  @returns EOK on success.
64
 */
65
int     main( int argc, char * argv[] );
66
 
67
/** Prints the application help.
68
 */
69
void    print_help( void );
70
 
71
/** Translates the character string to the address family number.
72
 *  @param name The address family name. Input parameter.
73
 *  @returns The corresponding address family number.
4720 mejdrech 74
 *  @returns EAFNOSUPPORTED if the address family is not supported.
4716 mejdrech 75
 */
4720 mejdrech 76
int     parse_address_family( const char * name );
4716 mejdrech 77
 
78
void 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" \
4720 mejdrech 91
        "\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
4716 mejdrech 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
 
113
int parse_address_family( const char * name ){
114
    if( str_lcmp( name, "AF_INET", 7 ) == 0 ){
115
        return AF_INET;
4720 mejdrech 116
    }else if( str_lcmp( name, "AF_INET6", 8 ) == 0 ){
117
        return AF_INET6;
4716 mejdrech 118
    }
4720 mejdrech 119
    return EAFNOSUPPORT;
4716 mejdrech 120
}
121
 
122
int main( int argc, char * argv[] ){
123
    ERROR_DECLARE;
124
 
125
    size_t              size            = 38;
126
    int                 verbose         = 0;
127
    int                 dont_fragment   = 0;
128
    ip_ttl_t            ttl             = 0;
129
    ip_tos_t            tos             = 0;
130
    int                 count           = 3;
131
    suseconds_t         timeout         = 3000;
4720 mejdrech 132
    int                 family          = AF_INET;
4716 mejdrech 133
 
4720 mejdrech 134
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
135
    uint8_t             address_data[ max_length ];
136
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
137
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
138
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
139
    socklen_t           addrlen;
140
    char                address_string[ INET6_ADDRSTRLEN ];
141
    uint8_t *           address_start;
4716 mejdrech 142
    int                 icmp_phone;
143
    struct timeval      time_before;
144
    struct timeval      time_after;
145
    int                 result;
146
    int                 value;
147
    int                 index;
148
 
149
    printf( "Task %d - ", task_get_id());
150
    printf( "%s\n", NAME );
151
 
152
    for( index = 1; index < argc - 1; ++ index ){
153
        if( argv[ index ][ 0 ] == '-' ){
154
            switch( argv[ index ][ 1 ] ){
155
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
156
                            break;
4720 mejdrech 157
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "address family", 0, parse_address_family ));
4716 mejdrech 158
                            break;
159
                case 'h':   print_help();
160
                            return EOK;
161
                            break;
162
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 ));
163
                            size = (value >= 0 ) ? ( size_t ) value : 0;
164
                            break;
165
                case 't':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "timeout", 0 ));
166
                            timeout = (value >= 0 ) ? ( suseconds_t ) value : 0;
167
                            break;
168
                case 'v':   verbose = 1;
169
                            break;
170
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
171
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
172
                            }else if( str_lcmp( argv[ index ] + 2, "dont_fragment", 13 ) == 0 ){
173
                                dont_fragment = 1;
174
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
4720 mejdrech 175
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "address family", 9, parse_address_family ));
4716 mejdrech 176
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
177
                                print_help();
178
                                return EOK;
179
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
180
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 7 ));
181
                                size = (value >= 0 ) ? ( size_t ) value : 0;
182
                            }else if( str_lcmp( argv[ index ] + 2, "timeout=", 8 ) == 0 ){
183
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "timeout", 7 ));
184
                                timeout = (value >= 0 ) ? ( suseconds_t ) value : 0;
185
                            }else if( str_lcmp( argv[ index ] + 2, "tos=", 4 ) == 0 ){
186
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "type of service", 7 ));
187
                                tos = (value >= 0 ) ? ( ip_tos_t ) value : 0;
188
                            }else if( str_lcmp( argv[ index ] + 2, "ttl=", 4 ) == 0 ){
189
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "time to live", 7 ));
190
                                ttl = (value >= 0 ) ? ( ip_ttl_t ) value : 0;
191
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
192
                                verbose = 1;
193
                            }else{
194
                                print_unrecognized( index, argv[ index ] + 2 );
195
                                print_help();
196
                                return EINVAL;
197
                            }
198
                            break;
199
                default:
200
                    print_unrecognized( index, argv[ index ] + 1 );
201
                    print_help();
202
                    return EINVAL;
203
            }
204
        }else{
205
            print_unrecognized( index, argv[ index ] );
206
            print_help();
207
            return EINVAL;
208
        }
209
    }
210
 
4720 mejdrech 211
    bzero( address_data, max_length );
212
    switch( family ){
213
        case AF_INET:
214
            address_in->sin_family = AF_INET;
215
            address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
216
            addrlen = sizeof( struct sockaddr_in );
217
            break;
218
        case AF_INET6:
219
            address_in6->sin6_family = AF_INET6;
220
            address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
221
            addrlen = sizeof( struct sockaddr_in6 );
222
            break;
223
        default:
224
            fprintf( stderr, "Protocol family is not supported\n" );
225
            return EAFNOSUPPORT;
226
    }
227
 
228
    if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){
4716 mejdrech 229
        fprintf( stderr, "Address parse error %d\n", ERROR_CODE );
230
        return ERROR_CODE;
231
    }
232
 
233
    icmp_phone = icmp_connect_module( SERVICE_ICMP );
234
    if( icmp_phone < 0 ){
235
        fprintf( stderr, "ICMP connect error %d\n", icmp_phone );
236
    }
237
 
238
    printf( "PING %d bytes of data\n", size );
4720 mejdrech 239
    if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
240
        fprintf( stderr, "Address error %d\n", ERROR_CODE );
241
    }else{
242
        printf( "Address %s:\n", address_string );
243
    }
4716 mejdrech 244
 
245
    while( count > 0 ){
246
        if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){
247
            fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
248
            return ERROR_CODE;
249
        }
4720 mejdrech 250
        result = icmp_echo_msg( icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen );
4716 mejdrech 251
        if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){
252
            fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
253
            return ERROR_CODE;
254
        }
255
        switch( result ){
256
            case ICMP_ECHO:
257
                printf( "Ping round trip time %d microseconds\n", tv_sub( & time_after, & time_before ));
258
                break;
4720 mejdrech 259
            case ETIMEOUT:
4716 mejdrech 260
                printf( "Timeouted.\n" );
261
                break;
262
            default:
4720 mejdrech 263
                print_error( stdout, result, NULL, "\n" );
4716 mejdrech 264
        }
265
        -- count;
266
    }
267
 
4717 mejdrech 268
    if( verbose ) printf( "Exiting\n" );
4716 mejdrech 269
 
270
    return EOK;
271
}
272
 
273
/** @}
274
 */