xref: /dpdk/lib/cmdline/cmdline_parse_ipaddr.h (revision 99a2dd95)
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 <[email protected]>
4*99a2dd95SBruce Richardson  * All rights reserved.
5*99a2dd95SBruce Richardson  */
6*99a2dd95SBruce Richardson 
7*99a2dd95SBruce Richardson #ifndef _PARSE_IPADDR_H_
8*99a2dd95SBruce Richardson #define _PARSE_IPADDR_H_
9*99a2dd95SBruce Richardson 
10*99a2dd95SBruce Richardson #include <cmdline_parse.h>
11*99a2dd95SBruce Richardson #include <rte_ip.h>
12*99a2dd95SBruce Richardson 
13*99a2dd95SBruce Richardson #ifdef __cplusplus
14*99a2dd95SBruce Richardson extern "C" {
15*99a2dd95SBruce Richardson #endif
16*99a2dd95SBruce Richardson 
17*99a2dd95SBruce Richardson #define CMDLINE_IPADDR_V4      0x01
18*99a2dd95SBruce Richardson #define CMDLINE_IPADDR_V6      0x02
19*99a2dd95SBruce Richardson #define CMDLINE_IPADDR_NETWORK 0x04
20*99a2dd95SBruce Richardson 
21*99a2dd95SBruce Richardson struct cmdline_ipaddr {
22*99a2dd95SBruce Richardson 	uint8_t family;
23*99a2dd95SBruce Richardson 	union {
24*99a2dd95SBruce Richardson 		struct in_addr ipv4;
25*99a2dd95SBruce Richardson 		struct in6_addr ipv6;
26*99a2dd95SBruce Richardson 	} addr;
27*99a2dd95SBruce Richardson 	unsigned int prefixlen; /* in case of network only */
28*99a2dd95SBruce Richardson };
29*99a2dd95SBruce Richardson typedef struct cmdline_ipaddr cmdline_ipaddr_t;
30*99a2dd95SBruce Richardson 
31*99a2dd95SBruce Richardson struct cmdline_token_ipaddr_data {
32*99a2dd95SBruce Richardson 	uint8_t flags;
33*99a2dd95SBruce Richardson };
34*99a2dd95SBruce Richardson 
35*99a2dd95SBruce Richardson struct cmdline_token_ipaddr {
36*99a2dd95SBruce Richardson 	struct cmdline_token_hdr hdr;
37*99a2dd95SBruce Richardson 	struct cmdline_token_ipaddr_data ipaddr_data;
38*99a2dd95SBruce Richardson };
39*99a2dd95SBruce Richardson typedef struct cmdline_token_ipaddr cmdline_parse_token_ipaddr_t;
40*99a2dd95SBruce Richardson 
41*99a2dd95SBruce Richardson extern struct cmdline_token_ops cmdline_token_ipaddr_ops;
42*99a2dd95SBruce Richardson 
43*99a2dd95SBruce Richardson int cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
44*99a2dd95SBruce Richardson 	void *res, unsigned ressize);
45*99a2dd95SBruce Richardson int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
46*99a2dd95SBruce Richardson 	unsigned int size);
47*99a2dd95SBruce Richardson 
48*99a2dd95SBruce Richardson #define TOKEN_IPADDR_INITIALIZER(structure, field)      \
49*99a2dd95SBruce Richardson {                                                       \
50*99a2dd95SBruce Richardson 	/* hdr */                                           \
51*99a2dd95SBruce Richardson 	{                                                   \
52*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
53*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
54*99a2dd95SBruce Richardson 	},                                                  \
55*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
56*99a2dd95SBruce Richardson 	{                                                   \
57*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V4 |             /* flags */     \
58*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V6,                              \
59*99a2dd95SBruce Richardson 	},                                                  \
60*99a2dd95SBruce Richardson }
61*99a2dd95SBruce Richardson 
62*99a2dd95SBruce Richardson #define TOKEN_IPV4_INITIALIZER(structure, field)        \
63*99a2dd95SBruce Richardson {                                                       \
64*99a2dd95SBruce Richardson 	/* hdr */                                           \
65*99a2dd95SBruce Richardson 	{                                                   \
66*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
67*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
68*99a2dd95SBruce Richardson 	},                                                  \
69*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
70*99a2dd95SBruce Richardson 	{                                                   \
71*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V4,              /* flags */     \
72*99a2dd95SBruce Richardson 	},                                                  \
73*99a2dd95SBruce Richardson }
74*99a2dd95SBruce Richardson 
75*99a2dd95SBruce Richardson #define TOKEN_IPV6_INITIALIZER(structure, field)        \
76*99a2dd95SBruce Richardson {                                                       \
77*99a2dd95SBruce Richardson 	/* hdr */                                           \
78*99a2dd95SBruce Richardson 	{                                                   \
79*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
80*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
81*99a2dd95SBruce Richardson 	},                                                  \
82*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
83*99a2dd95SBruce Richardson 	{                                                   \
84*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V6,              /* flags */     \
85*99a2dd95SBruce Richardson 	},                                                  \
86*99a2dd95SBruce Richardson }
87*99a2dd95SBruce Richardson 
88*99a2dd95SBruce Richardson #define TOKEN_IPNET_INITIALIZER(structure, field)       \
89*99a2dd95SBruce Richardson {                                                       \
90*99a2dd95SBruce Richardson 	/* hdr */                                           \
91*99a2dd95SBruce Richardson 	{                                                   \
92*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
93*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
94*99a2dd95SBruce Richardson 	},                                                  \
95*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
96*99a2dd95SBruce Richardson 	{                                                   \
97*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V4 |             /* flags */     \
98*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V6 |                             \
99*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_NETWORK,                         \
100*99a2dd95SBruce Richardson 	},                                                  \
101*99a2dd95SBruce Richardson }
102*99a2dd95SBruce Richardson 
103*99a2dd95SBruce Richardson #define TOKEN_IPV4NET_INITIALIZER(structure, field)     \
104*99a2dd95SBruce Richardson {                                                       \
105*99a2dd95SBruce Richardson 	/* hdr */                                           \
106*99a2dd95SBruce Richardson 	{                                                   \
107*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
108*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
109*99a2dd95SBruce Richardson 	},                                                  \
110*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
111*99a2dd95SBruce Richardson 	{                                                   \
112*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V4 |             /* flags */     \
113*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_NETWORK,                         \
114*99a2dd95SBruce Richardson 	},                                                  \
115*99a2dd95SBruce Richardson }
116*99a2dd95SBruce Richardson 
117*99a2dd95SBruce Richardson #define TOKEN_IPV6NET_INITIALIZER(structure, field)     \
118*99a2dd95SBruce Richardson {                                                       \
119*99a2dd95SBruce Richardson 	/* hdr */                                           \
120*99a2dd95SBruce Richardson 	{                                                   \
121*99a2dd95SBruce Richardson 		&cmdline_token_ipaddr_ops,      /* ops */       \
122*99a2dd95SBruce Richardson 		offsetof(structure, field),     /* offset */    \
123*99a2dd95SBruce Richardson 	},                                                  \
124*99a2dd95SBruce Richardson 	/* ipaddr_data */                                   \
125*99a2dd95SBruce Richardson 	{                                                   \
126*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_V4 |             /* flags */     \
127*99a2dd95SBruce Richardson 		CMDLINE_IPADDR_NETWORK,                         \
128*99a2dd95SBruce Richardson 	},                                                  \
129*99a2dd95SBruce Richardson }
130*99a2dd95SBruce Richardson 
131*99a2dd95SBruce Richardson #ifdef __cplusplus
132*99a2dd95SBruce Richardson }
133*99a2dd95SBruce Richardson #endif
134*99a2dd95SBruce Richardson 
135*99a2dd95SBruce Richardson #endif /* _PARSE_IPADDR_H_ */
136