ucimap-example.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 list_head list;
  22. struct list_head alias;
  23. const char *name;
  24. const char *proto;
  25. const char *ifname;
  26. unsigned char ipaddr[4];
  27. int test;
  28. bool enabled;
  29. struct ucimap_list *aliases;
  30. };
  31. struct uci_alias {
  32. struct list_head list;
  33. const char *name;
  34. struct uci_network *interface;
  35. };
  36. static int
  37. network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
  38. {
  39. struct uci_network *net = section;
  40. unsigned char *target = data->s;
  41. unsigned int tmp[4];
  42. int i;
  43. if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
  44. return -1;
  45. for (i = 0; i < 4; i++)
  46. target[i] = (char) tmp[i];
  47. return 0;
  48. }
  49. static int
  50. network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
  51. {
  52. struct uci_network *net = section;
  53. INIT_LIST_HEAD(&net->list);
  54. INIT_LIST_HEAD(&net->alias);
  55. net->name = s->e.name;
  56. net->test = -1;
  57. return 0;
  58. }
  59. static int
  60. network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
  61. {
  62. struct uci_alias *alias = section;
  63. INIT_LIST_HEAD(&alias->list);
  64. alias->name = s->e.name;
  65. return 0;
  66. }
  67. static int
  68. network_add_interface(struct uci_map *map, void *section)
  69. {
  70. struct uci_network *net = section;
  71. list_add(&net->list, &ifs);
  72. return 0;
  73. }
  74. static int
  75. network_add_alias(struct uci_map *map, void *section)
  76. {
  77. struct uci_alias *a = section;
  78. if (a->interface)
  79. list_add(&a->list, &a->interface->alias);
  80. return 0;
  81. }
  82. struct my_optmap {
  83. struct uci_optmap map;
  84. int test;
  85. };
  86. static struct uci_sectionmap network_interface;
  87. static struct uci_sectionmap network_alias;
  88. static struct my_optmap network_interface_options[] = {
  89. {
  90. .map = {
  91. UCIMAP_OPTION(struct uci_network, proto),
  92. .type = UCIMAP_STRING,
  93. .name = "proto",
  94. .data.s.maxlen = 32,
  95. }
  96. },
  97. {
  98. .map = {
  99. UCIMAP_OPTION(struct uci_network, ifname),
  100. .type = UCIMAP_STRING,
  101. .name = "ifname"
  102. }
  103. },
  104. {
  105. .map = {
  106. UCIMAP_OPTION(struct uci_network, ipaddr),
  107. .type = UCIMAP_CUSTOM,
  108. .name = "ipaddr",
  109. .parse = network_parse_ip,
  110. }
  111. },
  112. {
  113. .map = {
  114. UCIMAP_OPTION(struct uci_network, enabled),
  115. .type = UCIMAP_BOOL,
  116. .name = "enabled",
  117. }
  118. },
  119. {
  120. .map = {
  121. UCIMAP_OPTION(struct uci_network, test),
  122. .type = UCIMAP_INT,
  123. .name = "test"
  124. }
  125. },
  126. {
  127. .map = {
  128. UCIMAP_OPTION(struct uci_network, aliases),
  129. .type = UCIMAP_LIST | UCIMAP_SECTION,
  130. .data.sm = &network_alias
  131. }
  132. }
  133. };
  134. static struct uci_sectionmap network_interface = {
  135. .type = "interface",
  136. .alloc_len = sizeof(struct uci_network),
  137. .init = network_init_interface,
  138. .add = network_add_interface,
  139. .options = &network_interface_options[0].map,
  140. .n_options = ARRAY_SIZE(network_interface_options),
  141. .options_size = sizeof(struct my_optmap)
  142. };
  143. static struct uci_optmap network_alias_options[] = {
  144. {
  145. UCIMAP_OPTION(struct uci_alias, interface),
  146. .type = UCIMAP_SECTION,
  147. .data.sm = &network_interface
  148. }
  149. };
  150. static struct uci_sectionmap network_alias = {
  151. .type = "alias",
  152. .options = network_alias_options,
  153. .alloc_len = sizeof(struct uci_network),
  154. .init = network_init_alias,
  155. .add = network_add_alias,
  156. .n_options = ARRAY_SIZE(network_alias_options),
  157. };
  158. static struct uci_sectionmap *network_smap[] = {
  159. &network_interface,
  160. &network_alias,
  161. };
  162. static struct uci_map network_map = {
  163. .sections = network_smap,
  164. .n_sections = ARRAY_SIZE(network_smap),
  165. };
  166. int main(int argc, char **argv)
  167. {
  168. struct uci_context *ctx;
  169. struct uci_package *pkg;
  170. struct list_head *p, *p2;
  171. struct uci_network *net;
  172. struct uci_alias *alias;
  173. int i;
  174. INIT_LIST_HEAD(&ifs);
  175. ctx = uci_alloc_context();
  176. ucimap_init(&network_map);
  177. uci_load(ctx, "network", &pkg);
  178. ucimap_parse(&network_map, pkg);
  179. list_for_each(p, &ifs) {
  180. net = list_entry(p, struct uci_network, list);
  181. printf("New network section '%s'\n"
  182. " type: %s\n"
  183. " ifname: %s\n"
  184. " ipaddr: %d.%d.%d.%d\n"
  185. " test: %d\n"
  186. " enabled: %s\n",
  187. net->name,
  188. net->proto,
  189. net->ifname,
  190. net->ipaddr[0], net->ipaddr[1],
  191. net->ipaddr[2], net->ipaddr[3],
  192. net->test,
  193. (net->enabled ? "on" : "off"));
  194. for (i = 0; i < net->aliases->n_items; i++) {
  195. alias = net->aliases->item[i].section;
  196. printf("New alias: %s\n", alias->name);
  197. }
  198. #if 0
  199. net->ipaddr = "2.3.4.5";
  200. ucimap_set_changed(net, &net->ipaddr);
  201. ucimap_store_section(&network_map, pkg, net);
  202. uci_save(ctx, pkg);
  203. #endif
  204. }
  205. done:
  206. ucimap_cleanup(&network_map);
  207. uci_free_context(ctx);
  208. return 0;
  209. }