2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netdissect-stdinc.h>
26 #include "strtoaddr.h"
29 #define NS_INADDRSZ 4 /* IPv4 T_A */
33 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
37 #define NS_INT16SZ 2 /* #/bytes of data in a uint16_t */
41 * WARNING: Don't even consider trying to compile this on a system where
42 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
46 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
51 * convert presentation level IPv4 address to network order binary form.
53 * 1 if `src' is a valid input, else 0.
55 * does not touch `dst' unless it's returning 1.
60 strtoaddr(const char *src, void *dst)
72 * Collect number up to ``.''.
73 * Values are specified as for C:
74 * 0x=hex, 0=octal, isdigit=decimal.
81 if (c == 'x' || c == 'X')
83 else if (isdigit(c) && c != '9')
91 val = (val * 10) + digit;
100 * a.b.c (with c treated as 16 bits)
101 * a.b (with b treated as 24 bits)
102 * a (with a treated as 32 bits)
112 * Check for trailing characters.
114 if (c != '\0' && !isspace(c))
117 * Find the number of parts specified.
118 * It must be 4; we only support dotted quads, we don't
125 * parts[0-2] were set to the first 3 parts of the address;
126 * val was set to the 4th part.
128 * Check if any part is bigger than 255.
130 if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
133 * Add the other three parts to val.
135 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
138 memcpy(dst, &val, NS_INADDRSZ);
144 * strtoaddr6(src, dst)
145 * convert presentation level IPv6 address to network order binary form.
147 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
149 * (1) does not touch `dst' unless it's returning 1.
150 * (2) :: in a full address is silently ignored.
152 * inspired by Mark Andrews.
157 strtoaddr6(const char *src, void *dst)
159 static const char xdigits_l[] = "0123456789abcdef",
160 xdigits_u[] = "0123456789ABCDEF";
161 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
162 const char *xdigits, *curtok;
163 int ch, seen_xdigits;
166 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
167 endp = tp + NS_IN6ADDRSZ;
169 /* Leading :: requires some special handling. */
176 while ((ch = *src++) != '\0') {
179 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
180 pch = strchr((xdigits = xdigits_u), ch);
183 val |= (int)(pch - xdigits);
184 if (++seen_xdigits > 4)
195 } else if (*src == '\0')
197 if (tp + NS_INT16SZ > endp)
199 *tp++ = (u_char) (val >> 8) & 0xff;
200 *tp++ = (u_char) val & 0xff;
205 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
206 strtoaddr(curtok, tp) > 0) {
209 break; /*%< '\\0' was seen by strtoaddr(). */
214 if (tp + NS_INT16SZ > endp)
216 *tp++ = (u_char) (val >> 8) & 0xff;
217 *tp++ = (u_char) val & 0xff;
219 if (colonp != NULL) {
221 * Since some memmove()'s erroneously fail to handle
222 * overlapping regions, we'll do the shift by hand.
224 const ptrdiff_t n = tp - colonp;
229 for (i = 1; i <= n; i++) {
230 endp[- i] = colonp[n - i];
237 memcpy(dst, tmp, NS_IN6ADDRSZ);