dns.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <setjmp.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include "dns.h"
  20. devent_t *dns_events = NULL;
  21. /*
  22. * DCC functions
  23. */
  24. void dcc_dnswait(int idx, char *buf, int len)
  25. {
  26. /* Ignore anything now. */
  27. }
  28. void eof_dcc_dnswait(int idx)
  29. {
  30. putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
  31. iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
  32. killsock(dcc[idx].sock);
  33. lostdcc(idx);
  34. }
  35. static void display_dcc_dnswait(int idx, char *buf)
  36. {
  37. sprintf(buf, "dns waited %lis", now - dcc[idx].timeval);
  38. }
  39. static void kill_dcc_dnswait(int idx, void *x)
  40. {
  41. register struct dns_info *p = (struct dns_info *) x;
  42. if (p) {
  43. if (p->host)
  44. free(p->host);
  45. if (p->cbuf)
  46. free(p->cbuf);
  47. free(p);
  48. }
  49. }
  50. struct dcc_table DCC_DNSWAIT =
  51. {
  52. "DNSWAIT",
  53. DCT_VALIDIDX,
  54. eof_dcc_dnswait,
  55. dcc_dnswait,
  56. NULL,
  57. NULL,
  58. display_dcc_dnswait,
  59. kill_dcc_dnswait,
  60. NULL,
  61. NULL
  62. };
  63. /*
  64. * DCC events
  65. */
  66. /* Walk through every dcc entry and look for waiting DNS requests
  67. * of RES_HOSTBYIP for our IP address.
  68. */
  69. static void dns_dcchostbyip(IP ip, char *hostn, int ok, void *other)
  70. {
  71. int idx;
  72. for (idx = 0; idx < dcc_total; idx++) {
  73. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  74. (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) &&
  75. (dcc[idx].u.dns->ip == ip)) {
  76. if (dcc[idx].u.dns->host)
  77. free(dcc[idx].u.dns->host);
  78. dcc[idx].u.dns->host = calloc(1, strlen(hostn) + 1);
  79. strcpy(dcc[idx].u.dns->host, hostn);
  80. if (ok)
  81. dcc[idx].u.dns->dns_success(idx);
  82. else
  83. dcc[idx].u.dns->dns_failure(idx);
  84. }
  85. }
  86. }
  87. /* Walk through every dcc entry and look for waiting DNS requests
  88. * of RES_IPBYHOST for our hostname.
  89. */
  90. static void dns_dccipbyhost(IP ip, char *hostn, int ok, void *other)
  91. {
  92. int idx;
  93. for (idx = 0; idx < dcc_total; idx++) {
  94. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  95. (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
  96. !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
  97. dcc[idx].u.dns->ip = ip;
  98. if (ok)
  99. dcc[idx].u.dns->dns_success(idx);
  100. else
  101. dcc[idx].u.dns->dns_failure(idx);
  102. }
  103. }
  104. }
  105. devent_type DNS_DCCEVENT_HOSTBYIP = {
  106. "DCCEVENT_HOSTBYIP",
  107. dns_dcchostbyip
  108. };
  109. devent_type DNS_DCCEVENT_IPBYHOST = {
  110. "DCCEVENT_IPBYHOST",
  111. dns_dccipbyhost
  112. };
  113. void dcc_dnsipbyhost(char *hostn)
  114. {
  115. devent_t *de = NULL;
  116. for (de = dns_events; de; de = de->next) {
  117. if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
  118. (de->lookup == RES_IPBYHOST)) {
  119. if (de->res_data.hostname &&
  120. !egg_strcasecmp(de->res_data.hostname, hostn))
  121. /* No need to add anymore. */
  122. return;
  123. }
  124. }
  125. de = calloc(1, sizeof(devent_t));
  126. /* Link into list. */
  127. de->next = dns_events;
  128. dns_events = de;
  129. de->type = &DNS_DCCEVENT_IPBYHOST;
  130. de->lookup = RES_IPBYHOST;
  131. de->res_data.hostname = strdup(hostn);
  132. /* Send request. */
  133. dns_ipbyhost(hostn);
  134. }
  135. void dcc_dnshostbyip(IP ip)
  136. {
  137. devent_t *de = NULL;
  138. for (de = dns_events; de; de = de->next) {
  139. if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
  140. (de->lookup == RES_HOSTBYIP)) {
  141. if (de->res_data.ip_addr == ip)
  142. /* No need to add anymore. */
  143. return;
  144. }
  145. }
  146. de = calloc(1, sizeof(devent_t));
  147. /* Link into list. */
  148. de->next = dns_events;
  149. dns_events = de;
  150. de->type = &DNS_DCCEVENT_HOSTBYIP;
  151. de->lookup = RES_HOSTBYIP;
  152. de->res_data.ip_addr = ip;
  153. /* Send request. */
  154. dns_hostbyip(ip);
  155. }
  156. /*
  157. * Event functions
  158. */
  159. void call_hostbyip(IP ip, char *hostn, int ok)
  160. {
  161. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  162. while (de) {
  163. nde = de->next;
  164. if ((de->lookup == RES_HOSTBYIP) &&
  165. (!de->res_data.ip_addr || (de->res_data.ip_addr == ip))) {
  166. /* Remove the event from the list here, to avoid conflicts if one of
  167. * the event handlers re-adds another event. */
  168. if (ode)
  169. ode->next = de->next;
  170. else
  171. dns_events = de->next;
  172. if (de->type && de->type->event)
  173. de->type->event(ip, hostn, ok, de->other);
  174. else
  175. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  176. (de->type && de->type->name) ? de->type->name : "<empty>");
  177. free(de);
  178. de = ode;
  179. }
  180. ode = de;
  181. de = nde;
  182. }
  183. }
  184. void call_ipbyhost(char *hostn, IP ip, int ok)
  185. {
  186. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  187. while (de) {
  188. nde = de->next;
  189. if ((de->lookup == RES_IPBYHOST) &&
  190. (!de->res_data.hostname ||
  191. !egg_strcasecmp(de->res_data.hostname, hostn))) {
  192. /* Remove the event from the list here, to avoid conflicts if one of
  193. * the event handlers re-adds another event. */
  194. if (ode)
  195. ode->next = de->next;
  196. else
  197. dns_events = de->next;
  198. if (de->type && de->type->event)
  199. de->type->event(ip, hostn, ok, de->other);
  200. else
  201. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  202. (de->type && de->type->name) ? de->type->name : "<empty>");
  203. if (de->res_data.hostname)
  204. free(de->res_data.hostname);
  205. free(de);
  206. de = ode;
  207. }
  208. ode = de;
  209. de = nde;
  210. }
  211. }
  212. /*
  213. * Async DNS emulation functions
  214. */
  215. void block_dns_hostbyip(IP ip)
  216. {
  217. struct hostent *hp = NULL;
  218. IP addr = htonl(ip);
  219. static char s[UHOSTLEN] = "";
  220. if (!setjmp(alarmret)) {
  221. alarm(resolve_timeout);
  222. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  223. alarm(0);
  224. if (hp) {
  225. strncpyz(s, hp->h_name, sizeof s);
  226. } else
  227. strcpy(s, iptostr(addr));
  228. } else {
  229. hp = NULL;
  230. strcpy(s, iptostr(addr));
  231. }
  232. /* Call hooks. */
  233. call_hostbyip(ip, s, hp ? 1 : 0);
  234. }
  235. void block_dns_ipbyhost(char *host)
  236. {
  237. struct in_addr inaddr;
  238. /* Check if someone passed us an IP address as hostname
  239. * and return it straight away */
  240. if (egg_inet_aton(host, &inaddr)) {
  241. call_ipbyhost(host, ntohl(inaddr.s_addr), 1);
  242. return;
  243. }
  244. if (!setjmp(alarmret)) {
  245. struct hostent *hp = NULL;
  246. struct in_addr *in = NULL;
  247. IP ip = 0;
  248. alarm(resolve_timeout);
  249. hp = gethostbyname(host);
  250. alarm(0);
  251. if (hp) {
  252. in = (struct in_addr *) (hp->h_addr_list[0]);
  253. ip = (IP) (in->s_addr);
  254. call_ipbyhost(host, ntohl(ip), 1);
  255. return;
  256. }
  257. /* Fall through. */
  258. }
  259. call_ipbyhost(host, 0, 0);
  260. }