| 1 |
/* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form |
| 2 |
|
| 3 |
Copyright (C) 2006, 2008-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* |
| 19 |
* Copyright (c) 1996,1999 by Internet Software Consortium. |
| 20 |
* |
| 21 |
* Permission to use, copy, modify, and distribute this software for any |
| 22 |
* purpose with or without fee is hereby granted, provided that the above |
| 23 |
* copyright notice and this permission notice appear in all copies. |
| 24 |
* |
| 25 |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 26 |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 27 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 28 |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 29 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 30 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 31 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 32 |
* SOFTWARE. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include <config.h> |
| 36 |
|
| 37 |
/* Specification. */ |
| 38 |
#include <arpa/inet.h> |
| 39 |
|
| 40 |
#if HAVE_DECL_INET_PTON |
| 41 |
|
| 42 |
# undef inet_pton |
| 43 |
|
| 44 |
int |
| 45 |
rpl_inet_pton (int af, const char *restrict src, void *restrict dst) |
| 46 |
{ |
| 47 |
return inet_pton (af, src, dst); |
| 48 |
} |
| 49 |
|
| 50 |
#else |
| 51 |
|
| 52 |
# include <c-ctype.h> |
| 53 |
# include <string.h> |
| 54 |
# include <errno.h> |
| 55 |
|
| 56 |
# define NS_INADDRSZ 4 |
| 57 |
# define NS_IN6ADDRSZ 16 |
| 58 |
# define NS_INT16SZ 2 |
| 59 |
|
| 60 |
/* |
| 61 |
* WARNING: Don't even consider trying to compile this on a system where |
| 62 |
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
| 63 |
*/ |
| 64 |
|
| 65 |
static int inet_pton4 (const char *src, unsigned char *dst); |
| 66 |
# if HAVE_IPV6 |
| 67 |
static int inet_pton6 (const char *src, unsigned char *dst); |
| 68 |
# endif |
| 69 |
|
| 70 |
/* int |
| 71 |
* inet_pton(af, src, dst) |
| 72 |
* convert from presentation format (which usually means ASCII printable) |
| 73 |
* to network format (which is usually some kind of binary format). |
| 74 |
* return: |
| 75 |
* 1 if the address was valid for the specified address family |
| 76 |
* 0 if the address wasn't valid ('dst' is untouched in this case) |
| 77 |
* -1 if some other error occurred ('dst' is untouched in this case, too) |
| 78 |
* author: |
| 79 |
* Paul Vixie, 1996. |
| 80 |
*/ |
| 81 |
int |
| 82 |
inet_pton (int af, const char *restrict src, void *restrict dst) |
| 83 |
{ |
| 84 |
switch (af) |
| 85 |
{ |
| 86 |
case AF_INET: |
| 87 |
return (inet_pton4 (src, dst)); |
| 88 |
|
| 89 |
# if HAVE_IPV6 |
| 90 |
case AF_INET6: |
| 91 |
return (inet_pton6 (src, dst)); |
| 92 |
# endif |
| 93 |
|
| 94 |
default: |
| 95 |
errno = EAFNOSUPPORT; |
| 96 |
return (-1); |
| 97 |
} |
| 98 |
/* NOTREACHED */ |
| 99 |
} |
| 100 |
|
| 101 |
/* int |
| 102 |
* inet_pton4(src, dst) |
| 103 |
* like inet_aton() but without all the hexadecimal, octal (with the |
| 104 |
* exception of 0) and shorthand. |
| 105 |
* return: |
| 106 |
* 1 if 'src' is a valid dotted quad, else 0. |
| 107 |
* notice: |
| 108 |
* does not touch 'dst' unless it's returning 1. |
| 109 |
* author: |
| 110 |
* Paul Vixie, 1996. |
| 111 |
*/ |
| 112 |
static int |
| 113 |
inet_pton4 (const char *restrict src, unsigned char *restrict dst) |
| 114 |
{ |
| 115 |
unsigned char tmp[NS_INADDRSZ]; |
| 116 |
|
| 117 |
{ |
| 118 |
int saw_digit = 0; |
| 119 |
int octets = 0; |
| 120 |
unsigned char *tp = tmp; |
| 121 |
*tp = 0; |
| 122 |
int ch; |
| 123 |
while ((ch = *src++) != '\0') |
| 124 |
{ |
| 125 |
if (ch >= '0' && ch <= '9') |
| 126 |
{ |
| 127 |
if (saw_digit && *tp == 0) |
| 128 |
return (0); |
| 129 |
unsigned new = *tp * 10 + (ch - '0'); |
| 130 |
if (new > 255) |
| 131 |
return (0); |
| 132 |
*tp = new; |
| 133 |
if (!saw_digit) |
| 134 |
{ |
| 135 |
if (++octets > 4) |
| 136 |
return (0); |
| 137 |
saw_digit = 1; |
| 138 |
} |
| 139 |
} |
| 140 |
else if (ch == '.' && saw_digit) |
| 141 |
{ |
| 142 |
if (octets == 4) |
| 143 |
return (0); |
| 144 |
*++tp = 0; |
| 145 |
saw_digit = 0; |
| 146 |
} |
| 147 |
else |
| 148 |
return (0); |
| 149 |
} |
| 150 |
if (octets < 4) |
| 151 |
return (0); |
| 152 |
} |
| 153 |
memcpy (dst, tmp, NS_INADDRSZ); |
| 154 |
return (1); |
| 155 |
} |
| 156 |
|
| 157 |
# if HAVE_IPV6 |
| 158 |
|
| 159 |
/* int |
| 160 |
* inet_pton6(src, dst) |
| 161 |
* convert presentation level address to network order binary form. |
| 162 |
* return: |
| 163 |
* 1 if 'src' is a valid [RFC1884 2.2] address, else 0. |
| 164 |
* notice: |
| 165 |
* (1) does not touch 'dst' unless it's returning 1. |
| 166 |
* (2) :: in a full address is silently ignored. |
| 167 |
* credit: |
| 168 |
* inspired by Mark Andrews. |
| 169 |
* author: |
| 170 |
* Paul Vixie, 1996. |
| 171 |
*/ |
| 172 |
static int |
| 173 |
inet_pton6 (const char *restrict src, unsigned char *restrict dst) |
| 174 |
{ |
| 175 |
static const char xdigits[] = "0123456789abcdef"; |
| 176 |
unsigned char tmp[NS_IN6ADDRSZ]; |
| 177 |
|
| 178 |
/* Leading :: requires some special handling. */ |
| 179 |
if (*src == ':') |
| 180 |
if (*++src != ':') |
| 181 |
return (0); |
| 182 |
|
| 183 |
{ |
| 184 |
unsigned char *tp = memset (tmp, '\0', NS_IN6ADDRSZ); |
| 185 |
unsigned char *endp = tp + NS_IN6ADDRSZ; |
| 186 |
unsigned char *colonp = NULL; |
| 187 |
const char *curtok = src; |
| 188 |
int saw_xdigit = 0; |
| 189 |
unsigned int val = 0; |
| 190 |
int ch; |
| 191 |
while ((ch = c_tolower (*src++)) != '\0') |
| 192 |
{ |
| 193 |
const char *pch = strchr (xdigits, ch); |
| 194 |
if (pch != NULL) |
| 195 |
{ |
| 196 |
val <<= 4; |
| 197 |
val |= (pch - xdigits); |
| 198 |
if (val > 0xffff) |
| 199 |
return (0); |
| 200 |
saw_xdigit = 1; |
| 201 |
} |
| 202 |
else if (ch == ':') |
| 203 |
{ |
| 204 |
curtok = src; |
| 205 |
if (!saw_xdigit) |
| 206 |
{ |
| 207 |
if (colonp) |
| 208 |
return (0); |
| 209 |
colonp = tp; |
| 210 |
} |
| 211 |
else if (*src == '\0') |
| 212 |
return (0); |
| 213 |
else if (tp + NS_INT16SZ > endp) |
| 214 |
return (0); |
| 215 |
else |
| 216 |
{ |
| 217 |
*tp++ = (unsigned char) (val >> 8) & 0xff; |
| 218 |
*tp++ = (unsigned char) val & 0xff; |
| 219 |
saw_xdigit = 0; |
| 220 |
val = 0; |
| 221 |
} |
| 222 |
} |
| 223 |
else if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && |
| 224 |
inet_pton4 (curtok, tp) > 0) |
| 225 |
{ |
| 226 |
tp += NS_INADDRSZ; |
| 227 |
saw_xdigit = 0; |
| 228 |
break; /* '\0' was seen by inet_pton4(). */ |
| 229 |
} |
| 230 |
else |
| 231 |
return (0); |
| 232 |
} |
| 233 |
if (saw_xdigit) |
| 234 |
{ |
| 235 |
if (tp + NS_INT16SZ > endp) |
| 236 |
return (0); |
| 237 |
*tp++ = (unsigned char) (val >> 8) & 0xff; |
| 238 |
*tp++ = (unsigned char) val & 0xff; |
| 239 |
} |
| 240 |
if (colonp != NULL) |
| 241 |
{ |
| 242 |
if (tp == endp) |
| 243 |
return (0); |
| 244 |
/* |
| 245 |
* Since some memmove()'s erroneously fail to handle |
| 246 |
* overlapping regions, we'll do the shift by hand. |
| 247 |
*/ |
| 248 |
const int n = tp - colonp; |
| 249 |
for (int i = 1; i <= n; i++) |
| 250 |
{ |
| 251 |
endp[-i] = colonp[n - i]; |
| 252 |
colonp[n - i] = 0; |
| 253 |
} |
| 254 |
tp = endp; |
| 255 |
} |
| 256 |
if (tp != endp) |
| 257 |
return (0); |
| 258 |
} |
| 259 |
memcpy (dst, tmp, NS_IN6ADDRSZ); |
| 260 |
return (1); |
| 261 |
} |
| 262 |
|
| 263 |
# endif |
| 264 |
|
| 265 |
#endif |