Subversion Repositories HelenOS

Rev

Rev 4578 | 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.  *  Internet protocol address conversion functions implementation.
  35.  */
  36.  
  37. #include <errno.h>
  38. #include <mem.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #include "include/in.h"
  43. #include "include/in6.h"
  44. #include "include/inet.h"
  45. #include "include/socket.h"
  46.  
  47. int inet_pton( uint16_t family, const char * address, uint8_t * data ){
  48.     const char *    next;
  49.     char *          last;
  50.     int             index;
  51.     int             count;
  52.     int             base;
  53.     size_t          bytes;
  54.     size_t          shift;
  55.     unsigned long   value;
  56.  
  57.     if( ! data ) return EINVAL;
  58.     switch( family ){
  59.         case AF_INET:
  60.             count = 4;
  61.             base = 10;
  62.             bytes = 1;
  63.             break;
  64.         case AF_INET6:
  65.             count = 16;
  66.             base = 16;
  67.             bytes = 4;
  68.             break;
  69.         default:
  70.             return ENOTSUP;
  71.     }
  72.     if( ! address ){
  73.         bzero( data, count );
  74.         return ENOENT;
  75.     }
  76.     next = address;
  77.     index = 0;
  78.     do{
  79.         if( next && ( * next )){
  80.             if( index ) ++ next;
  81.             value = strtoul( next, & last, base );
  82.             next = last;
  83.             shift = bytes - 1;
  84.             do{
  85.                 // like little endian
  86.                 data[ index + shift ] = value;
  87.                 value >>= 8;
  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.