Changes in uspace/srv/net/inet.c [a64c64d:21580dd] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/inet.c

    ra64c64d r21580dd  
    4545#include "include/socket_codes.h"
    4646
    47 int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
    48         if((! data) || (! address)){
    49                 return EINVAL;
    50         }
     47int 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;
    5156
    52         switch(family){
    53                 case AF_INET:
    54                         // check the output buffer size
    55                         if(length < INET_ADDRSTRLEN){
    56                                 return ENOMEM;
    57                         }
    58                         // fill the buffer with the IPv4 address
    59                         snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
    60                         return EOK;
    61                 case AF_INET6:
    62                         // check the output buffer size
    63                         if(length < INET6_ADDRSTRLEN){
    64                                 return ENOMEM;
    65                         }
    66                         // fill the buffer with the IPv6 address
    67                         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]);
    68                         return EOK;
    69                 default:
    70                         return ENOTSUP;
    71         }
    72 }
    73 
    74 int inet_pton(uint16_t family, const char * address, uint8_t * data){
    75         /** The base number of the values.
    76          */
    77         int base;
    78         /** The number of bytes per a section.
    79          */
    80         size_t bytes;
    81         /** The number of bytes of the address data.
    82          */
    83         int count;
    84 
    85         const char * next;
    86         char * last;
    87         int index;
    88         size_t shift;
    89         unsigned long value;
    90 
    91         if(! data){
    92                 return EINVAL;
    93         }
    94 
    95         // set the processing parameters
    96         switch(family){
     57        if( ! data ) return EINVAL;
     58        switch( family ){
    9759                case AF_INET:
    9860                        count = 4;
     
    10870                        return ENOTSUP;
    10971        }
    110 
    111         // erase if no address
    112         if(! address){
    113                 bzero(data, count);
     72        if( ! address ){
     73                bzero( data, count );
    11474                return ENOENT;
    11575        }
    116 
    117         // process the string from the beginning
    11876        next = address;
    11977        index = 0;
    12078        do{
    121                 // if the actual character is set
    122                 if(next && (*next)){
    123 
    124                         // if not on the first character
    125                         if(index){
    126                                 // move to the next character
    127                                 ++ next;
    128                         }
    129 
    130                         // parse the actual integral value
    131                         value = strtoul(next, &last, base);
    132                         // remember the last problematic character
    133                         // should be either '.' or ':' but is ignored to be more generic
     79                if( next && ( * next )){
     80                        if( index ) ++ next;
     81                        value = strtoul( next, & last, base );
    13482                        next = last;
    135 
    136                         // fill the address data byte by byte
    13783                        shift = bytes - 1;
    13884                        do{
    13985                                // like little endian
    140                                 data[index + shift] = value;
     86                                data[ index + shift ] = value;
    14187                                value >>= 8;
    142                         }while(shift --);
    143 
     88                        }while( shift -- );
    14489                        index += bytes;
    14590                }else{
    146                         // erase the rest of the address
    147                         bzero(data + index, count - index);
     91                        bzero( data + index, count - index );
    14892                        return EOK;
    14993                }
    150         }while(index < count);
     94        }while( index < count );
     95        return EOK;
     96}
    15197
    152         return EOK;
     98int 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        }
    153109}
    154110
Note: See TracChangeset for help on using the changeset viewer.