1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
8 * For inet_ntop() functions:
10 * Copyright (c) 1996 by Internet Software Consortium.
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
17 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
19 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
21 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
22 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
34 #include <netinet/in.h>
37 #include <net/socket.h>
39 #include <sys/socket.h>
43 #include <rte_string_fns.h>
45 #include "cmdline_parse.h"
46 #include "cmdline_parse_ipaddr.h"
48 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
49 .parse = cmdline_parse_ipaddr,
50 .complete_get_nb = NULL,
51 .complete_get_elt = NULL,
52 .get_help = cmdline_get_help_ipaddr,
58 #define V4PREFIXMAX 32
61 * WARNING: Don't even consider trying to compile this on a system where
62 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
65 static int inet_pton4(const char *src, unsigned char *dst);
66 static int inet_pton6(const char *src, unsigned char *dst);
69 * inet_pton(af, src, dst)
70 * convert from presentation format (which usually means ASCII printable)
71 * to network format (which is usually some kind of binary format).
73 * 1 if the address was valid for the specified address family
74 * 0 if the address wasn't valid (`dst' is untouched in this case)
75 * -1 if some other error occurred (`dst' is untouched in this case, too)
80 my_inet_pton(int af, const char *src, void *dst)
84 return inet_pton4(src, dst);
86 return inet_pton6(src, dst);
95 * inet_pton4(src, dst)
96 * like inet_aton() but without all the hexadecimal and shorthand.
98 * 1 if `src' is a valid dotted quad, else 0.
100 * does not touch `dst' unless it's returning 1.
105 inet_pton4(const char *src, unsigned char *dst)
107 static const char digits[] = "0123456789";
108 int saw_digit, octets, ch;
109 unsigned char tmp[INADDRSZ], *tp;
114 while ((ch = *src++) != '\0') {
117 if ((pch = strchr(digits, ch)) != NULL) {
118 unsigned int new = *tp * 10 + (pch - digits);
127 *tp = (unsigned char)new;
128 } else if (ch == '.' && saw_digit) {
139 memcpy(dst, tmp, INADDRSZ);
144 * inet_pton6(src, dst)
145 * convert presentation level 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 inet_pton6(const char *src, unsigned char *dst)
159 static const char xdigits_l[] = "0123456789abcdef",
160 xdigits_u[] = "0123456789ABCDEF";
161 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
162 const char *xdigits = 0, *curtok = 0;
163 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
164 unsigned int val = 0;
165 unsigned dbloct_count = 0;
167 memset((tp = tmp), '\0', IN6ADDRSZ);
168 endp = tp + IN6ADDRSZ;
170 /* Leading :: requires some special handling. */
175 saw_xdigit = count_xdigit = 0;
178 while ((ch = *src++) != '\0') {
181 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
182 pch = strchr((xdigits = xdigits_u), ch);
184 if (count_xdigit >= 4)
187 val |= (pch - xdigits);
201 } else if (*src == '\0') {
204 if (tp + sizeof(int16_t) > endp)
206 *tp++ = (unsigned char) ((val >> 8) & 0xff);
207 *tp++ = (unsigned char) (val & 0xff);
214 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
215 inet_pton4(curtok, tp) > 0) {
219 break; /* '\0' was seen by inet_pton4(). */
224 if (tp + sizeof(int16_t) > endp)
226 *tp++ = (unsigned char) ((val >> 8) & 0xff);
227 *tp++ = (unsigned char) (val & 0xff);
230 if (colonp != NULL) {
231 /* if we already have 8 double octets, having a colon means error */
232 if (dbloct_count == 8)
236 * Since some memmove()'s erroneously fail to handle
237 * overlapping regions, we'll do the shift by hand.
239 const int n = tp - colonp;
242 for (i = 1; i <= n; i++) {
243 endp[- i] = colonp[n - i];
250 memcpy(dst, tmp, IN6ADDRSZ);
255 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
258 struct cmdline_token_ipaddr *tk2;
259 unsigned int token_len = 0;
260 char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
261 cmdline_ipaddr_t ipaddr;
262 char *prefix, *prefix_end;
265 if (res && ressize < sizeof(cmdline_ipaddr_t))
268 if (!buf || !tk || ! *buf)
271 tk2 = (struct cmdline_token_ipaddr *)tk;
273 while (!cmdline_isendoftoken(buf[token_len]))
276 /* if token is too big... */
277 if (token_len >= INET6_ADDRSTRLEN+4)
280 strlcpy(ip_str, buf, token_len + 1);
282 /* convert the network prefix */
283 if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
284 prefix = strrchr(ip_str, '/');
290 prefixlen = strtol(prefix, &prefix_end, 10);
291 if (errno || (*prefix_end != '\0')
292 || prefixlen < 0 || prefixlen > PREFIXMAX)
294 ipaddr.prefixlen = prefixlen;
297 ipaddr.prefixlen = 0;
300 /* convert the IP addr */
301 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
302 my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
303 prefixlen <= V4PREFIXMAX) {
304 ipaddr.family = AF_INET;
306 memcpy(res, &ipaddr, sizeof(ipaddr));
309 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
310 my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
311 ipaddr.family = AF_INET6;
313 memcpy(res, &ipaddr, sizeof(ipaddr));
320 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
323 struct cmdline_token_ipaddr *tk2;
328 tk2 = (struct cmdline_token_ipaddr *)tk;
330 switch (tk2->ipaddr_data.flags) {
331 case CMDLINE_IPADDR_V4:
332 snprintf(dstbuf, size, "IPv4");
334 case CMDLINE_IPADDR_V6:
335 snprintf(dstbuf, size, "IPv6");
337 case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
338 snprintf(dstbuf, size, "IPv4/IPv6");
340 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
341 snprintf(dstbuf, size, "IPv4 network");
343 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
344 snprintf(dstbuf, size, "IPv6 network");
346 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
347 snprintf(dstbuf, size, "IPv4/IPv6 network");
350 snprintf(dstbuf, size, "IPaddr (bad flags)");