net_dns.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/LinkedList2.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. LinkedList2 ipv4_dns_servers;
  49. LinkedList2Node instances_node; // node in instances
  50. };
  51. struct ipv4_dns_entry {
  52. LinkedList2Node list_node; // node in instance.ipv4_dns_servers
  53. uint32_t addr;
  54. int priority;
  55. };
  56. static LinkedList2 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. LinkedList2_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. LinkedList2_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. LinkedList2Node *n;
  81. while (n = LinkedList2_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. LinkedList2Iterator it;
  90. LinkedList2Iterator_InitForward(&it, &instances);
  91. LinkedList2Node *n;
  92. while (n = LinkedList2Iterator_Next(&it)) {
  93. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  94. LinkedList2Iterator eit;
  95. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  96. while (LinkedList2Iterator_Next(&eit)) {
  97. c++;
  98. }
  99. }
  100. return c;
  101. }
  102. struct dns_sort_entry {
  103. uint32_t addr;
  104. int priority;
  105. };
  106. static int dns_sort_comparator (const void *v1, const void *v2)
  107. {
  108. const struct dns_sort_entry *e1 = v1;
  109. const struct dns_sort_entry *e2 = v2;
  110. return B_COMPARE(e1->priority, e2->priority);
  111. }
  112. static int set_servers (void)
  113. {
  114. int ret = 0;
  115. // count servers
  116. size_t num_ipv4_dns_servers = num_servers();
  117. // allocate sort array
  118. struct dns_sort_entry *servers = BAllocArray(num_ipv4_dns_servers, sizeof(servers[0]));
  119. if (!servers) {
  120. goto fail0;
  121. }
  122. size_t num_servers = 0;
  123. // fill sort array
  124. LinkedList2Iterator it;
  125. LinkedList2Iterator_InitForward(&it, &instances);
  126. LinkedList2Node *n;
  127. while (n = LinkedList2Iterator_Next(&it)) {
  128. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  129. LinkedList2Iterator eit;
  130. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  131. LinkedList2Node *en;
  132. while (en = LinkedList2Iterator_Next(&eit)) {
  133. struct ipv4_dns_entry *e = UPPER_OBJECT(en, struct ipv4_dns_entry, list_node);
  134. servers[num_servers].addr = e->addr;
  135. servers[num_servers].priority= e->priority;
  136. num_servers++;
  137. }
  138. }
  139. ASSERT(num_servers == num_ipv4_dns_servers)
  140. // sort by priority
  141. // use a custom insertion sort instead of qsort() because we want a stable sort
  142. struct dns_sort_entry sort_temp;
  143. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator, &sort_temp);
  144. // copy addresses into an array
  145. uint32_t *addrs = BAllocArray(num_servers, sizeof(addrs[0]));
  146. if (!addrs) {
  147. goto fail1;
  148. }
  149. for (size_t i = 0; i < num_servers; i++) {
  150. addrs[i] = servers[i].addr;
  151. }
  152. // set servers
  153. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  154. goto fail2;
  155. }
  156. ret = 1;
  157. fail2:
  158. BFree(addrs);
  159. fail1:
  160. BFree(servers);
  161. fail0:
  162. return ret;
  163. }
  164. static int func_globalinit (struct NCDModuleInitParams params)
  165. {
  166. LinkedList2_Init(&instances);
  167. return 1;
  168. }
  169. static void func_new (NCDModuleInst *i)
  170. {
  171. // allocate instance
  172. struct instance *o = malloc(sizeof(*o));
  173. if (!o) {
  174. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  175. goto fail0;
  176. }
  177. o->i = i;
  178. NCDModuleInst_Backend_SetUser(i, o);
  179. // init servers list
  180. LinkedList2_Init(&o->ipv4_dns_servers);
  181. // get arguments
  182. NCDValRef servers_arg;
  183. NCDValRef priority_arg;
  184. if (!NCDVal_ListRead(o->i->args, 2, &servers_arg, &priority_arg)) {
  185. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  186. goto fail1;
  187. }
  188. if (!NCDVal_IsList(servers_arg) || !NCDVal_IsStringNoNulls(priority_arg)) {
  189. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  190. goto fail1;
  191. }
  192. int priority = atoi(NCDVal_StringValue(priority_arg));
  193. // read servers
  194. size_t count = NCDVal_ListCount(servers_arg);
  195. for (size_t j = 0; j < count; j++) {
  196. NCDValRef server_arg = NCDVal_ListGet(servers_arg, j);
  197. if (!NCDVal_IsStringNoNulls(server_arg)) {
  198. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  199. goto fail1;
  200. }
  201. uint32_t addr;
  202. if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(server_arg), &addr)) {
  203. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  204. goto fail1;
  205. }
  206. if (!add_ipv4_dns_entry(o, addr, priority)) {
  207. ModuleLog(o->i, BLOG_ERROR, "failed to add dns entry");
  208. goto fail1;
  209. }
  210. }
  211. // add to instances
  212. LinkedList2_Append(&instances, &o->instances_node);
  213. // set servers
  214. if (!set_servers()) {
  215. ModuleLog(o->i, BLOG_ERROR, "failed to set DNS servers");
  216. goto fail2;
  217. }
  218. // signal up
  219. NCDModuleInst_Backend_Up(o->i);
  220. return;
  221. fail2:
  222. LinkedList2_Remove(&instances, &o->instances_node);
  223. fail1:
  224. remove_ipv4_dns_entries(o);
  225. free(o);
  226. fail0:
  227. NCDModuleInst_Backend_SetError(i);
  228. NCDModuleInst_Backend_Dead(i);
  229. }
  230. static void func_die (void *vo)
  231. {
  232. struct instance *o = vo;
  233. NCDModuleInst *i = o->i;
  234. // remove from instances
  235. LinkedList2_Remove(&instances, &o->instances_node);
  236. // set servers
  237. set_servers();
  238. // free servers
  239. remove_ipv4_dns_entries(o);
  240. // free instance
  241. free(o);
  242. NCDModuleInst_Backend_Dead(i);
  243. }
  244. static const struct NCDModule modules[] = {
  245. {
  246. .type = "net.dns",
  247. .func_new = func_new,
  248. .func_die = func_die
  249. }, {
  250. .type = NULL
  251. }
  252. };
  253. const struct NCDModuleGroup ncdmodule_net_dns = {
  254. .func_globalinit = func_globalinit,
  255. .modules = modules
  256. };