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 parse_sp4_tokens(char **tokens, uint32_t n_tokens, 71 struct parse_status *status); 72 73 void 74 parse_sp6_tokens(char **tokens, uint32_t n_tokens, 75 struct parse_status *status); 76 77 void 78 parse_sa_tokens(char **tokens, uint32_t n_tokens, 79 struct parse_status *status); 80 81 void 82 parse_rt_tokens(char **tokens, uint32_t n_tokens, 83 struct parse_status *status); 84 85 int 86 parse_cfg_file(const char *cfg_filename); 87 88 #endif 89