Subversion Repositories HelenOS

Rev

Rev 4579 | Rev 4700 | 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.  */
  35.  
  36. #include <async.h>
  37. #include <malloc.h>
  38. #include <stdio.h>
  39.  
  40. #include <ipc/ipc.h>
  41. #include <ipc/services.h>
  42.  
  43. #include "../../err.h"
  44. #include "../../messages.h"
  45. #include "../../modules.h"
  46.  
  47. #include "../../structures/dynamic_fifo.h"
  48. #include "../../structures/packet/packet_client.h"
  49.  
  50. #include "../../include/in.h"
  51. #include "../../include/inet.h"
  52. #include "../../include/ip_client.h"
  53. #include "../../include/ip_interface.h"
  54. #include "../../include/ip_protocols.h"
  55. #include "../../include/socket.h"
  56. #include "../../include/socket_errno.h"
  57.  
  58. #include "../../socket/socket_core.h"
  59. #include "../../socket/socket_messages.h"
  60.  
  61. #include "../tl_messages.h"
  62.  
  63. #include "udp.h"
  64. #include "udp_header.h"
  65. #include "udp_module.h"
  66.  
  67. #define MAX_UDP_FRAGMENT_SIZE   65535
  68.  
  69. int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
  70. int udp_process_packet( packet_t packet );
  71. int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
  72. int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags );
  73. int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
  74. int socket_read_data( void ** data, size_t * length );
  75. int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in );
  76. int socket_write_data( void * data, size_t data_length );
  77.  
  78. udp_globals_t   udp_globals;
  79.  
  80. /** Initializes the module.
  81.  */
  82. int udp_initialize( async_client_conn_t client_connection ){
  83.     ERROR_DECLARE;
  84.  
  85.     udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
  86.     if( udp_globals.ip_phone < 0 ){
  87.         return udp_globals.ip_phone;
  88.     }
  89.     ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
  90.     ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
  91.     udp_globals.prefix += sizeof( udp_header_t );
  92.     udp_globals.content -= sizeof( udp_header_t );
  93.     return EOK;
  94. }
  95.  
  96. int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver ){
  97.     ERROR_DECLARE;
  98.  
  99.     if( ERROR_OCCURRED( udp_process_packet( packet ))){
  100.         pq_release( udp_globals.net_phone, packet_get_id( packet ));
  101.         return ERROR_CODE;
  102.     }
  103.  
  104.     return EOK;
  105. }
  106.  
  107. int udp_process_packet( packet_t packet ){
  108.     ERROR_DECLARE;
  109.  
  110.     uint8_t *       src;
  111.     uint8_t *       dest;
  112.     int             length;
  113.     void *          data;
  114.     udp_header_ref  header;
  115.     socket_core_ref *   socket;
  116.     packet_t        next_packet;
  117.     int             total_length;
  118. //  uint16_t        checksum;
  119.     int             fragments;
  120.     packet_t        tmp_packet;
  121.  
  122.     // get packet data
  123.     length = packet_get_addr( packet, & src, & dest );
  124.     if( length < 0 ) return length;
  125.     if( length != sizeof( in_addr_t )) return EINVAL;
  126.     // TODO received ipopts?
  127.     ERROR_PROPAGATE( ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ));
  128.  
  129.     //  TODO remove debug dump:
  130. /*  uint8_t *   rdata;
  131.     rdata = packet_get_data( packet );
  132.     printf( "Receiving udp packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( packet ), packet_get_data_length( packet ), rdata[ 0 ], rdata[ 1 ], rdata[ 2 ], rdata[ 3 ], rdata[ 4 ], rdata[ 5 ], rdata[ 6 ], rdata[ 7 ], rdata[ 8 ], rdata[ 9 ], rdata[ 10 ], rdata[ 11 ], rdata[ 12 ], rdata[ 13 ], rdata[ 14 ], rdata[ 15 ], rdata[ 16 ], rdata[ 17 ], rdata[ 18 ], rdata[ 19 ], rdata[ 20 ], rdata[ 21 ], rdata[ 22 ], rdata[ 23 ], rdata[ 24 ], rdata[ 25 ], rdata[ 26 ], rdata[ 27 ], rdata[ 28 ], rdata[ 29 ], rdata[ 30 ], rdata[ 31 ], rdata[ 32 ], rdata[ 33 ], rdata[ 34 ], rdata[ 35 ], rdata[ 36 ], rdata[ 37 ], rdata[ 38 ], rdata[ 39 ], rdata[ 40 ], rdata[ 41 ], rdata[ 42 ], rdata[ 43 ], rdata[ 44 ], rdata[ 45 ], rdata[ 46 ], rdata[ 47 ], rdata[ 48 ], rdata[ 49 ], rdata[ 50 ], rdata[ 51 ], rdata[ 52 ], rdata[ 53 ], rdata[ 54 ], rdata[ 55 ], rdata[ 56 ], rdata[ 57 ], rdata[ 58 ], rdata[ 59 ] );
  133. */
  134.     length = packet_get_data_length( packet );
  135.     if( length < 0 ) return length;
  136.     if( length < sizeof( udp_header_t )) return NO_DATA;
  137.     data = packet_get_data( packet );
  138.     if( ! data ) return NO_DATA;
  139.     // get udp header
  140.     header = ( udp_header_ref ) data;
  141.     // find the destination socket
  142.     socket = socket_ports_find( & udp_globals.sockets, ntohs( header->dest ));
  143.     if( ! socket ) return EADDRNOTAVAIL;
  144.     // count the received packet fragments
  145.     next_packet = packet;
  146.     fragments = 0;
  147.     total_length = ntohs( header->len );
  148.     do{
  149.         ++ fragments;
  150.         length = packet_get_data_length( packet );
  151.         if( length < 0 ) return length;
  152.         if( ! length ) return NO_DATA;
  153.         if( total_length < length ){
  154.             // cut of the suffix if too long
  155.             ERROR_PROPAGATE( packet_trim( next_packet, 0, length - total_length ));
  156.             // relese the rest of the packet fragments
  157.             tmp_packet = pq_next( next_packet );
  158.             while( tmp_packet ){
  159.                 next_packet = pq_detach( tmp_packet );
  160.                 pq_release( udp_globals.net_phone, packet_get_id( tmp_packet ));
  161.                 tmp_packet = next_packet;
  162.             }
  163.             break;
  164.         }
  165.         total_length -= length;
  166.     /*  if( header->header_checksum ){
  167.         }
  168.     */
  169.     }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 ));
  170.     // queue the received packet
  171.     ERROR_PROPAGATE( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ));
  172.  
  173.     // notify the destination socket
  174.     async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ** socket ).socket_id, fragments );
  175.     return EOK;
  176. }
  177.  
  178. int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  179.     ERROR_DECLARE;
  180.  
  181.     packet_t    packet;
  182.  
  183.     * answer_count = 0;
  184.     switch( IPC_GET_METHOD( * call )){
  185.         case NET_TL_RECEIVED:
  186.             ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
  187.             return udp_received_msg( IPC_GET_DEVICE( call ), packet, 0 );
  188.         case IPC_M_CONNECT_TO_ME:
  189.             return process_client_messages( callid, call, answer, answer_count );
  190.     }
  191.     return ENOTSUP;
  192. }
  193.  
  194. int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  195.     ERROR_DECLARE;
  196.  
  197.     int                     res;
  198.     bool                    keep_on_going = true;
  199.     socket_cores_t          local_sockets;
  200.     int                     app_phone = IPC_GET_PHONE( call );
  201.     void *                  addr;
  202.     size_t                  addrlen;
  203.  
  204.     /*
  205.      * Accept the connection
  206.      *  - Answer the first IPC_M_CONNECT_ME_TO call.
  207.      */
  208.     ipc_answer_0( callid, EOK );
  209.  
  210.     socket_cores_initialize( & local_sockets );
  211.  
  212.     while( keep_on_going ){
  213.         // refresh data
  214.         * answer_count = 0;
  215.         IPC_SET_RETVAL( * answer, 0 );
  216.         // just to be precize
  217.         IPC_SET_METHOD( * answer, 0 );
  218.         IPC_SET_ARG1( * answer, 0 );
  219.         IPC_SET_ARG2( * answer, 0 );
  220.         IPC_SET_ARG3( * answer, 0 );
  221.         IPC_SET_ARG4( * answer, 0 );
  222.         IPC_SET_ARG5( * answer, 0 );
  223.  
  224.         callid = async_get_call( call );
  225. //      printf( "message %d\n", IPC_GET_METHOD( * call ));
  226.  
  227.         switch( IPC_GET_METHOD( * call )){
  228.             case IPC_M_PHONE_HUNGUP:
  229.                 keep_on_going = false;
  230.                 res = EOK;
  231.                 break;
  232.             case NET_SOCKET:
  233.                 res = socket_create( & local_sockets, app_phone, SOCKET_SET_SOCKET_ID( answer ));
  234.                 * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
  235.                 * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
  236.                 * answer_count = 3;
  237.                 break;
  238.             case NET_SOCKET_BIND:
  239.                 if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
  240.                     res = ERROR_CODE;
  241.                     break;
  242.                 }
  243.                 res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
  244.                 free( addr );
  245.                 break;
  246.             case NET_SOCKET_SENDTO:
  247.                 if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
  248.                     res = ERROR_CODE;
  249.                     break;
  250.                 }
  251.                 res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_FLAGS( call ));
  252.                 free( addr );
  253.                 break;
  254.             case NET_SOCKET_RECVFROM:
  255.                 res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ));
  256.                 if( res > 0 ){
  257.                     * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
  258.                     * answer_count = 1;
  259.                     res = EOK;
  260.                 }
  261.                 break;
  262.             case NET_SOCKET_CLOSE:
  263.                 res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
  264.                 break;
  265.             case NET_SOCKET_GETSOCKOPT:
  266.             case NET_SOCKET_SETSOCKOPT:
  267.             default:
  268.                 res = ENOTSUP;
  269.                 break;
  270.         }
  271.  
  272. //      printf( "res = %d\n", res );
  273.  
  274.         switch( * answer_count ){
  275.             case 0:     ipc_answer_0( callid, res );
  276.                     continue;
  277.             case 1:     ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
  278.                     continue;
  279.             case 2:     ipc_answer_2( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ));
  280.                     continue;
  281.             case 3:     ipc_answer_3( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ));
  282.                     continue;
  283.             case 4:     ipc_answer_4( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ));
  284.                     continue;
  285.             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 ));
  286.                     continue;
  287.         }
  288.     }
  289.  
  290.     socket_cores_destroy( & local_sockets );
  291.  
  292.     return EOK;
  293. }
  294.  
  295. int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags ){
  296.     ERROR_DECLARE;
  297.  
  298.     socket_core_ref         socket;
  299.     struct sockaddr *       address;
  300.     struct sockaddr_in *    address_in;
  301.     packet_t                packet;
  302.     packet_t                next_packet;
  303.     udp_header_ref          header;
  304.     int                     index;
  305.     int                     total_length;
  306.     int                     length;
  307.  
  308.     if( addrlen < sizeof( struct sockaddr )) return EINVAL;
  309.     address = ( struct sockaddr * ) addr;
  310.     switch( address->sa_family ){
  311.         case AF_INET:
  312.             if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
  313.             address_in = ( struct sockaddr_in * ) addr;
  314.             socket = socket_cores_find( local_sockets, socket_id );
  315.             if( ! socket ) return ENOTSOCK;
  316.  
  317.             // TODO do not ask all the time
  318.             ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
  319.  
  320.             // read the first packet fragment
  321.             total_length = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in );
  322.             if( total_length < 0 ) return total_length;
  323.             // prefix the udp header
  324.             header = PACKET_PREFIX( packet, udp_header_t );
  325.             if( ! header ){
  326.                 pq_release( udp_globals.net_phone, packet_get_id( packet ));
  327.                 return ENOMEM;
  328.             }
  329.             // read the rest of the packet fragments
  330.             for( index = 1; index < fragments; ++ index ){
  331.                 length = socket_read_packet_data( & next_packet, 0, address_in );
  332.                 if( length < 0 ){
  333.                     pq_release( udp_globals.net_phone, packet_get_id( packet ));
  334.                     return length;
  335.                 }
  336.                 packet = pq_add( packet, next_packet, index, 0 );
  337.                 total_length += length;
  338.             }
  339.             // set the udp header
  340.             header->source = ( socket->port < 0 ) ? 0 : htons( socket->port );
  341.             header->dest = htons( address_in->sin_port );
  342.             header->len = htons( total_length + sizeof( udp_header_t ));
  343.             // TODO my ip address for the pseudo header checksum
  344.             header->check = 0;
  345.             // prepare the first packet fragment
  346.             if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
  347.                 pq_release( udp_globals.net_phone, packet_get_id( packet ));
  348.                 return ERROR_CODE;
  349.             }
  350.             // send the packet
  351.             return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP );
  352.         // TODO IPv6
  353.         default:
  354.             return EAFNOSUPPORT;
  355.     }
  356.     return EOK;
  357. }
  358.  
  359. int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){
  360.     ERROR_DECLARE;
  361.  
  362.     socket_core_ref socket;
  363.     int             packet_id;
  364.     packet_t        packet;
  365.     udp_header_ref  header;
  366.     struct sockaddr_in      address;
  367.     int             length;
  368.     packet_t        next_packet;
  369.     void *          data;
  370.     int             fragments;
  371.     int *           lengths;
  372.     int             index;
  373.     uint8_t *       addr;
  374.  
  375.     // find the socket
  376.     socket = socket_cores_find( local_sockets, socket_id );
  377.     if( ! socket ) return ENOTSOCK;
  378.     // get the next received packet
  379.     packet_id = dyn_fifo_value( & socket->received );
  380.     if( packet_id < 0 ) return packet_id;
  381.     ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
  382.     // get udp header
  383.     data = packet_get_data( packet );
  384.     if( ! data ){
  385.         pq_release( udp_globals.net_phone, packet_id );
  386.         return NO_DATA;
  387.     }
  388.     header = ( udp_header_ref ) data;
  389.     // set the source address
  390.     address.sin_family = PF_INET;
  391.     address.sin_port = ntohs( header->dest );
  392.     length = packet_get_addr( packet, & addr, NULL );
  393.     if( length != sizeof( address.sin_addr.s_addr )){
  394.         pq_release( udp_globals.net_phone, packet_id );
  395.         return EINVAL;
  396.     }
  397.     address.sin_addr.s_addr = *(( uint32_t * ) addr );
  398.     bzero( & address.sin_zero, sizeof( address.sin_zero ));
  399.     // send the source address
  400.     ERROR_PROPAGATE( socket_write_data( & address, sizeof( address )));
  401.     next_packet = pq_next( packet );
  402.     if( ! next_packet ){
  403.         // write all if only one fragment
  404.         ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t )));
  405.         // store the total length
  406.         length = packet_get_data_length( packet ) - sizeof( udp_header_t );
  407.     }else{
  408.         // count the packet fragments
  409.         fragments = 1;
  410.         next_packet = pq_next( packet );
  411.         while(( next_packet = pq_next( next_packet ))){
  412.             ++ fragments;
  413.         }
  414.         // compute and store the fragment lengths
  415.         lengths = ( int * ) malloc( sizeof( int ) * ( fragments + 1 ));
  416.         if( ! lengths ) return ENOMEM;
  417.         lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t );
  418.         lengths[ fragments ] = lengths[ 0 ];
  419.         next_packet = pq_next( packet );
  420.         for( index = 1; index < fragments; ++ index ){
  421.             lengths[ index ] = packet_get_data_length( next_packet );
  422.             lengths[ fragments ] += lengths[ index ];
  423.             next_packet = pq_next( packet );
  424.         }while( next_packet );
  425.         // write the fragment lengths
  426.         ERROR_PROPAGATE( socket_write_data( lengths, sizeof( int ) * ( fragments + 1 )));
  427.         // write the first fragment
  428.         ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), lengths[ 0 ] ));
  429.         next_packet = pq_next( packet );
  430.         // write the rest of the fragments
  431.         for( index = 1; index < fragments; ++ index ){
  432.             ERROR_PROPAGATE( socket_write_data( packet_get_data( next_packet ), lengths[ index ] ));
  433.             next_packet = pq_next( packet );
  434.         }while( next_packet );
  435.         // store the total length
  436.         length = lengths[ fragments ];
  437.         free( lengths );
  438.     }
  439.     // release the packet
  440.     dyn_fifo_pop( & socket->received );
  441.     pq_release( udp_globals.net_phone, packet_get_id( packet ));
  442.     // return the total length
  443.     return length;
  444. }
  445.  
  446. int socket_write_data( void * data, size_t data_length ){
  447.     size_t          length;
  448.     ipc_callid_t    callid;
  449.  
  450.     if(( ! ipc_data_read_receive( & callid, & length ))
  451.     || ( length < data_length )){
  452.         return EINVAL;
  453.     }
  454.     return ipc_data_read_finalize( callid, data, data_length );
  455. }
  456.  
  457. int socket_read_data( void ** data, size_t * length ){
  458.     ERROR_DECLARE;
  459.  
  460.     ipc_callid_t    callid;
  461.  
  462.     if( !( data && length )) return EBADMEM;
  463.     if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
  464.     * data = malloc( * length );
  465.     if( ! data ) return ENOMEM;
  466.     if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
  467.         free( data );
  468.         return ERROR_CODE;
  469.     }
  470.     return EOK;
  471. }
  472.  
  473. int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in ){
  474.     ERROR_DECLARE;
  475.  
  476.     ipc_callid_t    callid;
  477.     size_t          length;
  478.     void *          data;
  479.  
  480.     // get the data length
  481.     if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
  482.     // get a new packet
  483.     * packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, prefix + udp_globals.prefix, udp_globals.suffix );
  484.     if( ! packet ) return ENOMEM;
  485.     // allocate space in the packet
  486.     data = packet_suffix( * packet, length );
  487.     if( ! data ){
  488.         pq_release( udp_globals.net_phone, packet_get_id( * packet ));
  489.         return ENOMEM;
  490.     }
  491.     // read the data into the packet
  492.     if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
  493.     // set the packet destination address
  494.     || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
  495.         pq_release( udp_globals.net_phone, packet_get_id( * packet ));
  496.         return ERROR_CODE;
  497.     }
  498.     return length;
  499. }
  500.  
  501. /** @}
  502.  */
  503.