1 /* BSD LICENSE 2 * 3 * Copyright(c) 2016 Intel Corporation. All rights reserved. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Intel Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <netinet/in.h> 35 #include <netinet/ip.h> 36 37 #ifndef __PARSER_H 38 #define __PARSER_H 39 40 struct parse_status { 41 int status; 42 char parse_msg[256]; 43 }; 44 45 #define APP_CHECK(exp, status, fmt, ...) \ 46 do { \ 47 if (!(exp)) { \ 48 sprintf(status->parse_msg, fmt "\n", \ 49 ## __VA_ARGS__); \ 50 status->status = -1; \ 51 } else \ 52 status->status = 0; \ 53 } while (0) 54 55 #define APP_CHECK_PRESENCE(val, str, status) \ 56 APP_CHECK(val == 0, status, \ 57 "item \"%s\" already present", str) 58 59 #define APP_CHECK_TOKEN_EQUAL(tokens, index, ref, status) \ 60 APP_CHECK(strcmp(tokens[index], ref) == 0, status, \ 61 "unrecognized input \"%s\": expect \"%s\"\n", \ 62 tokens[index], ref) 63 64 static inline int 65 is_str_num(const char *str) 66 { 67 uint32_t i; 68 69 for (i = 0; i < strlen(str); i++) 70 if (!isdigit(str[i])) 71 return -1; 72 73 return 0; 74 } 75 76 #define APP_CHECK_TOKEN_IS_NUM(tokens, index, status) \ 77 APP_CHECK(is_str_num(tokens[index]) == 0, status, \ 78 "input \"%s\" is not valid number string", tokens[index]) 79 80 81 #define INCREMENT_TOKEN_INDEX(index, max_num, status) \ 82 do { \ 83 APP_CHECK(index + 1 < max_num, status, "reaching the end of " \ 84 "the token array"); \ 85 index++; \ 86 } while (0) 87 88 int 89 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask); 90 91 int 92 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask); 93 94 int 95 parse_range(const char *token, uint16_t *low, uint16_t *high); 96 97 void 98 parse_sp4_tokens(char **tokens, uint32_t n_tokens, 99 struct parse_status *status); 100 101 void 102 parse_sp6_tokens(char **tokens, uint32_t n_tokens, 103 struct parse_status *status); 104 105 void 106 parse_sa_tokens(char **tokens, uint32_t n_tokens, 107 struct parse_status *status); 108 109 void 110 parse_rt_tokens(char **tokens, uint32_t n_tokens, 111 struct parse_status *status); 112 113 int 114 parse_cfg_file(const char *cfg_filename); 115 116 #endif 117