parse.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @file parse.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * parse_number(string str)
  33. * parse_value(string str)
  34. * parse_ipv4_addr(string str)
  35. *
  36. * Variables:
  37. * succeeded - "true" or "false", reflecting success of the parsing
  38. * (empty) - normalized parsed value (only if succeeded)
  39. *
  40. * Synopsis:
  41. * parse_ipv4_cidr_addr(string str)
  42. *
  43. * Variables:
  44. * succeeded - "true" or "false", reflecting success of the parsing
  45. * (empty) - normalized CIDR notation address (only if succeeded)
  46. * addr - normalized address without prefix (only if succeeded)
  47. * prefix - normalized prefix without address (only if succeeded)
  48. */
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <inttypes.h>
  52. #include <misc/parse_number.h>
  53. #include <misc/ipaddr.h>
  54. #include <ncd/NCDValParser.h>
  55. #include <ncd/NCDModule.h>
  56. #include <ncd/static_strings.h>
  57. #include <generated/blog_channel_ncd_parse.h>
  58. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  59. struct instance {
  60. NCDModuleInst *i;
  61. NCDValMem mem;
  62. NCDValRef value;
  63. int succeeded;
  64. };
  65. struct ipv4_cidr_instance {
  66. NCDModuleInst *i;
  67. int succeeded;
  68. struct ipv4_ifaddr ifaddr;
  69. };
  70. enum {STRING_SUCCEEDED, STRING_ADDR, STRING_PREFIX};
  71. static struct NCD_string_request strings[] = {
  72. {"succeeded"}, {"addr"}, {"prefix"}, {NULL}
  73. };
  74. typedef int (*parse_func) (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out);
  75. static int parse_number (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
  76. {
  77. uintmax_t n;
  78. if (!parse_unsigned_integer_bin(str, str_len, &n)) {
  79. ModuleLog(i, BLOG_ERROR, "failed to parse number");
  80. return 0;
  81. }
  82. char buf[25];
  83. snprintf(buf, sizeof(buf), "%"PRIuMAX, n);
  84. *out = NCDVal_NewString(mem, buf);
  85. if (NCDVal_IsInvalid(*out)) {
  86. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. static int parse_value (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
  92. {
  93. if (!NCDValParser_Parse(str, str_len, mem, out)) {
  94. ModuleLog(i, BLOG_ERROR, "failed to parse value");
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. static int parse_ipv4_addr (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
  100. {
  101. uint32_t addr;
  102. if (!ipaddr_parse_ipv4_addr_bin(str, str_len, &addr)) {
  103. ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
  104. return 0;
  105. }
  106. char buf[IPADDR_PRINT_MAX];
  107. ipaddr_print_addr(addr, buf);
  108. *out = NCDVal_NewString(mem, buf);
  109. if (NCDVal_IsInvalid(*out)) {
  110. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, parse_func pfunc)
  116. {
  117. struct instance *o = vo;
  118. o->i = i;
  119. // read arguments
  120. NCDValRef str_arg;
  121. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  122. ModuleLog(i, BLOG_ERROR, "wrong arity");
  123. goto fail0;
  124. }
  125. if (!NCDVal_IsString(str_arg)) {
  126. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  127. goto fail0;
  128. }
  129. // init mem
  130. NCDValMem_Init(&o->mem);
  131. // parse
  132. o->succeeded = pfunc(i, NCDVal_StringValue(str_arg), NCDVal_StringLength(str_arg), &o->mem, &o->value);
  133. // signal up
  134. NCDModuleInst_Backend_Up(i);
  135. return;
  136. fail0:
  137. NCDModuleInst_Backend_SetError(i);
  138. NCDModuleInst_Backend_Dead(i);
  139. }
  140. static void func_die (void *vo)
  141. {
  142. struct instance *o = vo;
  143. // free mem
  144. NCDValMem_Free(&o->mem);
  145. NCDModuleInst_Backend_Dead(o->i);
  146. }
  147. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  148. {
  149. struct instance *o = vo;
  150. if (name == strings[STRING_SUCCEEDED].id) {
  151. const char *str = o->succeeded ? "true" : "false";
  152. *out = NCDVal_NewString(mem, str);
  153. if (NCDVal_IsInvalid(*out)) {
  154. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  155. }
  156. return 1;
  157. }
  158. if (o->succeeded && name == NCD_STRING_EMPTY) {
  159. *out = NCDVal_NewCopy(mem, o->value);
  160. if (NCDVal_IsInvalid(*out)) {
  161. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  162. }
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. static void func_new_parse_number (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  168. {
  169. new_templ(vo, i, params, parse_number);
  170. }
  171. static void func_new_parse_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  172. {
  173. new_templ(vo, i, params, parse_value);
  174. }
  175. static void func_new_parse_ipv4_addr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  176. {
  177. new_templ(vo, i, params, parse_ipv4_addr);
  178. }
  179. static void ipv4_cidr_addr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  180. {
  181. struct ipv4_cidr_instance *o = vo;
  182. o->i = i;
  183. NCDValRef str_arg;
  184. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  185. ModuleLog(i, BLOG_ERROR, "wrong arity");
  186. goto fail0;
  187. }
  188. if (!NCDVal_IsString(str_arg)) {
  189. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  190. goto fail0;
  191. }
  192. o->succeeded = ipaddr_parse_ipv4_ifaddr_bin(NCDVal_StringValue(str_arg), NCDVal_StringLength(str_arg), &o->ifaddr);
  193. NCDModuleInst_Backend_Up(i);
  194. return;
  195. fail0:
  196. NCDModuleInst_Backend_SetError(i);
  197. NCDModuleInst_Backend_Dead(i);
  198. }
  199. static int ipv4_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  200. {
  201. struct ipv4_cidr_instance *o = vo;
  202. if (name == strings[STRING_SUCCEEDED].id) {
  203. const char *str = o->succeeded ? "true" : "false";
  204. *out = NCDVal_NewString(mem, str);
  205. if (NCDVal_IsInvalid(*out)) {
  206. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  207. }
  208. return 1;
  209. }
  210. if (!o->succeeded) {
  211. return 0;
  212. }
  213. char str[IPADDR_PRINT_MAX];
  214. if (name == NCD_STRING_EMPTY) {
  215. ipaddr_print_ifaddr(o->ifaddr, str);
  216. }
  217. else if (name == strings[STRING_ADDR].id) {
  218. ipaddr_print_addr(o->ifaddr.addr, str);
  219. }
  220. else if (name == strings[STRING_PREFIX].id) {
  221. sprintf(str, "%d", o->ifaddr.prefix);
  222. }
  223. else {
  224. return 0;
  225. }
  226. *out = NCDVal_NewString(mem, str);
  227. if (NCDVal_IsInvalid(*out)) {
  228. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  229. }
  230. return 1;
  231. }
  232. static struct NCDModule modules[] = {
  233. {
  234. .type = "parse_number",
  235. .func_new2 = func_new_parse_number,
  236. .func_die = func_die,
  237. .func_getvar2 = func_getvar2,
  238. .alloc_size = sizeof(struct instance)
  239. }, {
  240. .type = "parse_value",
  241. .func_new2 = func_new_parse_value,
  242. .func_die = func_die,
  243. .func_getvar2 = func_getvar2,
  244. .alloc_size = sizeof(struct instance)
  245. }, {
  246. .type = "parse_ipv4_addr",
  247. .func_new2 = func_new_parse_ipv4_addr,
  248. .func_die = func_die,
  249. .func_getvar2 = func_getvar2,
  250. .alloc_size = sizeof(struct instance)
  251. }, {
  252. .type = "parse_ipv4_cidr_addr",
  253. .func_new2 = ipv4_cidr_addr_func_new,
  254. .func_getvar2 = ipv4_cidr_addr_func_getvar2,
  255. .alloc_size = sizeof(struct ipv4_cidr_instance)
  256. }, {
  257. .type = NULL
  258. }
  259. };
  260. const struct NCDModuleGroup ncdmodule_parse = {
  261. .modules = modules,
  262. .strings = strings
  263. };