ucimap-example.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * ucimap-example - sample code for the ucimap library
  3. * Copyright (C) 2008-2009 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. #include "list.h"
  20. struct list_head ifs;
  21. struct uci_network {
  22. struct ucimap_section_data map;
  23. struct list_head list;
  24. struct list_head alias;
  25. const char *name;
  26. const char *proto;
  27. const char *ifname;
  28. unsigned char *ipaddr;
  29. int test;
  30. bool enabled;
  31. struct ucimap_list *aliases;
  32. };
  33. struct uci_alias {
  34. struct ucimap_section_data map;
  35. struct list_head list;
  36. const char *name;
  37. struct uci_network *interface;
  38. };
  39. static int
  40. network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
  41. {
  42. unsigned char *target;
  43. int tmp[4];
  44. int i;
  45. if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
  46. return -1;
  47. target = malloc(4);
  48. if (!target)
  49. return -1;
  50. data->ptr = target;
  51. for (i = 0; i < 4; i++)
  52. target[i] = (char) tmp[i];
  53. return 0;
  54. }
  55. static int
  56. network_format_ip(void *section, struct uci_optmap *om, union ucimap_data *data, char **str)
  57. {
  58. static char buf[16];
  59. unsigned char *ip = (unsigned char *) data->ptr;
  60. if (ip) {
  61. sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  62. *str = buf;
  63. } else {
  64. *str = NULL;
  65. }
  66. return 0;
  67. }
  68. static void
  69. network_free_ip(void *section, struct uci_optmap *om, void *ptr)
  70. {
  71. free(ptr);
  72. }
  73. static int
  74. network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
  75. {
  76. struct uci_network *net = section;
  77. INIT_LIST_HEAD(&net->list);
  78. INIT_LIST_HEAD(&net->alias);
  79. net->name = s->e.name;
  80. net->test = -1;
  81. return 0;
  82. }
  83. static int
  84. network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
  85. {
  86. struct uci_alias *alias = section;
  87. INIT_LIST_HEAD(&alias->list);
  88. alias->name = s->e.name;
  89. return 0;
  90. }
  91. static int
  92. network_add_interface(struct uci_map *map, void *section)
  93. {
  94. struct uci_network *net = section;
  95. list_add_tail(&net->list, &ifs);
  96. return 0;
  97. }
  98. static int
  99. network_add_alias(struct uci_map *map, void *section)
  100. {
  101. struct uci_alias *a = section;
  102. if (a->interface)
  103. list_add_tail(&a->list, &a->interface->alias);
  104. return 0;
  105. }
  106. static struct ucimap_section_data *
  107. network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
  108. {
  109. struct uci_network *p = malloc(sizeof(struct uci_network));
  110. if (!p)
  111. return NULL;
  112. memset(p, 0, sizeof(struct uci_network));
  113. return &p->map;
  114. }
  115. struct my_optmap {
  116. struct uci_optmap map;
  117. int test;
  118. };
  119. static struct uci_sectionmap network_interface;
  120. static struct uci_sectionmap network_alias;
  121. static struct my_optmap network_interface_options[] = {
  122. {
  123. .map = {
  124. UCIMAP_OPTION(struct uci_network, proto),
  125. .type = UCIMAP_STRING,
  126. .data.s.maxlen = 32,
  127. }
  128. },
  129. {
  130. .map = {
  131. UCIMAP_OPTION(struct uci_network, ifname),
  132. .type = UCIMAP_STRING,
  133. }
  134. },
  135. {
  136. .map = {
  137. UCIMAP_OPTION(struct uci_network, ipaddr),
  138. .type = UCIMAP_CUSTOM,
  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. }
  149. },
  150. {
  151. .map = {
  152. UCIMAP_OPTION(struct uci_network, test),
  153. .type = UCIMAP_INT,
  154. }
  155. },
  156. {
  157. .map = {
  158. UCIMAP_OPTION(struct uci_network, aliases),
  159. .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
  160. .data.sm = &network_alias
  161. }
  162. }
  163. };
  164. static struct uci_sectionmap network_interface = {
  165. UCIMAP_SECTION(struct uci_network, map),
  166. .type = "interface",
  167. .alloc = network_allocate,
  168. .init = network_init_interface,
  169. .add = network_add_interface,
  170. .options = &network_interface_options[0].map,
  171. .n_options = ARRAY_SIZE(network_interface_options),
  172. .options_size = sizeof(struct my_optmap)
  173. };
  174. static struct uci_optmap network_alias_options[] = {
  175. {
  176. UCIMAP_OPTION(struct uci_alias, interface),
  177. .type = UCIMAP_SECTION,
  178. .data.sm = &network_interface
  179. }
  180. };
  181. static struct uci_sectionmap network_alias = {
  182. UCIMAP_SECTION(struct uci_alias, map),
  183. .type = "alias",
  184. .options = network_alias_options,
  185. .init = network_init_alias,
  186. .add = network_add_alias,
  187. .n_options = ARRAY_SIZE(network_alias_options),
  188. };
  189. static struct uci_sectionmap *network_smap[] = {
  190. &network_interface,
  191. &network_alias,
  192. };
  193. static struct uci_map network_map = {
  194. .sections = network_smap,
  195. .n_sections = ARRAY_SIZE(network_smap),
  196. };
  197. int main(int argc, char **argv)
  198. {
  199. struct uci_context *ctx;
  200. struct uci_package *pkg;
  201. struct list_head *p;
  202. struct uci_network *net;
  203. struct uci_alias *alias;
  204. bool set = false;
  205. int i;
  206. INIT_LIST_HEAD(&ifs);
  207. ctx = uci_alloc_context();
  208. if (!ctx)
  209. return -1;
  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. int n_aliases = 0;
  221. net = list_entry(p, struct uci_network, list);
  222. ipaddr = net->ipaddr;
  223. if (!ipaddr)
  224. ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
  225. printf("New network section '%s'\n"
  226. " type: %s\n"
  227. " ifname: %s\n"
  228. " ipaddr: %d.%d.%d.%d\n"
  229. " test: %d\n"
  230. " enabled: %s\n",
  231. net->name,
  232. net->proto,
  233. net->ifname,
  234. ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
  235. net->test,
  236. (net->enabled ? "on" : "off"));
  237. if (net->aliases->n_items > 0) {
  238. printf("Configured aliases:");
  239. for (i = 0; i < net->aliases->n_items; i++) {
  240. alias = net->aliases->item[i].ptr;
  241. printf(" %s", alias->name);
  242. }
  243. printf("\n");
  244. }
  245. list_for_each_entry(alias, &net->alias, list) {
  246. n_aliases++;
  247. for (i = 0; i < net->aliases->n_items; i++) {
  248. if (alias == net->aliases->item[i].ptr)
  249. goto next_alias;
  250. }
  251. printf("New alias: %s\n", alias->name);
  252. next_alias:
  253. continue;
  254. }
  255. if (set && !strcmp(net->name, "lan")) {
  256. ucimap_free_item(&net->map, &net->ipaddr);
  257. ucimap_set_changed(&net->map, &net->ipaddr);
  258. ucimap_resize_list(&net->map, &net->aliases, n_aliases);
  259. net->aliases->n_items = 0;
  260. list_for_each_entry(alias, &net->alias, list) {
  261. net->aliases->item[net->aliases->n_items++].ptr = alias;
  262. }
  263. ucimap_set_changed(&net->map, &net->aliases);
  264. ucimap_store_section(&network_map, pkg, &net->map);
  265. uci_save(ctx, pkg);
  266. }
  267. }
  268. ucimap_cleanup(&network_map);
  269. uci_free_context(ctx);
  270. return 0;
  271. }