2 * Copyright (c) 2009-2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * 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 <ucg_cmd_parse.h>
94 #include <ucg_cmd_parse_ipaddr.h>
100 * WARNING: Don't even consider trying to compile this on a system where
101 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
104 static int inet_pton4(const char *src, unsigned char *dst);
105 static int inet_pton6(const char *src, unsigned char *dst);
108 * inet_pton(af, src, dst)
109 * convert from presentation format (which usually means ASCII printable)
110 * to network format (which is usually some kind of binary format).
112 * 1 if the address was valid for the specified address family
113 * 0 if the address wasn't valid (`dst' is untouched in this case)
114 * -1 if some other error occurred (`dst' is untouched in this case, too)
119 my_inet_pton(int af, const char *src, void *dst)
123 return (inet_pton4(src, dst));
125 return (inet_pton6(src, dst));
127 errno = EAFNOSUPPORT;
134 * inet_pton4(src, dst)
135 * like inet_aton() but without all the hexadecimal and shorthand.
137 * 1 if `src' is a valid dotted quad, else 0.
139 * does not touch `dst' unless it's returning 1.
144 inet_pton4(const char *src, unsigned char *dst)
146 static const char digits[] = "0123456789";
147 int saw_digit, octets, ch;
148 unsigned char tmp[INADDRSZ], *tp;
153 while ((ch = *src++) != '\0') {
156 if ((pch = strchr(digits, ch)) != NULL) {
157 unsigned int new = *tp * 10 + (pch - digits);
166 *tp = (unsigned char)new;
167 } else if (ch == '.' && saw_digit) {
178 memcpy(dst, tmp, INADDRSZ);
183 * inet_pton6(src, dst)
184 * convert presentation level address to network order binary form.
186 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
188 * (1) does not touch `dst' unless it's returning 1.
189 * (2) :: in a full address is silently ignored.
191 * inspired by Mark Andrews.
196 inet_pton6(const char *src, unsigned char *dst)
198 static const char xdigits_l[] = "0123456789abcdef",
199 xdigits_u[] = "0123456789ABCDEF";
200 unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
201 const char *xdigits, *curtok;
202 int ch, saw_xdigit, count_xdigit;
205 memset((tp = tmp), '\0', IN6ADDRSZ);
206 endp = tp + IN6ADDRSZ;
208 /* Leading :: requires some special handling. */
213 saw_xdigit = count_xdigit = 0;
215 while ((ch = *src++) != '\0') {
218 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
219 pch = strchr((xdigits = xdigits_u), ch);
221 if (count_xdigit >= 4)
224 val |= (pch - xdigits);
238 } else if (*src == '\0') {
241 if (tp + sizeof(int16_t) > endp)
243 *tp++ = (unsigned char) ((val >> 8) & 0xff);
244 *tp++ = (unsigned char) (val & 0xff);
250 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
251 inet_pton4(curtok, tp) > 0) {
255 break; /* '\0' was seen by inet_pton4(). */
260 if (tp + sizeof(int16_t) > endp)
262 *tp++ = (unsigned char) ((val >> 8) & 0xff);
263 *tp++ = (unsigned char) (val & 0xff);
265 if (colonp != NULL) {
267 * Since some memmove()'s erroneously fail to handle
268 * overlapping regions, we'll do the shift by hand.
270 const int n = tp - colonp;
273 for (i = 1; i <= n; i++) {
274 endp[- i] = colonp[n - i];
281 memcpy(dst, tmp, IN6ADDRSZ);
286 cmd_parse_ipaddr(ucg_cmd_tk_hdr_t *tk, const char *buf,
287 void *res, unsigned ressize)
289 struct ucg_cmd_tk_ipaddr *tk2 =
290 (struct ucg_cmd_tk_ipaddr *)tk;
291 unsigned int token_len = 0;
292 char ip_str[INET6_ADDRSTRLEN+4]; /* '+4' is for prefixlen (if any) */
293 ucg_cmd_ipaddr_t ipaddr;
294 char *prefix, *prefix_end;
297 if (res && ressize < sizeof(ucg_cmd_ipaddr_t))
300 memset(&ipaddr, 0, sizeof(ipaddr));
302 /* if token is too big... */
303 token_len = snprintf(ip_str, sizeof(ip_str), "%s", buf);
304 if (token_len >= sizeof(ip_str))
307 /* convert the network prefix */
308 if (tk2->ipaddr_data.flags & UCG_CMD_IPADDR_NETWORK) {
309 prefix = strrchr(ip_str, '/');
315 prefixlen = strtol(prefix, &prefix_end, 10);
316 if (errno || (*prefix_end != '\0') )
318 ipaddr.prefixlen = prefixlen;
321 ipaddr.prefixlen = 0;
324 /* convert the IP addr */
325 if ((tk2->ipaddr_data.flags & UCG_CMD_IPADDR_V4) &&
326 my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1) {
327 ipaddr.family = AF_INET;
329 memcpy(res, &ipaddr, sizeof(ipaddr));
332 if ((tk2->ipaddr_data.flags & UCG_CMD_IPADDR_V6) &&
333 my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
334 ipaddr.family = AF_INET6;
336 memcpy(res, &ipaddr, sizeof(ipaddr));
344 cmd_help_ipaddr(ucg_cmd_tk_hdr_t *tk, char *dstbuf,
347 struct ucg_cmd_tk_ipaddr *tk2 =
348 (struct ucg_cmd_tk_ipaddr *)tk;
350 switch (tk2->ipaddr_data.flags) {
351 case UCG_CMD_IPADDR_V4:
352 snprintf(dstbuf, size, "<IPv4>");
354 case UCG_CMD_IPADDR_V6:
355 snprintf(dstbuf, size, "<IPv6>");
357 case UCG_CMD_IPADDR_V4 | UCG_CMD_IPADDR_V6:
358 snprintf(dstbuf, size, "<IPv4/IPv6>");
360 case UCG_CMD_IPADDR_NETWORK | UCG_CMD_IPADDR_V4:
361 snprintf(dstbuf, size, "<IPv4 network>");
363 case UCG_CMD_IPADDR_NETWORK | UCG_CMD_IPADDR_V6:
364 snprintf(dstbuf, size, "<IPv6 network>");
366 case UCG_CMD_IPADDR_NETWORK | UCG_CMD_IPADDR_V4 |
368 snprintf(dstbuf, size, "<IPv4/IPv6 network>");
371 snprintf(dstbuf, size, "<IPaddr (bad flags)>");
377 struct ucg_cmd_tk_ops cmd_token_ipaddr_ops = {
378 .parse = cmd_parse_ipaddr,
379 .complete_start = NULL,
380 .complete_iterate = NULL,
381 .complete_end = NULL,
382 .help = cmd_help_ipaddr,