Changes in uspace/srv/net/app/nettest1/nettest1.c [aadf01e:9d28b9c] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/nettest1/nettest1.c
raadf01e r9d28b9c 65 65 * @returns EOK on success. 66 66 */ 67 int main(int argc, char * argv[]);67 int main( int argc, char * argv[] ); 68 68 69 69 /** Prints the application help. 70 70 */ 71 void print_help(void);71 void print_help( void ); 72 72 73 73 /** Translates the character string to the protocol family number. … … 76 76 * @returns EPFNOSUPPORTED if the protocol family is not supported. 77 77 */ 78 int parse_protocol_family(const char * name);78 int parse_protocol_family( const char * name ); 79 79 80 80 /** Translates the character string to the socket type number. … … 83 83 * @returns ESOCKNOSUPPORTED if the socket type is not supported. 84 84 */ 85 int parse_socket_type(const char * name);85 int parse_socket_type( const char * name ); 86 86 87 87 /** Refreshes the data. … … 90 90 * @param[in] size The data block size in bytes. 91 91 */ 92 void refresh_data(char * data, size_t size);92 void refresh_data( char * data, size_t size ); 93 93 94 94 /** Creates new sockets. … … 101 101 * @returns Other error codes as defined for the socket() function. 102 102 */ 103 int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type);103 int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ); 104 104 105 105 /** Closes sockets. … … 110 110 * @returns Other error codes as defined for the closesocket() function. 111 111 */ 112 int sockets_close(int verbose, int * socket_ids, int sockets);112 int sockets_close( int verbose, int * socket_ids, int sockets ); 113 113 114 114 /** Connects sockets. … … 121 121 * @returns Other error codes as defined for the connect() function. 122 122 */ 123 int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen);123 int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ); 124 124 125 125 /** Sends data via sockets. … … 135 135 * @returns Other error codes as defined for the sendto() function. 136 136 */ 137 int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages);137 int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ); 138 138 139 139 /** Receives data via sockets. … … 149 149 * @returns Other error codes as defined for the recvfrom() function. 150 150 */ 151 int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages);151 int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); 152 152 153 153 /** Sends and receives data via sockets. … … 165 165 * @returns Other error codes as defined for the recvfrom() function. 166 166 */ 167 int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages);167 int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); 168 168 169 169 /** Prints a mark. … … 171 171 * @param[in] index The index of the mark to be printed. 172 172 */ 173 void print_mark(int index);174 175 void print_help( void){173 void print_mark( int index ); 174 175 void print_help( void ){ 176 176 printf( 177 177 "Network Networking test 1 aplication - sockets\n" \ … … 201 201 } 202 202 203 int parse_protocol_family( const char * name){204 if( str_lcmp(name, "PF_INET", 7) == 0){203 int parse_protocol_family( const char * name ){ 204 if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ 205 205 return PF_INET; 206 }else if( str_lcmp(name, "PF_INET6", 8) == 0){206 }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){ 207 207 return PF_INET6; 208 208 } … … 210 210 } 211 211 212 int parse_socket_type( const char * name){213 if( str_lcmp(name, "SOCK_DGRAM", 11) == 0){212 int parse_socket_type( const char * name ){ 213 if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){ 214 214 return SOCK_DGRAM; 215 }else if( str_lcmp(name, "SOCK_STREAM", 12) == 0){215 }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){ 216 216 return SOCK_STREAM; 217 217 } … … 219 219 } 220 220 221 void refresh_data( char * data, size_t size){222 size_t 221 void refresh_data( char * data, size_t size ){ 222 size_t length; 223 223 224 224 // fill the data 225 225 length = 0; 226 while(size > length + sizeof(NETTEST1_TEXT) - 1){ 227 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1); 228 length += sizeof(NETTEST1_TEXT) - 1; 229 } 230 memcpy(data + length, NETTEST1_TEXT, size - length); 231 data[size] = '\0'; 232 } 233 234 int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type){ 235 int index; 236 237 if(verbose){ 238 printf("Create\t"); 239 } 240 fflush(stdout); 241 for(index = 0; index < sockets; ++ index){ 242 socket_ids[index] = socket(family, type, 0); 243 if(socket_ids[index] < 0){ 244 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 245 socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n"); 246 return socket_ids[index]; 247 } 248 if(verbose){ 249 print_mark(index); 226 while( size > length + sizeof( NETTEST1_TEXT ) - 1 ){ 227 memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT ) - 1 ); 228 length += sizeof( NETTEST1_TEXT ) - 1; 229 } 230 memcpy( data + length, NETTEST1_TEXT, size - length ); 231 data[ size ] = '\0'; 232 } 233 234 int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ){ 235 int index; 236 237 if( verbose ) printf( "Create\t" ); 238 fflush( stdout ); 239 for( index = 0; index < sockets; ++ index ){ 240 socket_ids[ index ] = socket( family, type, 0 ); 241 if( socket_ids[ index ] < 0 ){ 242 printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] ); 243 socket_print_error( stderr, socket_ids[ index ], "Socket create: ", "\n" ); 244 return socket_ids[ index ]; 245 } 246 if( verbose ){ 247 print_mark( index ); 250 248 } 251 249 } … … 253 251 } 254 252 255 int sockets_close( int verbose, int * socket_ids, int sockets){253 int sockets_close( int verbose, int * socket_ids, int sockets ){ 256 254 ERROR_DECLARE; 257 255 258 int index; 259 260 if(verbose){ 261 printf("\tClose\t"); 262 } 263 fflush(stdout); 264 for(index = 0; index < sockets; ++ index){ 265 if(ERROR_OCCURRED(closesocket(socket_ids[index]))){ 266 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 267 socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n"); 256 int index; 257 258 if( verbose ) printf( "\tClose\t" ); 259 fflush( stdout ); 260 for( index = 0; index < sockets; ++ index ){ 261 if( ERROR_OCCURRED( closesocket( socket_ids[ index ] ))){ 262 printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] ); 263 socket_print_error( stderr, ERROR_CODE, "Socket close: ", "\n" ); 268 264 return ERROR_CODE; 269 265 } 270 if(verbose){ 271 print_mark(index); 272 } 266 if( verbose ) print_mark( index ); 273 267 } 274 268 return EOK; 275 269 } 276 270 277 int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen){271 int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){ 278 272 ERROR_DECLARE; 279 273 280 int index; 281 282 if(verbose){ 283 printf("\tConnect\t"); 284 } 285 fflush(stdout); 286 for(index = 0; index < sockets; ++ index){ 287 if(ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))){ 288 socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n"); 274 int index; 275 276 if( verbose ) printf( "\tConnect\t" ); 277 fflush( stdout ); 278 for( index = 0; index < sockets; ++ index ){ 279 if( ERROR_OCCURRED( connect( socket_ids[ index ], address, addrlen ))){ 280 socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" ); 289 281 return ERROR_CODE; 290 282 } 291 if(verbose){ 292 print_mark(index); 293 } 283 if( verbose ) print_mark( index ); 294 284 } 295 285 return EOK; 296 286 } 297 287 298 int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages){288 int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ){ 299 289 ERROR_DECLARE; 300 290 301 int index; 302 int message; 303 304 if(verbose){ 305 printf("\tSendto\t"); 306 } 307 fflush(stdout); 308 for(index = 0; index < sockets; ++ index){ 309 for(message = 0; message < messages; ++ message){ 310 if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))){ 311 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 312 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 291 int index; 292 int message; 293 294 if( verbose ) printf( "\tSendto\t" ); 295 fflush( stdout ); 296 for( index = 0; index < sockets; ++ index ){ 297 for( message = 0; message < messages; ++ message ){ 298 if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, addrlen ))){ 299 printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message ); 300 socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); 313 301 return ERROR_CODE; 314 302 } 315 303 } 316 if(verbose){ 317 print_mark(index); 318 } 304 if( verbose ) print_mark( index ); 319 305 } 320 306 return EOK; 321 307 } 322 308 323 int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){ 324 int value; 325 int index; 326 int message; 327 328 if(verbose){ 329 printf("\tRecvfrom\t"); 330 } 331 fflush(stdout); 332 for(index = 0; index < sockets; ++ index){ 333 for(message = 0; message < messages; ++ message){ 334 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 335 if(value < 0){ 336 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 337 socket_print_error(stderr, value, "Socket receive: ", "\n"); 309 int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){ 310 int value; 311 int index; 312 int message; 313 314 if( verbose ) printf( "\tRecvfrom\t" ); 315 fflush( stdout ); 316 for( index = 0; index < sockets; ++ index ){ 317 for( message = 0; message < messages; ++ message ){ 318 value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen ); 319 if( value < 0 ){ 320 printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message ); 321 socket_print_error( stderr, value, "Socket receive: ", "\n" ); 338 322 return value; 339 323 } 340 324 } 341 if(verbose){ 342 print_mark(index); 343 } 325 if( verbose ) print_mark( index ); 344 326 } 345 327 return EOK; 346 328 } 347 329 348 int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){330 int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){ 349 331 ERROR_DECLARE; 350 332 351 int value; 352 int index; 353 int message; 354 355 if(verbose){ 356 printf("\tSendto and recvfrom\t"); 357 } 358 fflush(stdout); 359 for(index = 0; index < sockets; ++ index){ 360 for(message = 0; message < messages; ++ message){ 361 if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, * addrlen))){ 362 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 363 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 333 int value; 334 int index; 335 int message; 336 337 if( verbose ) printf( "\tSendto and recvfrom\t" ); 338 fflush( stdout ); 339 for( index = 0; index < sockets; ++ index ){ 340 for( message = 0; message < messages; ++ message ){ 341 if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, * addrlen ))){ 342 printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message ); 343 socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); 364 344 return ERROR_CODE; 365 345 } 366 value = recvfrom( socket_ids[index], data, size, 0, address, addrlen);367 if( value < 0){368 printf( "Socket %d (%d), message %d error:\n", index, socket_ids[index], message);369 socket_print_error( stderr, value, "Socket receive: ", "\n");346 value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen ); 347 if( value < 0 ){ 348 printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message ); 349 socket_print_error( stderr, value, "Socket receive: ", "\n" ); 370 350 return value; 371 351 } 372 352 } 373 if(verbose){ 374 print_mark(index); 375 } 353 if( verbose ) print_mark( index ); 376 354 } 377 355 return EOK; 378 356 } 379 357 380 void print_mark( int index){381 if(( index + 1) % 10){382 printf( "*");358 void print_mark( int index ){ 359 if(( index + 1 ) % 10 ){ 360 printf( "*" ); 383 361 }else{ 384 printf( "|");385 } 386 fflush( stdout);387 } 388 389 int main( int argc, char * argv[]){362 printf( "|" ); 363 } 364 fflush( stdout ); 365 } 366 367 int main( int argc, char * argv[] ){ 390 368 ERROR_DECLARE; 391 369 392 size_t 393 int 394 sock_type_t 395 int 396 int 397 int 398 uint16_t 399 400 socklen_t max_length = sizeof(struct sockaddr_in6);401 uint8_t address_data[max_length];402 struct sockaddr * address = (struct sockaddr *) address_data;403 struct sockaddr_in * address_in = (struct sockaddr_in *) address;404 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address;405 socklen_t 406 // char address_string[ INET6_ADDRSTRLEN];407 uint8_t * 408 409 int * 370 size_t size = 27; 371 int verbose = 0; 372 sock_type_t type = SOCK_DGRAM; 373 int sockets = 10; 374 int messages = 10; 375 int family = PF_INET; 376 uint16_t port = 7; 377 378 socklen_t max_length = sizeof( struct sockaddr_in6 ); 379 uint8_t address_data[ max_length ]; 380 struct sockaddr * address = ( struct sockaddr * ) address_data; 381 struct sockaddr_in * address_in = ( struct sockaddr_in * ) address; 382 struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; 383 socklen_t addrlen; 384 // char address_string[ INET6_ADDRSTRLEN ]; 385 uint8_t * address_start; 386 387 int * socket_ids; 410 388 char * data; 411 int 412 int 413 struct timeval 414 struct timeval 415 416 printf( "Task %d - ", task_get_id());417 printf( "%s\n", NAME);418 419 if( argc <= 1){389 int value; 390 int index; 391 struct timeval time_before; 392 struct timeval time_after; 393 394 printf( "Task %d - ", task_get_id()); 395 printf( "%s\n", NAME ); 396 397 if( argc <= 1 ){ 420 398 print_help(); 421 399 return EINVAL; 422 400 } 423 401 424 for(index = 1; (index < argc - 1) || ((index == argc) && (argv[index][0] == '-')); ++ index){ 425 if(argv[index][0] == '-'){ 426 switch(argv[index][1]){ 427 case 'f': 428 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family)); 429 break; 430 case 'h': 431 print_help(); 432 return EOK; 433 break; 434 case 'm': 435 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &messages, "message count", 0)); 436 break; 437 case 'n': 438 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &sockets, "socket count", 0)); 439 break; 440 case 'p': 441 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 0)); 442 port = (uint16_t) value; 443 break; 444 case 's': 445 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "packet size", 0)); 446 size = (value >= 0) ? (size_t) value : 0; 447 break; 448 case 't': 449 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type)); 450 type = (sock_type_t) value; 451 break; 452 case 'v': 453 verbose = 1; 454 break; 455 case '-': 456 if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 457 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family)); 458 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 459 print_help(); 460 return EOK; 461 }else if(str_lcmp(argv[index] + 2, "messages=", 6) == 0){ 462 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &messages, "message count", 8)); 463 }else if(str_lcmp(argv[index] + 2, "sockets=", 6) == 0){ 464 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &sockets, "socket count", 8)); 465 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 466 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 7)); 467 port = (uint16_t) value; 468 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 469 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type)); 470 type = (sock_type_t) value; 471 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 472 verbose = 1; 473 }else{ 474 print_unrecognized(index, argv[index] + 2); 475 print_help(); 476 return EINVAL; 477 } 478 break; 402 for( index = 1; ( index < argc - 1 ) || (( index == argc ) && ( argv[ index ][ 0 ] == '-' )); ++ index ){ 403 if( argv[ index ][ 0 ] == '-' ){ 404 switch( argv[ index ][ 1 ] ){ 405 case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family )); 406 break; 407 case 'h': print_help(); 408 return EOK; 409 break; 410 case 'm': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 0 )); 411 break; 412 case 'n': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 0 )); 413 break; 414 case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); 415 port = ( uint16_t ) value; 416 break; 417 case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 )); 418 size = (value >= 0 ) ? ( size_t ) value : 0; 419 break; 420 case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type )); 421 type = ( sock_type_t ) value; 422 break; 423 case 'v': verbose = 1; 424 break; 425 case '-': if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ 426 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family )); 427 }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ 428 print_help(); 429 return EOK; 430 }else if( str_lcmp( argv[ index ] + 2, "messages=", 6 ) == 0 ){ 431 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 8 )); 432 }else if( str_lcmp( argv[ index ] + 2, "sockets=", 6 ) == 0 ){ 433 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 8 )); 434 }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ 435 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); 436 port = ( uint16_t ) value; 437 }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ 438 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type )); 439 type = ( sock_type_t ) value; 440 }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ 441 verbose = 1; 442 }else{ 443 print_unrecognized( index, argv[ index ] + 2 ); 444 print_help(); 445 return EINVAL; 446 } 447 break; 479 448 default: 480 print_unrecognized( index, argv[index] + 1);449 print_unrecognized( index, argv[ index ] + 1 ); 481 450 print_help(); 482 451 return EINVAL; 483 452 } 484 453 }else{ 485 print_unrecognized( index, argv[index]);454 print_unrecognized( index, argv[ index ] ); 486 455 print_help(); 487 456 return EINVAL; … … 489 458 } 490 459 491 bzero( address_data, max_length);492 switch( family){460 bzero( address_data, max_length ); 461 switch( family ){ 493 462 case PF_INET: 494 463 address_in->sin_family = AF_INET; 495 address_in->sin_port = htons( port);496 address_start = ( uint8_t *) &address_in->sin_addr.s_addr;497 addrlen = sizeof( struct sockaddr_in);464 address_in->sin_port = htons( port ); 465 address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; 466 addrlen = sizeof( struct sockaddr_in ); 498 467 break; 499 468 case PF_INET6: 500 469 address_in6->sin6_family = AF_INET6; 501 address_in6->sin6_port = htons( port);502 address_start = ( uint8_t *) &address_in6->sin6_addr.s6_addr;503 addrlen = sizeof( struct sockaddr_in6);470 address_in6->sin6_port = htons( port ); 471 address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; 472 addrlen = sizeof( struct sockaddr_in6 ); 504 473 break; 505 474 default: 506 fprintf( stderr, "Address family is not supported\n");475 fprintf( stderr, "Address family is not supported\n" ); 507 476 return EAFNOSUPPORT; 508 477 } 509 478 510 if( ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){511 fprintf( stderr, "Address parse error %d\n", ERROR_CODE);479 if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){ 480 fprintf( stderr, "Address parse error %d\n", ERROR_CODE ); 512 481 return ERROR_CODE; 513 482 } 514 483 515 if( size <= 0){516 fprintf( stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size);484 if( size <= 0 ){ 485 fprintf( stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size ); 517 486 size = 1024; 518 487 } 519 488 // size plus terminating null (\0) 520 data = ( char *) malloc(size + 1);521 if( ! data){522 fprintf( stderr, "Failed to allocate data buffer.\n");489 data = ( char * ) malloc( size + 1 ); 490 if( ! data ){ 491 fprintf( stderr, "Failed to allocate data buffer.\n" ); 523 492 return ENOMEM; 524 493 } 525 refresh_data( data, size);526 527 if( sockets <= 0){528 fprintf( stderr, "Socket count too small (%d). Using 2 instead.\n", sockets);494 refresh_data( data, size ); 495 496 if( sockets <= 0 ){ 497 fprintf( stderr, "Socket count too small (%d). Using 2 instead.\n", sockets ); 529 498 sockets = 2; 530 499 } 531 500 // count plus terminating null (\0) 532 socket_ids = ( int *) malloc(sizeof(int) * (sockets + 1));533 if( ! socket_ids){534 fprintf( stderr, "Failed to allocate receive buffer.\n");501 socket_ids = ( int * ) malloc( sizeof( int ) * ( sockets + 1 )); 502 if( ! socket_ids ){ 503 fprintf( stderr, "Failed to allocate receive buffer.\n" ); 535 504 return ENOMEM; 536 505 } 537 socket_ids[sockets] = NULL; 538 539 if(verbose){ 540 printf("Starting tests\n"); 541 } 542 543 if(verbose){ 544 printf("1 socket, 1 message\n"); 545 } 546 547 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 548 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 506 socket_ids[ sockets ] = NULL; 507 508 if( verbose ) printf( "Starting tests\n" ); 509 510 if( verbose ) printf( "1 socket, 1 message\n" ); 511 512 if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){ 513 fprintf( stderr, "Get time of day error %d\n", ERROR_CODE ); 549 514 return ERROR_CODE; 550 515 } 551 516 552 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 553 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 554 if(verbose){ 555 printf("\tOK\n"); 556 } 557 558 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 559 if(type == SOCK_STREAM){ 560 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 561 } 562 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 563 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 564 if(verbose){ 565 printf("\tOK\n"); 566 } 567 568 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 569 if(type == SOCK_STREAM){ 570 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 571 } 572 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, 1)); 573 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 574 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 575 if(verbose){ 576 printf("\tOK\n"); 577 } 578 579 if(verbose){ 580 printf("1 socket, %d messages\n", messages); 581 } 582 583 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 584 if(type == SOCK_STREAM){ 585 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 586 } 587 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 588 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 589 if(verbose){ 590 printf("\tOK\n"); 591 } 592 593 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 594 if(type == SOCK_STREAM){ 595 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 596 } 597 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, messages)); 598 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 599 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 600 if(verbose){ 601 printf("\tOK\n"); 602 } 603 604 if(verbose){ 605 printf("%d sockets, 1 message\n", sockets); 606 } 607 608 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 609 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 610 if(verbose){ 611 printf("\tOK\n"); 612 } 613 614 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 615 if(type == SOCK_STREAM){ 616 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 617 } 618 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 619 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 620 if(verbose){ 621 printf("\tOK\n"); 622 } 623 624 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 625 if(type == SOCK_STREAM){ 626 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 627 } 628 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, 1)); 629 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 630 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 631 if(verbose){ 632 printf("\tOK\n"); 633 } 634 635 if(verbose){ 636 printf("%d sockets, %d messages\n", sockets, messages); 637 } 638 639 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 640 if(type == SOCK_STREAM){ 641 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 642 } 643 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 644 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 645 if(verbose){ 646 printf("\tOK\n"); 647 } 648 649 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 650 if(type == SOCK_STREAM){ 651 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 652 } 653 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 654 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 655 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 656 657 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 658 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 517 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); 518 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); 519 if( verbose ) printf( "\tOK\n" ); 520 521 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); 522 if( type == SOCK_STREAM ){ 523 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); 524 } 525 ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); 526 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); 527 if( verbose ) printf( "\tOK\n" ); 528 529 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); 530 if( type == SOCK_STREAM ){ 531 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); 532 } 533 ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 )); 534 ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); 535 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); 536 if( verbose ) printf( "\tOK\n" ); 537 538 if( verbose ) printf( "1 socket, %d messages\n", messages ); 539 540 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); 541 if( type == SOCK_STREAM ){ 542 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); 543 } 544 ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); 545 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); 546 if( verbose ) printf( "\tOK\n" ); 547 548 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); 549 if( type == SOCK_STREAM ){ 550 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); 551 } 552 ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages )); 553 ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); 554 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); 555 if( verbose ) printf( "\tOK\n" ); 556 557 if( verbose ) printf( "%d sockets, 1 message\n", sockets ); 558 559 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); 560 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); 561 if( verbose ) printf( "\tOK\n" ); 562 563 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); 564 if( type == SOCK_STREAM ){ 565 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); 566 } 567 ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); 568 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); 569 if( verbose ) printf( "\tOK\n" ); 570 571 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); 572 if( type == SOCK_STREAM ){ 573 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); 574 } 575 ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 )); 576 ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); 577 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); 578 if( verbose ) printf( "\tOK\n" ); 579 580 if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages ); 581 582 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); 583 if( type == SOCK_STREAM ){ 584 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); 585 } 586 ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); 587 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); 588 if( verbose ) printf( "\tOK\n" ); 589 590 ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); 591 if( type == SOCK_STREAM ){ 592 ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); 593 } 594 ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages )); 595 ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); 596 ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); 597 598 if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){ 599 fprintf( stderr, "Get time of day error %d\n", ERROR_CODE ); 659 600 return ERROR_CODE; 660 601 } 661 602 662 if(verbose){ 663 printf("\tOK\n"); 664 } 665 666 printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 667 668 if(verbose){ 669 printf("Exiting\n"); 670 } 603 if( verbose ) printf( "\tOK\n" ); 604 605 printf( "Tested in %d microseconds\n", tv_sub( & time_after, & time_before )); 606 607 if( verbose ) printf( "Exiting\n" ); 671 608 672 609 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.