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


Ignore:
Timestamp:
2010-03-09T22:24:31Z (14 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
74520daf
Parents:
9f2ea28
Message:
  • code reorganization (no functional change)
File:
1 edited

Legend:

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

    r9f2ea28 ra64c64d  
    4545#include "include/socket_codes.h"
    4646
     47int 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
    4774int 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
    4885        const char * next;
    4986        char * last;
    5087        int index;
    51         int count;
    52         int base;
    53         size_t bytes;
    5488        size_t shift;
    5589        unsigned long value;
     
    5892                return EINVAL;
    5993        }
     94
     95        // set the processing parameters
    6096        switch(family){
    6197                case AF_INET:
     
    72108                        return ENOTSUP;
    73109        }
     110
     111        // erase if no address
    74112        if(! address){
    75113                bzero(data, count);
    76114                return ENOENT;
    77115        }
     116
     117        // process the string from the beginning
    78118        next = address;
    79119        index = 0;
    80120        do{
     121                // if the actual character is set
    81122                if(next && (*next)){
     123
     124                        // if not on the first character
    82125                        if(index){
     126                                // move to the next character
    83127                                ++ next;
    84128                        }
     129
     130                        // parse the actual integral value
    85131                        value = strtoul(next, &last, base);
     132                        // remember the last problematic character
     133                        // should be either '.' or ':' but is ignored to be more generic
    86134                        next = last;
     135
     136                        // fill the address data byte by byte
    87137                        shift = bytes - 1;
    88138                        do{
     
    91141                                value >>= 8;
    92142                        }while(shift --);
     143
    93144                        index += bytes;
    94145                }else{
     146                        // erase the rest of the address
    95147                        bzero(data + index, count - index);
    96148                        return EOK;
    97149                }
    98150        }while(index < count);
     151
    99152        return EOK;
    100 }
    101 
    102 int 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         }
    122153}
    123154
Note: See TracChangeset for help on using the changeset viewer.