xref: /dpdk/lib/cmdline/cmdline_parse_ipaddr.c (revision 99a2dd955fba6e4cc23b77d590a033650ced9c45)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation.
3*99a2dd95SBruce Richardson  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4*99a2dd95SBruce Richardson  * All rights reserved.
5*99a2dd95SBruce Richardson  */
6*99a2dd95SBruce Richardson 
7*99a2dd95SBruce Richardson #include <stdio.h>
8*99a2dd95SBruce Richardson #include <stdlib.h>
9*99a2dd95SBruce Richardson #include <string.h>
10*99a2dd95SBruce Richardson #include <errno.h>
11*99a2dd95SBruce Richardson 
12*99a2dd95SBruce Richardson #include <rte_string_fns.h>
13*99a2dd95SBruce Richardson 
14*99a2dd95SBruce Richardson #include "cmdline_parse.h"
15*99a2dd95SBruce Richardson #include "cmdline_parse_ipaddr.h"
16*99a2dd95SBruce Richardson 
17*99a2dd95SBruce Richardson struct cmdline_token_ops cmdline_token_ipaddr_ops = {
18*99a2dd95SBruce Richardson 	.parse = cmdline_parse_ipaddr,
19*99a2dd95SBruce Richardson 	.complete_get_nb = NULL,
20*99a2dd95SBruce Richardson 	.complete_get_elt = NULL,
21*99a2dd95SBruce Richardson 	.get_help = cmdline_get_help_ipaddr,
22*99a2dd95SBruce Richardson };
23*99a2dd95SBruce Richardson 
24*99a2dd95SBruce Richardson #define PREFIXMAX 128
25*99a2dd95SBruce Richardson #define V4PREFIXMAX 32
26*99a2dd95SBruce Richardson 
27*99a2dd95SBruce Richardson int
cmdline_parse_ipaddr(cmdline_parse_token_hdr_t * tk,const char * buf,void * res,unsigned ressize)28*99a2dd95SBruce Richardson cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
29*99a2dd95SBruce Richardson 	unsigned ressize)
30*99a2dd95SBruce Richardson {
31*99a2dd95SBruce Richardson 	struct cmdline_token_ipaddr *tk2;
32*99a2dd95SBruce Richardson 	unsigned int token_len = 0;
33*99a2dd95SBruce Richardson 	char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
34*99a2dd95SBruce Richardson 	cmdline_ipaddr_t ipaddr;
35*99a2dd95SBruce Richardson 	char *prefix, *prefix_end;
36*99a2dd95SBruce Richardson 	long prefixlen = 0;
37*99a2dd95SBruce Richardson 
38*99a2dd95SBruce Richardson 	if (res && ressize < sizeof(cmdline_ipaddr_t))
39*99a2dd95SBruce Richardson 		return -1;
40*99a2dd95SBruce Richardson 
41*99a2dd95SBruce Richardson 	if (!buf || !tk || ! *buf)
42*99a2dd95SBruce Richardson 		return -1;
43*99a2dd95SBruce Richardson 
44*99a2dd95SBruce Richardson 	tk2 = (struct cmdline_token_ipaddr *)tk;
45*99a2dd95SBruce Richardson 
46*99a2dd95SBruce Richardson 	while (!cmdline_isendoftoken(buf[token_len]))
47*99a2dd95SBruce Richardson 		token_len++;
48*99a2dd95SBruce Richardson 
49*99a2dd95SBruce Richardson 	/* if token is too big... */
50*99a2dd95SBruce Richardson 	if (token_len >= INET6_ADDRSTRLEN+4)
51*99a2dd95SBruce Richardson 		return -1;
52*99a2dd95SBruce Richardson 
53*99a2dd95SBruce Richardson 	strlcpy(ip_str, buf, token_len + 1);
54*99a2dd95SBruce Richardson 
55*99a2dd95SBruce Richardson 	/* convert the network prefix */
56*99a2dd95SBruce Richardson 	if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
57*99a2dd95SBruce Richardson 		prefix = strrchr(ip_str, '/');
58*99a2dd95SBruce Richardson 		if (prefix == NULL)
59*99a2dd95SBruce Richardson 			return -1;
60*99a2dd95SBruce Richardson 		*prefix = '\0';
61*99a2dd95SBruce Richardson 		prefix ++;
62*99a2dd95SBruce Richardson 		errno = 0;
63*99a2dd95SBruce Richardson 		prefixlen = strtol(prefix, &prefix_end, 10);
64*99a2dd95SBruce Richardson 		if (errno || (*prefix_end != '\0')
65*99a2dd95SBruce Richardson 			|| prefixlen < 0 || prefixlen > PREFIXMAX)
66*99a2dd95SBruce Richardson 			return -1;
67*99a2dd95SBruce Richardson 		ipaddr.prefixlen = prefixlen;
68*99a2dd95SBruce Richardson 	}
69*99a2dd95SBruce Richardson 	else {
70*99a2dd95SBruce Richardson 		ipaddr.prefixlen = 0;
71*99a2dd95SBruce Richardson 	}
72*99a2dd95SBruce Richardson 
73*99a2dd95SBruce Richardson 	/* convert the IP addr */
74*99a2dd95SBruce Richardson 	if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
75*99a2dd95SBruce Richardson 	    inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
76*99a2dd95SBruce Richardson 		prefixlen <= V4PREFIXMAX) {
77*99a2dd95SBruce Richardson 		ipaddr.family = AF_INET;
78*99a2dd95SBruce Richardson 		if (res)
79*99a2dd95SBruce Richardson 			memcpy(res, &ipaddr, sizeof(ipaddr));
80*99a2dd95SBruce Richardson 		return token_len;
81*99a2dd95SBruce Richardson 	}
82*99a2dd95SBruce Richardson 	if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
83*99a2dd95SBruce Richardson 	    inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
84*99a2dd95SBruce Richardson 		ipaddr.family = AF_INET6;
85*99a2dd95SBruce Richardson 		if (res)
86*99a2dd95SBruce Richardson 			memcpy(res, &ipaddr, sizeof(ipaddr));
87*99a2dd95SBruce Richardson 		return token_len;
88*99a2dd95SBruce Richardson 	}
89*99a2dd95SBruce Richardson 	return -1;
90*99a2dd95SBruce Richardson 
91*99a2dd95SBruce Richardson }
92*99a2dd95SBruce Richardson 
cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t * tk,char * dstbuf,unsigned int size)93*99a2dd95SBruce Richardson int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
94*99a2dd95SBruce Richardson 			    unsigned int size)
95*99a2dd95SBruce Richardson {
96*99a2dd95SBruce Richardson 	struct cmdline_token_ipaddr *tk2;
97*99a2dd95SBruce Richardson 
98*99a2dd95SBruce Richardson 	if (!tk || !dstbuf)
99*99a2dd95SBruce Richardson 		return -1;
100*99a2dd95SBruce Richardson 
101*99a2dd95SBruce Richardson 	tk2 = (struct cmdline_token_ipaddr *)tk;
102*99a2dd95SBruce Richardson 
103*99a2dd95SBruce Richardson 	switch (tk2->ipaddr_data.flags) {
104*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_V4:
105*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv4");
106*99a2dd95SBruce Richardson 		break;
107*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_V6:
108*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv6");
109*99a2dd95SBruce Richardson 		break;
110*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
111*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv4/IPv6");
112*99a2dd95SBruce Richardson 		break;
113*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
114*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv4 network");
115*99a2dd95SBruce Richardson 		break;
116*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
117*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv6 network");
118*99a2dd95SBruce Richardson 		break;
119*99a2dd95SBruce Richardson 	case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
120*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPv4/IPv6 network");
121*99a2dd95SBruce Richardson 		break;
122*99a2dd95SBruce Richardson 	default:
123*99a2dd95SBruce Richardson 		snprintf(dstbuf, size, "IPaddr (bad flags)");
124*99a2dd95SBruce Richardson 		break;
125*99a2dd95SBruce Richardson 	}
126*99a2dd95SBruce Richardson 	return 0;
127*99a2dd95SBruce Richardson }
128