net_dns.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <limits.h>
  38. #include <misc/offset.h>
  39. #include <misc/bsort.h>
  40. #include <misc/balloc.h>
  41. #include <misc/compare.h>
  42. #include <structure/LinkedList1.h>
  43. #include <ncd/NCDModule.h>
  44. #include <ncd/extra/NCDIfConfig.h>
  45. #include <ncd/extra/value_utils.h>
  46. #include <generated/blog_channel_ncd_net_dns.h>
  47. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. #define ModuleGlobal(i) ((i)->m->group->group_state)
  49. struct instance {
  50. NCDModuleInst *i;
  51. LinkedList1 ipv4_dns_servers;
  52. LinkedList1Node instances_node; // node in instances
  53. };
  54. struct ipv4_dns_entry {
  55. LinkedList1Node list_node; // node in instance.ipv4_dns_servers
  56. uint32_t addr;
  57. int priority;
  58. };
  59. struct global {
  60. LinkedList1 instances;
  61. };
  62. static struct ipv4_dns_entry * add_ipv4_dns_entry (struct instance *o, uint32_t addr, int priority)
  63. {
  64. // allocate entry
  65. struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
  66. if (!entry) {
  67. return NULL;
  68. }
  69. // set info
  70. entry->addr = addr;
  71. entry->priority = priority;
  72. // add to list
  73. LinkedList1_Append(&o->ipv4_dns_servers, &entry->list_node);
  74. return entry;
  75. }
  76. static void remove_ipv4_dns_entry (struct instance *o, struct ipv4_dns_entry *entry)
  77. {
  78. // remove from list
  79. LinkedList1_Remove(&o->ipv4_dns_servers, &entry->list_node);
  80. // free entry
  81. free(entry);
  82. }
  83. static void remove_ipv4_dns_entries (struct instance *o)
  84. {
  85. LinkedList1Node *n;
  86. while (n = LinkedList1_GetFirst(&o->ipv4_dns_servers)) {
  87. struct ipv4_dns_entry *e = UPPER_OBJECT(n, struct ipv4_dns_entry, list_node);
  88. remove_ipv4_dns_entry(o, e);
  89. }
  90. }
  91. static size_t num_servers (struct global *g)
  92. {
  93. size_t c = 0;
  94. for (LinkedList1Node *n = LinkedList1_GetFirst(&g->instances); n; n = LinkedList1Node_Next(n)) {
  95. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  96. for (LinkedList1Node *en = LinkedList1_GetFirst(&o->ipv4_dns_servers); en; en = LinkedList1Node_Next(en)) {
  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 (struct global *g)
  113. {
  114. int ret = 0;
  115. // count servers
  116. size_t num_ipv4_dns_servers = num_servers(g);
  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. for (LinkedList1Node *n = LinkedList1_GetFirst(&g->instances); n; n = LinkedList1Node_Next(n)) {
  125. struct instance *o = UPPER_OBJECT(n, struct instance, instances_node);
  126. for (LinkedList1Node *en = LinkedList1_GetFirst(&o->ipv4_dns_servers); en; en = LinkedList1Node_Next(en)) {
  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. struct dns_sort_entry sort_temp;
  137. BInsertionSort(servers, num_servers, sizeof(servers[0]), dns_sort_comparator, &sort_temp);
  138. // copy addresses into an array
  139. uint32_t *addrs = BAllocArray(num_servers, sizeof(addrs[0]));
  140. if (!addrs) {
  141. goto fail1;
  142. }
  143. for (size_t i = 0; i < num_servers; i++) {
  144. addrs[i] = servers[i].addr;
  145. }
  146. // set servers
  147. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  148. goto fail2;
  149. }
  150. ret = 1;
  151. fail2:
  152. BFree(addrs);
  153. fail1:
  154. BFree(servers);
  155. fail0:
  156. return ret;
  157. }
  158. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  159. {
  160. // allocate global state structure
  161. struct global *g = BAlloc(sizeof(*g));
  162. if (!g) {
  163. BLog(BLOG_ERROR, "BAlloc failed");
  164. return 0;
  165. }
  166. // set group state pointer
  167. group->group_state = g;
  168. // init instances list
  169. LinkedList1_Init(&g->instances);
  170. return 1;
  171. }
  172. static void func_globalfree (struct NCDInterpModuleGroup *group)
  173. {
  174. struct global *g = group->group_state;
  175. ASSERT(LinkedList1_IsEmpty(&g->instances))
  176. // free global state structure
  177. BFree(g);
  178. }
  179. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  180. {
  181. struct global *g = ModuleGlobal(i);
  182. struct instance *o = vo;
  183. o->i = i;
  184. // init servers list
  185. LinkedList1_Init(&o->ipv4_dns_servers);
  186. // get arguments
  187. NCDValRef servers_arg;
  188. NCDValRef priority_arg;
  189. if (!NCDVal_ListRead(params->args, 2, &servers_arg, &priority_arg)) {
  190. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  191. goto fail1;
  192. }
  193. if (!NCDVal_IsList(servers_arg) || !NCDVal_IsString(priority_arg)) {
  194. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  195. goto fail1;
  196. }
  197. uintmax_t priority;
  198. if (!ncd_read_uintmax(priority_arg, &priority) || priority > INT_MAX) {
  199. ModuleLog(o->i, BLOG_ERROR, "wrong priority");
  200. goto fail1;
  201. }
  202. // read servers
  203. size_t count = NCDVal_ListCount(servers_arg);
  204. for (size_t j = 0; j < count; j++) {
  205. NCDValRef server_arg = NCDVal_ListGet(servers_arg, j);
  206. if (!NCDVal_IsString(server_arg)) {
  207. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  208. goto fail1;
  209. }
  210. uint32_t addr;
  211. if (!ipaddr_parse_ipv4_addr_bin((char *)NCDVal_StringData(server_arg), NCDVal_StringLength(server_arg), &addr)) {
  212. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  213. goto fail1;
  214. }
  215. if (!add_ipv4_dns_entry(o, addr, priority)) {
  216. ModuleLog(o->i, BLOG_ERROR, "failed to add dns entry");
  217. goto fail1;
  218. }
  219. }
  220. // add to instances
  221. LinkedList1_Append(&g->instances, &o->instances_node);
  222. // set servers
  223. if (!set_servers(g)) {
  224. ModuleLog(o->i, BLOG_ERROR, "failed to set DNS servers");
  225. goto fail2;
  226. }
  227. // signal up
  228. NCDModuleInst_Backend_Up(o->i);
  229. return;
  230. fail2:
  231. LinkedList1_Remove(&g->instances, &o->instances_node);
  232. fail1:
  233. remove_ipv4_dns_entries(o);
  234. NCDModuleInst_Backend_DeadError(i);
  235. }
  236. static void func_die (void *vo)
  237. {
  238. struct instance *o = vo;
  239. struct global *g = ModuleGlobal(o->i);
  240. // remove from instances
  241. LinkedList1_Remove(&g->instances, &o->instances_node);
  242. // set servers
  243. set_servers(g);
  244. // free servers
  245. remove_ipv4_dns_entries(o);
  246. NCDModuleInst_Backend_Dead(o->i);
  247. }
  248. static struct NCDModule modules[] = {
  249. {
  250. .type = "net.dns",
  251. .func_new2 = func_new,
  252. .func_die = func_die,
  253. .alloc_size = sizeof(struct instance)
  254. }, {
  255. .type = NULL
  256. }
  257. };
  258. const struct NCDModuleGroup ncdmodule_net_dns = {
  259. .func_globalinit = func_globalinit,
  260. .func_globalfree = func_globalfree,
  261. .modules = modules
  262. };