Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4261 | 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 net
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  
  35.  */
  36.  
  37. #include <mem.h>
  38. #include <stdio.h>
  39.  
  40. #include "include/sockaddr.h"
  41. #include "include/socket.h"
  42.  
  43. #include "err.h"
  44.  
  45. int inet_pton( uint16_t family, const char * address, uint8_t * data ){
  46.     const char *    next;
  47.     char *          last;
  48.     int             index;
  49.     int             count;
  50.     int             base;
  51.     size_t          bytes;
  52.     size_t          shift;
  53.     unsigned long   value;
  54.  
  55.     if( ! data ) return EINVAL;
  56.     switch( family ){
  57.         case AF_INET:
  58.             count = 4;
  59.             base = 10;
  60.             bytes = 1;
  61.             break;
  62.         case AF_INET6:
  63.             count = 16;
  64.             base = 16;
  65.             bytes = 4;
  66.             break;
  67.         default:
  68.             return ENOTSUP;
  69.     }
  70.     if( ! address ){
  71.         bzero( data, count );
  72.         return ENOENT;
  73.     }
  74.     next = address;
  75.     index = 0;
  76.     do{
  77.         if( next && ( * next )){
  78.             if( index ) ++ next;
  79.             value = strtoul( next, & last, base );
  80.             next = last;
  81.             shift = bytes - 1;
  82.             do{
  83.                 // like little endian
  84.                 data[ index + shift ] = value;
  85. //              data[ index ] = value;
  86.                 value >>= 8;
  87. //              ++ index;
  88.             }while( shift -- );
  89.             index += bytes;
  90.         }else{
  91.             bzero( data + index, count - index );
  92.             return EOK;
  93.         }
  94.     }while( index < count );
  95.     return EOK;
  96. }
  97.  
  98. int inet_ntop( uint16_t family, const uint8_t * data, char * address, size_t length ){
  99.     if(( ! data ) || ( ! address )) return EINVAL;
  100.     switch( family ){
  101.         case AF_INET:   if( length < INET_ADDRSTRLEN ) return ENOMEM;
  102.                         snprintf( address, length, "%hhu.%hhu.%hhu.%hhu", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ] );
  103.                         return EOK;
  104.         case AF_INET6:  if( length < INET6_ADDRSTRLEN ) return ENOMEM;
  105.                         snprintf( address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] );
  106.                         return EOK;
  107.         default:        return ENOTSUP;
  108.     }
  109. }
  110.  
  111. /** @}
  112.  */
  113.