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