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