Rev 4740 | Rev 4749 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4740 | Rev 4743 | ||
---|---|---|---|
Line 36... | Line 36... | ||
36 | 36 | ||
37 | #include <malloc.h> |
37 | #include <malloc.h> |
38 | #include <stdio.h> |
38 | #include <stdio.h> |
39 | #include <string.h> |
39 | #include <string.h> |
40 | #include <task.h> |
40 | #include <task.h> |
- | 41 | #include <time.h> |
|
41 | 42 | ||
42 | #include "../../include/in.h" |
43 | #include "../../include/in.h" |
43 | #include "../../include/in6.h" |
44 | #include "../../include/in6.h" |
44 | #include "../../include/inet.h" |
45 | #include "../../include/inet.h" |
45 | #include "../../include/socket.h" |
46 | #include "../../include/socket.h" |
Line 81... | Line 82... | ||
81 | * @returns The corresponding socket type number. |
82 | * @returns The corresponding socket type number. |
82 | * @returns ESOCKNOSUPPORTED if the socket type is not supported. |
83 | * @returns ESOCKNOSUPPORTED if the socket type is not supported. |
83 | */ |
84 | */ |
84 | int parse_socket_type( const char * name ); |
85 | int parse_socket_type( const char * name ); |
85 | 86 | ||
86 | void refresh_data( char * data, int size ); |
87 | void refresh_data( char * data, size_t size ); |
87 | int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ); |
88 | int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ); |
88 | int sockets_close( int verbose, int * socket_ids, int sockets ); |
89 | int sockets_close( int verbose, int * socket_ids, int sockets ); |
89 | int sockets_bind( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ); |
90 | int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ); |
90 | int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ); |
91 | int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ); |
91 | int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); |
92 | int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); |
92 | int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); |
93 | int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ); |
93 | void print_mark( int index ); |
94 | void print_mark( int index ); |
94 | 95 | ||
Line 136... | Line 137... | ||
136 | return SOCK_STREAM; |
137 | return SOCK_STREAM; |
137 | } |
138 | } |
138 | return ESOCKTNOSUPPORT; |
139 | return ESOCKTNOSUPPORT; |
139 | } |
140 | } |
140 | 141 | ||
141 | void refresh_data( char * data, int size ){ |
142 | void refresh_data( char * data, size_t size ){ |
142 | int length; |
143 | size_t length; |
143 | 144 | ||
144 | // fill the data |
145 | // fill the data |
145 | length = 0; |
146 | length = 0; |
146 | while( size > length + sizeof( NETTEST1_TEXT )){ |
147 | while( size > length + sizeof( NETTEST1_TEXT )){ |
147 | memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT )); |
148 | memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT )); |
Line 183... | Line 184... | ||
183 | if( verbose ) print_mark( index ); |
184 | if( verbose ) print_mark( index ); |
184 | } |
185 | } |
185 | return EOK; |
186 | return EOK; |
186 | } |
187 | } |
187 | 188 | ||
188 | int sockets_bind( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){ |
189 | int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){ |
189 | ERROR_DECLARE; |
190 | ERROR_DECLARE; |
190 | 191 | ||
191 | int index; |
192 | int index; |
192 | 193 | ||
193 | if( verbose ) printf( "\tBind\t" ); |
194 | if( verbose ) printf( "\tConnect\t" ); |
194 | fflush( stdout ); |
195 | fflush( stdout ); |
195 | for( index = 0; index < sockets; ++ index ){ |
196 | for( index = 0; index < sockets; ++ index ){ |
196 | if( ERROR_OCCURRED( bind( socket_ids[ index ], address, addrlen ))){ |
197 | if( ERROR_OCCURRED( connect( socket_ids[ index ], address, addrlen ))){ |
197 | printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] ); |
- | |
198 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
198 | socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" ); |
199 | return ERROR_CODE; |
199 | return ERROR_CODE; |
200 | } |
200 | } |
201 | if( verbose ) print_mark( index ); |
201 | if( verbose ) print_mark( index ); |
202 | } |
202 | } |
203 | return EOK; |
203 | return EOK; |
Line 304... | Line 304... | ||
304 | 304 | ||
305 | int * socket_ids; |
305 | int * socket_ids; |
306 | char * data; |
306 | char * data; |
307 | int value; |
307 | int value; |
308 | int index; |
308 | int index; |
- | 309 | struct timeval time_before; |
|
- | 310 | struct timeval time_after; |
|
309 | 311 | ||
310 | printf( "Task %d - ", task_get_id()); |
312 | printf( "Task %d - ", task_get_id()); |
311 | printf( "%s\n", NAME ); |
313 | printf( "%s\n", NAME ); |
312 | 314 | ||
313 | if( argc <= 1 ){ |
315 | if( argc <= 1 ){ |
Line 423... | Line 425... | ||
423 | 425 | ||
424 | if( verbose ) printf( "Starting tests\n" ); |
426 | if( verbose ) printf( "Starting tests\n" ); |
425 | 427 | ||
426 | if( verbose ) printf( "1 socket, 1 message\n" ); |
428 | if( verbose ) printf( "1 socket, 1 message\n" ); |
427 | 429 | ||
- | 430 | if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){ |
|
- | 431 | fprintf( stderr, "Get time of day error %d\n", ERROR_CODE ); |
|
- | 432 | return ERROR_CODE; |
|
- | 433 | } |
|
- | 434 | ||
428 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
435 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
429 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
436 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
430 | if( verbose ) printf( "\tOK\n" ); |
437 | if( verbose ) printf( "\tOK\n" ); |
431 | 438 | ||
432 | if( type == SOCK_DGRAM ){ |
- | |
433 | /* ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1 )); |
- | |
434 | ERROR_PROPAGATE( sockets_bind( verbose, socket_ids, 1, address, addrlen )); |
- | |
435 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
- | |
436 | */ |
- | |
437 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
439 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
438 | /* if( type == SOCK_STREAM ){ |
440 | if( type == SOCK_STREAM ){ |
439 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen )); |
441 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); |
440 | } |
442 | } |
441 | */ ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); |
443 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); |
442 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
444 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
443 | if( verbose ) printf( "\tOK\n" ); |
445 | if( verbose ) printf( "\tOK\n" ); |
444 | 446 | ||
445 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
447 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
446 | /* if( type == SOCK_STREAM ){ |
448 | if( type == SOCK_STREAM ){ |
447 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen )); |
449 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); |
448 | } |
450 | } |
449 | */ ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 )); |
451 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 )); |
450 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); |
452 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 )); |
451 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
453 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
452 | if( verbose ) printf( "\tOK\n" ); |
454 | if( verbose ) printf( "\tOK\n" ); |
453 | 455 | ||
454 | if( verbose ) printf( "1 socket, %d messages\n", messages ); |
456 | if( verbose ) printf( "1 socket, %d messages\n", messages ); |
455 | 457 | ||
456 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
458 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
457 | /* if( type == SOCK_STREAM ){ |
459 | if( type == SOCK_STREAM ){ |
458 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen )); |
460 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); |
459 | } |
461 | } |
460 | */ ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); |
462 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); |
461 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
463 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
462 | if( verbose ) printf( "\tOK\n" ); |
464 | if( verbose ) printf( "\tOK\n" ); |
463 | 465 | ||
464 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
466 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type )); |
465 | /* if( type == SOCK_STREAM ){ |
467 | if( type == SOCK_STREAM ){ |
466 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen )); |
468 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen )); |
467 | } |
469 | } |
468 | */ ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages )); |
470 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages )); |
469 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); |
471 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages )); |
470 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
472 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 )); |
471 | if( verbose ) printf( "\tOK\n" ); |
473 | if( verbose ) printf( "\tOK\n" ); |
472 | 474 | ||
473 | if( verbose ) printf( "%d sockets, 1 message\n", sockets ); |
475 | if( verbose ) printf( "%d sockets, 1 message\n", sockets ); |
474 | 476 | ||
475 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
477 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
476 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
478 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
477 | if( verbose ) printf( "\tOK\n" ); |
479 | if( verbose ) printf( "\tOK\n" ); |
478 | 480 | ||
479 | /* ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets )); |
- | |
480 | ERROR_PROPAGATE( sockets_bind( verbose, socket_ids, sockets, address, addrlen )); |
- | |
481 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
- | |
482 | */ |
- | |
483 | - | ||
484 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
481 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
485 | /* if( type == SOCK_STREAM ){ |
482 | if( type == SOCK_STREAM ){ |
486 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen )); |
483 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); |
487 | } |
484 | } |
488 | */ ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); |
485 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); |
489 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
486 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
490 | if( verbose ) printf( "\tOK\n" ); |
487 | if( verbose ) printf( "\tOK\n" ); |
491 | 488 | ||
492 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
489 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
493 | /* if( type == SOCK_STREAM ){ |
490 | if( type == SOCK_STREAM ){ |
494 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen )); |
491 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); |
495 | } |
492 | } |
496 | */ ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 )); |
493 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 )); |
497 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); |
494 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 )); |
498 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
495 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
499 | if( verbose ) printf( "\tOK\n" ); |
496 | if( verbose ) printf( "\tOK\n" ); |
500 | 497 | ||
501 | if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages ); |
498 | if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages ); |
502 | 499 | ||
503 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
500 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
504 | /* if( type == SOCK_STREAM ){ |
501 | if( type == SOCK_STREAM ){ |
505 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen )); |
502 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); |
506 | } |
503 | } |
507 | */ ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); |
504 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); |
508 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
505 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
509 | if( verbose ) printf( "\tOK\n" ); |
506 | if( verbose ) printf( "\tOK\n" ); |
510 | 507 | ||
511 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
508 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type )); |
512 | /* if( type == SOCK_STREAM ){ |
509 | if( type == SOCK_STREAM ){ |
513 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen )); |
510 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen )); |
514 | } |
511 | } |
515 | */ ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages )); |
512 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages )); |
516 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); |
513 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages )); |
517 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
514 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets )); |
518 | if( verbose ) printf( "\tOK\n" ); |
- | |
519 | } |
- | |
520 | /* |
- | |
521 | if( type == SOCK_STREAM ){ |
- | |
522 | // TODO remove tests |
- | |
523 | address_in->sin_addr.s_addr = 0x3f26b75a; |
- | |
524 | address_in->sin_port = htons( 80 ); |
- | |
525 | if( ERROR_OCCURRED( connect( listening_id, address, sizeof( struct sockaddr_in )))){ |
- | |
526 | socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" ); |
- | |
527 | return ERROR_CODE; |
- | |
528 | } |
- | |
529 | if( ERROR_OCCURRED( send( listening_id, "ahoj nekdo", 10, 0 ))){ |
- | |
530 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); |
- | |
531 | return ERROR_CODE; |
- | |
532 | } |
- | |
533 | value = recv( socket_id, data, size, 0 ); |
- | |
534 | fprintf( stderr, "Socket receive: %d\n", value ); |
- | |
535 | if( ERROR_OCCURRED( send( listening_id, "ahoj nekdo", 10, 0 ))){ |
- | |
536 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); |
- | |
537 | return ERROR_CODE; |
- | |
538 | } |
- | |
539 | value = recvfrom( socket_id, data, size, 0, address, & addrlen ); |
- | |
540 | fprintf( stderr, "Socket receive: %d\n", value ); |
- | |
541 | 515 | ||
542 | if( ERROR_OCCURRED( closesocket( listening_id ))){ |
516 | if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){ |
543 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
- | |
544 | return ERROR_CODE; |
- | |
545 | } |
- | |
546 | listening_id = socket( family, type, 0 ); |
- | |
547 | if( listening_id < 0 ){ |
- | |
548 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
517 | fprintf( stderr, "Get time of day error %d\n", ERROR_CODE ); |
549 | return listening_id; |
- | |
550 | } |
- | |
551 | - | ||
552 | if( ERROR_OCCURRED( listen( listening_id, 3 ))){ |
- | |
553 | socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" ); |
- | |
554 | return ERROR_CODE; |
- | |
555 | } |
- | |
556 | }else{ |
- | |
557 | socket_id = listening_id; |
- | |
558 | } |
- | |
559 | if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){ |
- | |
560 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
- | |
561 | return ERROR_CODE; |
518 | return ERROR_CODE; |
562 | } |
519 | } |
- | 520 | ||
- | 521 | if( verbose ) printf( "\tOK\n" ); |
|
- | 522 | ||
- | 523 | printf( "Tested in %d microseconds\n", tv_sub( & time_after, & time_before )); |
|
563 | */ |
524 | |
564 | if( verbose ) printf( "Exiting\n" ); |
525 | if( verbose ) printf( "Exiting\n" ); |
565 | 526 | ||
566 | return EOK; |
527 | return EOK; |
567 | } |
528 | } |
568 | 529 |