1 /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form
3 Copyright (C) 2006, 2008-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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
38 #include <arpa/inet.h>
45 #define NS_IN6ADDRSZ 16
49 * WARNING: Don't even consider trying to compile this on a system where
50 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
53 static int inet_pton4 (const char *src, unsigned char *dst);
55 static int inet_pton6 (const char *src, unsigned char *dst);
59 * inet_pton(af, src, dst)
60 * convert from presentation format (which usually means ASCII printable)
61 * to network format (which is usually some kind of binary format).
63 * 1 if the address was valid for the specified address family
64 * 0 if the address wasn't valid (`dst' is untouched in this case)
65 * -1 if some other error occurred (`dst' is untouched in this case, too)
70 inet_pton (int af, const char *restrict src, void *restrict dst)
75 return (inet_pton4 (src, dst));
79 return (inet_pton6 (src, dst));
90 * inet_pton4(src, dst)
91 * like inet_aton() but without all the hexadecimal, octal (with the
92 * exception of 0) and shorthand.
94 * 1 if `src' is a valid dotted quad, else 0.
96 * does not touch `dst' unless it's returning 1.
101 inet_pton4 (const char *restrict src, unsigned char *restrict dst)
103 int saw_digit, octets, ch;
104 unsigned char tmp[NS_INADDRSZ], *tp;
109 while ((ch = *src++) != '\0')
112 if (ch >= '0' && ch <= '9')
114 unsigned new = *tp * 10 + (ch - '0');
116 if (saw_digit && *tp == 0)
128 else if (ch == '.' && saw_digit)
140 memcpy (dst, tmp, NS_INADDRSZ);
147 * inet_pton6(src, dst)
148 * convert presentation level address to network order binary form.
150 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
152 * (1) does not touch `dst' unless it's returning 1.
153 * (2) :: in a full address is silently ignored.
155 * inspired by Mark Andrews.
160 inet_pton6 (const char *restrict src, unsigned char *restrict dst)
162 static const char xdigits[] = "0123456789abcdef";
163 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
168 tp = memset (tmp, '\0', NS_IN6ADDRSZ);
169 endp = tp + NS_IN6ADDRSZ;
171 /* Leading :: requires some special handling. */
178 while ((ch = c_tolower (*src++)) != '\0')
182 pch = strchr (xdigits, ch);
186 val |= (pch - xdigits);
202 else if (*src == '\0')
206 if (tp + NS_INT16SZ > endp)
208 *tp++ = (u_char) (val >> 8) & 0xff;
209 *tp++ = (u_char) val & 0xff;
214 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
215 inet_pton4 (curtok, tp) > 0)
219 break; /* '\0' was seen by inet_pton4(). */
225 if (tp + NS_INT16SZ > endp)
227 *tp++ = (u_char) (val >> 8) & 0xff;
228 *tp++ = (u_char) val & 0xff;
233 * Since some memmove()'s erroneously fail to handle
234 * overlapping regions, we'll do the shift by hand.
236 const int n = tp - colonp;
241 for (i = 1; i <= n; i++)
243 endp[-i] = colonp[n - i];
250 memcpy (dst, tmp, NS_IN6ADDRSZ);