Subversion Repositories HelenOS

Rev

Rev 4738 | 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 net_tl
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Transport layer common functions implementation.
  35.  *  @see tl_common.h
  36.  */
  37.  
  38. #include <async.h>
  39. #include <ipc/services.h>
  40.  
  41. #include "../err.h"
  42.  
  43. #include "../structures/packet/packet.h"
  44. #include "../structures/packet/packet_client.h"
  45.  
  46. #include "../include/icmp_interface.h"
  47. #include "../include/in.h"
  48. #include "../include/in6.h"
  49. #include "../include/inet.h"
  50. #include "../include/socket_codes.h"
  51. #include "../include/socket_errno.h"
  52.  
  53. #include "tl_common.h"
  54.  
  55. int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){
  56.     const struct sockaddr_in *  address_in;
  57.     const struct sockaddr_in6 * address_in6;
  58.  
  59.     if(( addrlen <= 0 ) || (( size_t ) addrlen < sizeof( struct sockaddr ))) return EINVAL;
  60.     switch( addr->sa_family ){
  61.         case AF_INET:
  62.             if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
  63.             address_in = ( struct sockaddr_in * ) addr;
  64.             * port = ntohs( address_in->sin_port );
  65.             break;
  66.         case AF_INET6:
  67.             if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
  68.             address_in6 = ( struct sockaddr_in6 * ) addr;
  69.             * port = ntohs( address_in6->sin6_port );
  70.             break;
  71.         default:
  72.             return EAFNOSUPPORT;
  73.     }
  74.     return EOK;
  75. }
  76.  
  77. int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
  78.     struct sockaddr_in *    address_in;
  79.     struct sockaddr_in6 *   address_in6;
  80.     size_t                  length;
  81.  
  82.     if( addrlen < 0 ) return EINVAL;
  83.     length = ( size_t ) addrlen;
  84.     if( length < sizeof( struct sockaddr )) return EINVAL;
  85.     switch( addr->sa_family ){
  86.         case AF_INET:
  87.             if( length != sizeof( struct sockaddr_in )) return EINVAL;
  88.             address_in = ( struct sockaddr_in * ) addr;
  89.             address_in->sin_port = htons( port );
  90.             return EOK;
  91.         case AF_INET6:
  92.             if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
  93.             address_in6 = ( struct sockaddr_in6 * ) addr;
  94.             address_in6->sin6_port = htons( port );
  95.             return EOK;
  96.         default:
  97.             return EAFNOSUPPORT;
  98.     }
  99. }
  100.  
  101. int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){
  102.     packet_t    next;
  103.     uint8_t *   src;
  104.     int         length;
  105.  
  106.     // detach the first packet and release the others
  107.     next = pq_detach( packet );
  108.     if( next ){
  109.         pq_release( packet_phone, packet_get_id( next ));
  110.     }
  111.     length = packet_get_addr( packet, & src, NULL );
  112.     if(( length > 0 )
  113.     && ( ! error )
  114.     && ( icmp_phone >= 0 )
  115.     // set both addresses to the source one (avoids the source address deletion before setting the destination one)
  116.     && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
  117.         return EOK;
  118.     }else{
  119.         pq_release( packet_phone, packet_get_id( packet ));
  120.     }
  121.     return ENOENT;
  122. }
  123.  
  124. int tl_socket_read_packet_data( int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen ){
  125.     ERROR_DECLARE;
  126.  
  127.     ipc_callid_t    callid;
  128.     size_t          length;
  129.     void *          data;
  130.  
  131.     if( ! dimension ) return EINVAL;
  132.     // get the data length
  133.     if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
  134.     // get a new packet
  135.     * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix );
  136.     if( ! packet ) return ENOMEM;
  137.     // allocate space in the packet
  138.     data = packet_suffix( * packet, length );
  139.     if( ! data ){
  140.         pq_release( packet_phone, packet_get_id( * packet ));
  141.         return ENOMEM;
  142.     }
  143.     // read the data into the packet
  144.     if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
  145.     // set the packet destination address
  146.     || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){
  147.         pq_release( packet_phone, packet_get_id( * packet ));
  148.         return ERROR_CODE;
  149.     }
  150.     return ( int ) length;
  151. }
  152.  
  153. /** @}
  154.  */
  155.