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