Subversion Repositories HelenOS

Rev

Rev 4743 | 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->header_length = IP_COMPUTE_HEADER_LENGTH( sizeof( ip_header_t ) + ipopt_length );
  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 = IP_HEADER_LENGTH( header ) - sizeof( ip_header_t );
  86.         return sizeof( ip_header_t );
  87.     }else{
  88.         return IP_HEADER_LENGTH( header );
  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 IP_HEADER_LENGTH( header );
  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( ! header ) return EBADMEM;
  107.     if( headerlen == sizeof( ipv4_pseudo_header_t )){
  108.         header_in = ( ipv4_pseudo_header_ref ) header;
  109.         header_in->data_length = htons( data_length );
  110.         return EOK;
  111.     }else{
  112.         return EINVAL;
  113.     }
  114. }
  115.  
  116. 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 ){
  117.     ipv4_pseudo_header_ref  header_in;
  118.     struct sockaddr_in *    address_in;
  119.  
  120.     if( !( header && headerlen )) return EBADMEM;
  121.     if( !( src && dest && ( srclen > 0 ) && (( size_t ) srclen >= sizeof( struct sockaddr )) && ( srclen == destlen ) && ( src->sa_family == dest->sa_family ))) return EINVAL;
  122.     switch( src->sa_family ){
  123.         case AF_INET:
  124.             if( srclen != sizeof( struct sockaddr_in )) return EINVAL;
  125.             * headerlen = sizeof( * header_in );
  126.             header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
  127.             if( ! header_in ) return ENOMEM;
  128.             bzero( header_in, * headerlen );
  129.             address_in = ( struct sockaddr_in * ) dest;
  130.             header_in->destination_address = address_in->sin_addr.s_addr;
  131.             address_in = ( struct sockaddr_in * ) src;
  132.             header_in->source_address = address_in->sin_addr.s_addr;
  133.             header_in->protocol = protocol;
  134.             header_in->data_length = htons( data_length );
  135.             * header = ( ip_pseudo_header_ref ) header_in;
  136.             return EOK;
  137.         // TODO IPv6
  138. /*      case AF_INET6:
  139.             if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
  140.             address_in6 = ( struct sockaddr_in6 * ) addr;
  141.             return EOK;
  142. */      default:
  143.             return EAFNOSUPPORT;
  144.     }
  145. }
  146.  
  147. /** @}
  148.  */
  149.