net_dns.c 7.5 KB

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