Subversion Repositories HelenOS

Rev

Rev 4728 | Rev 4731 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup udp
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  UDP module implementation.
  35.  *  @see udp.h
  36.  */
  37.  
  38. #include <async.h>
  39. #include <fibril_sync.h>
  40. #include <malloc.h>
  41.  
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44.  
  45. #include "../../err.h"
  46. #include "../../messages.h"
  47. #include "../../modules.h"
  48.  
  49. #include "../../structures/dynamic_fifo.h"
  50. #include "../../structures/packet/packet_client.h"
  51.  
  52. #include "../../include/crc.h"
  53. #include "../../include/in.h"
  54. #include "../../include/in6.h"
  55. #include "../../include/inet.h"
  56. #include "../../include/ip_client.h"
  57. #include "../../include/ip_interface.h"
  58. #include "../../include/ip_protocols.h"
  59. #include "../../include/icmp_client.h"
  60. #include "../../include/icmp_interface.h"
  61. #include "../../include/net_interface.h"
  62. #include "../../include/socket_codes.h"
  63. #include "../../include/socket_errno.h"
  64.  
  65. #include "../../socket/socket_core.h"
  66. #include "../../socket/socket_messages.h"
  67.  
  68. #include "../tl_common.h"
  69. #include "../tl_messages.h"
  70.  
  71. #include "udp.h"
  72. #include "udp_header.h"
  73. #include "udp_module.h"
  74.  
  75. /** Default UDP checksum computing.
  76.  */
  77. #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING  true
  78.  
  79. /** Default UDP autobind when sending via unbound sockets.
  80.  */
  81. #define NET_DEFAULT_UDP_AUTOBINDING true
  82.  
  83. /** Maximum UDP fragment size.
  84.  */
  85. #define MAX_UDP_FRAGMENT_SIZE   65535
  86.  
  87. /** Free ports pool start.
  88.  */
  89. #define UDP_FREE_PORTS_START    1025
  90.  
  91. /** Free ports pool end.
  92.  */
  93. #define UDP_FREE_PORTS_END      65535
  94.  
  95. /** Processes the received UDP packet queue.
  96.  *  Is used as an entry point from the underlying IP module.
  97.  *  Locks the global lock and calls udp_process_packet() function.
  98.  *  @param device_id The device identifier. Ignored parameter.
  99.  *  @param packet The received packet queue. Input/output parameter.
  100.  *  @param receiver The target service. Ignored parameter.
  101.  *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
  102.  *  @returns EOK on success.
  103.  *  @returns Other error codes as defined for the udp_process_packet() function.
  104.  */
  105. int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
  106.  
  107. /** Processes the received UDP packet queue.
  108.  *  Notifies the destination socket application.
  109.  *  Releases the packet on error or sends an ICMP error notification..
  110.  *  @param packet The received packet queue. Input/output parameter.
  111.  *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
  112.  *  @returns EOK on success.
  113.  *  @returns EINVAL if the packet is not valid.
  114.  *  @returns EINVAL if the stored packet address is not the an_addr_t.
  115.  *  @returns EINVAL if the packet does not contain any data.
  116.  *  @returns NO_DATA if the packet content is shorter than the user datagram header.
  117.  *  @returns ENOMEM if there is not enough memory left.
  118.  *  @returns EADDRNOTAVAIL if the destination socket does not exist.
  119.  *  @returns Other error codes as defined for the ip_client_process_packet() function.
  120.  */
  121. int udp_process_packet( packet_t packet, services_t error );
  122.  
  123. /** Releases the packet and returns the result.
  124.  *  @param packet The packet queue to be released. Input parameter.
  125.  *  @param result The result to be returned. Input parameter.
  126.  *  @return The result parameter.
  127.  */
  128. int udp_release_and_return( packet_t packet, int result );
  129.  
  130. /** @name Socket messages processing functions
  131.  */
  132. /*@{*/
  133.  
  134. /** Processes the socket client messages.
  135.  *  Runs until the client module disconnects.
  136.  *  @param callid The message identifier. Input parameter.
  137.  *  @param call The message parameters. Input parameter.
  138.  *  @returns EOK on success.
  139.  *  @see socket.h
  140.  */
  141. int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
  142.  
  143. /** Sends data from the socket to the remote address.
  144.  *  Binds the socket to a free port if not already connected/bound.
  145.  *  Handles the NET_SOCKET_SENDTO message.
  146.  *  Supports AF_INET and AF_INET6 address families.
  147.  *  @param local_sockets The application local sockets. Input/output parameter.
  148.  *  @param socket_id Socket identifier. Input parameter.
  149.  *  @param addr The destination address. Input parameter.
  150.  *  @param addrlen The address length. Input parameter.
  151.  *  @param fragments The number of data fragments. Input parameter.
  152.  *  @param data_fragment_size The data fragment size in bytes. Input parameter.
  153.  *  @param flags Various send flags. Input parameter.
  154.  *  @returns EOK on success.
  155.  *  @returns EAFNOTSUPPORT if the address family is not supported.
  156.  *  @returns ENOTSOCK if the socket is not found.
  157.  *  @returns EINVAL if the address is invalid.
  158.  *  @returns ENOTCONN if the sending socket is not and cannot be bound.
  159.  *  @returns ENOMEM if there is not enough memory left.
  160.  *  @returns Other error codes as defined for the socket_read_packet_data() function.
  161.  *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
  162.  *  @returns Other error codes as defined for the ip_send_msg() function.
  163.  */
  164. 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 );
  165.  
  166. /** Receives data to the socket.
  167.  *  Handles the NET_SOCKET_RECVFROM message.
  168.  *  Replies the source address as well.
  169.  *  @param local_sockets The application local sockets. Input parameter.
  170.  *  @param socket_id Socket identifier. Input parameter.
  171.  *  @param flags Various receive flags. Input parameter.
  172.  *  @param addrlen The source address length. Output parameter.
  173.  *  @returns The number of bytes received.
  174.  *  @returns ENOTSOCK if the socket is not found.
  175.  *  @returns NO_DATA if there are no received packets or data.
  176.  *  @returns ENOMEM if there is not enough memory left.
  177.  *  @returns EINVAL if the received address is not an IP address.
  178.  *  @returns Other error codes as defined for the packet_translate() function.
  179.  *  @returns Other error codes as defined for the data_reply() function.
  180.  */
  181. int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
  182.  
  183. /*@}*/
  184.  
  185. /** UDP global data.
  186.  */
  187. udp_globals_t   udp_globals;
  188.  
  189. int udp_initialize( async_client_conn_t client_connection ){
  190.     ERROR_DECLARE;
  191.  
  192.     measured_string_t   names[] = {{ "UDP_CHECKSUM_COMPUTING", 22 }, { "UDP_AUTOBINDING", 15 }};
  193.     measured_string_ref configuration;
  194.     size_t              count = sizeof( names ) / sizeof( measured_string_t );
  195.     char *              data;
  196.  
  197.     fibril_rwlock_initialize( & udp_globals.lock );
  198.     fibril_rwlock_write_lock( & udp_globals.lock );
  199.     udp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP );
  200.     if( udp_globals.icmp_phone < 0 ){
  201.         return udp_globals.icmp_phone;
  202.     }
  203.     udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
  204.     if( udp_globals.ip_phone < 0 ){
  205.         return udp_globals.ip_phone;
  206.     }
  207.     ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix ));
  208.     ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
  209.     udp_globals.packet_dimension.prefix += sizeof( udp_header_t );
  210.     udp_globals.packet_dimension.content -= sizeof( udp_header_t );
  211.     udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
  212.     // get configuration
  213.     udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
  214.     udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
  215.     configuration = & names[ 0 ];
  216.     ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data ));
  217.     if( configuration ){
  218.         if( configuration[ 0 ].value ){
  219.             udp_globals.checksum_computing = ( configuration[ 0 ].value[ 0 ] == 'y' );
  220.         }
  221.         if( configuration[ 1 ].value ){
  222.             udp_globals.autobinding = ( configuration[ 1 ].value[ 0 ] == 'y' );
  223.         }
  224.         net_free_settings( configuration, data );
  225.     }
  226.     fibril_rwlock_write_unlock( & udp_globals.lock );
  227.     return EOK;
  228. }
  229.  
  230. int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
  231.     int result;
  232.  
  233.     fibril_rwlock_read_lock( & udp_globals.lock );
  234.     result = udp_process_packet( packet, error );
  235.     fibril_rwlock_read_unlock( & udp_globals.lock );
  236.  
  237.     return result;
  238. }
  239.  
  240. int udp_process_packet( packet_t packet, services_t error ){
  241.     ERROR_DECLARE;
  242.  
  243.     size_t          length;
  244.     size_t          offset;
  245.     int             result;
  246.     uint8_t *       data;
  247.     udp_header_ref  header;
  248.     socket_core_ref *   socket;
  249.     packet_t        next_packet;
  250.     size_t          total_length;
  251.     uint32_t        checksum;
  252.     int             fragments;
  253.     packet_t        tmp_packet;
  254.     icmp_type_t     type;
  255.     icmp_code_t     code;
  256.     ip_pseudo_header_ref    ip_header;
  257.     struct sockaddr *       src;
  258.     struct sockaddr *       dest;
  259.  
  260.     if( error ){
  261.         switch( error ){
  262.             case SERVICE_ICMP:
  263.                 // process error
  264.                 // TODO remove debug dump
  265.                 // length = icmp_client_header_length( packet );
  266.                 result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
  267.                 if( result < 0 ){
  268.                     return udp_release_and_return( packet, result );
  269.                 }
  270.                 printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) );
  271.                 length = ( size_t ) result;
  272.                 if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
  273.                     return udp_release_and_return( packet, ERROR_CODE );
  274.                 }
  275.                 break;
  276.             default:
  277.                 return udp_release_and_return( packet, ENOTSUP );
  278.         }
  279.     }
  280.     // TODO process received ipopts?
  281.     result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
  282.     if( result < 0 ){
  283.         return udp_release_and_return( packet, result );
  284.     }
  285.     offset = ( size_t ) result;
  286.  
  287.     length = packet_get_data_length( packet );
  288.     if( length <= 0 ){
  289.         return udp_release_and_return( packet, EINVAL );
  290.     }
  291.     if( length < sizeof( udp_header_t ) + offset ){
  292.         return udp_release_and_return( packet, NO_DATA );
  293.     }
  294.     data = packet_get_data( packet );
  295.     if( ! data ){
  296.         return udp_release_and_return( packet, NO_DATA );
  297.     }
  298.     // get udp header
  299.     header = ( udp_header_ref )( data + offset );
  300.     // find the destination socket
  301.     socket = socket_ports_find( & udp_globals.sockets, ntohs( header->destination_port ));
  302.     if( ! socket ){
  303.         tl_send_icmp_port_unreachable( udp_globals.net_phone, udp_globals.icmp_phone, packet, error );
  304.         return EADDRNOTAVAIL;
  305.     }
  306.  
  307.     // trim after successful processing to be able to send an ICMP error message!
  308.     ERROR_PROPAGATE( packet_trim( packet, offset, 0 ));
  309.  
  310.     // count the received packet fragments
  311.     next_packet = packet;
  312.     fragments = 0;
  313.     total_length = ntohs( header->total_length );
  314.     // compute header checksum if set
  315.     if( header->checksum && ( ! error )){
  316.         result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
  317.         if( result <= 0 ){
  318.             return udp_release_and_return( packet, result );
  319.         }
  320.         if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_UDP, src, result, dest, result, total_length, & ip_header, & length ))){
  321.             return udp_release_and_return( packet, ERROR_CODE );
  322.         }else{
  323.             checksum = compute_checksum( 0, ip_header, length );
  324.             // the udp header checksum will be added with the first fragment later
  325.             free( ip_header );
  326.         }
  327.     }else{
  328.         header->checksum = 0;
  329.         checksum = 0;
  330.     }
  331.  
  332.     do{
  333.         ++ fragments;
  334.         length = packet_get_data_length( next_packet );
  335.         if( length <= 0 ){
  336.             return udp_release_and_return( packet, NO_DATA );
  337.         }
  338.         if( total_length < length ){
  339.             if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){
  340.                 return udp_release_and_return( packet, ERROR_CODE );
  341.             }
  342.             // add partial checksum if set
  343.             if( header->checksum ){
  344.                 checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet ));
  345.             }
  346.             // relese the rest of the packet fragments
  347.             tmp_packet = pq_next( next_packet );
  348.             while( tmp_packet ){
  349.                 next_packet = pq_detach( tmp_packet );
  350.                 pq_release( udp_globals.net_phone, packet_get_id( tmp_packet ));
  351.                 tmp_packet = next_packet;
  352.             }
  353.             // exit the loop
  354.             break;
  355.         }
  356.         total_length -= length;
  357.         // add partial checksum if set
  358.         if( header->checksum ){
  359.             checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet ));
  360.         }
  361.     }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 ));
  362.  
  363.     // check checksum
  364.     if( header->checksum ){
  365.         if( flip_checksum( compact_checksum( checksum ))){
  366.             // TODO checksum error ICMP?
  367.             // TODO remove debug dump
  368.             printf("udp check failed %x => %x\n", header->checksum, flip_checksum( compact_checksum( checksum )));
  369.             return udp_release_and_return( packet, EINVAL );
  370.         }
  371.     }
  372.  
  373.     // queue the received packet
  374.     if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){
  375.         return udp_release_and_return( packet, ERROR_CODE );
  376.     }
  377.  
  378.     // notify the destination socket
  379.     async_msg_5(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) ( ** socket ).socket_id, 0, 0, 0, ( ipcarg_t ) fragments );
  380.     return EOK;
  381. }
  382.  
  383. int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  384.     ERROR_DECLARE;
  385.  
  386.     packet_t    packet;
  387.  
  388.     * answer_count = 0;
  389.     switch( IPC_GET_METHOD( * call )){
  390.         case NET_TL_RECEIVED:
  391.             if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
  392.                 ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP, IPC_GET_ERROR( call ));
  393.             }
  394.             return ERROR_CODE;
  395.         case IPC_M_CONNECT_TO_ME:
  396.             return udp_process_client_messages( callid, * call );
  397.     }
  398.     return ENOTSUP;
  399. }
  400.  
  401. int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
  402.     int                     res;
  403.     bool                    keep_on_going = true;
  404.     socket_cores_t          local_sockets;
  405.     int                     app_phone = IPC_GET_PHONE( & call );
  406.     struct sockaddr *       addr;
  407.     size_t                  addrlen;
  408. //  fibril_rwlock_t         lock;
  409.     ipc_call_t              answer;
  410.     int                     answer_count;
  411.  
  412.     /*
  413.      * Accept the connection
  414.      *  - Answer the first IPC_M_CONNECT_ME_TO call.
  415.      */
  416.     ipc_answer_0( callid, EOK );
  417.  
  418.     // The client connection is only in one fibril and therefore no additional locks are needed.
  419.  
  420.     socket_cores_initialize( & local_sockets );
  421. //  fibril_rwlock_initialize( & lock );
  422.  
  423.     while( keep_on_going ){
  424.         // refresh data
  425.         refresh_answer( & answer, & answer_count );
  426.  
  427.         callid = async_get_call( & call );
  428. //      printf( "message %d\n", IPC_GET_METHOD( * call ));
  429.  
  430.         switch( IPC_GET_METHOD( call )){
  431.             case IPC_M_PHONE_HUNGUP:
  432.                 keep_on_going = false;
  433.                 res = EOK;
  434.                 break;
  435.             case NET_SOCKET:
  436. //              fibril_rwlock_write_lock( & lock );
  437.                 res = socket_create( & local_sockets, app_phone, NULL, SOCKET_SET_SOCKET_ID( answer ));
  438. //              fibril_rwlock_write_unlock( & lock );
  439.                 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
  440.                 * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
  441.                 answer_count = 3;
  442.                 break;
  443.             case NET_SOCKET_BIND:
  444.                 res = data_receive(( void ** ) & addr, & addrlen );
  445.                 if( res == EOK ){
  446. //                  fibril_rwlock_write_lock( & lock );
  447.                     fibril_rwlock_write_lock( & udp_globals.lock );
  448.                     res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port );
  449.                     fibril_rwlock_write_unlock( & udp_globals.lock );
  450. //                  fibril_rwlock_write_unlock( & lock );
  451.                     free( addr );
  452.                 }
  453.                 break;
  454.             case NET_SOCKET_SENDTO:
  455.                 res = data_receive(( void ** ) & addr, & addrlen );
  456.                 if( res == EOK ){
  457. //                  fibril_rwlock_read_lock( & lock );
  458.                     fibril_rwlock_read_lock( & udp_globals.lock );
  459.                     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 ));
  460.                     fibril_rwlock_read_unlock( & udp_globals.lock );
  461. //                  fibril_rwlock_read_unlock( & lock );
  462.                     free( addr );
  463.                 }
  464.                 break;
  465.             case NET_SOCKET_RECVFROM:
  466. //              fibril_rwlock_read_lock( & lock );
  467.                 fibril_rwlock_read_lock( & udp_globals.lock );
  468.                 res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
  469.                 fibril_rwlock_read_unlock( & udp_globals.lock );
  470. //              fibril_rwlock_read_unlock( & lock );
  471.                 if( res > 0 ){
  472.                     * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
  473.                     * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
  474.                     answer_count = 2;
  475.                     res = EOK;
  476.                 }
  477.                 break;
  478.             case NET_SOCKET_CLOSE:
  479. //              fibril_rwlock_write_lock( & lock );
  480.                 fibril_rwlock_write_lock( & udp_globals.lock );
  481.                 res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
  482.                 fibril_rwlock_write_unlock( & udp_globals.lock );
  483. //              fibril_rwlock_write_unlock( & lock );
  484.                 break;
  485.             case NET_SOCKET_GETSOCKOPT:
  486.             case NET_SOCKET_SETSOCKOPT:
  487.             default:
  488.                 res = ENOTSUP;
  489.                 break;
  490.         }
  491.  
  492. //      printf( "res = %d\n", res );
  493.  
  494.         answer_call( callid, res, & answer, answer_count );
  495.     }
  496.  
  497.     // TODO call socket_destroy() on all bound!
  498.     socket_cores_destroy( & local_sockets );
  499.  
  500.     return EOK;
  501. }
  502.  
  503. 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 ){
  504.     ERROR_DECLARE;
  505.  
  506.     socket_core_ref         socket;
  507.     packet_t                packet;
  508.     packet_t                next_packet;
  509.     udp_header_ref          header;
  510.     int                     index;
  511.     size_t                  total_length;
  512.     int                     result;
  513.     uint16_t                dest_port;
  514.     uint32_t                checksum;
  515.     ip_pseudo_header_ref    ip_header;
  516.     size_t                  headerlen;
  517.     device_id_t             device_id;
  518.  
  519.     ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & dest_port ));
  520.  
  521.     socket = socket_cores_find( local_sockets, socket_id );
  522.     if( ! socket ) return ENOTSOCK;
  523.  
  524.     if(( socket->port <= 0 ) && udp_globals.autobinding ){
  525.         // bind the socket to a random free port if not bound
  526.         do{
  527.             // try to find a free port
  528.             fibril_rwlock_read_unlock( & udp_globals.lock );
  529.             fibril_rwlock_write_lock( & udp_globals.lock );
  530.             // might be changed in the meantime
  531.             if( socket->port <= 0 ){
  532.                 ERROR_PROPAGATE( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ));
  533.                 // set the next port as the search starting port number
  534.                 udp_globals.last_used_port = socket->port;
  535.             }
  536.             fibril_rwlock_write_unlock( & udp_globals.lock );
  537.             fibril_rwlock_read_lock( & udp_globals.lock );
  538.             // might be changed in the meantime
  539.         }while( socket->port <= 0 );
  540.     }
  541.  
  542.     // TODO do not ask all the time
  543.     ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix ));
  544.  
  545.     // read the first packet fragment
  546.     result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, sizeof( udp_header_t ), & udp_globals.packet_dimension, addr, addrlen );
  547.     if( result < 0 ) return result;
  548.     total_length = ( size_t ) result;
  549.     if( udp_globals.checksum_computing ){
  550.         checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet ));
  551.     }else{
  552.         checksum = 0;
  553.     }
  554.     // prefix the udp header
  555.     header = PACKET_PREFIX( packet, udp_header_t );
  556.     if( ! header ){
  557.         return udp_release_and_return( packet, ENOMEM );
  558.     }
  559.     bzero( header, sizeof( * header ));
  560.     // read the rest of the packet fragments
  561.     for( index = 1; index < fragments; ++ index ){
  562.         result = tl_socket_read_packet_data( udp_globals.net_phone, & next_packet, 0, & udp_globals.packet_dimension, addr, addrlen );
  563.         if( result < 0 ){
  564.             return udp_release_and_return( packet, result );
  565.         }
  566.         packet = pq_add( packet, next_packet, index, 0 );
  567.         total_length += ( size_t ) result;
  568.         if( udp_globals.checksum_computing ){
  569.             checksum = compute_checksum( checksum, packet_get_data( next_packet ), packet_get_data_length( next_packet ));
  570.         }
  571.     }
  572.     // set the udp header
  573.     header->source_port = htons(( socket->port > 0 ) ? socket->port : 0 );
  574.     header->destination_port = htons( dest_port );
  575.     header->total_length = htons( total_length + sizeof( * header ));
  576.     header->checksum = 0;
  577.     if( udp_globals.checksum_computing ){
  578.         if( ERROR_OCCURRED( ip_get_route_req( udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, & device_id, & ip_header, & headerlen ))){
  579.             return udp_release_and_return( packet, ERROR_CODE );
  580.         }
  581.         if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + sizeof( udp_header_t )))){
  582.             free( ip_header );
  583.             return udp_release_and_return( packet, ERROR_CODE );
  584.         }
  585. /*//        TODO remove debug dump:
  586.         uint8_t *   data;
  587.         data = ip_header;
  588.         printf( "ip_header:\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", headerlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ] );
  589. */      checksum = compute_checksum( checksum, ip_header, headerlen );
  590.         checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header ));
  591.         header->checksum = htons( flip_checksum( compact_checksum( checksum )));
  592.         free( ip_header );
  593.     }else{
  594.         device_id = -1;
  595.     }
  596.     // prepare the first packet fragment
  597.     if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
  598.         return udp_release_and_return( packet, ERROR_CODE );
  599.     }
  600.     // send the packet
  601.     return ip_send_msg( udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0 );
  602. }
  603.  
  604. int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
  605.     ERROR_DECLARE;
  606.  
  607.     socket_core_ref socket;
  608.     int             packet_id;
  609.     packet_t        packet;
  610.     udp_header_ref  header;
  611.     struct sockaddr *   addr;
  612.     size_t          length;
  613.     packet_t        next_packet;
  614.     uint8_t *       data;
  615.     size_t          fragments;
  616.     size_t *        lengths;
  617.     size_t          index;
  618.     int             result;
  619.  
  620.     // find the socket
  621.     socket = socket_cores_find( local_sockets, socket_id );
  622.     if( ! socket ) return ENOTSOCK;
  623.     // get the next received packet
  624.     packet_id = dyn_fifo_value( & socket->received );
  625.     if( packet_id < 0 ) return NO_DATA;
  626.     ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
  627.     // get udp header
  628.     data = packet_get_data( packet );
  629.     if( ! data ){
  630.         pq_release( udp_globals.net_phone, packet_id );
  631.         return NO_DATA;
  632.     }
  633.     header = ( udp_header_ref ) data;
  634.  
  635.     // set the source address port
  636.     result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL );
  637.     if( ERROR_OCCURRED( tl_set_address_port( addr, result, ntohs( header->source_port )))){
  638.         pq_release( udp_globals.net_phone, packet_id );
  639.         return ERROR_CODE;
  640.     }
  641.     * addrlen = ( size_t ) result;
  642.     // send the source address
  643.     ERROR_PROPAGATE( data_reply( addr, * addrlen ));
  644.  
  645.     next_packet = pq_next( packet );
  646.     if( ! next_packet ){
  647.         // write all if only one fragment
  648.         ERROR_PROPAGATE( data_reply( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t )));
  649.         // store the total length
  650.         length = packet_get_data_length( packet ) - sizeof( udp_header_t );
  651.     }else{
  652.         // count the packet fragments
  653.         fragments = 1;
  654.         next_packet = pq_next( packet );
  655.         while(( next_packet = pq_next( next_packet ))){
  656.             ++ fragments;
  657.         }
  658.         // compute and store the fragment lengths
  659.         lengths = ( size_t * ) malloc( sizeof( size_t ) * fragments + sizeof( size_t ));
  660.         if( ! lengths ) return ENOMEM;
  661.         lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t );
  662.         lengths[ fragments ] = lengths[ 0 ];
  663.         next_packet = pq_next( packet );
  664.         for( index = 1; index < fragments; ++ index ){
  665.             lengths[ index ] = packet_get_data_length( next_packet );
  666.             lengths[ fragments ] += lengths[ index ];
  667.             next_packet = pq_next( packet );
  668.         }while( next_packet );
  669.         // write the fragment lengths
  670.         ERROR_PROPAGATE( data_reply( lengths, sizeof( int ) * ( fragments + 1 )));
  671.         // write the first fragment
  672.         ERROR_PROPAGATE( data_reply( data + sizeof( udp_header_t ), lengths[ 0 ] ));
  673.         next_packet = pq_next( packet );
  674.         // write the rest of the fragments
  675.         for( index = 1; index < fragments; ++ index ){
  676.             ERROR_PROPAGATE( data_reply( packet_get_data( next_packet ), lengths[ index ] ));
  677.             next_packet = pq_next( packet );
  678.         }while( next_packet );
  679.         // store the total length
  680.         length = lengths[ fragments ];
  681.         free( lengths );
  682.     }
  683.     // release the packet
  684.     dyn_fifo_pop( & socket->received );
  685.     pq_release( udp_globals.net_phone, packet_get_id( packet ));
  686.     // return the total length
  687.     return ( int ) length;
  688. }
  689.  
  690. int udp_release_and_return( packet_t packet, int result ){
  691.     pq_release( udp_globals.net_phone, packet_get_id( packet ));
  692.     return result;
  693. }
  694.  
  695. /** @}
  696.  */
  697.