Subversion Repositories HelenOS

Rev

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

Rev 4699 Rev 4700
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 echo
29
/** @addtogroup echo
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <malloc.h>
36
#include <malloc.h>
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
 
40
 
41
#include "../../include/in.h"
41
#include "../../include/in.h"
42
#include "../../include/inet.h"
42
#include "../../include/inet.h"
43
#include "../../include/socket.h"
43
#include "../../include/socket.h"
44
 
44
 
45
#include "../../err.h"
45
#include "../../err.h"
46
 
46
 
47
/** Echo module name.
47
/** Echo module name.
48
 */
48
 */
49
#define NAME    "Echo"
49
#define NAME    "Echo"
50
 
50
 
51
/** Module entry point.
51
/** Module entry point.
52
 *  Reads command line parameters and starts listenning.
52
 *  Reads command line parameters and starts listenning.
53
 *  @param argc The number of command line parameters. Input parameter.
53
 *  @param argc The number of command line parameters. Input parameter.
54
 *  @param argv The command line parameters. Input parameter.
54
 *  @param argv The command line parameters. Input parameter.
55
 *  @returns EOK on success.
55
 *  @returns EOK on success.
56
 */
56
 */
57
int     main( int argc, char * argv[] );
57
int     main( int argc, char * argv[] );
58
 
58
 
59
/** Prints the application help.
59
/** Prints the application help.
60
 */
60
 */
61
void    print_help( void );
61
void    print_help( void );
62
 
62
 
63
/** Prints the parameter unrecognized message and the application help.
63
/** Prints the parameter unrecognized message and the application help.
64
 *  @param index The index of the parameter. Input parameter.
64
 *  @param index The index of the parameter. Input parameter.
65
 *  @param parameter The parameter name. Input parameter.
65
 *  @param parameter The parameter name. Input parameter.
66
 */
66
 */
67
void    print_unrecognized( int index, const char * parameter );
67
void    print_unrecognized( int index, const char * parameter );
68
 
68
 
69
/** Parses the next parameter as an integral number.
69
/** Parses the next parameter as an integral number.
70
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
70
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
71
 *  @param argc The total number of the parameters. Input parameter.
71
 *  @param argc The total number of the parameters. Input parameter.
72
 *  @param argv The parameters. Input parameter.
72
 *  @param argv The parameters. Input parameter.
73
 *  @param index The actual parameter index. Input/output parameter.
73
 *  @param index The actual parameter index. Input/output parameter.
74
 *  @param value The parsed parameter value. Output parameter.
74
 *  @param value The parsed parameter value. Output parameter.
75
 *  @param name The parameter name to be printed on errors. Input parameter.
75
 *  @param name The parameter name to be printed on errors. Input parameter.
76
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
76
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
77
 *  @returns EOK on success.
77
 *  @returns EOK on success.
78
 *  @returns EINVAL if the parameter is missing.
78
 *  @returns EINVAL if the parameter is missing.
79
 *  @returns EINVAL if the parameter is in wrong format.
79
 *  @returns EINVAL if the parameter is in wrong format.
80
 */
80
 */
81
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset );
81
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset );
82
 
82
 
83
/** Parses the next parameter as a character string.
83
/** Parses the next parameter as a character string.
84
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
84
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
85
 *  @param argc The total number of the parameters. Input parameter.
85
 *  @param argc The total number of the parameters. Input parameter.
86
 *  @param argv The parameters. Input parameter.
86
 *  @param argv The parameters. Input parameter.
87
 *  @param index The actual parameter index. Input/output parameter.
87
 *  @param index The actual parameter index. Input/output parameter.
88
 *  @param value The parsed parameter value. Output parameter.
88
 *  @param value The parsed parameter value. Output parameter.
89
 *  @param name The parameter name to be printed on errors. Input parameter.
89
 *  @param name The parameter name to be printed on errors. Input parameter.
90
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
90
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
91
 *  @returns EOK on success.
91
 *  @returns EOK on success.
92
 *  @returns EINVAL if the parameter is missing.
92
 *  @returns EINVAL if the parameter is missing.
93
 */
93
 */
94
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset );
94
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset );
95
 
95
 
96
/** Parses the next named parameter as an integral number.
96
/** Parses the next named parameter as an integral number.
97
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
97
 *  Uses the offseted actual parameter if the offset is set or the next one if not.
98
 *  Translates the parameter using the parse_value function.
98
 *  Translates the parameter using the parse_value function.
99
 *  @param argc The total number of the parameters. Input parameter.
99
 *  @param argc The total number of the parameters. Input parameter.
100
 *  @param argv The parameters. Input parameter.
100
 *  @param argv The parameters. Input parameter.
101
 *  @param index The actual parameter index. Input/output parameter.
101
 *  @param index The actual parameter index. Input/output parameter.
102
 *  @param value The parsed parameter value. Output parameter.
102
 *  @param value The parsed parameter value. Output parameter.
103
 *  @param name The parameter name to be printed on errors. Input parameter.
103
 *  @param name The parameter name to be printed on errors. Input parameter.
104
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
104
 *  @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
105
 *  @param parse_value The translation function to parse the named value.
105
 *  @param parse_value The translation function to parse the named value.
106
 *  @returns EOK on success.
106
 *  @returns EOK on success.
107
 *  @returns EINVAL if the parameter is missing.
107
 *  @returns EINVAL if the parameter is missing.
108
 *  @returns ENOENT if the parameter name has not been found.
108
 *  @returns ENOENT if the parameter name has not been found.
109
 */
109
 */
110
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value ));
110
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value ));
111
 
111
 
112
/** Translates the character string to the protocol family number.
112
/** Translates the character string to the protocol family number.
113
 *  @param name The protocol family name. Input parameter.
113
 *  @param name The protocol family name. Input parameter.
114
 *  @returns The corresponding protocol family number.
114
 *  @returns The corresponding protocol family number.
115
 */
115
 */
116
int parse_protocol_family( const char * name );
116
int parse_protocol_family( const char * name );
117
 
117
 
118
/** Translates the character string to the socket type number.
118
/** Translates the character string to the socket type number.
119
 *  @param name The socket type name. Input parameter.
119
 *  @param name The socket type name. Input parameter.
120
 *  @returns The corresponding socket type number.
120
 *  @returns The corresponding socket type number.
121
 */
121
 */
122
int parse_socket_type( const char * name );
122
int parse_socket_type( const char * name );
123
 
123
 
124
void print_help( void ){
124
void print_help( void ){
125
    printf(
125
    printf(
126
        "Network Echo aplication\n" \
126
        "Network Echo aplication\n" \
127
        "Usage: echo [options]\n" \
127
        "Usage: echo [options]\n" \
128
        "Where options are:\n" \
128
        "Where options are:\n" \
129
        "-p port_number | --port=port_number\n" \
129
        "-p port_number | --port=port_number\n" \
130
        "\tThe port number the application should listen at. The default is 7.\n" \
130
        "\tThe port number the application should listen at. The default is 7.\n" \
131
        "\n" \
131
        "\n" \
132
        "-s receive_size | --size=receive_size\n" \
132
        "-s receive_size | --size=receive_size\n" \
133
        "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
133
        "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
134
        "\n" \
134
        "\n" \
135
        "-c count | --count\n" \
135
        "-c count | --count\n" \
136
        "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
136
        "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
137
        "\n" \
137
        "\n" \
138
        "-r reply_string | --reply=reply_string\n" \
138
        "-r reply_string | --reply=reply_string\n" \
139
        "\tThe constant reply string. The default is the original data received.\n" \
139
        "\tThe constant reply string. The default is the original data received.\n" \
140
        "\n" \
140
        "\n" \
141
        "-f protocol_family | --family=protocol_family\n" \
141
        "-f protocol_family | --family=protocol_family\n" \
142
        "\tThe listenning socket protocol family. Only the PF_INET is supported.\n"
142
        "\tThe listenning socket protocol family. Only the PF_INET is supported.\n"
143
        "\n" \
143
        "\n" \
144
        "-h | --help\n" \
144
        "-h | --help\n" \
145
        "\tShow this application help.\n"
145
        "\tShow this application help.\n"
146
        "\n" \
146
        "\n" \
147
        "-t socket_type | --type=socket_type\n" \
147
        "-t socket_type | --type=socket_type\n" \
148
        "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \
148
        "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \
149
        "\n" \
149
        "\n" \
150
        "-v | --verbose\n" \
150
        "-v | --verbose\n" \
151
        "\tShow all output messages.\n"
151
        "\tShow all output messages.\n"
152
    );
152
    );
153
}
153
}
154
 
154
 
155
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset ){
155
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset ){
156
    char *  rest;
156
    char *  rest;
157
 
157
 
158
    if( offset ){
158
    if( offset ){
159
        * value = strtol( argv[ * index ] + offset, & rest, 10 );
159
        * value = strtol( argv[ * index ] + offset, & rest, 10 );
160
    }else if(( * index ) + 1 < argc ){
160
    }else if(( * index ) + 1 < argc ){
161
        ++ ( * index );
161
        ++ ( * index );
162
        * value = strtol( argv[ * index ], & rest, 10 );
162
        * value = strtol( argv[ * index ], & rest, 10 );
163
    }else{
163
    }else{
164
        fprintf( stderr, "Command line error: missing %s\n", name );
164
        fprintf( stderr, "Command line error: missing %s\n", name );
165
        return EINVAL;
165
        return EINVAL;
166
    }
166
    }
167
    if( rest && ( * rest )){
167
    if( rest && ( * rest )){
168
        fprintf( stderr, "Command line error: %s unrecognized (%d: %s)\n", name, * index, argv[ * index ] );
168
        fprintf( stderr, "Command line error: %s unrecognized (%d: %s)\n", name, * index, argv[ * index ] );
169
        return EINVAL;
169
        return EINVAL;
170
    }
170
    }
171
    return EOK;
171
    return EOK;
172
}
172
}
173
 
173
 
174
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset ){
174
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset ){
175
    if( offset ){
175
    if( offset ){
176
        * value = argv[ * index ] + offset;
176
        * value = argv[ * index ] + offset;
177
    }else if(( * index ) + 1 < argc ){
177
    }else if(( * index ) + 1 < argc ){
178
        ++ ( * index );
178
        ++ ( * index );
179
        * value = argv[ * index ];
179
        * value = argv[ * index ];
180
    }else{
180
    }else{
181
        fprintf( stderr, "Command line error: missing %s\n", name );
181
        fprintf( stderr, "Command line error: missing %s\n", name );
182
        return EINVAL;
182
        return EINVAL;
183
    }
183
    }
184
    return EOK;
184
    return EOK;
185
}
185
}
186
 
186
 
187
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value )){
187
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value )){
188
    ERROR_DECLARE;
188
    ERROR_DECLARE;
189
 
189
 
190
    char *  parameter;
190
    char *  parameter;
191
 
191
 
192
    ERROR_PROPAGATE( parse_parameter_string( argc, argv, index, & parameter, name, offset ));
192
    ERROR_PROPAGATE( parse_parameter_string( argc, argv, index, & parameter, name, offset ));
193
    * value = ( * parse_value )( parameter );
193
    * value = ( * parse_value )( parameter );
194
    if(( * value ) == ENOENT ){
194
    if(( * value ) == ENOENT ){
195
        fprintf( stderr, "Command line error: unrecognized %s value (%d: %s)\n", name, * index, parameter );
195
        fprintf( stderr, "Command line error: unrecognized %s value (%d: %s)\n", name, * index, parameter );
196
        return ENOENT;
196
        return ENOENT;
197
    }
197
    }
198
    return EOK;
198
    return EOK;
199
}
199
}
200
 
200
 
201
int parse_protocol_family( const char * name ){
201
int parse_protocol_family( const char * name ){
202
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
202
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
203
        return PF_INET;
203
        return PF_INET;
204
    }
204
    }
205
    return ENOENT;
205
    return ENOENT;
206
}
206
}
207
 
207
 
208
int parse_socket_type( const char * name ){
208
int parse_socket_type( const char * name ){
209
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
209
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
210
        return SOCK_DGRAM;
210
        return SOCK_DGRAM;
211
    }
211
    }
212
    return ENOENT;
212
    return ENOENT;
213
}
213
}
214
 
214
 
215
void print_unrecognized( int index, const char * parameter ){
215
void print_unrecognized( int index, const char * parameter ){
216
    fprintf( stderr, "Command line error - unrecognized parameter (%d: %s)\n", index, parameter );
216
    fprintf( stderr, "Command line error - unrecognized parameter (%d: %s)\n", index, parameter );
217
    print_help();
217
    print_help();
218
}
218
}
219
 
219
 
220
int main( int argc, char * argv[] ){
220
int main( int argc, char * argv[] ){
221
    ERROR_DECLARE;
221
    ERROR_DECLARE;
222
 
222
 
223
    int                 size            = 1024;
223
    int                 size            = 1024;
224
    int                 verbose         = 0;
224
    int                 verbose         = 0;
225
    char *              reply           = NULL;
225
    char *              reply           = NULL;
226
    sock_type_t         type            = SOCK_DGRAM;
226
    sock_type_t         type            = SOCK_DGRAM;
227
    int                 count           = -1;
227
    int                 count           = -1;
228
    struct sockaddr_in  address         = { .sin_family = PF_INET, .sin_port = 7 };
228
    struct sockaddr_in  address         = { .sin_family = PF_INET, .sin_port = 7 };
229
 
229
 
230
    int                 socket_id;
230
    int                 socket_id;
231
    int                 address_length;
231
    int                 address_length;
232
    char                address_string[ INET_ADDRSTRLEN ];
232
    char                address_string[ INET_ADDRSTRLEN ];
233
    char *              data;
233
    char *              data;
234
    int                 length;
234
    int                 length;
235
    int                 index;
235
    int                 index;
236
    size_t              reply_length;
236
    size_t              reply_length;
237
    int                 value;
237
    int                 value;
238
 
238
 
239
    printf( "Task %d - ", task_get_id());
239
    printf( "Task %d - ", task_get_id());
240
    printf( "%s\n", NAME );
240
    printf( "%s\n", NAME );
241
 
241
 
242
    for( index = 1; index < argc; ++ index ){
242
    for( index = 1; index < argc; ++ index ){
243
        if( argv[ index ][ 0 ] == '-' ){
243
        if( argv[ index ][ 0 ] == '-' ){
244
            switch( argv[ index ][ 1 ] ){
244
            switch( argv[ index ][ 1 ] ){
245
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
245
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
246
                            break;
246
                            break;
247
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family ));
247
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family ));
248
                            address.sin_family = ( uint16_t ) value;
248
                            address.sin_family = ( uint16_t ) value;
249
                            break;
249
                            break;
250
                case 'h':   print_help();
250
                case 'h':   print_help();
251
                            return EOK;
251
                            return EOK;
252
                            break;
252
                            break;
253
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
253
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
254
                            address.sin_port = value;
254
                            address.sin_port = value;
255
                            break;
255
                            break;
256
                case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
256
                case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
257
                            break;
257
                            break;
258
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 0 ));
258
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 0 ));
259
                            break;
259
                            break;
260
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 0, parse_socket_type ));
260
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 0, parse_socket_type ));
261
                            break;
261
                            break;
262
                case 'v':   verbose = 1;
262
                case 'v':   verbose = 1;
263
                            break;
263
                            break;
264
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
264
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
265
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
265
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
266
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
266
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
267
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family ));
267
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family ));
268
                                address.sin_family = value;
268
                                address.sin_family = value;
269
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
269
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
270
                                print_help();
270
                                print_help();
271
                                return EOK;
271
                                return EOK;
272
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
272
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
273
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
273
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
274
                                address.sin_port = value;
274
                                address.sin_port = value;
275
                            }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
275
                            }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
276
                                ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
276
                                ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
277
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
277
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
278
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 7 ));
278
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 7 ));
279
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
279
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
280
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 7, parse_socket_type ));
280
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 7, parse_socket_type ));
281
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
281
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
282
                                verbose = 1;
282
                                verbose = 1;
283
                            }else{
283
                            }else{
284
                                print_unrecognized( index, argv[ index ] + 2 );
284
                                print_unrecognized( index, argv[ index ] + 2 );
285
                                return EINVAL;
285
                                return EINVAL;
286
                            }
286
                            }
287
                            break;
287
                            break;
288
                default:
288
                default:
289
                    print_unrecognized( index, argv[ index ] + 1 );
289
                    print_unrecognized( index, argv[ index ] + 1 );
290
                    return EINVAL;
290
                    return EINVAL;
291
            }
291
            }
292
        }else{
292
        }else{
293
            print_unrecognized( index, argv[ index ] );
293
            print_unrecognized( index, argv[ index ] );
294
            return EINVAL;
294
            return EINVAL;
295
        }
295
        }
296
    }
296
    }
297
 
297
 
298
    if( size <= 0 ){
298
    if( size <= 0 ){
299
        fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
299
        fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
300
        size = 1024;
300
        size = 1024;
301
    }
301
    }
302
    data = ( char * ) malloc( size + 1 );
302
    data = ( char * ) malloc( size + 1 );
303
    if( ! data ){
303
    if( ! data ){
304
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
304
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
305
        return ENOMEM;
305
        return ENOMEM;
306
    }
306
    }
307
 
307
 
308
    reply_length = reply ? str_length( reply ) : 0;
308
    reply_length = reply ? str_length( reply ) : 0;
309
 
309
 
310
    socket_id = socket( address.sin_family, type, 0 );
310
    socket_id = socket( address.sin_family, type, 0 );
311
    if( socket_id < 0 ){
311
    if( socket_id < 0 ){
312
        fprintf( stderr, "Socket create error %d\n", socket_id );
312
        fprintf( stderr, "Socket create error %d\n", socket_id );
313
        return socket_id;
313
        return socket_id;
314
    }
314
    }
315
    if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){
315
    if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){
316
        fprintf( stderr, "Socket bind error %d\n", ERROR_CODE );
316
        fprintf( stderr, "Socket bind error %d\n", ERROR_CODE );
317
        return ERROR_CODE;
317
        return ERROR_CODE;
318
    }
318
    }
319
 
319
 
320
    if( verbose ) printf( "Listenning at %d\n", address.sin_port );
320
    if( verbose ) printf( "Listenning at %d\n", address.sin_port );
321
 
321
 
322
    while( count ){
322
    while( count ){
323
        address_length = sizeof( address );
323
        address_length = sizeof( address );
324
        length = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
324
        length = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
325
        if( length < 0 ){
325
        if( length < 0 ){
326
            fprintf( stderr, "Socket receive error %d\n", length );
326
            fprintf( stderr, "Socket receive error %d\n", length );
327
        }else{
327
        }else{
328
            if( verbose ){
328
            if( verbose ){
329
                if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){
329
                if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){
330
                    fprintf( stderr, "Received address error %d\n", ERROR_CODE );
330
                    fprintf( stderr, "Received address error %d\n", ERROR_CODE );
331
                    continue;
331
                    continue;
332
                }else{
332
                }else{
333
                    data[ length ] = '\0';
333
                    data[ length ] = '\0';
334
                    printf( "Received from %s:%d\n%s\n", address_string, address.sin_port, data );
334
                    printf( "Received from %s:%d\n%s\n", address_string, address.sin_port, data );
335
                }
335
                }
336
            }
336
            }
337
            if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : ( size_t ) length, 0, ( struct sockaddr * ) & address, sizeof( address )))){
337
            if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : ( size_t ) length, 0, ( struct sockaddr * ) & address, sizeof( address )))){
338
                fprintf( stderr, "Socket send error %d\n", ERROR_CODE );
338
                fprintf( stderr, "Socket send error %d\n", ERROR_CODE );
339
            }
339
            }
340
        }
340
        }
341
        if( count > 0 ) -- count;
341
        if( count > 0 ){
-
 
342
            -- count;
-
 
343
            if( verbose ) printf( "Waiting for next %d packet(s)\n", count );
-
 
344
        }
342
    }
345
    }
343
 
346
 
344
    if( verbose ) printf( "Closing the socket\n" );
347
    if( verbose ) printf( "Closing the socket\n" );
345
 
348
 
346
    if( ERROR_OCCURRED( closesocket( socket_id ))){
349
    if( ERROR_OCCURRED( closesocket( socket_id ))){
347
        fprintf( stderr, "Close socket error %d\n", ERROR_CODE );
350
        fprintf( stderr, "Close socket error %d\n", ERROR_CODE );
348
        return ERROR_CODE;
351
        return ERROR_CODE;
349
    }
352
    }
350
 
353
 
351
    if( verbose ) printf( "Exiting\n" );
354
    if( verbose ) printf( "Exiting\n" );
352
 
355
 
353
    return EOK;
356
    return EOK;
354
}
357
}
355
 
358
 
356
/** @}
359
/** @}
357
 */
360
 */
358
 
361