dns.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * dns.c -- part of dns.mod
  3. * domain lookup glue code for eggdrop
  4. *
  5. * Written by Fabian Knittel <fknittel@gmx.de>
  6. *
  7. * $Id: dns.c,v 1.27 2002/07/07 22:35:25 guppy Exp $
  8. */
  9. /*
  10. * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26. #define MODULE_NAME "dns"
  27. #include "src/mod/module.h"
  28. #include "dns.h"
  29. static void dns_event_success(struct resolve *rp, int type);
  30. static void dns_event_failure(struct resolve *rp, int type);
  31. static Function *global = NULL;
  32. #include "coredns.c"
  33. /*
  34. * DNS event related code
  35. */
  36. static void dns_event_success(struct resolve *rp, int type)
  37. {
  38. if (!rp)
  39. return;
  40. if (type == T_PTR) {
  41. debug2("DNS resolved %s to %s", iptostr(rp->ip), rp->hostn);
  42. call_hostbyip(ntohl(rp->ip), rp->hostn, 1);
  43. } else if (type == T_A) {
  44. debug2("DNS resolved %s to %s", rp->hostn, iptostr(rp->ip));
  45. call_ipbyhost(rp->hostn, ntohl(rp->ip), 1);
  46. }
  47. }
  48. static void dns_event_failure(struct resolve *rp, int type)
  49. {
  50. if (!rp)
  51. return;
  52. if (type == T_PTR) {
  53. static char s[UHOSTLEN];
  54. debug1("DNS resolve failed for %s", iptostr(rp->ip));
  55. strcpy(s, iptostr(rp->ip));
  56. call_hostbyip(ntohl(rp->ip), s, 0);
  57. } else if (type == T_A) {
  58. debug1("DNS resolve failed for %s", rp->hostn);
  59. call_ipbyhost(rp->hostn, 0, 0);
  60. } else
  61. debug2("DNS resolve failed for unknown %s / %s", iptostr(rp->ip),
  62. nonull(rp->hostn));
  63. return;
  64. }
  65. /*
  66. * DNS Socket related code
  67. */
  68. static void eof_dns_socket(int idx)
  69. {
  70. putlog(LOG_MISC, "*", "DNS Error: socket closed.");
  71. killsock(dcc[idx].sock);
  72. /* Try to reopen socket */
  73. if (init_dns_network()) {
  74. putlog(LOG_MISC, "*", "DNS socket successfully reopened!");
  75. dcc[idx].sock = resfd;
  76. dcc[idx].timeval = now;
  77. } else
  78. lostdcc(idx);
  79. }
  80. static void dns_socket(int idx, char *buf, int len)
  81. {
  82. dns_ack();
  83. }
  84. static void display_dns_socket(int idx, char *buf)
  85. {
  86. strcpy(buf, "dns (ready)");
  87. }
  88. static struct dcc_table DCC_DNS =
  89. {
  90. "DNS",
  91. DCT_LISTEN,
  92. eof_dns_socket,
  93. dns_socket,
  94. NULL,
  95. NULL,
  96. display_dns_socket,
  97. NULL,
  98. NULL,
  99. NULL
  100. };
  101. /*
  102. * DNS module related code
  103. */
  104. static void dns_free_cache(void)
  105. {
  106. struct resolve *rp, *rpnext;
  107. for (rp = expireresolves; rp; rp = rpnext) {
  108. rpnext = rp->next;
  109. if (rp->hostn)
  110. nfree(rp->hostn);
  111. nfree(rp);
  112. }
  113. expireresolves = NULL;
  114. }
  115. static int dns_cache_expmem(void)
  116. {
  117. struct resolve *rp;
  118. int size = 0;
  119. for (rp = expireresolves; rp; rp = rp->next) {
  120. size += sizeof(struct resolve);
  121. if (rp->hostn)
  122. size += strlen(rp->hostn) + 1;
  123. }
  124. return size;
  125. }
  126. static int dns_expmem(void)
  127. {
  128. return dns_cache_expmem();
  129. }
  130. static int dns_report(int idx, int details)
  131. {
  132. if (details) {
  133. dprintf(idx, " (cache uses %d bytes of memory)\n", dns_cache_expmem());
  134. dprintf(idx, " DNS resolver is active.\n");
  135. }
  136. return 0;
  137. }
  138. static char *dns_close()
  139. {
  140. int i;
  141. del_hook(HOOK_DNS_HOSTBYIP, (Function) dns_lookup);
  142. del_hook(HOOK_DNS_IPBYHOST, (Function) dns_forward);
  143. del_hook(HOOK_SECONDLY, (Function) dns_check_expires);
  144. for (i = 0; i < dcc_total; i++) {
  145. if (dcc[i].type == &DCC_DNS &&
  146. dcc[i].sock == resfd) {
  147. killsock(dcc[i].sock);
  148. lostdcc(i);
  149. break;
  150. }
  151. }
  152. dns_free_cache();
  153. module_undepend(MODULE_NAME);
  154. return NULL;
  155. }
  156. EXPORT_SCOPE char *dns_start();
  157. static Function dns_table[] =
  158. {
  159. /* 0 - 3 */
  160. (Function) dns_start,
  161. (Function) dns_close,
  162. (Function) dns_expmem,
  163. (Function) dns_report,
  164. /* 4 - 7 */
  165. };
  166. char *dns_start(Function *global_funcs)
  167. {
  168. int idx;
  169. global = global_funcs;
  170. module_register(MODULE_NAME, dns_table, 1, 0);
  171. if (!module_depend(MODULE_NAME, "eggdrop", 106, 0)) {
  172. module_undepend(MODULE_NAME);
  173. return "This module requires Eggdrop 1.6.0 or later.";
  174. }
  175. idx = new_dcc(&DCC_DNS, 0);
  176. if (idx < 0)
  177. return "NO MORE DCC CONNECTIONS -- Can't create DNS socket.";
  178. if (!init_dns_core()) {
  179. lostdcc(idx);
  180. return "DNS initialisation failed.";
  181. }
  182. dcc[idx].sock = resfd;
  183. dcc[idx].timeval = now;
  184. strcpy(dcc[idx].nick, "(dns)");
  185. add_hook(HOOK_SECONDLY, (Function) dns_check_expires);
  186. add_hook(HOOK_DNS_HOSTBYIP, (Function) dns_lookup);
  187. add_hook(HOOK_DNS_IPBYHOST, (Function) dns_forward);
  188. return NULL;
  189. }