parse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_hex_number(string str)
  34. * parse_value(string str)
  35. * parse_ipv4_addr(string str)
  36. * parse_ipv6_addr(string str)
  37. *
  38. * Variables:
  39. * succeeded - "true" or "false", reflecting success of the parsing
  40. * (empty) - normalized parsed value (only if succeeded)
  41. *
  42. * Synopsis:
  43. * parse_ipv4_cidr_addr(string str)
  44. * parse_ipv6_cidr_addr(string str)
  45. *
  46. * Variables:
  47. * succeeded - "true" or "false", reflecting success of the parsing
  48. * (empty) - normalized CIDR notation address (only if succeeded)
  49. * addr - normalized address without prefix (only if succeeded)
  50. * prefix - normalized prefix without address (only if succeeded)
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <inttypes.h>
  55. #include <misc/parse_number.h>
  56. #include <misc/ipaddr.h>
  57. #include <misc/ipaddr6.h>
  58. #include <ncd/NCDValParser.h>
  59. #include <ncd/module_common.h>
  60. #include <generated/blog_channel_ncd_parse.h>
  61. struct instance {
  62. NCDModuleInst *i;
  63. NCDValMem mem;
  64. NCDValRef value;
  65. int succeeded;
  66. };
  67. struct ipv4_cidr_instance {
  68. NCDModuleInst *i;
  69. int succeeded;
  70. struct ipv4_ifaddr ifaddr;
  71. };
  72. struct ipv6_cidr_instance {
  73. NCDModuleInst *i;
  74. int succeeded;
  75. struct ipv6_ifaddr ifaddr;
  76. };
  77. enum {STRING_ADDR, STRING_PREFIX};
  78. static const char *strings[] = {
  79. "addr", "prefix", NULL
  80. };
  81. typedef int (*parse_func) (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out);
  82. static int parse_number (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out)
  83. {
  84. uintmax_t n;
  85. if (!parse_unsigned_integer(str, &n)) {
  86. ModuleLog(i, BLOG_ERROR, "failed to parse number");
  87. return 0;
  88. }
  89. *out = ncd_make_uintmax(mem, n);
  90. if (NCDVal_IsInvalid(*out)) {
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static int parse_hex_number (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out)
  96. {
  97. uintmax_t n;
  98. if (!parse_unsigned_hex_integer(str, &n)) {
  99. ModuleLog(i, BLOG_ERROR, "failed to parse hex number");
  100. return 0;
  101. }
  102. *out = ncd_make_uintmax(mem, n);
  103. if (NCDVal_IsInvalid(*out)) {
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static int parse_value (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out)
  109. {
  110. if (!NCDValParser_Parse(str, mem, out)) {
  111. ModuleLog(i, BLOG_ERROR, "failed to parse value");
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. static int parse_ipv4_addr (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out)
  117. {
  118. uint32_t addr;
  119. if (!ipaddr_parse_ipv4_addr(str, &addr)) {
  120. ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
  121. return 0;
  122. }
  123. char buf[IPADDR_PRINT_MAX];
  124. ipaddr_print_addr(addr, buf);
  125. *out = NCDVal_NewString(mem, buf);
  126. if (NCDVal_IsInvalid(*out)) {
  127. return 0;
  128. }
  129. return 1;
  130. }
  131. static int parse_ipv6_addr (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out)
  132. {
  133. struct ipv6_addr addr;
  134. if (!ipaddr6_parse_ipv6_addr(str, &addr)) {
  135. ModuleLog(i, BLOG_ERROR, "failed to parse ipv6 addresss");
  136. return 0;
  137. }
  138. char buf[IPADDR6_PRINT_MAX];
  139. ipaddr6_print_addr(addr, buf);
  140. *out = NCDVal_NewString(mem, buf);
  141. if (NCDVal_IsInvalid(*out)) {
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, parse_func pfunc)
  147. {
  148. struct instance *o = vo;
  149. o->i = i;
  150. // read arguments
  151. NCDValRef str_arg;
  152. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  153. ModuleLog(i, BLOG_ERROR, "wrong arity");
  154. goto fail0;
  155. }
  156. if (!NCDVal_IsString(str_arg)) {
  157. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  158. goto fail0;
  159. }
  160. // init mem
  161. NCDValMem_Init(&o->mem, i->params->iparams->string_index);
  162. // parse
  163. o->succeeded = pfunc(i, NCDVal_StringMemRef(str_arg), &o->mem, &o->value);
  164. // signal up
  165. NCDModuleInst_Backend_Up(i);
  166. return;
  167. fail0:
  168. NCDModuleInst_Backend_DeadError(i);
  169. }
  170. static void func_die (void *vo)
  171. {
  172. struct instance *o = vo;
  173. // free mem
  174. NCDValMem_Free(&o->mem);
  175. NCDModuleInst_Backend_Dead(o->i);
  176. }
  177. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  178. {
  179. struct instance *o = vo;
  180. if (name == NCD_STRING_SUCCEEDED) {
  181. *out = ncd_make_boolean(mem, o->succeeded);
  182. return 1;
  183. }
  184. if (o->succeeded && name == NCD_STRING_EMPTY) {
  185. *out = NCDVal_NewCopy(mem, o->value);
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. static void func_new_parse_number (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  191. {
  192. new_templ(vo, i, params, parse_number);
  193. }
  194. static void func_new_parse_hex_number (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  195. {
  196. new_templ(vo, i, params, parse_hex_number);
  197. }
  198. static void func_new_parse_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  199. {
  200. new_templ(vo, i, params, parse_value);
  201. }
  202. static void func_new_parse_ipv4_addr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  203. {
  204. new_templ(vo, i, params, parse_ipv4_addr);
  205. }
  206. static void func_new_parse_ipv6_addr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  207. {
  208. new_templ(vo, i, params, parse_ipv6_addr);
  209. }
  210. static void ipv4_cidr_addr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  211. {
  212. struct ipv4_cidr_instance *o = vo;
  213. o->i = i;
  214. NCDValRef str_arg;
  215. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  216. ModuleLog(i, BLOG_ERROR, "wrong arity");
  217. goto fail0;
  218. }
  219. if (!NCDVal_IsString(str_arg)) {
  220. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  221. goto fail0;
  222. }
  223. o->succeeded = ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(str_arg), &o->ifaddr);
  224. NCDModuleInst_Backend_Up(i);
  225. return;
  226. fail0:
  227. NCDModuleInst_Backend_DeadError(i);
  228. }
  229. static int ipv4_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  230. {
  231. struct ipv4_cidr_instance *o = vo;
  232. if (name == NCD_STRING_SUCCEEDED) {
  233. *out = ncd_make_boolean(mem, o->succeeded);
  234. return 1;
  235. }
  236. if (!o->succeeded) {
  237. return 0;
  238. }
  239. char str[IPADDR_PRINT_MAX];
  240. if (name == NCD_STRING_EMPTY) {
  241. ipaddr_print_ifaddr(o->ifaddr, str);
  242. }
  243. else if (name == ModuleString(o->i, STRING_ADDR)) {
  244. ipaddr_print_addr(o->ifaddr.addr, str);
  245. }
  246. else if (name == ModuleString(o->i, STRING_PREFIX)) {
  247. sprintf(str, "%d", o->ifaddr.prefix);
  248. }
  249. else {
  250. return 0;
  251. }
  252. *out = NCDVal_NewString(mem, str);
  253. return 1;
  254. }
  255. static void ipv6_cidr_addr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  256. {
  257. struct ipv6_cidr_instance *o = vo;
  258. o->i = i;
  259. NCDValRef str_arg;
  260. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  261. ModuleLog(i, BLOG_ERROR, "wrong arity");
  262. goto fail0;
  263. }
  264. if (!NCDVal_IsString(str_arg)) {
  265. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  266. goto fail0;
  267. }
  268. o->succeeded = ipaddr6_parse_ipv6_ifaddr(NCDVal_StringMemRef(str_arg), &o->ifaddr);
  269. NCDModuleInst_Backend_Up(i);
  270. return;
  271. fail0:
  272. NCDModuleInst_Backend_DeadError(i);
  273. }
  274. static int ipv6_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  275. {
  276. struct ipv6_cidr_instance *o = vo;
  277. if (name == NCD_STRING_SUCCEEDED) {
  278. *out = ncd_make_boolean(mem, o->succeeded);
  279. return 1;
  280. }
  281. if (!o->succeeded) {
  282. return 0;
  283. }
  284. char str[IPADDR6_PRINT_MAX];
  285. if (name == NCD_STRING_EMPTY) {
  286. ipaddr6_print_ifaddr(o->ifaddr, str);
  287. }
  288. else if (name == ModuleString(o->i, STRING_ADDR)) {
  289. ipaddr6_print_addr(o->ifaddr.addr, str);
  290. }
  291. else if (name == ModuleString(o->i, STRING_PREFIX)) {
  292. sprintf(str, "%d", o->ifaddr.prefix);
  293. }
  294. else {
  295. return 0;
  296. }
  297. *out = NCDVal_NewString(mem, str);
  298. return 1;
  299. }
  300. static struct NCDModule modules[] = {
  301. {
  302. .type = "parse_number",
  303. .func_new2 = func_new_parse_number,
  304. .func_die = func_die,
  305. .func_getvar2 = func_getvar2,
  306. .alloc_size = sizeof(struct instance)
  307. }, {
  308. .type = "parse_hex_number",
  309. .func_new2 = func_new_parse_hex_number,
  310. .func_die = func_die,
  311. .func_getvar2 = func_getvar2,
  312. .alloc_size = sizeof(struct instance)
  313. }, {
  314. .type = "parse_value",
  315. .func_new2 = func_new_parse_value,
  316. .func_die = func_die,
  317. .func_getvar2 = func_getvar2,
  318. .alloc_size = sizeof(struct instance)
  319. }, {
  320. .type = "parse_ipv4_addr",
  321. .func_new2 = func_new_parse_ipv4_addr,
  322. .func_die = func_die,
  323. .func_getvar2 = func_getvar2,
  324. .alloc_size = sizeof(struct instance)
  325. }, {
  326. .type = "parse_ipv6_addr",
  327. .func_new2 = func_new_parse_ipv6_addr,
  328. .func_die = func_die,
  329. .func_getvar2 = func_getvar2,
  330. .alloc_size = sizeof(struct instance)
  331. }, {
  332. .type = "parse_ipv4_cidr_addr",
  333. .func_new2 = ipv4_cidr_addr_func_new,
  334. .func_getvar2 = ipv4_cidr_addr_func_getvar2,
  335. .alloc_size = sizeof(struct ipv4_cidr_instance)
  336. }, {
  337. .type = "parse_ipv6_cidr_addr",
  338. .func_new2 = ipv6_cidr_addr_func_new,
  339. .func_getvar2 = ipv6_cidr_addr_func_getvar2,
  340. .alloc_size = sizeof(struct ipv6_cidr_instance)
  341. }, {
  342. .type = NULL
  343. }
  344. };
  345. const struct NCDModuleGroup ncdmodule_parse = {
  346. .modules = modules,
  347. .strings = strings
  348. };