Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/net/inet.c

    r9d58539 ra2e3ee6  
    3939#include <net/in6.h>
    4040#include <net/inet.h>
    41 
     41#include <inet/addr.h>
    4242#include <errno.h>
    4343#include <mem.h>
     
    9494}
    9595
     96static int inet_pton4(const char *address, uint8_t *data)
     97{
     98        memset(data, 0, 4);
     99       
     100        const char *cur = address;
     101        size_t i = 0;
     102       
     103        while (i < 4) {
     104                int rc = str_uint8_t(cur, &cur, 10, false, &data[i]);
     105                if (rc != EOK)
     106                        return rc;
     107               
     108                i++;
     109               
     110                if (*cur == 0)
     111                        break;
     112               
     113                if (*cur != '.')
     114                        return EINVAL;
     115               
     116                if (i < 4)
     117                        cur++;
     118        }
     119       
     120        if ((i == 4) && (*cur != 0))
     121                return EINVAL;
     122       
     123        return EOK;
     124}
     125
     126static int inet_pton6(const char *address, uint8_t *data)
     127{
     128        // FIXME TODO
     129        return ENOTSUP;
     130}
     131
    96132/** Parses the character string into the address.
    97133 *
    98  *  If the string is shorter than the full address, zero bytes are added.
     134 * @param[in]  family  The address family.
     135 * @param[in]  address The character buffer to be parsed.
     136 * @param[out] data    The address data to be filled.
    99137 *
    100  *  @param[in] family   The address family.
    101  *  @param[in] address  The character buffer to be parsed.
    102  *  @param[out] data    The address data to be filled.
    103  *  @return             EOK on success.
    104  *  @return             EINVAL if the data parameter is NULL.
    105  *  @return             ENOENT if the address parameter is NULL.
    106  *  @return             ENOTSUP if the address family is not supported.
     138 * @return EOK on success.
     139 * @return EINVAL if the data parameter is NULL.
     140 * @return ENOENT if the address parameter is NULL.
     141 * @return ENOTSUP if the address family is not supported.
     142 *
    107143 */
    108144int inet_pton(uint16_t family, const char *address, uint8_t *data)
    109145{
    110         /** The base number of the values. */
    111         int base;
    112         /** The number of bytes per a section. */
    113         size_t bytes;
    114         /** The number of bytes of the address data. */
    115         int count;
    116 
    117         const char *next;
    118         char *last;
    119         int index;
    120         size_t shift;
    121         unsigned long value;
    122 
    123         if (!data)
    124                 return EINVAL;
    125 
    126         /* Set processing parameters */
    127146        switch (family) {
    128147        case AF_INET:
    129                 count = 4;
    130                 base = 10;
    131                 bytes = 1;
    132                 break;
    133 
     148                return inet_pton4(address, data);
    134149        case AF_INET6:
    135                 count = 16;
    136                 base = 16;
    137                 bytes = 4;
    138                 break;
    139 
     150                return inet_pton6(address, data);
    140151        default:
     152                /** Unknown address family */
    141153                return ENOTSUP;
    142154        }
    143 
    144         /* Erase if no address */
    145         if (!address) {
    146                 bzero(data, count);
    147                 return ENOENT;
    148         }
    149 
    150         /* Process string from the beginning */
    151         next = address;
    152         index = 0;
    153         do {
    154                 /* If the actual character is set */
    155                 if (next && *next) {
    156 
    157                         /* If not on the first character */
    158                         if (index) {
    159                                 /* Move to the next character */
    160                                 ++next;
    161                         }
    162 
    163                         /* Parse the actual integral value */
    164                         value = strtoul(next, &last, base);
    165                         /*
    166                          * Remember the last problematic character
    167                          * should be either '.' or ':' but is ignored to be
    168                          * more generic
    169                          */
    170                         next = last;
    171 
    172                         /* Fill the address data byte by byte */
    173                         shift = bytes - 1;
    174                         do {
    175                                 /* like little endian */
    176                                 data[index + shift] = value;
    177                                 value >>= 8;
    178                         } while(shift --);
    179 
    180                         index += bytes;
    181                 } else {
    182                         /* Erase the rest of the address */
    183                         bzero(data + index, count - index);
    184                         return EOK;
    185                 }
    186         } while (index < count);
    187 
    188         return EOK;
    189155}
    190156
Note: See TracChangeset for help on using the changeset viewer.