parse.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <string.h>
  52. #include <inttypes.h>
  53. #include <misc/parse_number.h>
  54. #include <misc/ipaddr.h>
  55. #include <ncd/NCDValParser.h>
  56. #include <ncd/NCDModule.h>
  57. #include <ncd/static_strings.h>
  58. #include <generated/blog_channel_ncd_parse.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. struct instance {
  61. NCDModuleInst *i;
  62. NCDValMem mem;
  63. NCDValRef value;
  64. int succeeded;
  65. };
  66. struct ipv4_cidr_instance {
  67. NCDModuleInst *i;
  68. int succeeded;
  69. struct ipv4_ifaddr ifaddr;
  70. };
  71. enum {STRING_SUCCEEDED, STRING_ADDR, STRING_PREFIX};
  72. static struct NCD_string_request strings[] = {
  73. {"succeeded"}, {"addr"}, {"prefix"}, {NULL}
  74. };
  75. typedef int (*parse_func) (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out);
  76. static int parse_number (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  77. {
  78. uintmax_t n;
  79. if (!parse_unsigned_integer(str, &n)) {
  80. ModuleLog(i, BLOG_ERROR, "failed to parse number");
  81. return 0;
  82. }
  83. char buf[25];
  84. snprintf(buf, sizeof(buf), "%"PRIuMAX, n);
  85. *out = NCDVal_NewString(mem, buf);
  86. if (NCDVal_IsInvalid(*out)) {
  87. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. static int parse_value (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  93. {
  94. if (!NCDValParser_Parse(str, strlen(str), mem, out)) {
  95. ModuleLog(i, BLOG_ERROR, "failed to parse value");
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. static int parse_ipv4_addr (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  101. {
  102. uint32_t addr;
  103. if (!ipaddr_parse_ipv4_addr((char *)str, &addr)) {
  104. ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
  105. return 0;
  106. }
  107. char buf[IPADDR_PRINT_MAX];
  108. ipaddr_print_addr(addr, buf);
  109. *out = NCDVal_NewString(mem, buf);
  110. if (NCDVal_IsInvalid(*out)) {
  111. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, parse_func pfunc)
  117. {
  118. struct instance *o = vo;
  119. o->i = i;
  120. // read arguments
  121. NCDValRef str_arg;
  122. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  123. ModuleLog(i, BLOG_ERROR, "wrong arity");
  124. goto fail0;
  125. }
  126. if (!NCDVal_IsString(str_arg)) {
  127. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  128. goto fail0;
  129. }
  130. // init mem
  131. NCDValMem_Init(&o->mem);
  132. // parse
  133. if (NCDVal_StringHasNulls(str_arg)) {
  134. ModuleLog(o->i, BLOG_ERROR, "string has nulls");
  135. o->succeeded = 0;
  136. } else {
  137. o->succeeded = pfunc(i, NCDVal_StringValue(str_arg), &o->mem, &o->value);
  138. }
  139. // signal up
  140. NCDModuleInst_Backend_Up(i);
  141. return;
  142. fail0:
  143. NCDModuleInst_Backend_SetError(i);
  144. NCDModuleInst_Backend_Dead(i);
  145. }
  146. static void func_die (void *vo)
  147. {
  148. struct instance *o = vo;
  149. // free mem
  150. NCDValMem_Free(&o->mem);
  151. NCDModuleInst_Backend_Dead(o->i);
  152. }
  153. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  154. {
  155. struct instance *o = vo;
  156. if (name == strings[STRING_SUCCEEDED].id) {
  157. const char *str = o->succeeded ? "true" : "false";
  158. *out = NCDVal_NewString(mem, str);
  159. if (NCDVal_IsInvalid(*out)) {
  160. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  161. }
  162. return 1;
  163. }
  164. if (o->succeeded && name == NCD_STRING_EMPTY) {
  165. *out = NCDVal_NewCopy(mem, o->value);
  166. if (NCDVal_IsInvalid(*out)) {
  167. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  168. }
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. static void func_new_parse_number (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  174. {
  175. new_templ(vo, i, params, parse_number);
  176. }
  177. static void func_new_parse_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  178. {
  179. new_templ(vo, i, params, parse_value);
  180. }
  181. static void func_new_parse_ipv4_addr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  182. {
  183. new_templ(vo, i, params, parse_ipv4_addr);
  184. }
  185. static void ipv4_cidr_addr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  186. {
  187. struct ipv4_cidr_instance *o = vo;
  188. o->i = i;
  189. NCDValRef str_arg;
  190. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  191. ModuleLog(i, BLOG_ERROR, "wrong arity");
  192. goto fail0;
  193. }
  194. if (!NCDVal_IsString(str_arg)) {
  195. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  196. goto fail0;
  197. }
  198. o->succeeded = ipaddr_parse_ipv4_ifaddr_bin(NCDVal_StringValue(str_arg), NCDVal_StringLength(str_arg), &o->ifaddr);
  199. NCDModuleInst_Backend_Up(i);
  200. return;
  201. fail0:
  202. NCDModuleInst_Backend_SetError(i);
  203. NCDModuleInst_Backend_Dead(i);
  204. }
  205. static int ipv4_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  206. {
  207. struct ipv4_cidr_instance *o = vo;
  208. if (name == strings[STRING_SUCCEEDED].id) {
  209. const char *str = o->succeeded ? "true" : "false";
  210. *out = NCDVal_NewString(mem, str);
  211. if (NCDVal_IsInvalid(*out)) {
  212. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  213. }
  214. return 1;
  215. }
  216. if (!o->succeeded) {
  217. return 0;
  218. }
  219. char str[IPADDR_PRINT_MAX];
  220. if (name == NCD_STRING_EMPTY) {
  221. ipaddr_print_ifaddr(o->ifaddr, str);
  222. }
  223. else if (name == strings[STRING_ADDR].id) {
  224. ipaddr_print_addr(o->ifaddr.addr, str);
  225. }
  226. else if (name == strings[STRING_PREFIX].id) {
  227. sprintf(str, "%d", o->ifaddr.prefix);
  228. }
  229. else {
  230. return 0;
  231. }
  232. *out = NCDVal_NewString(mem, str);
  233. if (NCDVal_IsInvalid(*out)) {
  234. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  235. }
  236. return 1;
  237. }
  238. static struct NCDModule modules[] = {
  239. {
  240. .type = "parse_number",
  241. .func_new2 = func_new_parse_number,
  242. .func_die = func_die,
  243. .func_getvar2 = func_getvar2,
  244. .alloc_size = sizeof(struct instance)
  245. }, {
  246. .type = "parse_value",
  247. .func_new2 = func_new_parse_value,
  248. .func_die = func_die,
  249. .func_getvar2 = func_getvar2,
  250. .alloc_size = sizeof(struct instance)
  251. }, {
  252. .type = "parse_ipv4_addr",
  253. .func_new2 = func_new_parse_ipv4_addr,
  254. .func_die = func_die,
  255. .func_getvar2 = func_getvar2,
  256. .alloc_size = sizeof(struct instance)
  257. }, {
  258. .type = "parse_ipv4_cidr_addr",
  259. .func_new2 = ipv4_cidr_addr_func_new,
  260. .func_getvar2 = ipv4_cidr_addr_func_getvar2,
  261. .alloc_size = sizeof(struct ipv4_cidr_instance)
  262. }, {
  263. .type = NULL
  264. }
  265. };
  266. const struct NCDModuleGroup ncdmodule_parse = {
  267. .modules = modules,
  268. .strings = strings
  269. };