parse.c 8.5 KB

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