Changeset 7715994 in mainline for uspace/srv/net/inet.c


Ignore:
Timestamp:
2010-03-13T12:17:02Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6ba20a6b
Parents:
d0febca (diff), 2070570 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    rd0febca r7715994  
    4545#include "include/socket_codes.h"
    4646
    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;
     47int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
     48        if((! data) || (! address)){
     49                return EINVAL;
     50        }
    5651
    57         if( ! data ) return EINVAL;
    58         switch( family ){
     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
     74int 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){
    5997                case AF_INET:
    6098                        count = 4;
     
    70108                        return ENOTSUP;
    71109        }
    72         if( ! address ){
    73                 bzero( data, count );
     110
     111        // erase if no address
     112        if(! address){
     113                bzero(data, count);
    74114                return ENOENT;
    75115        }
     116
     117        // process the string from the beginning
    76118        next = address;
    77119        index = 0;
    78120        do{
    79                 if( next && ( * next )){
    80                         if( index ) ++ next;
    81                         value = strtoul( next, & last, base );
     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
    82134                        next = last;
     135
     136                        // fill the address data byte by byte
    83137                        shift = bytes - 1;
    84138                        do{
    85139                                // like little endian
    86                                 data[ index + shift ] = value;
     140                                data[index + shift] = value;
    87141                                value >>= 8;
    88                         }while( shift -- );
     142                        }while(shift --);
     143
    89144                        index += bytes;
    90145                }else{
    91                         bzero( data + index, count - index );
     146                        // erase the rest of the address
     147                        bzero(data + index, count - index);
    92148                        return EOK;
    93149                }
    94         }while( index < count );
     150        }while(index < count);
     151
    95152        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         }
    109153}
    110154
Note: See TracChangeset for help on using the changeset viewer.