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 < 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 = 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 = address_in6->sin6_port;
  70.             break;
  71.         default:
  72.             return EAFNOSUPPORT;
  73.     }
  74.     return EOK;
  75. }
  76.  
  77.  
  78. int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
  79.     struct sockaddr_in *    address_in;
  80.     struct sockaddr_in6 *   address_in6;
  81.     size_t                  length;
  82.  
  83.     if( addrlen < 0 ) return EINVAL;
  84.     length = ( size_t ) addrlen;
  85.     if( length < sizeof( struct sockaddr )) return EINVAL;
  86.     switch( addr->sa_family ){
  87.         case AF_INET:
  88.             if( length != sizeof( struct sockaddr_in )) return EINVAL;
  89.             address_in = ( struct sockaddr_in * ) addr;
  90.             address_in->sin_port = port;
  91.             return EOK;
  92.         case AF_INET6:
  93.             if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
  94.             address_in6 = ( struct sockaddr_in6 * ) addr;
  95.             address_in6->sin6_port = port;
  96.             return EOK;
  97.         default:
  98.             return EAFNOSUPPORT;
  99.     }
  100. }
  101.  
  102. void tl_send_icmp_port_unreachable( int packet_phone, int icmp_phone, packet_t packet, services_t error ){
  103.     packet_t    next;
  104.     uint8_t *   src;
  105.     int         length;
  106.  
  107.     // detach the first packet and release the others
  108.     next = pq_detach( packet );
  109.     if( next ){
  110.         pq_release( packet_phone, packet_get_id( next ));
  111.     }
  112.     length = packet_get_addr( packet, & src, NULL );
  113.     if(( length > 0 )
  114.     && ( ! error )
  115.     && ( icmp_phone >= 0 )
  116.     // set both addresses to the source one (avoids the source address deletion before setting the destination one)
  117.     && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
  118.         icmp_destination_unreachable_msg( icmp_phone, ICMP_PORT_UNREACH, 0, packet );
  119.     }else{
  120.         pq_release( packet_phone, packet_get_id( packet ));
  121.     }
  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.