ucimap-example.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. .name = "proto",
  127. .data.s.maxlen = 32,
  128. }
  129. },
  130. {
  131. .map = {
  132. UCIMAP_OPTION(struct uci_network, ifname),
  133. .type = UCIMAP_STRING,
  134. .name = "ifname"
  135. }
  136. },
  137. {
  138. .map = {
  139. UCIMAP_OPTION(struct uci_network, ipaddr),
  140. .type = UCIMAP_CUSTOM,
  141. .name = "ipaddr",
  142. .parse = network_parse_ip,
  143. .format = network_format_ip,
  144. .free = network_free_ip,
  145. }
  146. },
  147. {
  148. .map = {
  149. UCIMAP_OPTION(struct uci_network, enabled),
  150. .type = UCIMAP_BOOL,
  151. .name = "enabled",
  152. }
  153. },
  154. {
  155. .map = {
  156. UCIMAP_OPTION(struct uci_network, test),
  157. .type = UCIMAP_INT,
  158. .name = "test"
  159. }
  160. },
  161. {
  162. .map = {
  163. UCIMAP_OPTION(struct uci_network, aliases),
  164. .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
  165. .data.sm = &network_alias
  166. }
  167. }
  168. };
  169. static struct uci_sectionmap network_interface = {
  170. UCIMAP_SECTION(struct uci_network, map),
  171. .type = "interface",
  172. .alloc = network_allocate,
  173. .init = network_init_interface,
  174. .add = network_add_interface,
  175. .options = &network_interface_options[0].map,
  176. .n_options = ARRAY_SIZE(network_interface_options),
  177. .options_size = sizeof(struct my_optmap)
  178. };
  179. static struct uci_optmap network_alias_options[] = {
  180. {
  181. UCIMAP_OPTION(struct uci_alias, interface),
  182. .type = UCIMAP_SECTION,
  183. .data.sm = &network_interface
  184. }
  185. };
  186. static struct uci_sectionmap network_alias = {
  187. UCIMAP_SECTION(struct uci_alias, map),
  188. .type = "alias",
  189. .options = network_alias_options,
  190. .init = network_init_alias,
  191. .add = network_add_alias,
  192. .n_options = ARRAY_SIZE(network_alias_options),
  193. };
  194. static struct uci_sectionmap *network_smap[] = {
  195. &network_interface,
  196. &network_alias,
  197. };
  198. static struct uci_map network_map = {
  199. .sections = network_smap,
  200. .n_sections = ARRAY_SIZE(network_smap),
  201. };
  202. int main(int argc, char **argv)
  203. {
  204. struct uci_context *ctx;
  205. struct uci_package *pkg;
  206. struct list_head *p;
  207. struct uci_network *net;
  208. struct uci_alias *alias;
  209. bool set = false;
  210. int i;
  211. INIT_LIST_HEAD(&ifs);
  212. ctx = uci_alloc_context();
  213. if (!ctx)
  214. return -1;
  215. ucimap_init(&network_map);
  216. if ((argc >= 2) && !strcmp(argv[1], "-s")) {
  217. uci_set_savedir(ctx, "./test/save");
  218. set = true;
  219. }
  220. uci_set_confdir(ctx, "./test/config");
  221. uci_load(ctx, "network", &pkg);
  222. ucimap_parse(&network_map, pkg);
  223. list_for_each(p, &ifs) {
  224. const unsigned char *ipaddr;
  225. int n_aliases = 0;
  226. net = list_entry(p, struct uci_network, list);
  227. ipaddr = net->ipaddr;
  228. if (!ipaddr)
  229. ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
  230. printf("New network section '%s'\n"
  231. " type: %s\n"
  232. " ifname: %s\n"
  233. " ipaddr: %d.%d.%d.%d\n"
  234. " test: %d\n"
  235. " enabled: %s\n",
  236. net->name,
  237. net->proto,
  238. net->ifname,
  239. ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
  240. net->test,
  241. (net->enabled ? "on" : "off"));
  242. if (net->aliases->n_items > 0) {
  243. printf("Configured aliases:");
  244. for (i = 0; i < net->aliases->n_items; i++) {
  245. alias = net->aliases->item[i].ptr;
  246. printf(" %s", alias->name);
  247. }
  248. printf("\n");
  249. }
  250. list_for_each_entry(alias, &net->alias, list) {
  251. n_aliases++;
  252. for (i = 0; i < net->aliases->n_items; i++) {
  253. if (alias == net->aliases->item[i].ptr)
  254. goto next_alias;
  255. }
  256. printf("New alias: %s\n", alias->name);
  257. next_alias:
  258. continue;
  259. }
  260. if (set && !strcmp(net->name, "lan")) {
  261. ucimap_free_item(&net->map, &net->ipaddr);
  262. ucimap_set_changed(&net->map, &net->ipaddr);
  263. ucimap_resize_list(&net->map, &net->aliases, n_aliases);
  264. net->aliases->n_items = 0;
  265. list_for_each_entry(alias, &net->alias, list) {
  266. net->aliases->item[net->aliases->n_items++].ptr = alias;
  267. }
  268. ucimap_set_changed(&net->map, &net->aliases);
  269. ucimap_store_section(&network_map, pkg, &net->map);
  270. uci_save(ctx, pkg);
  271. }
  272. }
  273. ucimap_cleanup(&network_map);
  274. uci_free_context(ctx);
  275. return 0;
  276. }