You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dns_wrapper.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * dns_wrapper.c:
  20. *
  21. * The DNS wrapper listens to the port 53 for DNS hostname resolution queries,
  22. * it then resolves the hostname by using the ANDNA system and sends back the
  23. * resolved ip. In this way, every program can use ANDNA: just set
  24. * "nameserver localhost"
  25. * in /etc/resolv.conf ;)
  26. */
  27. #include "includes.h"
  28. #include "inet.h"
  29. #include "endianness.h"
  30. #include "map.h"
  31. #include "gmap.h"
  32. #include "bmap.h"
  33. #include "route.h"
  34. #include "request.h"
  35. #include "pkts.h"
  36. #include "tracer.h"
  37. #include "qspn.h"
  38. #include "radar.h"
  39. #include "netsukuku.h"
  40. #include "daemon.h"
  41. #include "crypto.h"
  42. #include "andna_cache.h"
  43. #include "andna.h"
  44. #include "andns.h"
  45. #include "dns_wrapper.h"
  46. #include "common.h"
  47. /*
  48. * dns_exec_pkt: resolve the hostname contained in the DNS query and sends
  49. * the reply to from.
  50. * `passed_argv' is a pointer to a dns_exec_pkt_argv struct.
  51. */
  52. void *
  53. dns_exec_pkt(void *passed_argv)
  54. {
  55. struct dns_exec_pkt_argv argv;
  56. char buf[MAX_DNS_PKT_SZ];
  57. char answer_buffer[ANDNS_MAX_SZ];
  58. int answer_length;
  59. int bytes_sent;
  60. memcpy(&argv, passed_argv, sizeof(struct dns_exec_pkt_argv));
  61. memcpy(&buf, argv.rpkt, argv.rpkt_sz);
  62. pthread_mutex_unlock(&dns_exec_lock);
  63. if (argv.rpkt_sz < MIN_PKT_SZ) {
  64. debug(DBG_NORMAL, "Received malformed DNS packet");
  65. return 0;
  66. }
  67. printf("dns_exec_pkt buf: %s rpkt %s", buf, argv.rpkt);
  68. /* Unpack the DNS query and resolve the hostname */
  69. if (!andns_rslv(buf, argv.rpkt_sz, answer_buffer, &answer_length))
  70. return 0;
  71. /* Send the DNS reply */
  72. bytes_sent = inet_sendto(argv.sk, answer_buffer, answer_length, 0,
  73. &argv.from, argv.from_len);
  74. error
  75. ("bytes_sent is: %d argv.sk is: %d answer_buffer is: %s answer_length is: %d argv.from is: %p agrv.from_len is: %p",
  76. bytes_sent, argv.sk, answer_buffer, answer_length, argv.from,
  77. argv.from_len);
  78. if (bytes_sent != answer_length)
  79. debug(DBG_SOFT, ERROR_MSG "inet_sendto error: %s", ERROR_POS,
  80. strerror(errno));
  81. return 0;
  82. }
  83. /*
  84. * dns_wrapper_daemon: It receives DNS query pkts, resolves them in ANDNA and
  85. * replies with a DNS reply.
  86. * It listens to `port'.
  87. */
  88. void
  89. dns_wrapper_daemon(u_short port)
  90. {
  91. struct dns_exec_pkt_argv exec_pkt_argv;
  92. char buf[MAX_DNS_PKT_SZ];
  93. fd_set fdset;
  94. int ret, sk;
  95. pthread_t thread;
  96. pthread_attr_t t_attr;
  97. ssize_t err = -1;
  98. #ifdef DEBUG
  99. int select_errors = 0;
  100. #endif
  101. pthread_attr_init(&t_attr);
  102. pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
  103. pthread_mutex_init(&dns_exec_lock, 0);
  104. debug(DBG_SOFT, "Preparing the dns_udp listening socket on port %d",
  105. port);
  106. sk = prepare_listen_socket(my_family, SOCK_DGRAM, port, 0);
  107. if (sk == -1)
  108. return;
  109. debug(DBG_NORMAL, "DNS wrapper daemon on port %d up & running", port);
  110. for (;;) {
  111. if (!sk)
  112. fatal("The dns_wrapper_daemon socket got corrupted");
  113. FD_ZERO(&fdset);
  114. FD_SET(sk, &fdset);
  115. ret = select(sk + 1, &fdset, NULL, NULL, NULL);
  116. if (sigterm_timestamp)
  117. /* NetsukukuD has been closed */
  118. break;
  119. if (ret < 0) {
  120. #ifdef DEBUG
  121. if (select_errors > 20)
  122. break;
  123. select_errors++;
  124. #endif
  125. error("dns_wrapper_daemonp: select error: %s",
  126. strerror(errno));
  127. continue;
  128. }
  129. if (!FD_ISSET(sk, &fdset))
  130. continue;
  131. setzero(&buf, MAX_DNS_PKT_SZ);
  132. setzero(&exec_pkt_argv.from, sizeof(struct sockaddr));
  133. setzero(&exec_pkt_argv, sizeof(struct dns_exec_pkt_argv));
  134. exec_pkt_argv.from_len = my_family == AF_INET ?
  135. sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
  136. /* we get the DNS query */
  137. err = inet_recvfrom(sk, buf, MAX_DNS_PKT_SZ, MSG_WAITALL,
  138. &exec_pkt_argv.from, &exec_pkt_argv.from_len);
  139. if (err < 0) {
  140. debug(DBG_NOISE, "dns_wrapper_daemonp: recv of the dns"
  141. " query pkt aborted!");
  142. continue;
  143. }
  144. /* Exec the pkt in another thread */
  145. exec_pkt_argv.sk = sk;
  146. exec_pkt_argv.rpkt_sz = err;
  147. exec_pkt_argv.rpkt = buf;
  148. printf("dns_wrapper_daemon buf: %s rpkt %s", buf, exec_pkt_argv.rpkt);
  149. pthread_mutex_lock(&dns_exec_lock);
  150. pthread_create(&thread, &t_attr, dns_exec_pkt,
  151. (void *) &exec_pkt_argv);
  152. pthread_mutex_lock(&dns_exec_lock);
  153. pthread_mutex_unlock(&dns_exec_lock);
  154. }
  155. }
  156. void *
  157. dns_wrapper_thread(void *null)
  158. {
  159. dns_wrapper_daemon(DNS_WRAPPER_PORT);
  160. return 0;
  161. }