]> The Tcpdump Group git mirrors - tcpdump/blob - strtoaddr.c
Don't require IPv6 library support in order to support IPv6 addresses.
[tcpdump] / strtoaddr.c
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 *
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.
8 *
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.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <netdissect-stdinc.h>
23 #include <stddef.h>
24 #include <string.h>
25
26 #include "strtoaddr.h"
27
28 #ifndef NS_INADDRSZ
29 #define NS_INADDRSZ 4 /* IPv4 T_A */
30 #endif
31
32 #ifndef NS_IN6ADDRSZ
33 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
34 #endif
35
36 #ifndef NS_INT16SZ
37 #define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
38 #endif
39
40 /*%
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.
43 */
44
45 #ifndef NS_IN6ADDRSZ
46 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
47 #endif
48
49 /* int
50 * strtoaddr(src, dst)
51 * convert presentation level IPv4 address to network order binary form.
52 * return:
53 * 1 if `src' is a valid input, else 0.
54 * notice:
55 * does not touch `dst' unless it's returning 1.
56 * author:
57 * Paul Vixie, 1996.
58 */
59 int
60 strtoaddr(const char *src, void *dst)
61 {
62 uint32_t val;
63 u_int digit;
64 ptrdiff_t n;
65 unsigned char c;
66 u_int parts[4];
67 u_int *pp = parts;
68
69 c = *src;
70 for (;;) {
71 /*
72 * Collect number up to ``.''.
73 * Values are specified as for C:
74 * 0x=hex, 0=octal, isdigit=decimal.
75 */
76 if (!isdigit(c))
77 return (0);
78 val = 0;
79 if (c == '0') {
80 c = *++src;
81 if (c == 'x' || c == 'X')
82 return (0);
83 else if (isdigit(c) && c != '9')
84 return (0);
85 }
86 for (;;) {
87 if (isdigit(c)) {
88 digit = c - '0';
89 if (digit >= 10)
90 break;
91 val = (val * 10) + digit;
92 c = *++src;
93 } else
94 break;
95 }
96 if (c == '.') {
97 /*
98 * Internet format:
99 * a.b.c.d
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)
103 */
104 if (pp >= parts + 3)
105 return (0);
106 *pp++ = val;
107 c = *++src;
108 } else
109 break;
110 }
111 /*
112 * Check for trailing characters.
113 */
114 if (c != '\0' && !isspace(c))
115 return (0);
116 /*
117 * Concoct the address according to
118 * the number of parts specified.
119 */
120 n = pp - parts + 1;
121 /* Takes dotted-quad only. it does not take shorthand. */
122 if (n != 4)
123 return (0);
124 switch (n) {
125
126 case 0:
127 return (0); /* initial nondigit */
128
129 case 1: /* a -- 32 bits */
130 break;
131
132 case 2: /* a.b -- 8.24 bits */
133 if (parts[0] > 0xff || val > 0xffffff)
134 return (0);
135 val |= parts[0] << 24;
136 break;
137
138 case 3: /* a.b.c -- 8.8.16 bits */
139 if ((parts[0] | parts[1]) > 0xff || val > 0xffff)
140 return (0);
141 val |= (parts[0] << 24) | (parts[1] << 16);
142 break;
143
144 case 4: /* a.b.c.d -- 8.8.8.8 bits */
145 if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
146 return (0);
147 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
148 break;
149 }
150 if (dst) {
151 val = htonl(val);
152 memcpy(dst, &val, NS_INADDRSZ);
153 }
154 return (1);
155 }
156
157 /* int
158 * strtoaddr6(src, dst)
159 * convert presentation level IPv6 address to network order binary form.
160 * return:
161 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
162 * notice:
163 * (1) does not touch `dst' unless it's returning 1.
164 * (2) :: in a full address is silently ignored.
165 * credit:
166 * inspired by Mark Andrews.
167 * author:
168 * Paul Vixie, 1996.
169 */
170 int
171 strtoaddr6(const char *src, void *dst)
172 {
173 static const char xdigits_l[] = "0123456789abcdef",
174 xdigits_u[] = "0123456789ABCDEF";
175 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
176 const char *xdigits, *curtok;
177 int ch, seen_xdigits;
178 u_int val;
179
180 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
181 endp = tp + NS_IN6ADDRSZ;
182 colonp = NULL;
183 /* Leading :: requires some special handling. */
184 if (*src == ':')
185 if (*++src != ':')
186 return (0);
187 curtok = src;
188 seen_xdigits = 0;
189 val = 0;
190 while ((ch = *src++) != '\0') {
191 const char *pch;
192
193 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
194 pch = strchr((xdigits = xdigits_u), ch);
195 if (pch != NULL) {
196 val <<= 4;
197 val |= (int)(pch - xdigits);
198 if (++seen_xdigits > 4)
199 return (0);
200 continue;
201 }
202 if (ch == ':') {
203 curtok = src;
204 if (!seen_xdigits) {
205 if (colonp)
206 return (0);
207 colonp = tp;
208 continue;
209 } else if (*src == '\0')
210 return (0);
211 if (tp + NS_INT16SZ > endp)
212 return (0);
213 *tp++ = (u_char) (val >> 8) & 0xff;
214 *tp++ = (u_char) val & 0xff;
215 seen_xdigits = 0;
216 val = 0;
217 continue;
218 }
219 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
220 strtoaddr(curtok, tp) > 0) {
221 tp += NS_INADDRSZ;
222 seen_xdigits = 0;
223 break; /*%< '\\0' was seen by strtoaddr(). */
224 }
225 return (0);
226 }
227 if (seen_xdigits) {
228 if (tp + NS_INT16SZ > endp)
229 return (0);
230 *tp++ = (u_char) (val >> 8) & 0xff;
231 *tp++ = (u_char) val & 0xff;
232 }
233 if (colonp != NULL) {
234 /*
235 * Since some memmove()'s erroneously fail to handle
236 * overlapping regions, we'll do the shift by hand.
237 */
238 const ptrdiff_t n = tp - colonp;
239 int i;
240
241 if (tp == endp)
242 return (0);
243 for (i = 1; i <= n; i++) {
244 endp[- i] = colonp[n - i];
245 colonp[n - i] = 0;
246 }
247 tp = endp;
248 }
249 if (tp != endp)
250 return (0);
251 memcpy(dst, tmp, NS_IN6ADDRSZ);
252 return (1);
253 }