dns.c 6.4 KB

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