]>
The Tcpdump Group git mirrors - tcpdump/blob - strtoaddr.c
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.
20 #include "netdissect-stdinc.h"
24 #include "netdissect-ctype.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.
47 * convert presentation level IPv4 address to network order binary form.
49 * 1 if `src' is a valid input, else 0.
51 * does not touch `dst' unless it's returning 1.
56 strtoaddr(const char *src
, void *dst
)
68 * Collect number up to ``.''.
69 * Values are specified as for C:
70 * 0x=hex, 0=octal, isdigit=decimal.
72 if (!ND_ASCII_ISDIGIT(c
))
77 if (c
== 'x' || c
== 'X')
79 else if (ND_ASCII_ISDIGIT(c
) && c
!= '9')
83 if (ND_ASCII_ISDIGIT(c
)) {
85 val
= (val
* 10) + digit
;
94 * a.b.c (with c treated as 16 bits)
95 * a.b (with b treated as 24 bits)
96 * a (with a treated as 32 bits)
106 * Check for trailing characters.
108 if (c
!= '\0' && c
!= ' ' && c
!= '\t')
111 * Find the number of parts specified.
112 * It must be 4; we only support dotted quads, we don't
119 * parts[0-2] were set to the first 3 parts of the address;
120 * val was set to the 4th part.
122 * Check if any part is bigger than 255.
124 if ((parts
[0] | parts
[1] | parts
[2] | val
) > 0xff)
127 * Add the other three parts to val.
129 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
132 memcpy(dst
, &val
, NS_INADDRSZ
);
138 * strtoaddr6(src, dst)
139 * convert presentation level IPv6 address to network order binary form.
141 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
143 * (1) does not touch `dst' unless it's returning 1.
144 * (2) :: in a full address is silently ignored.
146 * inspired by Mark Andrews.
151 strtoaddr6(const char *src
, void *dst
)
153 static const char xdigits_l
[] = "0123456789abcdef",
154 xdigits_u
[] = "0123456789ABCDEF";
155 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
156 const char *xdigits
, *curtok
;
157 int ch
, seen_xdigits
;
160 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
161 endp
= tp
+ NS_IN6ADDRSZ
;
163 /* Leading :: requires some special handling. */
170 while ((ch
= *src
++) != '\0') {
173 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
174 pch
= strchr((xdigits
= xdigits_u
), ch
);
177 val
|= (int)(pch
- xdigits
);
178 if (++seen_xdigits
> 4)
189 } else if (*src
== '\0')
191 if (tp
+ NS_INT16SZ
> endp
)
193 *tp
++ = (u_char
) (val
>> 8) & 0xff;
194 *tp
++ = (u_char
) val
& 0xff;
199 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
200 strtoaddr(curtok
, tp
) > 0) {
203 break; /*%< '\\0' was seen by strtoaddr(). */
208 if (tp
+ NS_INT16SZ
> endp
)
210 *tp
++ = (u_char
) (val
>> 8) & 0xff;
211 *tp
++ = (u_char
) val
& 0xff;
213 if (colonp
!= NULL
) {
215 * Since some memmove()'s erroneously fail to handle
216 * overlapping regions, we'll do the shift by hand.
218 const ptrdiff_t n
= tp
- colonp
;
223 for (i
= 1; i
<= n
; i
++) {
224 endp
[- i
] = colonp
[n
- i
];
231 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);