Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
4603 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 echo
30
 *  @{
31
 */
32
 
33
/** @file
4704 mejdrech 34
 *  Echo application.
35
 *  Answers received packets.
4603 mejdrech 36
 */
37
 
38
#include <malloc.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <task.h>
42
 
43
#include "../../include/in.h"
4720 mejdrech 44
#include "../../include/in6.h"
4603 mejdrech 45
#include "../../include/inet.h"
46
#include "../../include/socket.h"
47
 
48
#include "../../err.h"
49
 
4709 mejdrech 50
#include "../parse.h"
4720 mejdrech 51
#include "../print_error.h"
4709 mejdrech 52
 
4603 mejdrech 53
/** Echo module name.
54
 */
55
#define NAME    "Echo"
56
 
57
/** Module entry point.
58
 *  Reads command line parameters and starts listenning.
59
 *  @param argc The number of command line parameters. Input parameter.
60
 *  @param argv The command line parameters. Input parameter.
61
 *  @returns EOK on success.
62
 */
63
int     main( int argc, char * argv[] );
64
 
65
/** Prints the application help.
66
 */
67
void    print_help( void );
68
 
69
/** Translates the character string to the protocol family number.
4699 mejdrech 70
 *  @param name The protocol family name. Input parameter.
4603 mejdrech 71
 *  @returns The corresponding protocol family number.
4720 mejdrech 72
 *  @returns EPFNOSUPPORTED if the protocol family is not supported.
4603 mejdrech 73
 */
4720 mejdrech 74
int     parse_protocol_family( const char * name );
4603 mejdrech 75
 
76
/** Translates the character string to the socket type number.
4699 mejdrech 77
 *  @param name The socket type name. Input parameter.
4603 mejdrech 78
 *  @returns The corresponding socket type number.
4720 mejdrech 79
 *  @returns ESOCKNOSUPPORTED if the socket type is not supported.
4603 mejdrech 80
 */
4720 mejdrech 81
int     parse_socket_type( const char * name );
4603 mejdrech 82
 
83
void print_help( void ){
84
    printf(
85
        "Network Echo aplication\n" \
86
        "Usage: echo [options]\n" \
87
        "Where options are:\n" \
4709 mejdrech 88
        "-c count | --count=count\n" \
4603 mejdrech 89
        "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
90
        "\n" \
91
        "-f protocol_family | --family=protocol_family\n" \
4720 mejdrech 92
        "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
4603 mejdrech 93
        "\n" \
94
        "-h | --help\n" \
95
        "\tShow this application help.\n"
96
        "\n" \
4709 mejdrech 97
        "-p port_number | --port=port_number\n" \
98
        "\tThe port number the application should listen at. The default is 7.\n" \
99
        "\n" \
100
        "-r reply_string | --reply=reply_string\n" \
101
        "\tThe constant reply string. The default is the original data received.\n" \
102
        "\n" \
103
        "-s receive_size | --size=receive_size\n" \
104
        "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
105
        "\n" \
4603 mejdrech 106
        "-t socket_type | --type=socket_type\n" \
107
        "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \
108
        "\n" \
109
        "-v | --verbose\n" \
110
        "\tShow all output messages.\n"
111
    );
112
}
113
 
114
int parse_protocol_family( const char * name ){
115
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
116
        return PF_INET;
4720 mejdrech 117
    }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
118
        return PF_INET6;
4603 mejdrech 119
    }
4720 mejdrech 120
    return EPFNOSUPPORT;
4603 mejdrech 121
}
122
 
123
int parse_socket_type( const char * name ){
124
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
125
        return SOCK_DGRAM;
126
    }
4720 mejdrech 127
    return ESOCKTNOSUPPORT;
4603 mejdrech 128
}
129
 
130
int main( int argc, char * argv[] ){
131
    ERROR_DECLARE;
132
 
4708 mejdrech 133
    size_t              size            = 1024;
4603 mejdrech 134
    int                 verbose         = 0;
135
    char *              reply           = NULL;
136
    sock_type_t         type            = SOCK_DGRAM;
137
    int                 count           = -1;
4709 mejdrech 138
    int                 family          = PF_INET;
4720 mejdrech 139
    uint16_t            port            = 7;
4603 mejdrech 140
 
4720 mejdrech 141
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
142
    uint8_t             address_data[ max_length ];
143
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
144
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
145
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
146
    socklen_t           addrlen;
147
    char                address_string[ INET6_ADDRSTRLEN ];
148
    uint8_t *           address_start;
4603 mejdrech 149
    int                 socket_id;
150
    char *              data;
4708 mejdrech 151
    size_t              length;
4603 mejdrech 152
    int                 index;
153
    size_t              reply_length;
154
    int                 value;
155
 
156
    printf( "Task %d - ", task_get_id());
157
    printf( "%s\n", NAME );
158
 
159
    for( index = 1; index < argc; ++ index ){
160
        if( argv[ index ][ 0 ] == '-' ){
161
            switch( argv[ index ][ 1 ] ){
162
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
163
                            break;
4720 mejdrech 164
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
4603 mejdrech 165
                            break;
166
                case 'h':   print_help();
167
                            return EOK;
168
                            break;
169
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
4720 mejdrech 170
                            port = ( uint16_t ) value;
4603 mejdrech 171
                            break;
172
                case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
173
                            break;
4708 mejdrech 174
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 ));
175
                            size = (value >= 0 ) ? ( size_t ) value : 0;
4603 mejdrech 176
                            break;
4708 mejdrech 177
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 0, parse_socket_type ));
178
                            type = ( sock_type_t ) value;
4603 mejdrech 179
                            break;
180
                case 'v':   verbose = 1;
181
                            break;
182
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
183
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
184
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
4720 mejdrech 185
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
4603 mejdrech 186
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
187
                                print_help();
188
                                return EOK;
189
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
190
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
4720 mejdrech 191
                                port = ( uint16_t ) value;
4603 mejdrech 192
                            }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
193
                                ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
194
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
4708 mejdrech 195
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 ));
196
                                size = (value >= 0 ) ? ( size_t ) value : 0;
4603 mejdrech 197
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
4708 mejdrech 198
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 7, parse_socket_type ));
199
                                type = ( sock_type_t ) value;
4603 mejdrech 200
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
201
                                verbose = 1;
202
                            }else{
203
                                print_unrecognized( index, argv[ index ] + 2 );
4709 mejdrech 204
                                print_help();
4603 mejdrech 205
                                return EINVAL;
206
                            }
207
                            break;
208
                default:
209
                    print_unrecognized( index, argv[ index ] + 1 );
4709 mejdrech 210
                    print_help();
4603 mejdrech 211
                    return EINVAL;
212
            }
213
        }else{
214
            print_unrecognized( index, argv[ index ] );
4709 mejdrech 215
            print_help();
4603 mejdrech 216
            return EINVAL;
217
        }
218
    }
219
 
220
    if( size <= 0 ){
221
        fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
222
        size = 1024;
223
    }
224
    data = ( char * ) malloc( size + 1 );
225
    if( ! data ){
226
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
227
        return ENOMEM;
228
    }
229
 
230
    reply_length = reply ? str_length( reply ) : 0;
231
 
4709 mejdrech 232
    socket_id = socket( family, type, 0 );
4603 mejdrech 233
    if( socket_id < 0 ){
234
        fprintf( stderr, "Socket create error %d\n", socket_id );
235
        return socket_id;
236
    }
4720 mejdrech 237
 
238
    bzero( address_data, max_length );
239
    switch( family ){
240
        case PF_INET:
241
            address_in->sin_family = AF_INET;
242
            address_in->sin_port = port;
243
            addrlen = sizeof( struct sockaddr_in );
244
            break;
245
        case PF_INET6:
246
            address_in6->sin6_family = AF_INET6;
247
            address_in6->sin6_port = port;
248
            addrlen = sizeof( struct sockaddr_in6 );
249
            break;
250
        default:
251
            fprintf( stderr, "Protocol family is not supported\n" );
252
            return EAFNOSUPPORT;
253
    }
254
    if( ERROR_OCCURRED( bind( socket_id, address, addrlen ))){
255
        socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
4603 mejdrech 256
        return ERROR_CODE;
257
    }
258
 
4720 mejdrech 259
    if( verbose ) printf( "Listenning at %d\n", port );
4603 mejdrech 260
 
261
    while( count ){
4720 mejdrech 262
        addrlen = max_length;
263
        value = recvfrom( socket_id, data, size, 0, address, & addrlen );
4708 mejdrech 264
        if( value < 0 ){
4720 mejdrech 265
            socket_print_error( stderr, value, "Socket receive: ", "\n" );
4603 mejdrech 266
        }else{
4708 mejdrech 267
            length = ( size_t ) value;
4603 mejdrech 268
            if( verbose ){
4720 mejdrech 269
                address_start = NULL;
270
                switch( address->sa_family ){
271
                    case AF_INET:
272
                        port = address_in->sin_port;
273
                        address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
274
                        break;
275
                    case AF_INET6:
276
                        port = address_in6->sin6_port;
277
                        address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
278
                        break;
279
                    default:
280
                        fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family );
4603 mejdrech 281
                }
4720 mejdrech 282
                if( address_start ){
283
                    if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
284
                        fprintf( stderr, "Received address error %d\n", ERROR_CODE );
285
                    }else{
286
                        data[ length ] = '\0';
287
                        printf( "Received from %s:%d\n%s\n", address_string, port, data );
288
                    }
289
                }
4603 mejdrech 290
            }
4720 mejdrech 291
            if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){
292
                socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
4603 mejdrech 293
            }
294
        }
4700 mejdrech 295
        if( count > 0 ){
296
            -- count;
297
            if( verbose ) printf( "Waiting for next %d packet(s)\n", count );
298
        }
4603 mejdrech 299
    }
300
 
301
    if( verbose ) printf( "Closing the socket\n" );
302
 
303
    if( ERROR_OCCURRED( closesocket( socket_id ))){
4720 mejdrech 304
        socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
4603 mejdrech 305
        return ERROR_CODE;
306
    }
307
 
308
    if( verbose ) printf( "Exiting\n" );
309
 
310
    return EOK;
311
}
312
 
313
/** @}
314
 */