parse.c 8.5 KB

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