dns.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * dns.c -- handles:
  3. * DNS resolve calls and events
  4. * provides the code used by the bot if the DNS module is not loaded
  5. * DNS Tcl commands
  6. *
  7. */
  8. #include "common.h"
  9. #include "dccutil.h"
  10. #include "main.h"
  11. #include "net.h"
  12. #include "misc.h"
  13. #include "src/mod/dns.mod/dns.h"
  14. #include <netdb.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include "dns.h"
  19. devent_t *dns_events = NULL;
  20. /*
  21. * DCC functions
  22. */
  23. void dcc_dnswait(int idx, char *buf, int len)
  24. {
  25. /* Ignore anything now. */
  26. }
  27. void eof_dcc_dnswait(int idx)
  28. {
  29. putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
  30. iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
  31. killsock(dcc[idx].sock);
  32. lostdcc(idx);
  33. }
  34. static void display_dcc_dnswait(int idx, char *buf)
  35. {
  36. sprintf(buf, "dns waited %lis", now - dcc[idx].timeval);
  37. }
  38. static void kill_dcc_dnswait(int idx, void *x)
  39. {
  40. register struct dns_info *p = (struct dns_info *) x;
  41. if (p) {
  42. if (p->host)
  43. free(p->host);
  44. if (p->cbuf)
  45. free(p->cbuf);
  46. free(p);
  47. }
  48. }
  49. struct dcc_table DCC_DNSWAIT =
  50. {
  51. "DNSWAIT",
  52. DCT_VALIDIDX,
  53. eof_dcc_dnswait,
  54. dcc_dnswait,
  55. NULL,
  56. NULL,
  57. display_dcc_dnswait,
  58. kill_dcc_dnswait,
  59. NULL,
  60. NULL
  61. };
  62. /*
  63. * DCC events
  64. */
  65. /* Walk through every dcc entry and look for waiting DNS requests
  66. * of RES_HOSTBYIP for our IP address.
  67. */
  68. static void dns_dcchostbyip(in_addr_t ip, char *hostn, int ok, void *other)
  69. {
  70. for (int idx = 0; idx < dcc_total; idx++) {
  71. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  72. (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) &&
  73. (dcc[idx].u.dns->ip == ip)) {
  74. if (dcc[idx].u.dns->host)
  75. free(dcc[idx].u.dns->host);
  76. dcc[idx].u.dns->host = (char *) calloc(1, strlen(hostn) + 1);
  77. strcpy(dcc[idx].u.dns->host, hostn);
  78. if (ok)
  79. dcc[idx].u.dns->dns_success(idx);
  80. else
  81. dcc[idx].u.dns->dns_failure(idx);
  82. }
  83. }
  84. }
  85. /* Walk through every dcc entry and look for waiting DNS requests
  86. * of RES_IPBYHOST for our hostname.
  87. */
  88. static void dns_dccipbyhost(in_addr_t ip, char *hostn, int ok, void *other)
  89. {
  90. for (int idx = 0; idx < dcc_total; idx++) {
  91. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  92. (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
  93. !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
  94. dcc[idx].u.dns->ip = ip;
  95. if (ok)
  96. dcc[idx].u.dns->dns_success(idx);
  97. else
  98. dcc[idx].u.dns->dns_failure(idx);
  99. }
  100. }
  101. }
  102. devent_type DNS_DCCEVENT_HOSTBYIP = {
  103. "DCCEVENT_HOSTBYIP",
  104. dns_dcchostbyip
  105. };
  106. devent_type DNS_DCCEVENT_IPBYHOST = {
  107. "DCCEVENT_IPBYHOST",
  108. dns_dccipbyhost
  109. };
  110. void dcc_dnsipbyhost(char *hostn)
  111. {
  112. devent_t *de = NULL;
  113. for (de = dns_events; de; de = de->next) {
  114. if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
  115. (de->lookup == RES_IPBYHOST)) {
  116. if (de->res_data.hostname &&
  117. !egg_strcasecmp(de->res_data.hostname, hostn))
  118. /* No need to add anymore. */
  119. return;
  120. }
  121. }
  122. de = (devent_t *) calloc(1, sizeof(devent_t));
  123. /* Link into list. */
  124. de->next = dns_events;
  125. dns_events = de;
  126. de->type = &DNS_DCCEVENT_IPBYHOST;
  127. de->lookup = RES_IPBYHOST;
  128. de->res_data.hostname = strdup(hostn);
  129. /* Send request. */
  130. dns_ipbyhost(hostn);
  131. }
  132. void dcc_dnshostbyip(in_addr_t ip)
  133. {
  134. devent_t *de = NULL;
  135. for (de = dns_events; de; de = de->next) {
  136. if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
  137. (de->lookup == RES_HOSTBYIP)) {
  138. if (de->res_data.ip_addr == ip)
  139. /* No need to add anymore. */
  140. return;
  141. }
  142. }
  143. de = (devent_t *) calloc(1, sizeof(devent_t));
  144. /* Link into list. */
  145. de->next = dns_events;
  146. dns_events = de;
  147. de->type = &DNS_DCCEVENT_HOSTBYIP;
  148. de->lookup = RES_HOSTBYIP;
  149. de->res_data.ip_addr = ip;
  150. /* Send request. */
  151. dns_hostbyip(ip);
  152. }
  153. /*
  154. * Event functions
  155. */
  156. void call_hostbyip(in_addr_t ip, char *hostn, int ok)
  157. {
  158. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  159. while (de) {
  160. nde = de->next;
  161. if ((de->lookup == RES_HOSTBYIP) &&
  162. (!de->res_data.ip_addr || (de->res_data.ip_addr == ip))) {
  163. /* Remove the event from the list here, to avoid conflicts if one of
  164. * the event handlers re-adds another event. */
  165. if (ode)
  166. ode->next = de->next;
  167. else
  168. dns_events = de->next;
  169. if (de->type && de->type->event)
  170. de->type->event(ip, hostn, ok, de->other);
  171. else
  172. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  173. (de->type && de->type->name) ? de->type->name : "<empty>");
  174. free(de);
  175. de = ode;
  176. }
  177. ode = de;
  178. de = nde;
  179. }
  180. }
  181. void call_ipbyhost(char *hostn, in_addr_t ip, int ok)
  182. {
  183. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  184. while (de) {
  185. nde = de->next;
  186. if ((de->lookup == RES_IPBYHOST) &&
  187. (!de->res_data.hostname ||
  188. !egg_strcasecmp(de->res_data.hostname, hostn))) {
  189. /* Remove the event from the list here, to avoid conflicts if one of
  190. * the event handlers re-adds another event. */
  191. if (ode)
  192. ode->next = de->next;
  193. else
  194. dns_events = de->next;
  195. if (de->type && de->type->event)
  196. de->type->event(ip, hostn, ok, de->other);
  197. else
  198. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  199. (de->type && de->type->name) ? de->type->name : "<empty>");
  200. if (de->res_data.hostname)
  201. free(de->res_data.hostname);
  202. free(de);
  203. de = ode;
  204. }
  205. ode = de;
  206. de = nde;
  207. }
  208. }