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


Ignore:
File:
1 edited

Legend:

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

    ra64c64d raadf01e  
    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         }
    51 
    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 
    7447int 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 
    8548        const char * next;
    8649        char * last;
    8750        int index;
     51        int count;
     52        int base;
     53        size_t bytes;
    8854        size_t shift;
    8955        unsigned long value;
     
    9258                return EINVAL;
    9359        }
    94 
    95         // set the processing parameters
    9660        switch(family){
    9761                case AF_INET:
     
    10872                        return ENOTSUP;
    10973        }
    110 
    111         // erase if no address
    11274        if(! address){
    11375                bzero(data, count);
    11476                return ENOENT;
    11577        }
    116 
    117         // process the string from the beginning
    11878        next = address;
    11979        index = 0;
    12080        do{
    121                 // if the actual character is set
    12281                if(next && (*next)){
    123 
    124                         // if not on the first character
    12582                        if(index){
    126                                 // move to the next character
    12783                                ++ next;
    12884                        }
    129 
    130                         // parse the actual integral value
    13185                        value = strtoul(next, &last, base);
    132                         // remember the last problematic character
    133                         // should be either '.' or ':' but is ignored to be more generic
    13486                        next = last;
    135 
    136                         // fill the address data byte by byte
    13787                        shift = bytes - 1;
    13888                        do{
     
    14191                                value >>= 8;
    14292                        }while(shift --);
    143 
    14493                        index += bytes;
    14594                }else{
    146                         // erase the rest of the address
    14795                        bzero(data + index, count - index);
    14896                        return EOK;
    14997                }
    15098        }while(index < count);
     99        return EOK;
     100}
    151101
    152         return EOK;
     102int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
     103        if((! data) || (! address)){
     104                return EINVAL;
     105        }
     106        switch(family){
     107                case AF_INET:
     108                        if(length < INET_ADDRSTRLEN){
     109                                return ENOMEM;
     110                        }
     111                        snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
     112                        return EOK;
     113                case AF_INET6:
     114                        if(length < INET6_ADDRSTRLEN){
     115                                return ENOMEM;
     116                        }
     117                        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]);
     118                        return EOK;
     119                default:
     120                        return ENOTSUP;
     121        }
    153122}
    154123
Note: See TracChangeset for help on using the changeset viewer.