1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
12 #include <rte_string_fns.h>
14 #include "cmdline_parse.h"
15 #include "cmdline_parse_ipaddr.h"
17 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
18 .parse = cmdline_parse_ipaddr,
19 .complete_get_nb = NULL,
20 .complete_get_elt = NULL,
21 .get_help = cmdline_get_help_ipaddr,
25 #define V4PREFIXMAX 32
28 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
31 struct cmdline_token_ipaddr *tk2;
32 unsigned int token_len = 0;
33 char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
34 cmdline_ipaddr_t ipaddr;
35 char *prefix, *prefix_end;
38 if (res && ressize < sizeof(cmdline_ipaddr_t))
41 if (!buf || !tk || ! *buf)
44 tk2 = (struct cmdline_token_ipaddr *)tk;
46 while (!cmdline_isendoftoken(buf[token_len]))
49 /* if token is too big... */
50 if (token_len >= INET6_ADDRSTRLEN+4)
53 strlcpy(ip_str, buf, token_len + 1);
55 /* convert the network prefix */
56 if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
57 prefix = strrchr(ip_str, '/');
63 prefixlen = strtol(prefix, &prefix_end, 10);
64 if (errno || (*prefix_end != '\0')
65 || prefixlen < 0 || prefixlen > PREFIXMAX)
67 ipaddr.prefixlen = prefixlen;
73 /* convert the IP addr */
74 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
75 inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
76 prefixlen <= V4PREFIXMAX) {
77 ipaddr.family = AF_INET;
79 memcpy(res, &ipaddr, sizeof(ipaddr));
82 if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
83 inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
84 ipaddr.family = AF_INET6;
86 memcpy(res, &ipaddr, sizeof(ipaddr));
93 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
96 struct cmdline_token_ipaddr *tk2;
101 tk2 = (struct cmdline_token_ipaddr *)tk;
103 switch (tk2->ipaddr_data.flags) {
104 case CMDLINE_IPADDR_V4:
105 snprintf(dstbuf, size, "IPv4");
107 case CMDLINE_IPADDR_V6:
108 snprintf(dstbuf, size, "IPv6");
110 case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
111 snprintf(dstbuf, size, "IPv4/IPv6");
113 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
114 snprintf(dstbuf, size, "IPv4 network");
116 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
117 snprintf(dstbuf, size, "IPv6 network");
119 case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
120 snprintf(dstbuf, size, "IPv4/IPv6 network");
123 snprintf(dstbuf, size, "IPaddr (bad flags)");