Subversion Repositories HelenOS

Rev

Rev 4717 | Rev 4731 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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