Rev 4707 | Rev 4713 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4707 | Rev 4708 | ||
---|---|---|---|
Line 224... | Line 224... | ||
224 | } |
224 | } |
225 | 225 | ||
226 | int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
226 | int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
227 | ERROR_DECLARE; |
227 | ERROR_DECLARE; |
228 | 228 | ||
229 | int length; |
229 | size_t length; |
230 | int offset; |
230 | size_t offset; |
- | 231 | int result; |
|
231 | uint8_t * data; |
232 | uint8_t * data; |
232 | udp_header_ref header; |
233 | udp_header_ref header; |
233 | socket_core_ref * socket; |
234 | socket_core_ref * socket; |
234 | packet_t next_packet; |
235 | packet_t next_packet; |
235 | int total_length; |
236 | size_t total_length; |
236 | // uint16_t checksum; |
237 | // uint16_t checksum; |
237 | int fragments; |
238 | int fragments; |
238 | packet_t tmp_packet; |
239 | packet_t tmp_packet; |
239 | icmp_type_t type; |
240 | icmp_type_t type; |
240 | icmp_code_t code; |
241 | icmp_code_t code; |
Line 243... | Line 244... | ||
243 | switch( error ){ |
244 | switch( error ){ |
244 | case SERVICE_ICMP: |
245 | case SERVICE_ICMP: |
245 | // process error |
246 | // process error |
246 | // TODO remove debug dump |
247 | // TODO remove debug dump |
247 | // length = icmp_client_header_length( packet ); |
248 | // length = icmp_client_header_length( packet ); |
248 | length = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
249 | result = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
249 | if( length < 0 ){ |
250 | if( result < 0 ){ |
250 | return release_and_return( packet, length ); |
251 | return release_and_return( packet, result ); |
251 | } |
252 | } |
252 | printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) ); |
253 | printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) ); |
- | 254 | length = ( size_t ) result; |
|
253 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
255 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
254 | return release_and_return( packet, ERROR_CODE ); |
256 | return release_and_return( packet, ERROR_CODE ); |
255 | } |
257 | } |
256 | break; |
258 | break; |
257 | default: |
259 | default: |
258 | return release_and_return( packet, ENOTSUP ); |
260 | return release_and_return( packet, ENOTSUP ); |
259 | } |
261 | } |
260 | } |
262 | } |
261 | // TODO process received ipopts? |
263 | // TODO process received ipopts? |
262 | offset = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
264 | result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
263 | if( offset < 0 ){ |
265 | if( result < 0 ){ |
264 | return release_and_return( packet, offset ); |
266 | return release_and_return( packet, result ); |
265 | } |
267 | } |
- | 268 | offset = ( size_t ) result; |
|
266 | 269 | ||
267 | length = packet_get_data_length( packet ); |
270 | length = packet_get_data_length( packet ); |
268 | if( length <= 0 ){ |
271 | if( length <= 0 ){ |
269 | return release_and_return( packet, EINVAL ); |
272 | return release_and_return( packet, EINVAL ); |
270 | } |
273 | } |
Line 290... | Line 293... | ||
290 | fragments = 0; |
293 | fragments = 0; |
291 | total_length = ntohs( header->len ); |
294 | total_length = ntohs( header->len ); |
292 | do{ |
295 | do{ |
293 | ++ fragments; |
296 | ++ fragments; |
294 | length = packet_get_data_length( packet ); |
297 | length = packet_get_data_length( packet ); |
295 | if( ! length ){ |
298 | if( length <= 0 ){ |
296 | return release_and_return( packet, NO_DATA ); |
299 | return release_and_return( packet, NO_DATA ); |
297 | } |
300 | } |
298 | if( total_length < length ){ |
301 | if( total_length < length ){ |
299 | // cut of the suffix if too long |
302 | // cut of the suffix if too long |
300 | if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){ |
303 | if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){ |
Line 318... | Line 321... | ||
318 | if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
321 | if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
319 | return release_and_return( packet, ERROR_CODE ); |
322 | return release_and_return( packet, ERROR_CODE ); |
320 | } |
323 | } |
321 | 324 | ||
322 | // notify the destination socket |
325 | // notify the destination socket |
323 | async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ** socket ).socket_id, fragments ); |
326 | async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) ( ** socket ).socket_id, ( ipcarg_t ) fragments ); |
324 | return EOK; |
327 | return EOK; |
325 | } |
328 | } |
326 | 329 | ||
327 | int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
330 | int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
328 | ERROR_DECLARE; |
331 | ERROR_DECLARE; |
Line 446... | Line 449... | ||
446 | } |
449 | } |
447 | 450 | ||
448 | // printf( "res = %d\n", res ); |
451 | // printf( "res = %d\n", res ); |
449 | 452 | ||
450 | switch( answer_count ){ |
453 | switch( answer_count ){ |
451 | case 0: ipc_answer_0( callid, res ); |
454 | case 0: ipc_answer_0( callid, ( ipcarg_t ) res ); |
452 | continue; |
455 | continue; |
453 | case 1: ipc_answer_1( callid, res, IPC_GET_ARG1( answer )); |
456 | case 1: ipc_answer_1( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer )); |
454 | continue; |
457 | continue; |
455 | case 2: ipc_answer_2( callid, res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer )); |
458 | case 2: ipc_answer_2( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer )); |
456 | continue; |
459 | continue; |
457 | case 3: ipc_answer_3( callid, res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer )); |
460 | case 3: ipc_answer_3( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer )); |
458 | continue; |
461 | continue; |
459 | case 4: ipc_answer_4( callid, res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer )); |
462 | case 4: ipc_answer_4( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer )); |
460 | continue; |
463 | continue; |
461 | default: ipc_answer_5( callid, res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer ), IPC_GET_ARG5( answer )); |
464 | default: ipc_answer_5( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer ), IPC_GET_ARG5( answer )); |
462 | continue; |
465 | continue; |
463 | } |
466 | } |
464 | } |
467 | } |
465 | 468 | ||
466 | socket_cores_destroy( & local_sockets ); |
469 | socket_cores_destroy( & local_sockets ); |
Line 476... | Line 479... | ||
476 | struct sockaddr_in * address_in; |
479 | struct sockaddr_in * address_in; |
477 | packet_t packet; |
480 | packet_t packet; |
478 | packet_t next_packet; |
481 | packet_t next_packet; |
479 | udp_header_ref header; |
482 | udp_header_ref header; |
480 | int index; |
483 | int index; |
481 | int total_length; |
484 | size_t total_length; |
482 | int length; |
485 | int result; |
483 | 486 | ||
484 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
487 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
485 | address = ( struct sockaddr * ) addr; |
488 | address = ( struct sockaddr * ) addr; |
486 | switch( address->sa_family ){ |
489 | switch( address->sa_family ){ |
487 | case AF_INET: |
490 | case AF_INET: |
Line 503... | Line 506... | ||
503 | } |
506 | } |
504 | // TODO do not ask all the time |
507 | // TODO do not ask all the time |
505 | ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix )); |
508 | ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix )); |
506 | 509 | ||
507 | // read the first packet fragment |
510 | // read the first packet fragment |
508 | total_length = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in ); |
511 | result = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in ); |
509 | if( total_length < 0 ) return total_length; |
512 | if( result < 0 ) return result; |
- | 513 | total_length = ( size_t ) result; |
|
510 | // prefix the udp header |
514 | // prefix the udp header |
511 | header = PACKET_PREFIX( packet, udp_header_t ); |
515 | header = PACKET_PREFIX( packet, udp_header_t ); |
512 | if( ! header ){ |
516 | if( ! header ){ |
513 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
517 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
514 | return ENOMEM; |
518 | return ENOMEM; |
515 | } |
519 | } |
516 | // read the rest of the packet fragments |
520 | // read the rest of the packet fragments |
517 | for( index = 1; index < fragments; ++ index ){ |
521 | for( index = 1; index < fragments; ++ index ){ |
518 | length = socket_read_packet_data( & next_packet, 0, address_in ); |
522 | result = socket_read_packet_data( & next_packet, 0, address_in ); |
519 | if( length < 0 ){ |
523 | if( result < 0 ){ |
520 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
524 | return release_and_return( packet, result ); |
521 | return length; |
- | |
522 | } |
525 | } |
523 | packet = pq_add( packet, next_packet, index, 0 ); |
526 | packet = pq_add( packet, next_packet, index, 0 ); |
524 | total_length += length; |
527 | total_length += ( size_t ) result; |
525 | } |
528 | } |
526 | // set the udp header |
529 | // set the udp header |
527 | header->source = ( socket->port < 0 ) ? 0 : htons( socket->port ); |
530 | header->source = htons( socket->port ); |
528 | header->dest = htons( address_in->sin_port ); |
531 | header->dest = htons( address_in->sin_port ); |
529 | header->len = htons( total_length + sizeof( udp_header_t )); |
532 | header->len = htons( total_length + sizeof( udp_header_t )); |
530 | // TODO my ip address for the pseudo header checksum |
533 | // TODO my ip address for the pseudo header checksum |
531 | header->check = 0; |
534 | header->check = 0; |
532 | // prepare the first packet fragment |
535 | // prepare the first packet fragment |
Line 535... | Line 538... | ||
535 | return ERROR_CODE; |
538 | return ERROR_CODE; |
536 | } |
539 | } |
537 | // send the packet |
540 | // send the packet |
538 | return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 ); |
541 | return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 ); |
539 | // TODO IPv6 |
542 | // TODO IPv6 |
540 | default: |
- | |
541 | return EAFNOSUPPORT; |
- | |
542 | } |
543 | } |
543 | return EOK; |
544 | return EAFNOSUPPORT; |
544 | } |
545 | } |
545 | 546 | ||
546 | int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){ |
547 | int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){ |
547 | ERROR_DECLARE; |
548 | ERROR_DECLARE; |
548 | 549 | ||
549 | socket_core_ref socket; |
550 | socket_core_ref socket; |
550 | int packet_id; |
551 | int packet_id; |
551 | packet_t packet; |
552 | packet_t packet; |
552 | udp_header_ref header; |
553 | udp_header_ref header; |
553 | struct sockaddr_in address; |
554 | struct sockaddr_in address; |
554 | int length; |
555 | size_t length; |
555 | packet_t next_packet; |
556 | packet_t next_packet; |
556 | void * data; |
557 | uint8_t * data; |
557 | int fragments; |
558 | size_t fragments; |
558 | int * lengths; |
559 | size_t * lengths; |
- | 560 | int result; |
|
559 | int index; |
561 | size_t index; |
560 | uint8_t * addr; |
562 | uint8_t * addr; |
561 | 563 | ||
562 | // find the socket |
564 | // find the socket |
563 | socket = socket_cores_find( local_sockets, socket_id ); |
565 | socket = socket_cores_find( local_sockets, socket_id ); |
564 | if( ! socket ) return ENOTSOCK; |
566 | if( ! socket ) return ENOTSOCK; |
Line 574... | Line 576... | ||
574 | } |
576 | } |
575 | header = ( udp_header_ref ) data; |
577 | header = ( udp_header_ref ) data; |
576 | // set the source address |
578 | // set the source address |
577 | address.sin_family = PF_INET; |
579 | address.sin_family = PF_INET; |
578 | address.sin_port = ntohs( header->source ); |
580 | address.sin_port = ntohs( header->source ); |
579 | length = packet_get_addr( packet, & addr, NULL ); |
581 | result = packet_get_addr( packet, & addr, NULL ); |
580 | if( length != sizeof( address.sin_addr.s_addr )){ |
582 | if( result != sizeof( address.sin_addr.s_addr )){ |
581 | pq_release( udp_globals.net_phone, packet_id ); |
583 | pq_release( udp_globals.net_phone, packet_id ); |
582 | return EINVAL; |
584 | return EINVAL; |
583 | } |
585 | } |
584 | address.sin_addr.s_addr = *(( uint32_t * ) addr ); |
586 | address.sin_addr.s_addr = *(( uint32_t * ) addr ); |
585 | bzero( & address.sin_zero, sizeof( address.sin_zero )); |
587 | bzero( & address.sin_zero, sizeof( address.sin_zero )); |
Line 597... | Line 599... | ||
597 | next_packet = pq_next( packet ); |
599 | next_packet = pq_next( packet ); |
598 | while(( next_packet = pq_next( next_packet ))){ |
600 | while(( next_packet = pq_next( next_packet ))){ |
599 | ++ fragments; |
601 | ++ fragments; |
600 | } |
602 | } |
601 | // compute and store the fragment lengths |
603 | // compute and store the fragment lengths |
602 | lengths = ( int * ) malloc( sizeof( int ) * ( fragments + 1 )); |
604 | lengths = ( size_t * ) malloc( sizeof( size_t ) * fragments + sizeof( size_t )); |
603 | if( ! lengths ) return ENOMEM; |
605 | if( ! lengths ) return ENOMEM; |
604 | lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t ); |
606 | lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t ); |
605 | lengths[ fragments ] = lengths[ 0 ]; |
607 | lengths[ fragments ] = lengths[ 0 ]; |
606 | next_packet = pq_next( packet ); |
608 | next_packet = pq_next( packet ); |
607 | for( index = 1; index < fragments; ++ index ){ |
609 | for( index = 1; index < fragments; ++ index ){ |
Line 625... | Line 627... | ||
625 | } |
627 | } |
626 | // release the packet |
628 | // release the packet |
627 | dyn_fifo_pop( & socket->received ); |
629 | dyn_fifo_pop( & socket->received ); |
628 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
630 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
629 | // return the total length |
631 | // return the total length |
630 | return length; |
632 | return ( int ) length; |
631 | } |
633 | } |
632 | 634 | ||
633 | int socket_write_data( void * data, size_t data_length ){ |
635 | int socket_write_data( void * data, size_t data_length ){ |
634 | size_t length; |
636 | size_t length; |
635 | ipc_callid_t callid; |
637 | ipc_callid_t callid; |
Line 680... | Line 682... | ||
680 | // set the packet destination address |
682 | // set the packet destination address |
681 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){ |
683 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){ |
682 | pq_release( udp_globals.net_phone, packet_get_id( * packet )); |
684 | pq_release( udp_globals.net_phone, packet_get_id( * packet )); |
683 | return ERROR_CODE; |
685 | return ERROR_CODE; |
684 | } |
686 | } |
685 | return length; |
687 | return ( int ) length; |
686 | } |
688 | } |
687 | 689 | ||
688 | static int release_and_return( packet_t packet, int result ){ |
690 | static int release_and_return( packet_t packet, int result ){ |
689 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
691 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
690 | return result; |
692 | return result; |
Line 703... | Line 705... | ||
703 | length = packet_get_addr( packet, & src, NULL ); |
705 | length = packet_get_addr( packet, & src, NULL ); |
704 | if(( length > 0 ) |
706 | if(( length > 0 ) |
705 | && ( ! error ) |
707 | && ( ! error ) |
706 | && ( udp_globals.icmp_phone >= 0 ) |
708 | && ( udp_globals.icmp_phone >= 0 ) |
707 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
709 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
708 | && ( packet_set_addr( packet, src, src, length ) == EOK )){ |
710 | && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){ |
709 | icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet ); |
711 | icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet ); |
710 | }else{ |
712 | }else{ |
711 | return release_and_return( packet, EINVAL ); |
713 | release_and_return( packet, EINVAL ); |
712 | } |
714 | } |
713 | } |
715 | } |
714 | 716 | ||
715 | /** @} |
717 | /** @} |
716 | */ |
718 | */ |