ucimap-example.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * ucimap-example - sample code for the ucimap library
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <strings.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <ucimap.h>
  19. struct list_head ifs;
  20. struct uci_network {
  21. struct ucimap_section_data map;
  22. struct list_head list;
  23. struct list_head alias;
  24. const char *name;
  25. const char *proto;
  26. const char *ifname;
  27. unsigned char *ipaddr;
  28. int test;
  29. bool enabled;
  30. struct ucimap_list *aliases;
  31. };
  32. struct uci_alias {
  33. struct ucimap_section_data map;
  34. struct list_head list;
  35. const char *name;
  36. struct uci_network *interface;
  37. };
  38. static int
  39. network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
  40. {
  41. unsigned char *target;
  42. int tmp[4];
  43. int i;
  44. if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
  45. return -1;
  46. target = malloc(4);
  47. if (!target)
  48. return -1;
  49. *data->data = target;
  50. for (i = 0; i < 4; i++)
  51. target[i] = (char) tmp[i];
  52. return 0;
  53. }
  54. static int
  55. network_format_ip(void *sction, struct uci_optmap *om, union ucimap_data *data, char **str)
  56. {
  57. static char buf[16];
  58. unsigned char *ip = (unsigned char *) data->data[0];
  59. if (ip) {
  60. sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  61. *str = buf;
  62. } else {
  63. *str = NULL;
  64. }
  65. return 0;
  66. }
  67. static void
  68. network_free_ip(void *section, struct uci_optmap *om, void *ptr)
  69. {
  70. free(ptr);
  71. }
  72. static int
  73. network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
  74. {
  75. struct uci_network *net = section;
  76. INIT_LIST_HEAD(&net->list);
  77. INIT_LIST_HEAD(&net->alias);
  78. net->name = s->e.name;
  79. net->test = -1;
  80. return 0;
  81. }
  82. static int
  83. network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
  84. {
  85. struct uci_alias *alias = section;
  86. INIT_LIST_HEAD(&alias->list);
  87. alias->name = s->e.name;
  88. return 0;
  89. }
  90. static int
  91. network_add_interface(struct uci_map *map, void *section)
  92. {
  93. struct uci_network *net = section;
  94. list_add_tail(&net->list, &ifs);
  95. return 0;
  96. }
  97. static int
  98. network_add_alias(struct uci_map *map, void *section)
  99. {
  100. struct uci_alias *a = section;
  101. if (a->interface)
  102. list_add_tail(&a->list, &a->interface->alias);
  103. return 0;
  104. }
  105. static struct ucimap_section_data *
  106. network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
  107. {
  108. struct uci_network *p = malloc(sizeof(struct uci_network));
  109. memset(p, 0, sizeof(struct uci_network));
  110. return &p->map;
  111. }
  112. struct my_optmap {
  113. struct uci_optmap map;
  114. int test;
  115. };
  116. static struct uci_sectionmap network_interface;
  117. static struct uci_sectionmap network_alias;
  118. static struct my_optmap network_interface_options[] = {
  119. {
  120. .map = {
  121. UCIMAP_OPTION(struct uci_network, proto),
  122. .type = UCIMAP_STRING,
  123. .name = "proto",
  124. .data.s.maxlen = 32,
  125. }
  126. },
  127. {
  128. .map = {
  129. UCIMAP_OPTION(struct uci_network, ifname),
  130. .type = UCIMAP_STRING,
  131. .name = "ifname"
  132. }
  133. },
  134. {
  135. .map = {
  136. UCIMAP_OPTION(struct uci_network, ipaddr),
  137. .type = UCIMAP_CUSTOM,
  138. .name = "ipaddr",
  139. .parse = network_parse_ip,
  140. .format = network_format_ip,
  141. .free = network_free_ip,
  142. }
  143. },
  144. {
  145. .map = {
  146. UCIMAP_OPTION(struct uci_network, enabled),
  147. .type = UCIMAP_BOOL,
  148. .name = "enabled",
  149. }
  150. },
  151. {
  152. .map = {
  153. UCIMAP_OPTION(struct uci_network, test),
  154. .type = UCIMAP_INT,
  155. .name = "test"
  156. }
  157. },
  158. {
  159. .map = {
  160. UCIMAP_OPTION(struct uci_network, aliases),
  161. .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
  162. .data.sm = &network_alias
  163. }
  164. }
  165. };
  166. static struct uci_sectionmap network_interface = {
  167. UCIMAP_SECTION(struct uci_network, map),
  168. .type = "interface",
  169. .alloc = network_allocate,
  170. .init = network_init_interface,
  171. .add = network_add_interface,
  172. .options = &network_interface_options[0].map,
  173. .n_options = ARRAY_SIZE(network_interface_options),
  174. .options_size = sizeof(struct my_optmap)
  175. };
  176. static struct uci_optmap network_alias_options[] = {
  177. {
  178. UCIMAP_OPTION(struct uci_alias, interface),
  179. .type = UCIMAP_SECTION,
  180. .data.sm = &network_interface
  181. }
  182. };
  183. static struct uci_sectionmap network_alias = {
  184. UCIMAP_SECTION(struct uci_alias, map),
  185. .type = "alias",
  186. .options = network_alias_options,
  187. .init = network_init_alias,
  188. .add = network_add_alias,
  189. .n_options = ARRAY_SIZE(network_alias_options),
  190. };
  191. static struct uci_sectionmap *network_smap[] = {
  192. &network_interface,
  193. &network_alias,
  194. };
  195. static struct uci_map network_map = {
  196. .sections = network_smap,
  197. .n_sections = ARRAY_SIZE(network_smap),
  198. };
  199. int main(int argc, char **argv)
  200. {
  201. struct uci_context *ctx;
  202. struct uci_package *pkg;
  203. struct list_head *p;
  204. struct uci_network *net;
  205. struct uci_alias *alias;
  206. bool set = false;
  207. int i;
  208. INIT_LIST_HEAD(&ifs);
  209. ctx = uci_alloc_context();
  210. ucimap_init(&network_map);
  211. if ((argc >= 2) && !strcmp(argv[1], "-s")) {
  212. uci_set_savedir(ctx, "./test/save");
  213. set = true;
  214. }
  215. uci_set_confdir(ctx, "./test/config");
  216. uci_load(ctx, "network", &pkg);
  217. ucimap_parse(&network_map, pkg);
  218. list_for_each(p, &ifs) {
  219. const unsigned char *ipaddr;
  220. net = list_entry(p, struct uci_network, list);
  221. ipaddr = net->ipaddr;
  222. if (!ipaddr)
  223. ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
  224. printf("New network section '%s'\n"
  225. " type: %s\n"
  226. " ifname: %s\n"
  227. " ipaddr: %d.%d.%d.%d\n"
  228. " test: %d\n"
  229. " enabled: %s\n",
  230. net->name,
  231. net->proto,
  232. net->ifname,
  233. ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
  234. net->test,
  235. (net->enabled ? "on" : "off"));
  236. if (net->aliases->n_items > 0) {
  237. printf("Configured aliases:");
  238. for (i = 0; i < net->aliases->n_items; i++) {
  239. alias = net->aliases->item[i].ptr;
  240. printf(" %s", alias->name);
  241. }
  242. printf("\n");
  243. }
  244. list_for_each_entry(alias, &net->alias, list) {
  245. for (i = 0; i < net->aliases->n_items; i++) {
  246. if (alias == net->aliases->item[i].ptr)
  247. goto next_alias;
  248. }
  249. printf("New alias: %s\n", alias->name);
  250. next_alias:
  251. continue;
  252. }
  253. if (set && !strcmp(net->name, "lan")) {
  254. ucimap_free_item(&net->map, &net->ipaddr);
  255. ucimap_set_changed(&net->map, &net->ipaddr);
  256. ucimap_store_section(&network_map, pkg, &net->map);
  257. uci_save(ctx, pkg);
  258. }
  259. }
  260. ucimap_cleanup(&network_map);
  261. uci_free_context(ctx);
  262. return 0;
  263. }