Rev 4704 | Rev 4709 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4704 | Rev 4708 | ||
|---|---|---|---|
| Line 232... | Line 232... | ||
| 232 | } |
232 | } |
| 233 | 233 | ||
| 234 | int main( int argc, char * argv[] ){ |
234 | int main( int argc, char * argv[] ){ |
| 235 | ERROR_DECLARE; |
235 | ERROR_DECLARE; |
| 236 | 236 | ||
| 237 | int size = 1024; |
237 | size_t size = 1024; |
| 238 | int verbose = 0; |
238 | int verbose = 0; |
| 239 | char * reply = NULL; |
239 | char * reply = NULL; |
| 240 | sock_type_t type = SOCK_DGRAM; |
240 | sock_type_t type = SOCK_DGRAM; |
| 241 | int count = -1; |
241 | int count = -1; |
| 242 | struct sockaddr_in address = { .sin_family = PF_INET, .sin_port = 7 }; |
242 | struct sockaddr_in address = { .sin_family = PF_INET, .sin_port = 7 }; |
| 243 | 243 | ||
| 244 | int socket_id; |
244 | int socket_id; |
| 245 | int address_length; |
245 | int address_length; |
| 246 | char address_string[ INET_ADDRSTRLEN ]; |
246 | char address_string[ INET_ADDRSTRLEN ]; |
| 247 | char * data; |
247 | char * data; |
| 248 | int length; |
248 | size_t length; |
| 249 | int index; |
249 | int index; |
| 250 | size_t reply_length; |
250 | size_t reply_length; |
| 251 | int value; |
251 | int value; |
| 252 | 252 | ||
| 253 | printf( "Task %d - ", task_get_id()); |
253 | printf( "Task %d - ", task_get_id()); |
| Line 263... | Line 263... | ||
| 263 | break; |
263 | break; |
| 264 | case 'h': print_help(); |
264 | case 'h': print_help(); |
| 265 | return EOK; |
265 | return EOK; |
| 266 | break; |
266 | break; |
| 267 | 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 )); |
| 268 | address.sin_port = value; |
268 | address.sin_port = ( uint16_t ) value; |
| 269 | break; |
269 | break; |
| 270 | 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 )); |
| 271 | break; |
271 | break; |
| 272 | 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, & value, "receive size", 0 )); |
| - | 273 | size = (value >= 0 ) ? ( size_t ) value : 0; |
|
| 273 | break; |
274 | break; |
| 274 | case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 0, parse_socket_type )); |
275 | case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 0, parse_socket_type )); |
| - | 276 | type = ( sock_type_t ) value; |
|
| 275 | break; |
277 | break; |
| 276 | case 'v': verbose = 1; |
278 | case 'v': verbose = 1; |
| 277 | break; |
279 | break; |
| 278 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
280 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
| 279 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 )) |
281 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 )) |
| 280 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
282 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
| 281 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family )); |
283 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family )); |
| 282 | address.sin_family = value; |
284 | address.sin_family = ( uint16_t ) value; |
| 283 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
285 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
| 284 | print_help(); |
286 | print_help(); |
| 285 | return EOK; |
287 | return EOK; |
| 286 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
288 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
| 287 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
289 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
| 288 | address.sin_port = value; |
290 | address.sin_port = ( uint16_t ) value; |
| 289 | }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){ |
291 | }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){ |
| 290 | ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 )); |
292 | ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 )); |
| 291 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
293 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
| 292 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 7 )); |
294 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 )); |
| - | 295 | size = (value >= 0 ) ? ( size_t ) value : 0; |
|
| 293 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
296 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
| 294 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 7, parse_socket_type )); |
297 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 7, parse_socket_type )); |
| - | 298 | type = ( sock_type_t ) value; |
|
| 295 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
299 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
| 296 | verbose = 1; |
300 | verbose = 1; |
| 297 | }else{ |
301 | }else{ |
| 298 | print_unrecognized( index, argv[ index ] + 2 ); |
302 | print_unrecognized( index, argv[ index ] + 2 ); |
| 299 | return EINVAL; |
303 | return EINVAL; |
| Line 333... | Line 337... | ||
| 333 | 337 | ||
| 334 | if( verbose ) printf( "Listenning at %d\n", address.sin_port ); |
338 | if( verbose ) printf( "Listenning at %d\n", address.sin_port ); |
| 335 | 339 | ||
| 336 | while( count ){ |
340 | while( count ){ |
| 337 | address_length = sizeof( address ); |
341 | address_length = sizeof( address ); |
| 338 | length = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length ); |
342 | value = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length ); |
| 339 | if( length < 0 ){ |
343 | if( value < 0 ){ |
| 340 | fprintf( stderr, "Socket receive error %d\n", length ); |
344 | fprintf( stderr, "Socket receive error %d\n", value ); |
| 341 | }else{ |
345 | }else{ |
| - | 346 | length = ( size_t ) value; |
|
| 342 | if( verbose ){ |
347 | if( verbose ){ |
| 343 | if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){ |
348 | if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){ |
| 344 | fprintf( stderr, "Received address error %d\n", ERROR_CODE ); |
349 | fprintf( stderr, "Received address error %d\n", ERROR_CODE ); |
| 345 | continue; |
350 | continue; |
| 346 | }else{ |
351 | }else{ |
| 347 | data[ length ] = '\0'; |
352 | data[ length ] = '\0'; |
| 348 | printf( "Received from %s:%d\n%s\n", address_string, address.sin_port, data ); |
353 | printf( "Received from %s:%d\n%s\n", address_string, address.sin_port, data ); |
| 349 | } |
354 | } |
| 350 | } |
355 | } |
| 351 | if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : ( size_t ) length, 0, ( struct sockaddr * ) & address, sizeof( address )))){ |
356 | if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, ( struct sockaddr * ) & address, sizeof( address )))){ |
| 352 | fprintf( stderr, "Socket send error %d\n", ERROR_CODE ); |
357 | fprintf( stderr, "Socket send error %d\n", ERROR_CODE ); |
| 353 | } |
358 | } |
| 354 | } |
359 | } |
| 355 | if( count > 0 ){ |
360 | if( count > 0 ){ |
| 356 | -- count; |
361 | -- count; |