parse.c 8.6 KB

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