2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * - Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 * For inet_ntop() functions:
65 * Copyright (c) 1996 by Internet Software Consortium.
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies.
71 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
72 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
73 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
74 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
75 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
76 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
77 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
88 #include <netinet/in.h>
90 #include <net/socket.h>
93 #include "cmdline_parse.h"
94 #include "cmdline_parse_ipaddr.h"
96 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
97 .parse = cmdline_parse_ipaddr,
98 .complete_get_nb = NULL,
99 .complete_get_elt = NULL,
100 .get_help = cmdline_get_help_ipaddr,
107 * WARNING: Don't even consider trying to compile this on a system where
108 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
111 static int inet_pton4(const char *src, unsigned char *dst);
112 static int inet_pton6(const char *src, unsigned char *dst);
115 * inet_pton(af, src, dst)
116 * convert from presentation format (which usually means ASCII printable)
117 * to network format (which is usually some kind of binary format).
119 * 1 if the address was valid for the specified address family
120 * 0 if the address wasn't valid (`dst' is untouched in this case)
121 * -1 if some other error occurred (`dst' is untouched in this case, too)
126 my_inet_pton(int af, const char *src, void *dst)
130 return (inet_pton4(src, dst));
132 return (inet_pton6(src, dst));
134 errno = EAFNOSUPPORT;
141 * inet_pton4(src, dst)
142 * like inet_aton() but without all the hexadecimal and shorthand.
144 * 1 if `src' is a valid dotted quad, else 0.
146 * does not touch `dst' unless it's returning 1.
151 inet_pton4(const char *src, unsigned char *dst)
153 static const char digits[] = "0123456789";
154 int saw_digit, octets, ch;
155 unsigned char tmp[INADDRSZ], *tp;
160 while ((ch = *src++) != '\0') {
163 if ((pch = strchr(digits, ch)) != NULL) {
164 unsigned int new = *tp * 10 + (pch - digits);
173 *tp = (unsigned char)new;
174 } else if (ch == '.' && saw_digit) {
185 memcpy(dst, tmp, INADDRSZ);
190 * inet_pton6(src, dst)
191 * convert presentation level address to network order binary form.
193 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
195 * (1) does not touch `dst' unless it's returning 1.
196 * (2) :: in a full address is silently ignored.
198 * inspired by Mark Andrews.
203 inet_pton6(const char *src, unsigned char *dst)
205 static const char xdigits_l[] = "0123456789abcdef",
206 xdigits_u[] = "0123456789ABCDEF";
207 unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
208 const char *xdigits, *curtok;
209 int ch, saw_xdigit, count_xdigit;
212 memset((tp = tmp), '\0', IN6ADDRSZ);
213 endp = tp + IN6ADDRSZ;
215 /* Leading :: requires some special handling. */
220 saw_xdigit = count_xdigit = 0;
222 while ((ch = *src++) != '\0') {
225 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
226 pch = strchr((xdigits = xdigits_u), ch);
228 if (count_xdigit >= 4)
231 val |= (pch - xdigits);
245 } else if (*src == '\0') {
248 if (tp + sizeof(int16_t) > endp)
250 *tp++ = (unsigned char) ((val >> 8) & 0xff);
251 *tp++ = (unsigned char) (val & 0xff);
257 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
258 inet_pton4(curtok, tp) > 0) {
262 break; /* '\0' was seen by inet_pton4(). */
267 if (tp + sizeof(int16_t) > endp)
269 *tp++ = (unsigned char) ((val >> 8) & 0xff);
270 *tp++ = (unsigned char) (val & 0xff);
272 if (colonp != NULL) {
274 * Since some memmove()'s erroneously fail to handle
275 * overlapping regions, we'll do the shift by hand.
277 const int n = tp - colonp;
280 for (i = 1; i <= n; i++) {
281 endp[- i] = colonp[n - i];
288 memcpy(dst, tmp, IN6ADDRSZ);
293 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
295 struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
296 unsigned int token_len = 0;
297 char ip_str[INET6_ADDRSTRLEN+4]; /* '+4' is for prefixlen (if any) */
298 cmdline_ipaddr_t *ipaddr = res;
299 char *prefix, *prefix_end;
305 while (!cmdline_isendoftoken(buf[token_len]))
308 /* if token is too big... */
309 if (token_len >= INET6_ADDRSTRLEN+4)
312 snprintf(ip_str, token_len+1, "%s", buf);
314 /* convert the network prefix */
315 if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
316 prefix = strrchr(ip_str, '/');
322 prefixlen = strtol(prefix, &prefix_end, 10);
323 if (errno || (*prefix_end != '\0') )
325 ipaddr->prefixlen = prefixlen;
328 ipaddr->prefixlen = 0;
331 /* convert the IP addr */
332 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
333 my_inet_pton(AF_INET, ip_str, &ipaddr->addr.ipv4) == 1) {
334 ipaddr->family = AF_INET;
337 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
338 my_inet_pton(AF_INET6, ip_str, &ipaddr->addr.ipv6) == 1) {
339 ipaddr->family = AF_INET6;
346 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
349 struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
351 switch (tk2->ipaddr_data.flags) {
352 case CMDLINE_IPADDR_V4:
353 snprintf(dstbuf, size, "IPv4");
355 case CMDLINE_IPADDR_V6:
356 snprintf(dstbuf, size, "IPv6");
358 case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
359 snprintf(dstbuf, size, "IPv4/IPv6");
361 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
362 snprintf(dstbuf, size, "IPv4 network");
364 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
365 snprintf(dstbuf, size, "IPv6 network");
367 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
368 snprintf(dstbuf, size, "IPv4/IPv6 network");
371 snprintf(dstbuf, size, "IPaddr (bad flags)");