Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2009 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 ip
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  IP client interface implementation.
  35.  *  @see ip_client.h
  36.  */
  37.  
  38. #include <errno.h>
  39.  
  40. #include <sys/types.h>
  41.  
  42. #include "../../include/ip_client.h"
  43. #include "../../include/socket_errno.h"
  44.  
  45. #include "../../structures/packet/packet.h"
  46. #include "../../structures/packet/packet_client.h"
  47.  
  48. #include "ip_header.h"
  49.  
  50. int ip_client_prepare_packet( packet_t packet, ip_protocol_t protocol, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, size_t ipopt_length ){
  51.     ip_header_ref   header;
  52.     uint8_t *       data;
  53.     size_t          padding;
  54.  
  55.     padding =  ipopt_length % 4;
  56.     if( padding ){
  57.         padding = 4 - padding;
  58.         ipopt_length += padding;
  59.     }
  60.     data = ( uint8_t * ) packet_prefix( packet, sizeof( ip_header_t ) + padding );
  61.     if( ! data ) return ENOMEM;
  62.     while( padding -- ) data[ sizeof( ip_header_t ) + padding ] = IPOPT_NOOP;
  63.     header = ( ip_header_ref ) data;
  64.     header->ihl = ( uint8_t ) ( sizeof( ip_header_t ) + ipopt_length ) / 4;
  65.     header->ttl = ttl ? (( ttl <= MAXTTL ) ? ttl : MAXTTL ) : IPDEFTTL;
  66.     header->tos = tos;
  67.     header->protocol = protocol;
  68.     if( dont_fragment ) header->flags = IPFLAG_DONT_FRAGMENT;
  69.     return EOK;
  70. }
  71.  
  72. int ip_client_process_packet( packet_t packet, ip_protocol_t * protocol, ip_ttl_t * ttl, ip_tos_t * tos, int * dont_fragment, size_t * ipopt_length ){
  73.     ip_header_ref   header;
  74.  
  75.     header = ( ip_header_ref ) packet_get_data( packet );
  76.     if(( ! header )
  77.     || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
  78.         return ENOMEM;
  79.     }
  80.     if( protocol ) * protocol = header->protocol;
  81.     if( ttl ) * ttl = header->ttl;
  82.     if( tos ) * tos = header->tos;
  83.     if( dont_fragment ) * dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT;
  84.     if( ipopt_length ){
  85.         * ipopt_length = header->ihl * 4 - sizeof( ip_header_t );
  86.         return sizeof( ip_header_t );
  87.     }else{
  88.         return header->ihl * 4;
  89.     }
  90. }
  91.  
  92. size_t ip_client_header_length( packet_t packet ){
  93.     ip_header_ref   header;
  94.  
  95.     header = ( ip_header_ref ) packet_get_data( packet );
  96.     if(( ! header )
  97.     || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
  98.         return 0;
  99.     }
  100.     return header->ihl * 4u;
  101. }
  102.  
  103. int ip_client_set_pseudo_header_data_length( ip_pseudo_header_ref header, size_t headerlen, size_t data_length ){
  104.     ipv4_pseudo_header_ref  header_in;
  105.  
  106.     if( headerlen == sizeof( ipv4_pseudo_header_t )){
  107.         header_in = ( ipv4_pseudo_header_ref ) header;
  108.         header_in->data_length = htons( data_length );
  109.         return EOK;
  110.     }else{
  111.         return EINVAL;
  112.     }
  113. }
  114.  
  115. int ip_client_get_pseudo_header( ip_protocol_t protocol, struct sockaddr * src, socklen_t srclen, struct sockaddr * dest, socklen_t destlen, size_t data_length, ip_pseudo_header_ref * header, size_t * headerlen ){
  116.     ipv4_pseudo_header_ref  header_in;
  117.     struct sockaddr_in *    address_in;
  118.  
  119.     if( !( header && headerlen )) return EBADMEM;
  120.     if( !( src && dest && ( srclen >= sizeof( struct sockaddr )) && ( srclen == destlen ) && ( src->sa_family == dest->sa_family ))) return EINVAL;
  121.     switch( src->sa_family ){
  122.         case AF_INET:
  123.             if( srclen != sizeof( struct sockaddr_in )) return EINVAL;
  124.             * headerlen = sizeof( * header_in );
  125.             header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
  126.             if( ! header_in ) return ENOMEM;
  127.             bzero( header_in, * headerlen );
  128.             address_in = ( struct sockaddr_in * ) dest;
  129.             header_in->destination_address = address_in->sin_addr.s_addr;
  130.             address_in = ( struct sockaddr_in * ) src;
  131.             header_in->source_address = address_in->sin_addr.s_addr;
  132.             header_in->protocol = protocol;
  133.             header_in->data_length = htons( data_length );
  134.             * header = ( ip_pseudo_header_ref ) header_in;
  135.             return EOK;
  136.         // TODO IPv6
  137. /*      case AF_INET6:
  138.             if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
  139.             address_in6 = ( struct sockaddr_in6 * ) addr;
  140.             return EOK;
  141. */      default:
  142.             return EAFNOSUPPORT;
  143.     }
  144. }
  145.  
  146. /** @}
  147.  */
  148.