| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Internet protocol address conversion functions implementation.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <net/socket_codes.h>
|
|---|
| 38 | #include <net/in.h>
|
|---|
| 39 | #include <net/in6.h>
|
|---|
| 40 | #include <net/inet.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <mem.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <str.h>
|
|---|
| 46 |
|
|---|
| 47 | /** Prints the address into the character buffer.
|
|---|
| 48 | *
|
|---|
| 49 | * @param[in] family The address family.
|
|---|
| 50 | * @param[in] data The address data.
|
|---|
| 51 | * @param[out] address The character buffer to be filled.
|
|---|
| 52 | * @param[in] length The buffer length.
|
|---|
| 53 | * @return EOK on success.
|
|---|
| 54 | * @return EINVAL if the data or address parameter is NULL.
|
|---|
| 55 | * @return ENOMEM if the character buffer is not long enough.
|
|---|
| 56 | * @return ENOTSUP if the address family is not supported.
|
|---|
| 57 | */
|
|---|
| 58 | int
|
|---|
| 59 | inet_ntop(uint16_t family, const uint8_t *data, char *address, size_t length)
|
|---|
| 60 | {
|
|---|
| 61 | if ((!data) || (!address))
|
|---|
| 62 | return EINVAL;
|
|---|
| 63 |
|
|---|
| 64 | switch (family) {
|
|---|
| 65 | case AF_INET:
|
|---|
| 66 | /* Check output buffer size */
|
|---|
| 67 | if (length < INET_ADDRSTRLEN)
|
|---|
| 68 | return ENOMEM;
|
|---|
| 69 |
|
|---|
| 70 | /* Fill buffer with IPv4 address */
|
|---|
| 71 | snprintf(address, length, "%hhu.%hhu.%hhu.%hhu",
|
|---|
| 72 | data[0], data[1], data[2], data[3]);
|
|---|
| 73 |
|
|---|
| 74 | return EOK;
|
|---|
| 75 |
|
|---|
| 76 | case AF_INET6:
|
|---|
| 77 | /* Check output buffer size */
|
|---|
| 78 | if (length < INET6_ADDRSTRLEN)
|
|---|
| 79 | return ENOMEM;
|
|---|
| 80 |
|
|---|
| 81 | /* Fill buffer with IPv6 address */
|
|---|
| 82 | snprintf(address, length,
|
|---|
| 83 | "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:"
|
|---|
| 84 | "%hhx%hhx:%hhx%hhx",
|
|---|
| 85 | data[0], data[1], data[2], data[3], data[4], data[5],
|
|---|
| 86 | data[6], data[7], data[8], data[9], data[10], data[11],
|
|---|
| 87 | data[12], data[13], data[14], data[15]);
|
|---|
| 88 |
|
|---|
| 89 | return EOK;
|
|---|
| 90 |
|
|---|
| 91 | default:
|
|---|
| 92 | return ENOTSUP;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Parses the character string into the address.
|
|---|
| 97 | *
|
|---|
| 98 | * If the string is shorter than the full address, zero bytes are added.
|
|---|
| 99 | *
|
|---|
| 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.
|
|---|
| 107 | */
|
|---|
| 108 | int inet_pton(uint16_t family, const char *address, uint8_t *data)
|
|---|
| 109 | {
|
|---|
| 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 */
|
|---|
| 127 | switch (family) {
|
|---|
| 128 | case AF_INET:
|
|---|
| 129 | count = 4;
|
|---|
| 130 | base = 10;
|
|---|
| 131 | bytes = 1;
|
|---|
| 132 | break;
|
|---|
| 133 |
|
|---|
| 134 | case AF_INET6:
|
|---|
| 135 | count = 16;
|
|---|
| 136 | base = 16;
|
|---|
| 137 | bytes = 4;
|
|---|
| 138 | break;
|
|---|
| 139 |
|
|---|
| 140 | default:
|
|---|
| 141 | return ENOTSUP;
|
|---|
| 142 | }
|
|---|
| 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;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** @}
|
|---|
| 192 | */
|
|---|