net_dns.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @file net_dns.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * DNS servers module.
  25. *
  26. * Synopsis: net.dns(list(string) servers, string priority)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <misc/offset.h>
  31. #include <misc/bsort.h>
  32. #include <misc/balloc.h>
  33. #include <structure/LinkedList2.h>
  34. #include <ncd/NCDModule.h>
  35. #include <ncd/NCDIfConfig.h>
  36. #include <generated/blog_channel_ncd_net_dns.h>
  37. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  38. struct instance {
  39. NCDModuleInst *i;
  40. LinkedList2 ipv4_dns_servers;
  41. LinkedList2Node instances_node; // node in instances
  42. };
  43. struct ipv4_dns_entry {
  44. LinkedList2Node list_node; // node in instance.ipv4_dns_servers
  45. uint32_t addr;
  46. int priority;
  47. };
  48. static LinkedList2 instances;
  49. static struct ipv4_dns_entry * add_ipv4_dns_entry (struct instance *o, uint32_t addr, int priority)
  50. {
  51. // allocate entry
  52. struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
  53. if (!entry) {
  54. return NULL;
  55. }
  56. // set info
  57. entry->addr = addr;
  58. entry->priority = priority;
  59. // add to list
  60. LinkedList2_Append(&o->ipv4_dns_servers, &entry->list_node);
  61. return entry;
  62. }
  63. static void remove_ipv4_dns_entry (struct instance *o, struct ipv4_dns_entry *entry)
  64. {
  65. // remove from list
  66. LinkedList2_Remove(&o->ipv4_dns_servers, &entry->list_node);
  67. // free entry
  68. free(entry);
  69. }
  70. static void remove_ipv4_dns_entries (struct instance *o)
  71. {
  72. LinkedList2Node *n;
  73. while (n = LinkedList2_GetFirst(&o->ipv4_dns_servers)) {
  74. struct ipv4_dns_entry *e = UPPER_OBJECT(n, struct ipv4_dns_entry, list_node);
  75. remove_ipv4_dns_entry(o, e);
  76. }
  77. }
  78. static size_t num_servers (void)
  79. {
  80. size_t c = 0;
  81. LinkedList2Iterator it;
  82. LinkedList2Iterator_InitForward(&it, &instances);
  83. LinkedList2Node *n;
  84. while (n = LinkedList2Iterator_Next(&it)) {
  85. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  86. LinkedList2Iterator eit;
  87. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  88. while (LinkedList2Iterator_Next(&eit)) {
  89. c++;
  90. }
  91. }
  92. return c;
  93. }
  94. struct dns_sort_entry {
  95. uint32_t addr;
  96. int priority;
  97. };
  98. static int dns_sort_comparator (const void *v1, const void *v2)
  99. {
  100. const struct dns_sort_entry *e1 = v1;
  101. const struct dns_sort_entry *e2 = v2;
  102. if (e1->priority < e2->priority) {
  103. return -1;
  104. }
  105. if (e1->priority > e2->priority) {
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static int set_servers (void)
  111. {
  112. int ret = 0;
  113. // count servers
  114. size_t num_ipv4_dns_servers = num_servers();
  115. // allocate sort array
  116. struct dns_sort_entry *servers = BAllocArray(num_ipv4_dns_servers, sizeof(servers[0]));
  117. if (!servers) {
  118. goto fail0;
  119. }
  120. size_t num_servers = 0;
  121. // fill sort array
  122. LinkedList2Iterator it;
  123. LinkedList2Iterator_InitForward(&it, &instances);
  124. LinkedList2Node *n;
  125. while (n = LinkedList2Iterator_Next(&it)) {
  126. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  127. LinkedList2Iterator eit;
  128. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  129. LinkedList2Node *en;
  130. while (en = LinkedList2Iterator_Next(&eit)) {
  131. struct ipv4_dns_entry *e = UPPER_OBJECT(en, struct ipv4_dns_entry, list_node);
  132. servers[num_servers].addr = e->addr;
  133. servers[num_servers].priority= e->priority;
  134. num_servers++;
  135. }
  136. }
  137. ASSERT(num_servers == num_ipv4_dns_servers)
  138. // sort by priority
  139. // use a custom insertion sort instead of qsort() because we want a stable sort
  140. struct dns_sort_entry sort_temp;
  141. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator, &sort_temp);
  142. // copy addresses into an array
  143. uint32_t *addrs = BAllocArray(num_servers, sizeof(addrs[0]));
  144. if (!addrs) {
  145. goto fail1;
  146. }
  147. for (size_t i = 0; i < num_servers; i++) {
  148. addrs[i] = servers[i].addr;
  149. }
  150. // set servers
  151. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  152. goto fail2;
  153. }
  154. ret = 1;
  155. fail2:
  156. BFree(addrs);
  157. fail1:
  158. BFree(servers);
  159. fail0:
  160. return ret;
  161. }
  162. static int func_globalinit (struct NCDModuleInitParams params)
  163. {
  164. LinkedList2_Init(&instances);
  165. return 1;
  166. }
  167. static void func_new (NCDModuleInst *i)
  168. {
  169. // allocate instance
  170. struct instance *o = malloc(sizeof(*o));
  171. if (!o) {
  172. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  173. goto fail0;
  174. }
  175. NCDModuleInst_Backend_SetUser(i, o);
  176. // init arguments
  177. o->i = i;
  178. // init servers list
  179. LinkedList2_Init(&o->ipv4_dns_servers);
  180. // get arguments
  181. NCDValue *servers_arg;
  182. NCDValue *priority_arg;
  183. if (!NCDValue_ListRead(o->i->args, 2, &servers_arg, &priority_arg)) {
  184. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  185. goto fail1;
  186. }
  187. if (NCDValue_Type(servers_arg) != NCDVALUE_LIST || NCDValue_Type(priority_arg) != NCDVALUE_STRING) {
  188. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  189. goto fail1;
  190. }
  191. int priority = atoi(NCDValue_StringValue(priority_arg));
  192. // read servers
  193. NCDValue *server_arg = NCDValue_ListFirst(servers_arg);
  194. while (server_arg) {
  195. if (NCDValue_Type(server_arg) != NCDVALUE_STRING) {
  196. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  197. goto fail1;
  198. }
  199. uint32_t addr;
  200. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(server_arg), &addr)) {
  201. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  202. goto fail1;
  203. }
  204. if (!add_ipv4_dns_entry(o, addr, priority)) {
  205. ModuleLog(o->i, BLOG_ERROR, "failed to add dns entry");
  206. goto fail1;
  207. }
  208. server_arg = NCDValue_ListNext(servers_arg, server_arg);
  209. }
  210. // add to instances
  211. LinkedList2_Append(&instances, &o->instances_node);
  212. // set servers
  213. if (!set_servers()) {
  214. ModuleLog(o->i, BLOG_ERROR, "failed to set DNS servers");
  215. goto fail2;
  216. }
  217. // signal up
  218. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  219. return;
  220. fail2:
  221. LinkedList2_Remove(&instances, &o->instances_node);
  222. fail1:
  223. remove_ipv4_dns_entries(o);
  224. free(o);
  225. fail0:
  226. NCDModuleInst_Backend_SetError(i);
  227. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  228. }
  229. static void func_die (void *vo)
  230. {
  231. struct instance *o = vo;
  232. NCDModuleInst *i = o->i;
  233. // remove from instances
  234. LinkedList2_Remove(&instances, &o->instances_node);
  235. // set servers
  236. set_servers();
  237. // free servers
  238. remove_ipv4_dns_entries(o);
  239. // free instance
  240. free(o);
  241. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  242. }
  243. static const struct NCDModule modules[] = {
  244. {
  245. .type = "net.dns",
  246. .func_new = func_new,
  247. .func_die = func_die
  248. }, {
  249. .type = NULL
  250. }
  251. };
  252. const struct NCDModuleGroup ncdmodule_net_dns = {
  253. .func_globalinit = func_globalinit,
  254. .modules = modules
  255. };