ucimap-example.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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[4];
  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 = (unsigned char *) data->s;
  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. for (i = 0; i < 4; i++)
  47. target[i] = (char) tmp[i];
  48. return 0;
  49. }
  50. static int
  51. network_format_ip(void *sction, struct uci_optmap *om, union ucimap_data *data, char **str)
  52. {
  53. static char buf[16];
  54. unsigned char *ip = (unsigned char *) data->s;
  55. sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  56. *str = buf;
  57. return 0;
  58. }
  59. static int
  60. network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
  61. {
  62. struct uci_network *net = section;
  63. INIT_LIST_HEAD(&net->list);
  64. INIT_LIST_HEAD(&net->alias);
  65. net->name = s->e.name;
  66. net->test = -1;
  67. return 0;
  68. }
  69. static int
  70. network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
  71. {
  72. struct uci_alias *alias = section;
  73. INIT_LIST_HEAD(&alias->list);
  74. alias->name = s->e.name;
  75. return 0;
  76. }
  77. static int
  78. network_add_interface(struct uci_map *map, void *section)
  79. {
  80. struct uci_network *net = section;
  81. list_add_tail(&net->list, &ifs);
  82. return 0;
  83. }
  84. static int
  85. network_add_alias(struct uci_map *map, void *section)
  86. {
  87. struct uci_alias *a = section;
  88. if (a->interface)
  89. list_add_tail(&a->list, &a->interface->alias);
  90. return 0;
  91. }
  92. static struct ucimap_section_data *
  93. network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
  94. {
  95. struct uci_network *p = malloc(sizeof(struct uci_network));
  96. memset(p, 0, sizeof(struct uci_network));
  97. return &p->map;
  98. }
  99. struct my_optmap {
  100. struct uci_optmap map;
  101. int test;
  102. };
  103. static struct uci_sectionmap network_interface;
  104. static struct uci_sectionmap network_alias;
  105. static struct my_optmap network_interface_options[] = {
  106. {
  107. .map = {
  108. UCIMAP_OPTION(struct uci_network, proto),
  109. .type = UCIMAP_STRING,
  110. .name = "proto",
  111. .data.s.maxlen = 32,
  112. }
  113. },
  114. {
  115. .map = {
  116. UCIMAP_OPTION(struct uci_network, ifname),
  117. .type = UCIMAP_STRING,
  118. .name = "ifname"
  119. }
  120. },
  121. {
  122. .map = {
  123. UCIMAP_OPTION(struct uci_network, ipaddr),
  124. .type = UCIMAP_CUSTOM,
  125. .name = "ipaddr",
  126. .parse = network_parse_ip,
  127. .format = network_format_ip,
  128. }
  129. },
  130. {
  131. .map = {
  132. UCIMAP_OPTION(struct uci_network, enabled),
  133. .type = UCIMAP_BOOL,
  134. .name = "enabled",
  135. }
  136. },
  137. {
  138. .map = {
  139. UCIMAP_OPTION(struct uci_network, test),
  140. .type = UCIMAP_INT,
  141. .name = "test"
  142. }
  143. },
  144. {
  145. .map = {
  146. UCIMAP_OPTION(struct uci_network, aliases),
  147. .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
  148. .data.sm = &network_alias
  149. }
  150. }
  151. };
  152. static struct uci_sectionmap network_interface = {
  153. UCIMAP_SECTION(struct uci_network, map),
  154. .type = "interface",
  155. .alloc = network_allocate,
  156. .init = network_init_interface,
  157. .add = network_add_interface,
  158. .options = &network_interface_options[0].map,
  159. .n_options = ARRAY_SIZE(network_interface_options),
  160. .options_size = sizeof(struct my_optmap)
  161. };
  162. static struct uci_optmap network_alias_options[] = {
  163. {
  164. UCIMAP_OPTION(struct uci_alias, interface),
  165. .type = UCIMAP_SECTION,
  166. .data.sm = &network_interface
  167. }
  168. };
  169. static struct uci_sectionmap network_alias = {
  170. UCIMAP_SECTION(struct uci_alias, map),
  171. .type = "alias",
  172. .options = network_alias_options,
  173. .init = network_init_alias,
  174. .add = network_add_alias,
  175. .n_options = ARRAY_SIZE(network_alias_options),
  176. };
  177. static struct uci_sectionmap *network_smap[] = {
  178. &network_interface,
  179. &network_alias,
  180. };
  181. static struct uci_map network_map = {
  182. .sections = network_smap,
  183. .n_sections = ARRAY_SIZE(network_smap),
  184. };
  185. int main(int argc, char **argv)
  186. {
  187. struct uci_context *ctx;
  188. struct uci_package *pkg;
  189. struct list_head *p;
  190. struct uci_network *net;
  191. struct uci_alias *alias;
  192. int i;
  193. INIT_LIST_HEAD(&ifs);
  194. ctx = uci_alloc_context();
  195. ucimap_init(&network_map);
  196. uci_set_confdir(ctx, "./test/config");
  197. uci_load(ctx, "network", &pkg);
  198. ucimap_parse(&network_map, pkg);
  199. list_for_each(p, &ifs) {
  200. net = list_entry(p, struct uci_network, list);
  201. printf("New network section '%s'\n"
  202. " type: %s\n"
  203. " ifname: %s\n"
  204. " ipaddr: %d.%d.%d.%d\n"
  205. " test: %d\n"
  206. " enabled: %s\n",
  207. net->name,
  208. net->proto,
  209. net->ifname,
  210. net->ipaddr[0], net->ipaddr[1],
  211. net->ipaddr[2], net->ipaddr[3],
  212. net->test,
  213. (net->enabled ? "on" : "off"));
  214. for (i = 0; i < net->aliases->n_items; i++) {
  215. alias = net->aliases->item[i].ptr;
  216. printf("New alias: %s\n", alias->name);
  217. }
  218. #if 0
  219. memcpy(net->ipaddr, "\x01\x03\x04\x05", 4);
  220. ucimap_set_changed(&net->map, &net->ipaddr);
  221. ucimap_store_section(&network_map, pkg, &net->map);
  222. uci_save(ctx, pkg);
  223. #endif
  224. }
  225. ucimap_cleanup(&network_map);
  226. uci_free_context(ctx);
  227. return 0;
  228. }