Changeset ede63e4 in mainline
- Timestamp:
- 2010-01-04T23:25:48Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- eac9722
- Parents:
- 1a0fb3f8
- Location:
- uspace/srv/net
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/socket/socket_client.c
r1a0fb3f8 rede63e4 40 40 #include <async.h> 41 41 #include <fibril_synch.h> 42 #include <limits.h> 43 #include <stdlib.h> 42 44 43 45 #include <ipc/services.h> … … 74 76 */ 75 77 #define SOCKET_CONNECT_TIMEOUT ( 1 * 1000 * 1000 ) 78 79 /** Maximum number of random attempts to find a new socket identifier before switching to the sequence. 80 */ 81 #define SOCKET_ID_TRIES 100 76 82 77 83 /** Type definition of the socket specific data. … … 155 161 */ 156 162 int udp_phone; 163 // /** The last socket identifier. 164 // */ 165 // int last_id; 157 166 /** Active sockets. 158 167 */ … … 168 177 .tcp_phone = -1, 169 178 .udp_phone = -1, 179 // .last_id = 0, 170 180 .sockets = NULL, 171 181 .lock = { … … 199 209 */ 200 210 static sockets_ref socket_get_sockets( void ); 211 212 /** Tries to find a new free socket identifier. 213 * @returns The new socket identifier. 214 * @returns ELIMIT if there is no socket identifier available. 215 */ 216 static int socket_generate_new_id( void ); 201 217 202 218 /** Default thread for new connections. … … 288 304 socket_globals.sockets = NULL; 289 305 } 306 srand( task_get_id()); 290 307 } 291 308 return socket_globals.sockets; 309 } 310 311 static int socket_generate_new_id( void ){ 312 sockets_ref sockets; 313 int socket_id; 314 int count; 315 316 sockets = socket_get_sockets(); 317 count = 0; 318 // socket_id = socket_globals.last_id; 319 do{ 320 if( count < SOCKET_ID_TRIES ){ 321 socket_id = rand() % INT_MAX; 322 ++ count; 323 }else if( count == SOCKET_ID_TRIES ){ 324 socket_id = 1; 325 ++ count; 326 // only this branch for last_id 327 }else{ 328 if( socket_id < INT_MAX ){ 329 ++ socket_id; 330 /* }else if( socket_globals.last_id ){ 331 * socket_globals.last_id = 0; 332 * socket_id = 1; 333 */ }else{ 334 return ELIMIT; 335 } 336 } 337 }while( sockets_find( sockets, socket_id )); 338 // last_id = socket_id 339 return socket_id; 292 340 } 293 341 … … 311 359 ipc_call_t call; 312 360 socket_ref socket; 313 socket_ref new_socket;314 361 315 362 while( true ){ … … 318 365 switch( IPC_GET_METHOD( call )){ 319 366 case NET_SOCKET_RECEIVED: 320 fibril_rwlock_read_lock( & socket_globals.lock );321 // find the socket322 socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( call ));323 if( ! socket ){324 ERROR_CODE = ENOTSOCK;325 }else{326 fibril_mutex_lock( & socket->receive_lock );327 // push the number of received packet fragments328 if( ! ERROR_OCCURRED( dyn_fifo_push( & socket->received, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_MAX_RECEIVED_SIZE ))){329 // signal the received packet330 fibril_condvar_signal( & socket->receive_signal );331 }332 fibril_mutex_unlock( & socket->receive_lock );333 }334 fibril_rwlock_read_unlock( & socket_globals.lock );335 break;336 367 case NET_SOCKET_ACCEPTED: 337 fibril_rwlock_read_lock( & socket_globals.lock );338 // find the socket339 socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( call ));340 if( ! socket ){341 ERROR_CODE = ENOTSOCK;342 }else{343 // create a new scoket344 new_socket = ( socket_ref ) malloc( sizeof( socket_t ));345 if( ! new_socket ){346 ERROR_CODE = ENOMEM;347 }else{348 bzero( new_socket, sizeof( * new_socket ));349 socket_initialize( new_socket, SOCKET_GET_NEW_SOCKET_ID( call ), socket->phone, socket->service );350 ERROR_CODE = sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket );351 if( ERROR_CODE < 0 ){352 free( new_socket );353 }else{354 // push the new socket identifier355 fibril_mutex_lock( & socket->accept_lock );356 if( ERROR_OCCURRED( dyn_fifo_push( & socket->accepted, new_socket->socket_id, SOCKET_MAX_ACCEPTED_SIZE ))){357 sockets_exclude( socket_get_sockets(), new_socket->socket_id );358 }else{359 // signal the accepted socket360 fibril_condvar_signal( & socket->accept_signal );361 }362 fibril_mutex_unlock( & socket->accept_lock );363 ERROR_CODE = EOK;364 }365 }366 }367 fibril_rwlock_read_unlock( & socket_globals.lock );368 break;369 368 case NET_SOCKET_DATA_FRAGMENT_SIZE: 370 369 fibril_rwlock_read_lock( & socket_globals.lock ); … … 374 373 ERROR_CODE = ENOTSOCK; 375 374 }else{ 376 fibril_rwlock_write_lock( & socket->sending_lock ); 377 // set the data fragment size 378 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( call ); 379 fibril_rwlock_write_unlock( & socket->sending_lock ); 380 ERROR_CODE = EOK; 375 switch( IPC_GET_METHOD( call )){ 376 case NET_SOCKET_RECEIVED: 377 fibril_mutex_lock( & socket->receive_lock ); 378 // push the number of received packet fragments 379 if( ! ERROR_OCCURRED( dyn_fifo_push( & socket->received, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_MAX_RECEIVED_SIZE ))){ 380 // signal the received packet 381 fibril_condvar_signal( & socket->receive_signal ); 382 } 383 fibril_mutex_unlock( & socket->receive_lock ); 384 break; 385 case NET_SOCKET_ACCEPTED: 386 // push the new socket identifier 387 fibril_mutex_lock( & socket->accept_lock ); 388 if( ERROR_OCCURRED( dyn_fifo_push( & socket->accepted, SOCKET_GET_NEW_SOCKET_ID( call ), SOCKET_MAX_ACCEPTED_SIZE ))){ 389 sockets_exclude( socket_get_sockets(), SOCKET_GET_NEW_SOCKET_ID( call )); 390 }else{ 391 // signal the accepted socket 392 fibril_condvar_signal( & socket->accept_signal ); 393 } 394 fibril_mutex_unlock( & socket->accept_lock ); 395 break; 396 default: 397 ERROR_CODE = ENOTSUP; 398 } 399 if(( SOCKET_GET_DATA_FRAGMENT_SIZE( call ) > 0 ) 400 && ( SOCKET_GET_DATA_FRAGMENT_SIZE( call ) != socket->data_fragment_size )){ 401 fibril_rwlock_write_lock( & socket->sending_lock ); 402 // set the data fragment size 403 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( call ); 404 fibril_rwlock_write_unlock( & socket->sending_lock ); 405 } 381 406 } 382 407 fibril_rwlock_read_unlock( & socket_globals.lock ); 383 break;384 408 default: 385 409 ERROR_CODE = ENOTSUP; … … 396 420 int socket_id; 397 421 services_t service; 422 int count; 398 423 399 424 // find the appropriate service … … 437 462 if( ! socket ) return ENOMEM; 438 463 bzero( socket, sizeof( * socket )); 464 count = 0; 465 fibril_rwlock_write_lock( & socket_globals.lock ); 439 466 // request a new socket 440 if( ERROR_OCCURRED(( int ) async_req_3_3( phone, NET_SOCKET, 0, 0, service, ( ipcarg_t * ) & socket_id, ( ipcarg_t * ) & socket->data_fragment_size, ( ipcarg_t * ) & socket->header_size ))){ 467 socket_id = socket_generate_new_id(); 468 if( socket_id <= 0 ){ 469 fibril_rwlock_write_unlock( & socket_globals.lock ); 470 free( socket ); 471 return socket_id; 472 } 473 if( ERROR_OCCURRED(( int ) async_req_3_3( phone, NET_SOCKET, socket_id, 0, service, NULL, ( ipcarg_t * ) & socket->data_fragment_size, ( ipcarg_t * ) & socket->header_size ))){ 474 fibril_rwlock_write_unlock( & socket_globals.lock ); 441 475 free( socket ); 442 476 return ERROR_CODE; … … 445 479 socket_initialize( socket, socket_id, phone, service ); 446 480 // store the new socket 447 fibril_rwlock_write_lock( & socket_globals.lock );448 481 ERROR_CODE = sockets_add( socket_get_sockets(), socket_id, socket ); 449 482 fibril_rwlock_write_unlock( & socket_globals.lock ); … … 509 542 int accept( int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen ){ 510 543 socket_ref socket; 544 socket_ref new_socket; 511 545 aid_t message_id; 512 546 int result; … … 515 549 if(( ! cliaddr ) || ( ! addrlen )) return EBADMEM; 516 550 517 fibril_rwlock_ read_lock( & socket_globals.lock );551 fibril_rwlock_write_lock( & socket_globals.lock ); 518 552 // find the socket 519 553 socket = sockets_find( socket_get_sockets(), socket_id ); 520 554 if( ! socket ){ 521 fibril_rwlock_ read_unlock( & socket_globals.lock );555 fibril_rwlock_write_unlock( & socket_globals.lock ); 522 556 return ENOTSOCK; 523 557 } … … 526 560 ++ socket->blocked; 527 561 while( dyn_fifo_value( & socket->accepted ) <= 0 ){ 528 fibril_rwlock_ read_unlock( & socket_globals.lock );562 fibril_rwlock_write_unlock( & socket_globals.lock ); 529 563 fibril_condvar_wait( & socket->accept_signal, & socket->accept_lock ); 530 fibril_rwlock_ read_lock( & socket_globals.lock );564 fibril_rwlock_write_lock( & socket_globals.lock ); 531 565 } 532 566 -- socket->blocked; 567 568 // create a new scoket 569 new_socket = ( socket_ref ) malloc( sizeof( socket_t )); 570 if( ! new_socket ){ 571 fibril_mutex_unlock( & socket->accept_lock ); 572 fibril_rwlock_write_unlock( & socket_globals.lock ); 573 return ENOMEM; 574 } 575 bzero( new_socket, sizeof( * new_socket )); 576 socket_id = socket_generate_new_id(); 577 if( socket_id <= 0 ){ 578 fibril_mutex_unlock( & socket->accept_lock ); 579 fibril_rwlock_write_unlock( & socket_globals.lock ); 580 free( new_socket ); 581 return socket_id; 582 } 583 socket_initialize( new_socket, socket_id, socket->phone, socket->service ); 584 result = sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket ); 585 if( result < 0 ){ 586 fibril_mutex_unlock( & socket->accept_lock ); 587 fibril_rwlock_write_unlock( & socket_globals.lock ); 588 free( new_socket ); 589 return result; 590 } 591 533 592 // request accept 534 message_id = async_send_ 3( socket->phone, NET_SOCKET_ACCEPT, ( ipcarg_t ) socket->socket_id, 0, socket->service, & answer );593 message_id = async_send_5( socket->phone, NET_SOCKET_ACCEPT, ( ipcarg_t ) socket->socket_id, 0, socket->service, 0, new_socket->socket_id, & answer ); 535 594 // read address 536 async_data_read_start( socket->phone, cliaddr, * addrlen );537 fibril_rwlock_ read_unlock( & socket_globals.lock );595 ipc_data_read_start( socket->phone, cliaddr, * addrlen ); 596 fibril_rwlock_write_unlock( & socket_globals.lock ); 538 597 async_wait_for( message_id, ( ipcarg_t * ) & result ); 539 598 if( result > 0 ){ 599 if( result != socket_id ){ 600 result = EINVAL; 601 } 540 602 // dequeue the accepted socket if successful 541 603 dyn_fifo_pop( & socket->accepted ); 542 604 // set address length 543 605 * addrlen = SOCKET_GET_ADDRESS_LENGTH( answer ); 606 new_socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( answer ); 544 607 }else if( result == ENOTSOCK ){ 545 608 // empty the queue if no accepted sockets … … 609 672 ipcarg_t result; 610 673 size_t fragments; 674 ipc_call_t answer; 611 675 612 676 if( ! data ) return EBADMEM; … … 621 685 fibril_rwlock_read_lock( & socket->sending_lock ); 622 686 // compute data fragment count 623 fragments = ( datalength + socket->header_size ) / socket->data_fragment_size; 624 if(( datalength + socket->header_size ) % socket->data_fragment_size ) ++ fragments; 687 if( socket->data_fragment_size > 0 ){ 688 fragments = ( datalength + socket->header_size ) / socket->data_fragment_size; 689 if(( datalength + socket->header_size ) % socket->data_fragment_size ) ++ fragments; 690 }else{ 691 fragments = 1; 692 } 625 693 // request send 626 message_id = async_send_5( socket->phone, message, ( ipcarg_t ) socket->socket_id, socket->data_fragment_size, socket->service, ( ipcarg_t ) flags, fragments, NULL);694 message_id = async_send_5( socket->phone, message, ( ipcarg_t ) socket->socket_id, ( fragments == 1 ? datalength : socket->data_fragment_size ), socket->service, ( ipcarg_t ) flags, fragments, & answer ); 627 695 // send the address if given 628 696 if(( ! toaddr ) || ( async_data_write_start( socket->phone, toaddr, addrlen ) == EOK )){ … … 643 711 } 644 712 } 713 async_wait_for( message_id, & result ); 714 if(( SOCKET_GET_DATA_FRAGMENT_SIZE( answer ) > 0 ) 715 && ( SOCKET_GET_DATA_FRAGMENT_SIZE( answer ) != socket->data_fragment_size )){ 716 // set the data fragment size 717 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( answer ); 718 } 645 719 fibril_rwlock_read_unlock( & socket->sending_lock ); 646 720 fibril_rwlock_read_unlock( & socket_globals.lock ); 647 async_wait_for( message_id, & result );648 721 return ( int ) result; 649 722 } -
uspace/srv/net/socket/socket_core.c
r1a0fb3f8 rede63e4 35 35 */ 36 36 37 #include <limits.h> 38 #include <stdlib.h> 39 37 40 #include "../err.h" 38 41 … … 51 54 52 55 #include "socket_core.h" 56 57 /** Maximum number of random attempts to find a new socket identifier before switching to the sequence. 58 */ 59 #define SOCKET_ID_TRIES 100 53 60 54 61 /** Bound port sockets. … … 94 101 */ 95 102 int socket_port_add_core( socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length ); 103 104 /** Tries to find a new free socket identifier. 105 * @param[in] local_sockets The local sockets to be searched. 106 * @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false. 107 * @returns The new socket identifier. 108 * @returns ELIMIT if there is no socket identifier available. 109 */ 110 static int socket_generate_new_id( socket_cores_ref local_sockets, int positive ); 96 111 97 112 INT_MAP_IMPLEMENT( socket_cores, socket_core_t ); … … 230 245 } 231 246 247 248 static int socket_generate_new_id( socket_cores_ref local_sockets, int positive ){ 249 int socket_id; 250 int count; 251 252 count = 0; 253 // socket_id = socket_globals.last_id; 254 do{ 255 if( count < SOCKET_ID_TRIES ){ 256 socket_id = rand() % INT_MAX; 257 ++ count; 258 }else if( count == SOCKET_ID_TRIES ){ 259 socket_id = 1; 260 ++ count; 261 // only this branch for last_id 262 }else{ 263 if( socket_id < INT_MAX ){ 264 ++ socket_id; 265 /* }else if( socket_globals.last_id ){ 266 * socket_globals.last_id = 0; 267 * socket_id = 1; 268 */ }else{ 269 return ELIMIT; 270 } 271 } 272 }while( socket_cores_find( local_sockets, (( positive ? 1 : -1 ) * socket_id ))); 273 // last_id = socket_id 274 return socket_id; 275 } 276 232 277 int socket_create( socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id ){ 233 278 ERROR_DECLARE; … … 235 280 socket_core_ref socket; 236 281 int res; 237 238 if( ! socket_id ) return EBADMEM; 282 int positive; 283 284 if( ! socket_id ) return EINVAL; 285 // store the socket 286 if( * socket_id <= 0 ){ 287 positive = ( * socket_id == 0 ); 288 * socket_id = socket_generate_new_id( local_sockets, positive ); 289 if( * socket_id <= 0 ){ 290 return * socket_id; 291 } 292 if( ! positive ){ 293 * socket_id *= -1; 294 } 295 }else if( socket_cores_find( local_sockets, * socket_id )){ 296 return EEXIST; 297 } 239 298 socket = ( socket_core_ref ) malloc( sizeof( * socket )); 240 299 if( ! socket ) return ENOMEM; … … 254 313 return ERROR_CODE; 255 314 } 256 // get a next free socket number 257 socket->socket_id = socket_cores_count( local_sockets ) + 1; 258 // store the socket 315 socket->socket_id = * socket_id; 259 316 res = socket_cores_add( local_sockets, socket->socket_id, socket ); 260 317 if( res < 0 ){ … … 264 321 return res; 265 322 } 266 // return the socket identifier267 * socket_id = socket->socket_id;268 323 return EOK; 269 324 } -
uspace/srv/net/socket/socket_core.h
r1a0fb3f8 rede63e4 177 177 * @param[in] app_phone The application phone. 178 178 * @param[in] specific_data The socket specific data. 179 * @param[ out] socket_id The new socket identifier.180 * @returns EOK on success. 181 * @returns E BADMEMif the socket_id parameter is NULL.179 * @param[in,out] socket_id The new socket identifier. A new identifier is chosen if set to zero (0) or negative. A negative identifier is chosen if set to negative. 180 * @returns EOK on success. 181 * @returns EINVAL if the socket_id parameter is NULL. 182 182 * @returns ENOMEM if there is not enough memory left. 183 183 */ -
uspace/srv/net/socket/socket_messages.h
r1a0fb3f8 rede63e4 142 142 #define SOCKET_GET_OPT_LEVEL( call ) ( int ) IPC_GET_ARG2( call ) 143 143 144 /** Sets the address length in the message answer.145 * @param[out] answer The message answer structure.146 */147 #define SOCKET_SET_ADDRESS_LENGTH( answer ) ( socklen_t * ) & IPC_GET_ARG2( answer )148 149 /** Returns the address length message parameter.150 * @param[in] call The message call structure.151 */152 #define SOCKET_GET_ADDRESS_LENGTH( call ) ( socklen_t ) IPC_GET_ARG2( call )153 154 144 /** Returns the data fragment size message parameter. 155 145 * @param[in] call The message call structure. … … 161 151 */ 162 152 #define SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) ( size_t * ) & IPC_GET_ARG2( answer ) 153 154 /** Sets the address length in the message answer. 155 * @param[out] answer The message answer structure. 156 */ 157 #define SOCKET_SET_ADDRESS_LENGTH( answer ) ( socklen_t * ) & IPC_GET_ARG3( answer ) 158 159 /** Returns the address length message parameter. 160 * @param[in] call The message call structure. 161 */ 162 #define SOCKET_GET_ADDRESS_LENGTH( call ) ( socklen_t ) IPC_GET_ARG3( call ) 163 163 164 164 /** Sets the header size in the message answer. -
uspace/srv/net/structures/int_map.h
r1a0fb3f8 rede63e4 82 82 void name##_exclude_index( name##_ref map, int index ); \ 83 83 type * name##_find( name##_ref map, int key ); \ 84 int name##_update( name##_ref map, int key, int new_key ); \ 84 85 type * name##_get_index( name##_ref map, int index ); \ 85 86 int name##_initialize( name##_ref map ); \ … … 178 179 } \ 179 180 return NULL; \ 181 } \ 182 \ 183 int name##_update( name##_ref map, int key, int new_key ){ \ 184 if( name##_is_valid( map )){ \ 185 int index; \ 186 \ 187 for( index = 0; index < map->next; ++ index ){ \ 188 if( name##_item_is_valid( &( map->items[ index ] ))){ \ 189 if( map->items[ index ].key == new_key ){ \ 190 return EEXIST; \ 191 }else if( map->items[ index ].key == key ){ \ 192 map->items[ index ].key = new_key; \ 193 return EOK; \ 194 } \ 195 } \ 196 } \ 197 } \ 198 return ENOENT; \ 180 199 } \ 181 200 \ -
uspace/srv/net/tl/icmp/icmp.c
r1a0fb3f8 rede63e4 350 350 // TODO do not ask all the time 351 351 ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); 352 packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, sizeof( icmp_header_t )+ icmp_globals.prefix, icmp_globals.suffix );352 packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, ICMP_HEADER_SIZE + icmp_globals.prefix, icmp_globals.suffix ); 353 353 if( ! packet ) return ENOMEM; 354 354 … … 548 548 } 549 549 ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); 550 icmp_globals.prefix += sizeof( icmp_header_t );551 icmp_globals.content -= sizeof( icmp_header_t );550 icmp_globals.prefix += ICMP_HEADER_SIZE; 551 icmp_globals.content -= ICMP_HEADER_SIZE; 552 552 // get configuration 553 553 icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING; … … 610 610 length = packet_get_data_length( packet ); 611 611 if( length <= 0 ) return EINVAL; 612 if( length < sizeof( icmp_header_t )) return EINVAL;612 if( length < ICMP_HEADER_SIZE) return EINVAL; 613 613 data = packet_get_data( packet ); 614 614 if( ! data ) return EINVAL; -
uspace/srv/net/tl/icmp/icmp_header.h
r1a0fb3f8 rede63e4 43 43 #include "../../include/in.h" 44 44 #include "../../include/icmp_codes.h" 45 46 /** ICMP header size in bytes. 47 */ 48 #define ICMP_HEADER_SIZE sizeof( icmp_header_t ) 45 49 46 50 /** Type definition of the echo specific data. -
uspace/srv/net/tl/tcp/tcp.c
r1a0fb3f8 rede63e4 205 205 int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ); 206 206 int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ); 207 int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags );208 int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen );207 int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags ); 208 int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen ); 209 209 int tcp_close_message( socket_cores_ref local_sockets, int socket_id ); 210 210 … … 298 298 return tcp_release_and_return( packet, EINVAL ); 299 299 } 300 if( length < sizeof( tcp_header_t )+ offset ){300 if( length < TCP_HEADER_SIZE + offset ){ 301 301 return tcp_release_and_return( packet, NO_DATA ); 302 302 } … … 668 668 ERROR_DECLARE; 669 669 670 packet_dimension_ref packet_dimension; 671 670 672 assert( socket ); 671 673 assert( socket_data ); … … 676 678 677 679 // queue the received packet 678 if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ 680 if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE )) 681 || ERROR_OCCURRED( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ))){ 679 682 return tcp_release_and_return( packet, ERROR_CODE ); 680 683 } … … 684 687 685 688 // notify the destination socket 686 async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments );689 async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ), 0, 0, ( ipcarg_t ) fragments ); 687 690 return EOK; 688 691 } … … 783 786 // printf( "addr %p\n", socket_data->addr, socket_data->addrlen ); 784 787 // create a socket 788 socket_id = -1; 785 789 if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){ 786 790 free( socket_data->addr ); … … 789 793 } 790 794 795 socket_id *= -1; 791 796 printf("new_sock %d\n", socket_id); 792 797 socket_data->pseudo_header = listening_socket_data->pseudo_header; … … 901 906 if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, socket->socket_id, listening_socket_data->backlog ))){ 902 907 // notify the destination socket 903 async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, 0, 0, 0, ( ipcarg_t ) socket->socket_id );908 async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, ( ipcarg_t ) socket->socket_id ); 904 909 fibril_rwlock_write_unlock( socket_data->local_lock ); 905 910 return EOK; … … 1035 1040 bzero( socket_data, sizeof( * socket_data )); 1036 1041 socket_data->state = TCP_SOCKET_INITIAL; 1037 socket_data->device_id = -1;1042 socket_data->device_id = DEVICE_INVALID_ID; 1038 1043 socket_data->window = NET_DEFAULT_TCP_WINDOW; 1039 1044 socket_data->treshold = socket_data->window; … … 1051 1056 fibril_mutex_initialize( & socket_data->operation.mutex ); 1052 1057 fibril_condvar_initialize( & socket_data->operation.condvar ); 1058 socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE; 1053 1059 } 1054 1060 … … 1065 1071 tcp_socket_data_ref socket_data; 1066 1072 socket_core_ref socket; 1073 packet_dimension_ref packet_dimension; 1067 1074 1068 1075 /* … … 1096 1103 socket_data->local_sockets = & local_sockets; 1097 1104 fibril_rwlock_write_lock( & lock ); 1105 * SOCKET_SET_SOCKET_ID( answer ) = SOCKET_GET_SOCKET_ID( call ); 1098 1106 res = socket_create( & local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID( answer )); 1099 1107 fibril_rwlock_write_unlock( & lock ); 1100 1108 if( res == EOK ){ 1101 * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( tcp_header_t ); 1102 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE; 1109 if( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, DEVICE_INVALID_ID, & packet_dimension ) == EOK ){ 1110 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ); 1111 } 1112 // * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE; 1113 * SOCKET_SET_HEADER_SIZE( answer ) = TCP_HEADER_SIZE; 1103 1114 answer_count = 3; 1104 1115 }else{ … … 1138 1149 res = data_receive(( void ** ) & addr, & addrlen ); 1139 1150 if( res == EOK ){ 1140 // the global lock may released in the tcp_connect_message() function1151 // the global lock may be released in the tcp_connect_message() function 1141 1152 fibril_rwlock_write_lock( & tcp_globals.lock ); 1142 1153 fibril_rwlock_write_lock( & lock ); … … 1152 1163 fibril_rwlock_read_lock( & tcp_globals.lock ); 1153 1164 fibril_rwlock_write_lock( & lock ); 1154 res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), & addrlen );1165 res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_NEW_SOCKET_ID( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), & addrlen ); 1155 1166 fibril_rwlock_write_unlock( & lock ); 1156 1167 fibril_rwlock_read_unlock( & tcp_globals.lock ); … … 1158 1169 * SOCKET_SET_SOCKET_ID( answer ) = res; 1159 1170 * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; 1160 answer_count = 2;1171 answer_count = 3; 1161 1172 } 1162 1173 break; … … 1164 1175 fibril_rwlock_read_lock( & tcp_globals.lock ); 1165 1176 fibril_rwlock_write_lock( & lock ); 1166 res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_ GET_DATA_FRAGMENT_SIZE( call), SOCKET_GET_FLAGS( call ));1177 res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), SOCKET_GET_FLAGS( call )); 1167 1178 if( res != EOK ){ 1168 1179 fibril_rwlock_write_unlock( & lock ); 1169 1180 fibril_rwlock_read_unlock( & tcp_globals.lock ); 1181 }else{ 1182 answer_count = 2; 1170 1183 } 1171 1184 break; … … 1175 1188 fibril_rwlock_read_lock( & tcp_globals.lock ); 1176 1189 fibril_rwlock_write_lock( & lock ); 1177 res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_ GET_DATA_FRAGMENT_SIZE( call), SOCKET_GET_FLAGS( call ));1190 res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), SOCKET_GET_FLAGS( call )); 1178 1191 if( res != EOK ){ 1179 1192 fibril_rwlock_write_unlock( & lock ); 1180 1193 fibril_rwlock_read_unlock( & tcp_globals.lock ); 1194 }else{ 1195 answer_count = 2; 1181 1196 } 1182 1197 free( addr ); … … 1204 1219 * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; 1205 1220 * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; 1206 answer_count = 2;1221 answer_count = 3; 1207 1222 res = EOK; 1208 1223 } … … 1725 1740 } 1726 1741 1727 int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ){1742 int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags ){ 1728 1743 ERROR_DECLARE; 1729 1744 … … 1738 1753 1739 1754 assert( local_sockets ); 1755 assert( data_fragment_size ); 1740 1756 1741 1757 // find the socket … … 1753 1769 ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); 1754 1770 1755 // TODO return the device_id + data_fragment_size if different - the client should send it again 1756 // ( two messages are better than ip fragmentation ) 1771 * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ); 1757 1772 1758 1773 for( index = 0; index < fragments; ++ index ){ 1759 1774 // read the data fragment 1760 result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, sizeof( tcp_header_t ), packet_dimension, socket_data->addr, socket_data->addrlen );1775 result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen ); 1761 1776 if( result < 0 ) return result; 1762 1777 total_length = ( size_t ) result; … … 1844 1859 ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); 1845 1860 // get a new packet 1846 * packet = packet_get_4( tcp_globals.net_phone, sizeof( tcp_header_t ), packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix );1861 * packet = packet_get_4( tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix ); 1847 1862 if( ! * packet ) return ENOMEM; 1848 1863 // allocate space in the packet … … 1856 1871 } 1857 1872 1858 int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ){1873 int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen ){ 1859 1874 ERROR_DECLARE; 1860 1875 … … 1862 1877 socket_core_ref socket; 1863 1878 tcp_socket_data_ref socket_data; 1879 packet_dimension_ref packet_dimension; 1864 1880 1865 1881 assert( local_sockets ); 1882 assert( data_fragment_size ); 1866 1883 assert( addrlen ); 1867 1884 … … 1887 1904 socket_data = ( tcp_socket_data_ref ) accepted->specific_data; 1888 1905 assert( socket_data ); 1906 // TODO can it be in another state? 1889 1907 if( socket_data->state == TCP_SOCKET_ESTABLISHED ){ 1890 1908 ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen )); 1909 ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); 1891 1910 * addrlen = socket_data->addrlen; 1911 * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ); 1912 if( new_socket_id > 0 ){ 1913 ERROR_PROPAGATE( socket_cores_update( local_sockets, accepted->socket_id, new_socket_id )); 1914 accepted->socket_id = new_socket_id; 1915 } 1892 1916 } 1893 1917 dyn_fifo_pop( & socket->accepted ); -
uspace/srv/net/tl/tcp/tcp_header.h
r1a0fb3f8 rede63e4 41 41 #include <sys/types.h> 42 42 43 /** TCP header size in bytes. 44 */ 45 #define TCP_HEADER_SIZE sizeof( tcp_header_t ) 46 43 47 /** Returns the actual TCP header length in bytes. 44 48 * @param[in] header The TCP packet header. -
uspace/srv/net/tl/udp/udp.c
r1a0fb3f8 rede63e4 97 97 * Is used as an entry point from the underlying IP module. 98 98 * Locks the global lock and calls udp_process_packet() function. 99 * @param device_id The device identifier. Ignored parameter.99 * @param[in] device_id The receiving device identifier. 100 100 * @param[in,out] packet The received packet queue. 101 101 * @param receiver The target service. Ignored parameter. … … 109 109 * Notifies the destination socket application. 110 110 * Releases the packet on error or sends an ICMP error notification.. 111 * @param[in] device_id The receiving device identifier. 111 112 * @param[in,out] packet The received packet queue. 112 113 * @param[in] error The packet error reporting service. Prefixes the received packet. … … 120 121 * @returns Other error codes as defined for the ip_client_process_packet() function. 121 122 */ 122 int udp_process_packet( packet_t packet, services_t error );123 int udp_process_packet( device_id_t device_id, packet_t packet, services_t error ); 123 124 124 125 /** Releases the packet and returns the result. … … 151 152 * @param[in] addrlen The address length. 152 153 * @param[in] fragments The number of data fragments. 153 * @param[ in] data_fragment_size The data fragment size in bytes.154 * @param[out] data_fragment_size The data fragment size in bytes. 154 155 * @param[in] flags Various send flags. 155 156 * @returns EOK on success. … … 163 164 * @returns Other error codes as defined for the ip_send_msg() function. 164 165 */ 165 int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t data_fragment_size, int flags );166 int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags ); 166 167 167 168 /** Receives data to the socket. … … 235 236 236 237 fibril_rwlock_write_lock( & udp_globals.lock ); 237 result = udp_process_packet( packet, error );238 result = udp_process_packet( device_id, packet, error ); 238 239 if( result != EOK ){ 239 240 fibril_rwlock_write_unlock( & udp_globals.lock ); … … 243 244 } 244 245 245 int udp_process_packet( packet_t packet, services_t error ){246 int udp_process_packet( device_id_t device_id, packet_t packet, services_t error ){ 246 247 ERROR_DECLARE; 247 248 … … 261 262 struct sockaddr * src; 262 263 struct sockaddr * dest; 264 packet_dimension_ref packet_dimension; 263 265 264 266 if( error ){ … … 292 294 return udp_release_and_return( packet, EINVAL ); 293 295 } 294 if( length < sizeof( udp_header_t )+ offset ){296 if( length < UDP_HEADER_SIZE + offset ){ 295 297 return udp_release_and_return( packet, NO_DATA ); 296 298 } … … 380 382 381 383 // queue the received packet 382 if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ 384 if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE )) 385 || ERROR_OCCURRED( tl_get_ip_packet_dimension( udp_globals.ip_phone, & udp_globals.dimensions, device_id, & packet_dimension ))){ 383 386 return udp_release_and_return( packet, ERROR_CODE ); 384 387 } … … 386 389 // notify the destination socket 387 390 fibril_rwlock_write_unlock( & udp_globals.lock ); 391 async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, packet_dimension->content, 0, 0, ( ipcarg_t ) fragments ); 392 /* fibril_rwlock_write_unlock( & udp_globals.lock ); 388 393 async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments ); 389 return EOK;394 */ return EOK; 390 395 } 391 396 … … 418 423 ipc_call_t answer; 419 424 int answer_count; 425 packet_dimension_ref packet_dimension; 420 426 421 427 /* … … 444 450 case NET_SOCKET: 445 451 fibril_rwlock_write_lock( & lock ); 452 * SOCKET_SET_SOCKET_ID( answer ) = SOCKET_GET_SOCKET_ID( call ); 446 453 res = socket_create( & local_sockets, app_phone, NULL, SOCKET_SET_SOCKET_ID( answer )); 447 454 fibril_rwlock_write_unlock( & lock ); 448 // TODO max fragment size 449 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE; 450 * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t ); 451 answer_count = 3; 455 if( res == EOK ){ 456 if( tl_get_ip_packet_dimension( udp_globals.ip_phone, & udp_globals.dimensions, DEVICE_INVALID_ID, & packet_dimension ) == EOK ){ 457 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = packet_dimension->content; 458 } 459 // * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE; 460 * SOCKET_SET_HEADER_SIZE( answer ) = UDP_HEADER_SIZE; 461 answer_count = 3; 462 } 452 463 break; 453 464 case NET_SOCKET_BIND: … … 467 478 fibril_rwlock_read_lock( & lock ); 468 479 fibril_rwlock_write_lock( & udp_globals.lock ); 469 res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_ GET_DATA_FRAGMENT_SIZE( call), SOCKET_GET_FLAGS( call ));480 res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), SOCKET_GET_FLAGS( call )); 470 481 if( res != EOK ){ 471 482 fibril_rwlock_write_unlock( & udp_globals.lock ); 483 }else{ 484 answer_count = 2; 472 485 } 473 486 fibril_rwlock_read_unlock( & lock ); … … 484 497 * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; 485 498 * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; 486 answer_count = 2;499 answer_count = 3; 487 500 res = EOK; 488 501 } … … 513 526 } 514 527 515 int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t data_fragment_size, int flags ){528 int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags ){ 516 529 ERROR_DECLARE; 517 530 … … 571 584 572 585 // read the first packet fragment 573 result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, sizeof( udp_header_t ), packet_dimension, addr, addrlen );586 result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, UDP_HEADER_SIZE, packet_dimension, addr, addrlen ); 574 587 if( result < 0 ) return result; 575 588 total_length = ( size_t ) result; … … 606 619 // return udp_release_and_return( packet, ERROR_CODE ); 607 620 // } 608 if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + sizeof( udp_header_t )))){621 if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + UDP_HEADER_SIZE))){ 609 622 free( ip_header ); 610 623 return udp_release_and_return( packet, ERROR_CODE ); … … 615 628 free( ip_header ); 616 629 }else{ 617 device_id = -1;630 device_id = DEVICE_INVALID_ID; 618 631 } 619 632 // prepare the first packet fragment … … 665 678 666 679 // trim the header 667 ERROR_PROPAGATE( packet_trim( packet, sizeof( udp_header_t ), 0 ));680 ERROR_PROPAGATE( packet_trim( packet, UDP_HEADER_SIZE, 0 )); 668 681 669 682 // reply the packets -
uspace/srv/net/tl/udp/udp_header.h
r1a0fb3f8 rede63e4 41 41 #include <sys/types.h> 42 42 43 /** UDP header size in bytes. 44 */ 45 #define UDP_HEADER_SIZE sizeof( udp_header_t ) 46 43 47 /** Type definition of the user datagram header. 44 48 * @see udp_header
Note:
See TracChangeset
for help on using the changeset viewer.