net_dns.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <misc/compare.h>
  41. #include <structure/LinkedList1.h>
  42. #include <ncd/NCDModule.h>
  43. #include <ncd/NCDIfConfig.h>
  44. #include <generated/blog_channel_ncd_net_dns.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. struct instance {
  47. NCDModuleInst *i;
  48. LinkedList1 ipv4_dns_servers;
  49. LinkedList1Node instances_node; // node in instances
  50. };
  51. struct ipv4_dns_entry {
  52. LinkedList1Node list_node; // node in instance.ipv4_dns_servers
  53. uint32_t addr;
  54. int priority;
  55. };
  56. static LinkedList1 instances;
  57. static struct ipv4_dns_entry * add_ipv4_dns_entry (struct instance *o, uint32_t addr, int priority)
  58. {
  59. // allocate entry
  60. struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
  61. if (!entry) {
  62. return NULL;
  63. }
  64. // set info
  65. entry->addr = addr;
  66. entry->priority = priority;
  67. // add to list
  68. LinkedList1_Append(&o->ipv4_dns_servers, &entry->list_node);
  69. return entry;
  70. }
  71. static void remove_ipv4_dns_entry (struct instance *o, struct ipv4_dns_entry *entry)
  72. {
  73. // remove from list
  74. LinkedList1_Remove(&o->ipv4_dns_servers, &entry->list_node);
  75. // free entry
  76. free(entry);
  77. }
  78. static void remove_ipv4_dns_entries (struct instance *o)
  79. {
  80. LinkedList1Node *n;
  81. while (n = LinkedList1_GetFirst(&o->ipv4_dns_servers)) {
  82. struct ipv4_dns_entry *e = UPPER_OBJECT(n, struct ipv4_dns_entry, list_node);
  83. remove_ipv4_dns_entry(o, e);
  84. }
  85. }
  86. static size_t num_servers (void)
  87. {
  88. size_t c = 0;
  89. for (LinkedList1Node *n = LinkedList1_GetFirst(&instances); n; n = LinkedList1Node_Next(n)) {
  90. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  91. for (LinkedList1Node *en = LinkedList1_GetFirst(&o->ipv4_dns_servers); en; en = LinkedList1Node_Next(en)) {
  92. c++;
  93. }
  94. }
  95. return c;
  96. }
  97. struct dns_sort_entry {
  98. uint32_t addr;
  99. int priority;
  100. };
  101. static int dns_sort_comparator (const void *v1, const void *v2)
  102. {
  103. const struct dns_sort_entry *e1 = v1;
  104. const struct dns_sort_entry *e2 = v2;
  105. return B_COMPARE(e1->priority, e2->priority);
  106. }
  107. static int set_servers (void)
  108. {
  109. int ret = 0;
  110. // count servers
  111. size_t num_ipv4_dns_servers = num_servers();
  112. // allocate sort array
  113. struct dns_sort_entry *servers = BAllocArray(num_ipv4_dns_servers, sizeof(servers[0]));
  114. if (!servers) {
  115. goto fail0;
  116. }
  117. size_t num_servers = 0;
  118. // fill sort array
  119. for (LinkedList1Node *n = LinkedList1_GetFirst(&instances); n; n = LinkedList1Node_Next(n)) {
  120. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  121. for (LinkedList1Node *en = LinkedList1_GetFirst(&o->ipv4_dns_servers); en; en = LinkedList1Node_Next(en)) {
  122. struct ipv4_dns_entry *e = UPPER_OBJECT(en, struct ipv4_dns_entry, list_node);
  123. servers[num_servers].addr = e->addr;
  124. servers[num_servers].priority= e->priority;
  125. num_servers++;
  126. }
  127. }
  128. ASSERT(num_servers == num_ipv4_dns_servers)
  129. // sort by priority
  130. // use a custom insertion sort instead of qsort() because we want a stable sort
  131. struct dns_sort_entry sort_temp;
  132. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator, &sort_temp);
  133. // copy addresses into an array
  134. uint32_t *addrs = BAllocArray(num_servers, sizeof(addrs[0]));
  135. if (!addrs) {
  136. goto fail1;
  137. }
  138. for (size_t i = 0; i < num_servers; i++) {
  139. addrs[i] = servers[i].addr;
  140. }
  141. // set servers
  142. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  143. goto fail2;
  144. }
  145. ret = 1;
  146. fail2:
  147. BFree(addrs);
  148. fail1:
  149. BFree(servers);
  150. fail0:
  151. return ret;
  152. }
  153. static int func_globalinit (struct NCDModuleInitParams params)
  154. {
  155. LinkedList1_Init(&instances);
  156. return 1;
  157. }
  158. static void func_new (void *vo, NCDModuleInst *i)
  159. {
  160. struct instance *o = vo;
  161. o->i = i;
  162. // init servers list
  163. LinkedList1_Init(&o->ipv4_dns_servers);
  164. // get arguments
  165. NCDValRef servers_arg;
  166. NCDValRef priority_arg;
  167. if (!NCDVal_ListRead(o->i->args, 2, &servers_arg, &priority_arg)) {
  168. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  169. goto fail1;
  170. }
  171. if (!NCDVal_IsList(servers_arg) || !NCDVal_IsStringNoNulls(priority_arg)) {
  172. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  173. goto fail1;
  174. }
  175. int priority = atoi(NCDVal_StringValue(priority_arg));
  176. // read servers
  177. size_t count = NCDVal_ListCount(servers_arg);
  178. for (size_t j = 0; j < count; j++) {
  179. NCDValRef server_arg = NCDVal_ListGet(servers_arg, j);
  180. if (!NCDVal_IsStringNoNulls(server_arg)) {
  181. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  182. goto fail1;
  183. }
  184. uint32_t addr;
  185. if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(server_arg), &addr)) {
  186. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  187. goto fail1;
  188. }
  189. if (!add_ipv4_dns_entry(o, addr, priority)) {
  190. ModuleLog(o->i, BLOG_ERROR, "failed to add dns entry");
  191. goto fail1;
  192. }
  193. }
  194. // add to instances
  195. LinkedList1_Append(&instances, &o->instances_node);
  196. // set servers
  197. if (!set_servers()) {
  198. ModuleLog(o->i, BLOG_ERROR, "failed to set DNS servers");
  199. goto fail2;
  200. }
  201. // signal up
  202. NCDModuleInst_Backend_Up(o->i);
  203. return;
  204. fail2:
  205. LinkedList1_Remove(&instances, &o->instances_node);
  206. fail1:
  207. remove_ipv4_dns_entries(o);
  208. NCDModuleInst_Backend_SetError(i);
  209. NCDModuleInst_Backend_Dead(i);
  210. }
  211. static void func_die (void *vo)
  212. {
  213. struct instance *o = vo;
  214. // remove from instances
  215. LinkedList1_Remove(&instances, &o->instances_node);
  216. // set servers
  217. set_servers();
  218. // free servers
  219. remove_ipv4_dns_entries(o);
  220. NCDModuleInst_Backend_Dead(o->i);
  221. }
  222. static const struct NCDModule modules[] = {
  223. {
  224. .type = "net.dns",
  225. .func_new2 = func_new,
  226. .func_die = func_die,
  227. .alloc_size = sizeof(struct instance)
  228. }, {
  229. .type = NULL
  230. }
  231. };
  232. const struct NCDModuleGroup ncdmodule_net_dns = {
  233. .func_globalinit = func_globalinit,
  234. .modules = modules
  235. };