xref: /dpdk/examples/ipsec-secgw/parser.h (revision 7adf992fb9bf7162a7edc45b50d10fbb1d57824d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4 
5 #include <sys/types.h>
6 #include <netinet/in.h>
7 #include <netinet/ip.h>
8 
9 #ifndef __PARSER_H
10 #define __PARSER_H
11 
12 struct parse_status {
13 	int status;
14 	char parse_msg[256];
15 };
16 
17 #define	APP_CHECK(exp, st, fmt, ...)					\
18 do {									\
19 	if (!(exp)) {							\
20 		sprintf((st)->parse_msg, fmt "\n",			\
21 			## __VA_ARGS__);				\
22 		(st)->status = -1;					\
23 	} else								\
24 		(st)->status = 0;					\
25 } while (0)
26 
27 #define APP_CHECK_PRESENCE(val, str, status)				\
28 	APP_CHECK(val == 0, status,					\
29 		"item \"%s\" already present", str)
30 
31 #define APP_CHECK_TOKEN_EQUAL(tokens, index, ref, status)		\
32 	APP_CHECK(strcmp(tokens[index], ref) == 0, status,		\
33 		"unrecognized input \"%s\": expect \"%s\"\n",		\
34 		tokens[index], ref)
35 
36 static inline int
37 is_str_num(const char *str)
38 {
39 	uint32_t i;
40 
41 	for (i = 0; i < strlen(str); i++)
42 		if (!isdigit(str[i]))
43 			return -1;
44 
45 	return 0;
46 }
47 
48 #define APP_CHECK_TOKEN_IS_NUM(tokens, index, status)			\
49 	APP_CHECK(is_str_num(tokens[index]) == 0, status,		\
50 	"input \"%s\" is not valid number string", tokens[index])
51 
52 
53 #define INCREMENT_TOKEN_INDEX(index, max_num, status)			\
54 do {									\
55 	APP_CHECK(index + 1 < max_num, status, "reaching the end of "	\
56 		"the token array");					\
57 	index++;							\
58 } while (0)
59 
60 int
61 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask);
62 
63 int
64 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask);
65 
66 int
67 parse_range(const char *token, uint16_t *low, uint16_t *high);
68 
69 void
70 sp4_sort_arr(void);
71 
72 void
73 parse_sp4_tokens(char **tokens, uint32_t n_tokens,
74 	struct parse_status *status);
75 
76 void
77 sp6_sort_arr(void);
78 
79 void
80 parse_sp6_tokens(char **tokens, uint32_t n_tokens,
81 	struct parse_status *status);
82 
83 void
84 sa_sort_arr(void);
85 
86 void
87 parse_sa_tokens(char **tokens, uint32_t n_tokens,
88 	struct parse_status *status);
89 
90 void
91 parse_rt_tokens(char **tokens, uint32_t n_tokens,
92 	struct parse_status *status);
93 
94 int
95 parse_cfg_file(const char *cfg_filename);
96 
97 #endif
98