net_dns.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @file net_dns.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * DNS servers module.
  32. *
  33. * Synopsis: net.dns(list(string) servers, string priority)
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <misc/offset.h>
  38. #include <misc/bsort.h>
  39. #include <misc/balloc.h>
  40. #include <structure/LinkedList2.h>
  41. #include <ncd/NCDModule.h>
  42. #include <ncd/NCDIfConfig.h>
  43. #include <generated/blog_channel_ncd_net_dns.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct instance {
  46. NCDModuleInst *i;
  47. LinkedList2 ipv4_dns_servers;
  48. LinkedList2Node instances_node; // node in instances
  49. };
  50. struct ipv4_dns_entry {
  51. LinkedList2Node list_node; // node in instance.ipv4_dns_servers
  52. uint32_t addr;
  53. int priority;
  54. };
  55. static LinkedList2 instances;
  56. static struct ipv4_dns_entry * add_ipv4_dns_entry (struct instance *o, uint32_t addr, int priority)
  57. {
  58. // allocate entry
  59. struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
  60. if (!entry) {
  61. return NULL;
  62. }
  63. // set info
  64. entry->addr = addr;
  65. entry->priority = priority;
  66. // add to list
  67. LinkedList2_Append(&o->ipv4_dns_servers, &entry->list_node);
  68. return entry;
  69. }
  70. static void remove_ipv4_dns_entry (struct instance *o, struct ipv4_dns_entry *entry)
  71. {
  72. // remove from list
  73. LinkedList2_Remove(&o->ipv4_dns_servers, &entry->list_node);
  74. // free entry
  75. free(entry);
  76. }
  77. static void remove_ipv4_dns_entries (struct instance *o)
  78. {
  79. LinkedList2Node *n;
  80. while (n = LinkedList2_GetFirst(&o->ipv4_dns_servers)) {
  81. struct ipv4_dns_entry *e = UPPER_OBJECT(n, struct ipv4_dns_entry, list_node);
  82. remove_ipv4_dns_entry(o, e);
  83. }
  84. }
  85. static size_t num_servers (void)
  86. {
  87. size_t c = 0;
  88. LinkedList2Iterator it;
  89. LinkedList2Iterator_InitForward(&it, &instances);
  90. LinkedList2Node *n;
  91. while (n = LinkedList2Iterator_Next(&it)) {
  92. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  93. LinkedList2Iterator eit;
  94. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  95. while (LinkedList2Iterator_Next(&eit)) {
  96. c++;
  97. }
  98. }
  99. return c;
  100. }
  101. struct dns_sort_entry {
  102. uint32_t addr;
  103. int priority;
  104. };
  105. static int dns_sort_comparator (const void *v1, const void *v2)
  106. {
  107. const struct dns_sort_entry *e1 = v1;
  108. const struct dns_sort_entry *e2 = v2;
  109. if (e1->priority < e2->priority) {
  110. return -1;
  111. }
  112. if (e1->priority > e2->priority) {
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. static int set_servers (void)
  118. {
  119. int ret = 0;
  120. // count servers
  121. size_t num_ipv4_dns_servers = num_servers();
  122. // allocate sort array
  123. struct dns_sort_entry *servers = BAllocArray(num_ipv4_dns_servers, sizeof(servers[0]));
  124. if (!servers) {
  125. goto fail0;
  126. }
  127. size_t num_servers = 0;
  128. // fill sort array
  129. LinkedList2Iterator it;
  130. LinkedList2Iterator_InitForward(&it, &instances);
  131. LinkedList2Node *n;
  132. while (n = LinkedList2Iterator_Next(&it)) {
  133. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  134. LinkedList2Iterator eit;
  135. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  136. LinkedList2Node *en;
  137. while (en = LinkedList2Iterator_Next(&eit)) {
  138. struct ipv4_dns_entry *e = UPPER_OBJECT(en, struct ipv4_dns_entry, list_node);
  139. servers[num_servers].addr = e->addr;
  140. servers[num_servers].priority= e->priority;
  141. num_servers++;
  142. }
  143. }
  144. ASSERT(num_servers == num_ipv4_dns_servers)
  145. // sort by priority
  146. // use a custom insertion sort instead of qsort() because we want a stable sort
  147. struct dns_sort_entry sort_temp;
  148. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator, &sort_temp);
  149. // copy addresses into an array
  150. uint32_t *addrs = BAllocArray(num_servers, sizeof(addrs[0]));
  151. if (!addrs) {
  152. goto fail1;
  153. }
  154. for (size_t i = 0; i < num_servers; i++) {
  155. addrs[i] = servers[i].addr;
  156. }
  157. // set servers
  158. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  159. goto fail2;
  160. }
  161. ret = 1;
  162. fail2:
  163. BFree(addrs);
  164. fail1:
  165. BFree(servers);
  166. fail0:
  167. return ret;
  168. }
  169. static int func_globalinit (struct NCDModuleInitParams params)
  170. {
  171. LinkedList2_Init(&instances);
  172. return 1;
  173. }
  174. static void func_new (NCDModuleInst *i)
  175. {
  176. // allocate instance
  177. struct instance *o = malloc(sizeof(*o));
  178. if (!o) {
  179. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  180. goto fail0;
  181. }
  182. NCDModuleInst_Backend_SetUser(i, o);
  183. // init arguments
  184. o->i = i;
  185. // init servers list
  186. LinkedList2_Init(&o->ipv4_dns_servers);
  187. // get arguments
  188. NCDValue *servers_arg;
  189. NCDValue *priority_arg;
  190. if (!NCDValue_ListRead(o->i->args, 2, &servers_arg, &priority_arg)) {
  191. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  192. goto fail1;
  193. }
  194. if (NCDValue_Type(servers_arg) != NCDVALUE_LIST || NCDValue_Type(priority_arg) != NCDVALUE_STRING) {
  195. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  196. goto fail1;
  197. }
  198. int priority = atoi(NCDValue_StringValue(priority_arg));
  199. // read servers
  200. NCDValue *server_arg = NCDValue_ListFirst(servers_arg);
  201. while (server_arg) {
  202. if (NCDValue_Type(server_arg) != NCDVALUE_STRING) {
  203. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  204. goto fail1;
  205. }
  206. uint32_t addr;
  207. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(server_arg), &addr)) {
  208. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  209. goto fail1;
  210. }
  211. if (!add_ipv4_dns_entry(o, addr, priority)) {
  212. ModuleLog(o->i, BLOG_ERROR, "failed to add dns entry");
  213. goto fail1;
  214. }
  215. server_arg = NCDValue_ListNext(servers_arg, server_arg);
  216. }
  217. // add to instances
  218. LinkedList2_Append(&instances, &o->instances_node);
  219. // set servers
  220. if (!set_servers()) {
  221. ModuleLog(o->i, BLOG_ERROR, "failed to set DNS servers");
  222. goto fail2;
  223. }
  224. // signal up
  225. NCDModuleInst_Backend_Up(o->i);
  226. return;
  227. fail2:
  228. LinkedList2_Remove(&instances, &o->instances_node);
  229. fail1:
  230. remove_ipv4_dns_entries(o);
  231. free(o);
  232. fail0:
  233. NCDModuleInst_Backend_SetError(i);
  234. NCDModuleInst_Backend_Dead(i);
  235. }
  236. static void func_die (void *vo)
  237. {
  238. struct instance *o = vo;
  239. NCDModuleInst *i = o->i;
  240. // remove from instances
  241. LinkedList2_Remove(&instances, &o->instances_node);
  242. // set servers
  243. set_servers();
  244. // free servers
  245. remove_ipv4_dns_entries(o);
  246. // free instance
  247. free(o);
  248. NCDModuleInst_Backend_Dead(i);
  249. }
  250. static const struct NCDModule modules[] = {
  251. {
  252. .type = "net.dns",
  253. .func_new = func_new,
  254. .func_die = func_die
  255. }, {
  256. .type = NULL
  257. }
  258. };
  259. const struct NCDModuleGroup ncdmodule_net_dns = {
  260. .func_globalinit = func_globalinit,
  261. .modules = modules
  262. };