dnsserv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strlen
  16. #define string_strcmp
  17. #include "benc/List.h"
  18. #include "benc/Dict.h"
  19. #include "util/Base32.h"
  20. #include "util/platform/libc/string.h"
  21. #include "memory/MallocAllocator.h"
  22. #include "util/events/EventBase.h"
  23. #include "io/FileWriter.h"
  24. #include "io/FileReader.h"
  25. #include "benc/serialization/BencSerializer.h"
  26. #include "benc/serialization/json/JsonBencSerializer.h"
  27. #include "util/log/WriterLog.h"
  28. #include "util/platform/Sockaddr.h"
  29. #include "interface/addressable/UDPAddrInterface.h"
  30. #include "interface/DNSServer.h"
  31. #include "interface/RainflyClient.h"
  32. #include "util/Assert.h"
  33. #include "util/platform/libc/string.h"
  34. static void genconf()
  35. {
  36. printf("{\n"
  37. " \"dns\": {\n"
  38. " \"bind\": \"[::]:53\",\n"
  39. " \"authorities\": [\n"
  40. " \"7kuc3jcyql3cm8lx5zdj8vc0tkz8679kyx83utbm1ub5bxpf4mf1.mittens.h\",\n"
  41. " \"tvlxu5rbcj76rfdmsw9xd3kjn79fhv6kpvl2hzv98637j4rdj1b1.tom.h\",\n"
  42. " \"kkxfwnm3upf0jv35jq4lx0dn0z3m9bh71gv84cdjlcp68w1qckt1.maru.h\",\n"
  43. " \"02wmqfu7v0kdq17fwv68hk646bdvhcr8ybk2ycy7ddzv21n5nb60.scruffy.h\"\n"
  44. " ],\n"
  45. "\n"
  46. " \"servers\": [\n"
  47. " \"[fc71:ec46:57a0:2bbc:537d:b680:3630:93e4]:9001\",\n"
  48. " \"[fc8e:9a1c:27c3:281b:29b1:1a04:3701:c125]:9001\",\n"
  49. " \"[fcad:0450:4a40:9778:14e2:e442:6678:3161]:9001\",\n"
  50. " \"[fc2f:baa8:4a89:2db5:6789:aa75:07e6:4cb2]:9001\"\n"
  51. " ]\n"
  52. "\n"
  53. " // Where your non .h domain lookups will go\n"
  54. " \"legacy\": [\n"
  55. " \"4.2.2.1:53\"\n"
  56. " \"8.8.8.8:53\"\n"
  57. " ]\n"
  58. " \"minSignatures\":2\n"
  59. " }\n"
  60. "}\n");
  61. }
  62. int main(int argc, char** argv)
  63. {
  64. if (argc > 1 && !strcmp("--genconf", argv[argc-1])) {
  65. genconf();
  66. return 0;
  67. }
  68. struct Allocator* alloc = MallocAllocator_new(1<<22);
  69. struct EventBase* base = EventBase_new(alloc);
  70. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  71. struct Log* logger = WriterLog_new(logWriter, alloc);
  72. struct Random* rand = Random_new(alloc, logger, NULL);
  73. struct Reader* stdinReader = FileReader_new(stdin, alloc);
  74. Dict config;
  75. if (JsonBencSerializer_get()->parseDictionary(stdinReader, alloc, &config)) {
  76. Log_critical(logger, "Failed to parse configuration");
  77. return -1;
  78. }
  79. Dict* dns = Dict_getDict(&config, String_CONST("dns"));
  80. if (!dns) {
  81. Log_critical(logger, "No DNS in configuration");
  82. return -1;
  83. }
  84. struct Sockaddr_storage addr;
  85. Assert_true(!Sockaddr_parse("::", &addr));
  86. struct AddrInterface* ifaceB = UDPAddrInterface_new(base, &addr.addr, alloc, NULL, logger);
  87. struct RainflyClient* client = RainflyClient_new(ifaceB, base, rand, logger);
  88. String* bind = Dict_getString(dns, String_CONST("bind"));
  89. Assert_true(!Sockaddr_parse(bind ? bind->bytes : "[::]:5353", &addr));
  90. struct AddrInterface* iface = UDPAddrInterface_new(base, &addr.addr, alloc, NULL, logger);
  91. struct DNSServer* dnsServer = DNSServer_new(iface, logger, client);
  92. List* auth = Dict_getList(dns, String_CONST("authorities"));
  93. for (int i = 0; i < (int)List_size(auth); i++) {
  94. String* str = List_getString(auth, i);
  95. if (!str) {
  96. Log_warn(logger, "Element [%d] in [dns.authorities] list of wrong type", i);
  97. continue;
  98. }
  99. uint8_t key[32] = {0};
  100. if (str->len < 52 || Base32_decode(key, 32, str->bytes, 52) != 32) {
  101. Log_warn(logger, "Failed to parse key [%s]", str->bytes);
  102. continue;
  103. }
  104. if (RainflyClient_addKey(client, key)) {
  105. Log_warn(logger, "Failed to add key to RainflyClient [%s]", str->bytes);
  106. }
  107. }
  108. List* servers = Dict_getList(dns, String_CONST("servers"));
  109. for (int i = 0; i < (int)List_size(servers); i++) {
  110. String* str = List_getString(servers, i);
  111. if (!str) {
  112. Log_warn(logger, "Element [%d] in [dns.servers] list of wrong type", i);
  113. continue;
  114. }
  115. struct Sockaddr_storage node;
  116. if (Sockaddr_parse(str->bytes, &node)) {
  117. Log_warn(logger, "Failed to parse server name [%s]", str->bytes);
  118. continue;
  119. }
  120. if (RainflyClient_addServer(client, &node.addr)) {
  121. Log_warn(logger, "Failed to add server to RainflyClient [%s]", str->bytes);
  122. }
  123. }
  124. List* legacy = Dict_getList(dns, String_CONST("legacy"));
  125. for (int i = 0; i < (int)List_size(legacy); i++) {
  126. String* str = List_getString(legacy, i);
  127. if (!str) {
  128. Log_warn(logger, "Element [%d] in [dns.legacy] list of wrong type", i);
  129. continue;
  130. }
  131. struct Sockaddr_storage node;
  132. if (Sockaddr_parse(str->bytes, &node)) {
  133. Log_warn(logger, "Failed to parse legacy server name [%s]", str->bytes);
  134. continue;
  135. }
  136. if (DNSServer_addServer(dnsServer, &node.addr)) {
  137. Log_warn(logger, "Failed to add server to DNSServer [%s]", str->bytes);
  138. }
  139. }
  140. EventBase_beginLoop(base);
  141. }