net_dns.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <stddef.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/offset.h>
  32. #include <misc/bsort.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. // count servers
  113. size_t num_ipv4_dns_servers = num_servers();
  114. // allocate sort array
  115. struct dns_sort_entry servers[num_ipv4_dns_servers];
  116. size_t num_servers = 0;
  117. // fill sort array
  118. LinkedList2Iterator it;
  119. LinkedList2Iterator_InitForward(&it, &instances);
  120. LinkedList2Node *n;
  121. while (n = LinkedList2Iterator_Next(&it)) {
  122. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  123. LinkedList2Iterator eit;
  124. LinkedList2Iterator_InitForward(&eit, &o->ipv4_dns_servers);
  125. LinkedList2Node *en;
  126. while (en = LinkedList2Iterator_Next(&eit)) {
  127. struct ipv4_dns_entry *e = UPPER_OBJECT(en, struct ipv4_dns_entry, list_node);
  128. servers[num_servers].addr = e->addr;
  129. servers[num_servers].priority= e->priority;
  130. num_servers++;
  131. }
  132. }
  133. ASSERT(num_servers == num_ipv4_dns_servers)
  134. // sort by priority
  135. // use a custom insertion sort instead of qsort() because we want a stable sort
  136. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator);
  137. // copy addresses into an array
  138. uint32_t addrs[num_servers];
  139. for (size_t i = 0; i < num_servers; i++) {
  140. addrs[i] = servers[i].addr;
  141. }
  142. // set servers
  143. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int func_globalinit (struct NCDModuleInitParams params)
  149. {
  150. LinkedList2_Init(&instances);
  151. return 1;
  152. }
  153. static void * func_new (NCDModuleInst *i)
  154. {
  155. // allocate instance
  156. struct instance *o = malloc(sizeof(*o));
  157. if (!o) {
  158. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  159. goto fail0;
  160. }
  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. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  203. return o;
  204. fail2:
  205. LinkedList2_Remove(&instances, &o->instances_node);
  206. fail1:
  207. remove_ipv4_dns_entries(o);
  208. free(o);
  209. fail0:
  210. return NULL;
  211. }
  212. static void func_free (void *vo)
  213. {
  214. struct instance *o = vo;
  215. // remove from instances
  216. LinkedList2_Remove(&instances, &o->instances_node);
  217. // set servers
  218. set_servers();
  219. // free servers
  220. remove_ipv4_dns_entries(o);
  221. // free instance
  222. free(o);
  223. }
  224. static const struct NCDModule modules[] = {
  225. {
  226. .type = "net.dns",
  227. .func_new = func_new,
  228. .func_free = func_free
  229. }, {
  230. .type = NULL
  231. }
  232. };
  233. const struct NCDModuleGroup ncdmodule_net_dns = {
  234. .func_globalinit = func_globalinit,
  235. .modules = modules
  236. };