adns.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2013 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /* async dns
  21. *
  22. * This is based on eggdrop1.9's dns code and not GNU adns.
  23. *
  24. */
  25. #include "common.h"
  26. #include "adns.h"
  27. #include "egg_timer.h"
  28. #include "main.h"
  29. #include "net.h"
  30. #include "misc.h"
  31. #include "socket.h"
  32. #include <netinet/in.h>
  33. #include <sys/socket.h>
  34. #include <arpa/inet.h>
  35. #include <bdlib/src/Array.h>
  36. #include <bdlib/src/String.h>
  37. typedef struct dns_query {
  38. struct dns_query *next;
  39. bd::Array<bd::String>* answer;
  40. dns_callback_t callback;
  41. void *client_data;
  42. time_t expiretime;
  43. char *query;
  44. char *ip;
  45. int id;
  46. // int timer_id;
  47. int answers;
  48. int remaining;
  49. int lowest_ttl;
  50. } dns_query_t;
  51. /* RFC1035
  52. 1 1 1 1 1 1
  53. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  54. 1 1 1 1 1 0 9 8 7 6 5 4 3 2 1 0
  55. 5 4 3 2 1 1
  56. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  57. | ID |
  58. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  59. |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
  60. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  61. | QDCOUNT |
  62. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  63. | ANCOUNT |
  64. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  65. | NSCOUNT |
  66. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  67. | ARCOUNT |
  68. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  69. */
  70. typedef struct {
  71. unsigned short id;
  72. unsigned short flags;
  73. unsigned short question_count;
  74. unsigned short answer_count;
  75. unsigned short ns_count;
  76. unsigned short ar_count;
  77. } dns_header_t;
  78. #define GET_QR(x) (((x) >> 15) & BIT0)
  79. #define GET_OPCODE(x) (((x) >> 11) & (BIT3|BIT2|BIT1|BIT0))
  80. #define GET_AA(x) (((x) >> 10) & BIT0)
  81. #define GET_TC(x) (((x) >> 9) & BIT0)
  82. #define GET_RD(x) (((x) >> 8) & BIT0)
  83. #define GET_RA(x) (((x) >> 7) & BIT0)
  84. #define GET_RCODE(x) ((x) & (BIT3|BIT2|BIT1|BIT0))
  85. #define SET_RD(x) (x) |= ((x) | (1 << 8))
  86. #define HEAD_SIZE 12
  87. /* RFC1035
  88. 1 1 1 1 1 1
  89. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  90. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  91. | |
  92. / /
  93. / NAME /
  94. | |
  95. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  96. | TYPE |
  97. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  98. | CLASS |
  99. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  100. | TTL |
  101. | |
  102. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  103. | RDLENGTH |
  104. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
  105. / RDATA /
  106. / /
  107. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  108. */
  109. typedef struct {
  110. /* char name[]; */
  111. unsigned short type;
  112. unsigned short dclass;
  113. int ttl;
  114. unsigned short rdlength;
  115. /* char rdata[]; */
  116. } dns_rr_t;
  117. #define RR_SIZE 10
  118. /* Entries from resolv.conf */
  119. typedef struct dns_server {
  120. char *ip;
  121. int idx;
  122. } dns_server_t;
  123. /* Entries from hosts */
  124. typedef struct {
  125. char *host;
  126. char *ip;
  127. } dns_host_t;
  128. typedef struct {
  129. bd::Array<bd::String>* answer;
  130. char *query;
  131. time_t expiretime;
  132. } dns_cache_t;
  133. static dns_header_t _dns_header = {0, 0, 0, 0, 0, 0};
  134. static dns_query_t *query_head = NULL;
  135. static dns_host_t *hosts = NULL;
  136. static int nhosts = 0;
  137. static dns_cache_t *cache = NULL;
  138. static int ncache = 0;
  139. static dns_server_t *servers = NULL;
  140. static int nservers = 0;
  141. static int cur_server = -1;
  142. static char separators[] = " ,\t\r\n";
  143. int dns_idx = -1;
  144. int dns_sock = -1;
  145. const char *dns_ip = NULL;
  146. static int make_header(char *buf, int id);
  147. static int cut_host(const char *host, char *query);
  148. static int read_resolv(char *fname);
  149. static void read_hosts(char *fname);
  150. static int get_dns_idx();
  151. //static void dns_resend_queries();
  152. static int cache_find(const char *);
  153. //static int dns_on_read(void *client_data, int idx, char *buf, int len);
  154. //static int dns_on_eof(void *client_data, int idx, int err, const char *errmsg);
  155. static void dns_read(int idx, char*, int&, bool blocking = 0);
  156. static void dns_on_read(int idx, char *buf, int atr);
  157. static void dns_on_eof(int idx);
  158. static const char *dns_next_server();
  159. static int parse_reply(char *response, size_t nbytes, const char* server_ip, bool blocking = 0);
  160. interval_t async_lookup_timeout = 10;
  161. interval_t async_server_timeout = 20;
  162. //int resend_on_read = 0;
  163. static void
  164. dns_display(int idx, char *buf, size_t bufsiz)
  165. {
  166. simple_snprintf(buf, bufsiz, "named waited %ds", (int) (now - dcc[idx].timeval));
  167. }
  168. static void
  169. dns_reinit(int idx)
  170. {
  171. sdprintf("Re-opening dns socket...");
  172. killsock(dcc[idx].sock);
  173. lostdcc(idx);
  174. dns_idx = -1;
  175. dns_sock = -1;
  176. dns_ip = NULL;
  177. if (!get_dns_idx())
  178. sdprintf("Successfully reopened dns socket");
  179. else
  180. sdprintf("Failed to reopen dns socket");
  181. }
  182. static void
  183. dns_timeout(int idx)
  184. {
  185. sdprintf("DNS socket timed out");
  186. /*egg_dns_cancel(dcc[idx].u.dns_id, 1);*/
  187. // resend_on_read = 1;
  188. dns_reinit(idx);
  189. // sleep(2);
  190. // dns_resend_queries();
  191. }
  192. static struct dcc_table dns_handler = {
  193. "adns",
  194. DCT_VALIDIDX,
  195. dns_on_eof,
  196. dns_on_read,
  197. NULL,
  198. dns_timeout,
  199. dns_display,
  200. NULL,
  201. NULL,
  202. NULL
  203. };
  204. static void dcc_dnswait(int idx, char *buf, int len)
  205. {
  206. /* Ignore anything now. */
  207. }
  208. static void eof_dcc_dnswait(int idx)
  209. {
  210. putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
  211. iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
  212. killsock(dcc[idx].sock);
  213. lostdcc(idx);
  214. }
  215. static void display_dcc_dnswait(int idx, char *buf, size_t bufsiz)
  216. {
  217. simple_snprintf(buf, bufsiz, "dns waited %ds", (int) (now - dcc[idx].timeval));
  218. }
  219. static void kill_dcc_dnswait(int idx, void *x)
  220. {
  221. struct dns_info *p = (struct dns_info *) x;
  222. if (p) {
  223. if (p->cbuf)
  224. free(p->cbuf);
  225. if (p->cptr)
  226. free(p->cptr);
  227. // free(p) is same thing here.
  228. free(dcc[idx].u.other);
  229. dcc[idx].u.other = NULL;
  230. }
  231. }
  232. struct dcc_table DCC_DNSWAIT = {
  233. "DNSWAIT",
  234. DCT_VALIDIDX,
  235. eof_dcc_dnswait,
  236. dcc_dnswait,
  237. NULL,
  238. NULL,
  239. display_dcc_dnswait,
  240. kill_dcc_dnswait,
  241. NULL,
  242. NULL
  243. };
  244. /*
  245. static void async_timeout(void *client_data)
  246. {
  247. int id = (int) client_data;
  248. sdprintf("%d timed out", id);
  249. egg_dns_cancel(id, 1);
  250. }
  251. */
  252. char s1_7[3] = "",s2_3[3] = "",s2_2[3] = "";
  253. static void free_query(dns_query_t*& q) {
  254. if (q->ip)
  255. free(q->ip);
  256. if (q->answer)
  257. delete q->answer;
  258. if (q->query)
  259. free(q->query);
  260. free(q);
  261. q = NULL;
  262. }
  263. static int get_dns_id() {
  264. while (1) {
  265. int id = randint(65534);
  266. bool found = 0;
  267. // Make sure this ID is not already in use, avoid a race condition
  268. for (dns_query_t *q = query_head; q; q = q->next) {
  269. if (q->id == id) {
  270. found = 1;
  271. break;
  272. }
  273. }
  274. if (found) continue;
  275. return id;
  276. }
  277. }
  278. static dns_query_t *alloc_query(void *client_data, dns_callback_t callback, const char *query)
  279. {
  280. dns_query_t *q = (dns_query_t *) my_calloc(1, sizeof(*q));
  281. q->id = get_dns_id();
  282. q->query = strdup(query);
  283. q->answers = 0;
  284. q->answer = new bd::Array<bd::String>;
  285. q->lowest_ttl = 0;
  286. q->callback = callback;
  287. q->client_data = client_data;
  288. q->expiretime = now + async_lookup_timeout;
  289. q->next = query_head;
  290. query_head = q;
  291. return q;
  292. }
  293. static void query_transform_ip(dns_query_t* q, const char* ip) {
  294. /* We need to transform the ip address into the proper form
  295. * for reverse lookup. */
  296. if (strchr(ip, ':')) {
  297. char temp[128] = "";
  298. socket_ipv6_to_dots(ip, temp);
  299. size_t iplen = strlen(temp) + 9 + 1;
  300. q->ip = (char *) my_calloc(1, iplen);
  301. // reverse_ip(temp, q->ip);
  302. strlcat(q->ip, temp, iplen);
  303. strlcat(q->ip, "ip6.arpa", iplen);
  304. }
  305. else {
  306. size_t iplen = strlen(ip) + 13 + 1;
  307. q->ip = (char *) my_calloc(1, iplen);
  308. reverse_ip(ip, q->ip);
  309. strlcat(q->ip, ".in-addr.arpa", iplen);
  310. }
  311. }
  312. static int get_dns_idx()
  313. {
  314. int i, sock;
  315. sock = -1;
  316. for (i = 0; i < nservers; i++) {
  317. if (!dns_ip) dns_ip = dns_next_server();
  318. sock = socket_create(dns_ip, DNS_PORT, NULL, 0, SOCKET_CLIENT | SOCKET_NONBLOCK | SOCKET_UDP);
  319. if (sock < 0) {
  320. /* Try the next server. */
  321. dns_ip = NULL;
  322. }
  323. else break;
  324. }
  325. if (i == nservers) return 1;
  326. // dns_idx = sockbuf_new();
  327. // sockbuf_set_handler(dns_idx, &dns_handler, NULL);
  328. // sockbuf_set_sock(dns_idx, sock, 0);
  329. // allocsock(sock, SOCK_CONNECT);
  330. if (sock >= 0 && dns_ip) {
  331. dns_idx = new_dcc(&dns_handler, 0);
  332. if (dns_idx < 0) {
  333. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create dns connection.");
  334. killsock(sock);
  335. return 1;
  336. }
  337. sdprintf("dns_idx: %d", dns_idx);
  338. dcc[dns_idx].sock = sock;
  339. dns_sock = sock;
  340. sdprintf("dns_sock: %d", dcc[dns_idx].sock);
  341. strlcpy(dcc[dns_idx].host, dns_ip, sizeof(dcc[dns_idx].host));
  342. strlcpy(dcc[dns_idx].nick, "(adns)", sizeof(dcc[dns_idx].nick));
  343. sdprintf("dns_ip: %s", dns_ip);
  344. dcc[dns_idx].timeval = now;
  345. dns_handler.timeout_val = 0;
  346. return 0;
  347. }
  348. return 1;
  349. }
  350. void egg_dns_send(char *query, int len, bool blocking)
  351. {
  352. if (dns_idx >= 0 && dcc[dns_idx].sock == -1) {
  353. lostdcc(dns_idx);
  354. dns_idx = -1;
  355. }
  356. if (dns_idx < 0) {
  357. if (get_dns_idx()) {
  358. sdprintf("get_dns_idx() failed in egg_dns_send");
  359. return;
  360. }
  361. }
  362. if (!dns_handler.timeout_val) {
  363. dns_handler.timeout_val = &async_server_timeout;
  364. #ifdef DEBUG
  365. sdprintf("SETTING TIMEOUT to %d", async_server_timeout);
  366. #endif
  367. dcc[dns_idx].timeval = now;
  368. }
  369. if (blocking)
  370. socket_set_nonblock(dcc[dns_idx].sock, 0);
  371. if (write(dcc[dns_idx].sock, query, len) == -1) {
  372. ;
  373. }
  374. // sockbuf_write(dns_idx, query, len);
  375. }
  376. dns_query_t *find_query(const char *host)
  377. {
  378. dns_query_t *q = NULL;
  379. for (q = query_head; q; q = q->next)
  380. if (!strcasecmp(q->query, host))
  381. return q;
  382. return NULL;
  383. }
  384. #define DEFAULT_DNS_TYPE (DNS_LOOKUP_A|DNS_LOOKUP_AAAA)
  385. static void dns_send_query(dns_query_t *q, int type = DEFAULT_DNS_TYPE, bool blocking = 0)
  386. {
  387. char buf[512] = "";
  388. int len;
  389. q->remaining = 0;
  390. if (!q->ip) {
  391. if (type & DNS_LOOKUP_A) {
  392. /* Send the ipv4 query. */
  393. q->remaining++;
  394. len = make_header(buf, q->id);
  395. len += cut_host(q->query, buf + len);
  396. buf[len] = 0; len++; buf[len] = DNS_A; len++;
  397. buf[len] = 0; len++; buf[len] = 1; len++;
  398. egg_dns_send(buf, len, blocking);
  399. }
  400. #ifdef USE_IPV6
  401. if (type & DNS_LOOKUP_AAAA) {
  402. /* Now send the ipv6 query. */
  403. q->remaining++;
  404. len = make_header(buf, q->id);
  405. len += cut_host(q->query, buf + len);
  406. buf[len] = 0; len++; buf[len] = DNS_AAAA; len++;
  407. buf[len] = 0; len++; buf[len] = 1; len++;
  408. egg_dns_send(buf, len, blocking);
  409. }
  410. #endif
  411. } else if (q->ip) {
  412. q->remaining++;
  413. len = make_header(buf, q->id);
  414. len += cut_host(q->ip, buf + len);
  415. buf[len] = 0; len++; buf[len] = DNS_PTR; len++;
  416. buf[len] = 0; len++; buf[len] = 1; len++;
  417. egg_dns_send(buf, len, blocking);
  418. }
  419. }
  420. static void dns_send_query_blocking(dns_query_t* q, int type = DEFAULT_DNS_TYPE) {
  421. dns_send_query(q, type, 1);
  422. }
  423. /*
  424. void dns_resend_queries()
  425. {
  426. dns_query_t *q = NULL;
  427. for (q = query_head; q; q = q->next) {
  428. if (now >= q->expiretime) {
  429. sdprintf("RESENDING: %s", q->query);
  430. dns_send_query(q);
  431. }
  432. }
  433. }
  434. */
  435. /*
  436. void dns_create_timeout_timer(dns_query_t **qm, const char *query, int timeout)
  437. {
  438. dns_query_t *q = *qm;
  439. egg_timeval_t howlong;
  440. howlong.sec = timeout;
  441. howlong.usec = 0;
  442. q->timer_id = timer_create_complex(&howlong, query, (Function) async_timeout, (void *) q->id, 0);
  443. }
  444. */
  445. /* Perform an async dns lookup. This is host -> ip. For ip -> host, use
  446. * egg_dns_reverse(). We return a dns id that you can use to cancel the
  447. * lookup. */
  448. int egg_dns_lookup(const char *host, interval_t timeout, dns_callback_t callback, void *client_data, int type)
  449. {
  450. dns_query_t *q = NULL;
  451. int i, cache_id;
  452. if (is_dotted_ip(host)) {
  453. /* If it's already an ip, we're done. */
  454. bd::Array<bd::String> answer;
  455. sdprintf("egg_dns_lookup(%s, %d): Already an ip.", host, timeout);
  456. answer << host;
  457. callback(-1, client_data, host, answer);
  458. return(-1);
  459. }
  460. /* Ok, now see if it's in our host cache. */
  461. for (i = 0; i < nhosts; i++) {
  462. if (!strcasecmp(host, hosts[i].host)) {
  463. bd::Array<bd::String> answer;
  464. sdprintf("egg_dns_lookup(%s, %d): Found in hosts -> %s", host, timeout, hosts[i].ip);
  465. answer << hosts[i].ip;
  466. callback(-1, client_data, host, answer);
  467. return(-1);
  468. }
  469. }
  470. cache_id = cache_find(host);
  471. if (cache_id >= 0) {
  472. // cache[cache_id].answer->shuffle();
  473. sdprintf("egg_dns_lookup(%s, %d): Found in cache -> %s", host, timeout, cache[cache_id].answer->join(',').c_str());
  474. callback(-1, client_data, host, *(cache[cache_id].answer));
  475. return(-1);
  476. }
  477. /* check if the query was already made */
  478. if ((q = find_query(host))) {
  479. sdprintf("egg_dns_lookup(%s, %d): Already querying -> %d", host, timeout, q->id);
  480. return(-2);
  481. }
  482. /* Allocate our query struct. */
  483. q = alloc_query(client_data, callback, host);
  484. sdprintf("egg_dns_lookup(%s, %d) -> %d", host, timeout, q->id);
  485. dns_send_query(q, type);
  486. // /* setup a timer to detect dead ns */
  487. // dns_create_timeout_timer(&q, host, timeout);
  488. /* Send the ipv4 query. */
  489. return(q->id);
  490. }
  491. bd::Array<bd::String> dns_send_blocking(dns_query_t* q, interval_t timeout, int type = DEFAULT_DNS_TYPE) {
  492. bd::Array<bd::String> answer;
  493. dns_send_query_blocking(q, type);
  494. char buf[512] = "";
  495. int atr;
  496. bool read_error = 0;
  497. /* Now the key difference, call read() on the socket to block until it returns */
  498. if (!setjmp(alarmret)) {
  499. read_more:
  500. alarm(timeout);
  501. dns_read(dns_idx, buf, atr, 1);
  502. read_error = errno ? 1 : 0;
  503. alarm(0);
  504. if (!read_error) {
  505. dcc[dns_idx].timeval = now;
  506. if (parse_reply(buf, atr, dns_ip, 1)) {
  507. dns_on_eof(dns_idx);
  508. } else if (errno == EAGAIN) {
  509. goto read_more;
  510. } else if (q->answer) {
  511. answer = *(q->answer);
  512. }
  513. }
  514. }
  515. if (q) {
  516. dns_query_t *prev = NULL, *cur = NULL;
  517. for (cur = query_head; cur; cur = cur->next) {
  518. if (cur->id == q->id) break;
  519. prev = cur;
  520. }
  521. if (cur) {
  522. if (prev) prev->next = cur->next;
  523. else query_head = cur->next;
  524. }
  525. free_query(q);
  526. }
  527. // Disable blocking on the dns socket.
  528. socket_set_nonblock(dcc[dns_idx].sock, 1);
  529. return answer;
  530. }
  531. bd::Array<bd::String> dns_blocking_loop(const char* what, interval_t timeout, const char* from, int type = DEFAULT_DNS_TYPE, bool is_ip = 0) {
  532. /* Allocate our query struct. */
  533. dns_query_t *q = NULL;
  534. bd::Array<bd::String> answer;
  535. static int cnt = 0;
  536. try_again:
  537. q = alloc_query(NULL, NULL, what);
  538. if (is_ip)
  539. query_transform_ip(q, what);
  540. sdprintf("%s(%s, %d) -> %d", from, what, timeout, q->id);
  541. if (cnt++ < nservers) {
  542. answer = dns_send_blocking(q, timeout, type);
  543. // No answers received?? Blocking DNS demands an answer, check the next server.
  544. if (answer.size() == 0 && cnt < nservers) {
  545. dns_on_eof(dns_idx);
  546. goto try_again;
  547. }
  548. }
  549. cnt = 0;
  550. return answer;
  551. }
  552. bd::Array<bd::String> dns_lookup_block(const char *host, interval_t timeout, int type)
  553. {
  554. if (is_dotted_ip(host)) {
  555. bd::Array<bd::String> answer;
  556. /* If it's already an ip, we're done. */
  557. sdprintf("dns_lookup_block(%s, %d): Already an ip.", host, timeout);
  558. answer << host;
  559. return answer;
  560. }
  561. /* Ok, now see if it's in our host cache. */
  562. for (int i = 0; i < nhosts; i++) {
  563. if (!strcasecmp(host, hosts[i].host)) {
  564. bd::Array<bd::String> answer;
  565. sdprintf("dns_lookup_block(%s, %d): Found in hosts -> %s", host, timeout, hosts[i].ip);
  566. answer << hosts[i].ip;
  567. return answer;
  568. }
  569. }
  570. int cache_id = cache_find(host);
  571. if (cache_id >= 0) {
  572. // cache[cache_id].answer->shuffle();
  573. sdprintf("dns_lookup_block(%s, %d): Found in cache -> %s", host, timeout, cache[cache_id].answer->join(',').c_str());
  574. return *(cache[cache_id].answer);
  575. }
  576. return dns_blocking_loop(host, timeout, "dns_lookup_block", type);
  577. }
  578. /* Perform a blocking dns reverse lookup. This does ip -> host. For host -> ip
  579. * use egg_dns_lookup(). */
  580. bd::Array<bd::String> dns_reverse_block(const char *ip, interval_t timeout)
  581. {
  582. if (!is_dotted_ip(ip)) {
  583. bd::Array<bd::String> answer;
  584. /* If it's not a valid ip, don't even make the request. */
  585. sdprintf("dns_reverse_block(%s, %d): Not an ip.", ip, timeout);
  586. answer << ip;
  587. return answer;
  588. }
  589. /* Ok, see if we have it in our host cache. */
  590. for (int i = 0; i < nhosts; i++) {
  591. if (!strcasecmp(hosts[i].ip, ip)) {
  592. bd::Array<bd::String> answer;
  593. sdprintf("dns_reverse_block(%s, %d): Found in hosts -> %s", ip, timeout, hosts[i].host);
  594. answer << hosts[i].host;
  595. return answer;
  596. }
  597. }
  598. int cache_id = cache_find(ip);
  599. if (cache_id >= 0) {
  600. // cache[cache_id].answer->shuffle();
  601. sdprintf("dns_reverse_block(%s, %d): Found in cache -> %s", ip, timeout, cache[cache_id].answer->join(',').c_str());
  602. return *(cache[cache_id].answer);
  603. }
  604. return dns_blocking_loop(ip, timeout, "dns_reverse_block", 0, 1);
  605. }
  606. /* Perform an async dns reverse lookup. This does ip -> host. For host -> ip
  607. * use egg_dns_lookup(). We return a dns id that you can use to cancel the
  608. * lookup. */
  609. int egg_dns_reverse(const char *ip, interval_t timeout, dns_callback_t callback, void *client_data)
  610. {
  611. dns_query_t *q;
  612. int i, cache_id;
  613. if (!is_dotted_ip(ip)) {
  614. /* If it's not a valid ip, don't even make the request. */
  615. sdprintf("egg_dns_reverse(%s, %d): Not an ip.", ip, timeout);
  616. callback(-1, client_data, ip, NULL);
  617. return(-1);
  618. }
  619. /* Ok, see if we have it in our host cache. */
  620. for (i = 0; i < nhosts; i++) {
  621. if (!strcasecmp(hosts[i].ip, ip)) {
  622. bd::Array<bd::String> answer;
  623. sdprintf("egg_dns_reverse(%s, %d): Found in hosts -> %s", ip, timeout, hosts[i].host);
  624. answer << hosts[i].host;
  625. callback(-1, client_data, ip, answer);
  626. return(-1);
  627. }
  628. }
  629. cache_id = cache_find(ip);
  630. if (cache_id >= 0) {
  631. // cache[cache_id].answer->shuffle();
  632. sdprintf("egg_dns_reverse(%s, %d): Found in cache -> %s", ip, timeout, cache[cache_id].answer->join(',').c_str());
  633. callback(-1, client_data, ip, *(cache[cache_id].answer));
  634. return(-1);
  635. }
  636. /* check if the query was already made */
  637. if ((q = find_query(ip))) {
  638. sdprintf("egg_dns_reverse(%s, %d): Already querying -> %d", ip, timeout, q->id);
  639. return(-1);
  640. }
  641. q = alloc_query(client_data, callback, ip);
  642. sdprintf("egg_dns_reverse(%s, %d) -> %d", ip, timeout, q->id);
  643. query_transform_ip(q, ip);
  644. dns_send_query(q);
  645. // /* setup timer to detect dead ns */
  646. // dns_create_timeout_timer(&q, ip, timeout);
  647. return(q->id);
  648. }
  649. static void dns_read(int idx, char* buf, int& atr, bool blocking) {
  650. atr = read(dcc[idx].sock, buf, 512);
  651. if (atr == -1) {
  652. if (errno == EAGAIN) {
  653. do {
  654. atr = read(dcc[idx].sock, buf, 512);
  655. } while (errno == EAGAIN && blocking);
  656. }
  657. if (atr == -1) {
  658. dns_on_eof(idx);
  659. errno = EPIPE;
  660. return;
  661. }
  662. }
  663. #ifdef DEBUG
  664. sdprintf("SETTING TIMEOUT to 0: Received reply.");
  665. #endif
  666. dns_handler.timeout_val = 0;
  667. errno = 0;
  668. return;
  669. }
  670. static void dns_on_read(int idx, char* buf, int atr)
  671. {
  672. dcc[idx].timeval = now;
  673. // if (resend_on_read) {
  674. // resend_on_read = 0;
  675. // dns_resend_queries();
  676. // return;
  677. // }
  678. dns_read(idx, buf, atr);
  679. if (parse_reply(buf, atr, dns_ip))
  680. dns_on_eof(idx);
  681. return;
  682. }
  683. static void dns_on_eof(int idx)
  684. {
  685. sdprintf("EOF on dns idx: %d sock: %d (%s)", idx, dcc[idx].sock, dcc[idx].host);
  686. dns_reinit(idx);
  687. return;
  688. }
  689. /* for .restart
  690. int egg_dns_shutdown(void)
  691. {
  692. int i;
  693. if (nservers > 0) {
  694. for (i = 0; i < nservers; i++) {
  695. if (servers[i].ip) free(servers[i].ip);
  696. }
  697. free(servers); servers = NULL;
  698. nservers = 0;
  699. }
  700. if (nhosts > 0) {
  701. for (i = 0; i < nhosts; i++) {
  702. if (hosts[i].host) free(hosts[i].host);
  703. if (hosts[i].ip) free(hosts[i].ip);
  704. }
  705. free(hosts); hosts = NULL;
  706. nhosts = 0;
  707. }
  708. return (0);
  709. }
  710. */
  711. static const char *dns_next_server()
  712. {
  713. if (!servers || nservers < 1) return("127.0.0.1");
  714. cur_server++;
  715. if (cur_server >= nservers) cur_server = 0;
  716. return(servers[cur_server].ip);
  717. }
  718. static void add_dns_server(char *ip)
  719. {
  720. servers = (dns_server_t *) my_realloc(servers, (nservers+1)*sizeof(*servers));
  721. servers[nservers].ip = strdup(ip);
  722. nservers++;
  723. sdprintf("Added NS: %s", ip);
  724. }
  725. static void add_host(char *host, char *ip)
  726. {
  727. hosts = (dns_host_t *) my_realloc(hosts, (nhosts+1)*sizeof(*hosts));
  728. hosts[nhosts].host = strdup(host);
  729. hosts[nhosts].ip = strdup(ip);
  730. nhosts++;
  731. }
  732. static int cache_expired(int id)
  733. {
  734. if (cache[id].expiretime && (now >= cache[id].expiretime)) return(1);
  735. return (0);
  736. }
  737. static void cache_del(int id)
  738. {
  739. delete cache[id].answer;
  740. free(cache[id].query);
  741. cache[id].expiretime = 0;
  742. ncache--;
  743. if (id < ncache) memcpy(&cache[id], &cache[ncache], sizeof(dns_cache_t));
  744. else bzero(&cache[id], sizeof(dns_cache_t));
  745. cache = (dns_cache_t *) my_realloc(cache, (ncache+1)*sizeof(*cache));
  746. }
  747. static void cache_add(const char *query, bd::Array<bd::String> answer, int ttl)
  748. {
  749. cache = (dns_cache_t *) my_realloc(cache, (ncache+1)*sizeof(*cache));
  750. bzero(&cache[ncache], sizeof(cache[ncache]));
  751. cache[ncache].query = strdup(query);
  752. cache[ncache].answer = new bd::Array<bd::String>;
  753. *(cache[ncache].answer) = answer;
  754. cache[ncache].expiretime = now + ttl;
  755. ncache++;
  756. }
  757. static int cache_find(const char *query)
  758. {
  759. int i;
  760. for (i = 0; i < ncache; i++)
  761. if (!strcasecmp(cache[i].query, query)) return (i);
  762. return (-1);
  763. }
  764. void dns_cache_flush()
  765. {
  766. int i = 0;
  767. for (i = 0; i < ncache; i++) {
  768. cache_del(i);
  769. if (i == ncache) break;
  770. i--;
  771. }
  772. }
  773. static int read_thing(char *buf, char *ip)
  774. {
  775. int skip, len;
  776. skip = strspn(buf, separators);
  777. buf += skip;
  778. len = strcspn(buf, separators);
  779. memcpy(ip, buf, len);
  780. ip[len] = 0;
  781. return(skip + len);
  782. }
  783. static int read_resolv(char *fname)
  784. {
  785. FILE *fp;
  786. char buf[512], ip[512];
  787. int count = 0;
  788. fp = fopen(fname, "r");
  789. if (!fp) return 0;
  790. while (fgets(buf, sizeof(buf), fp)) {
  791. if (!strncasecmp(buf, "nameserver", 10)) {
  792. read_thing(buf+10, ip);
  793. if (strlen(ip)) {
  794. add_dns_server(ip);
  795. ++count;
  796. }
  797. }
  798. }
  799. fclose(fp);
  800. return count;
  801. }
  802. static void read_hosts(char *fname)
  803. {
  804. FILE *fp;
  805. char buf[512], ip[512], host[512];
  806. int skip, n;
  807. fp = fopen(fname, "r");
  808. if (!fp) return;
  809. while (fgets(buf, sizeof(buf), fp)) {
  810. if (strchr(buf, '#')) continue;
  811. skip = read_thing(buf, ip);
  812. if (!strlen(ip)) continue;
  813. while ((n = read_thing(buf+skip, host))) {
  814. skip += n;
  815. if (strlen(host)) add_host(host, ip);
  816. }
  817. }
  818. fclose(fp);
  819. }
  820. static int make_header(char *buf, int id)
  821. {
  822. _dns_header.question_count = htons(1);
  823. // _dns_header.id = htons(id);
  824. _dns_header.id = id;
  825. memcpy(buf, &_dns_header, HEAD_SIZE);
  826. return(HEAD_SIZE);
  827. }
  828. static int cut_host(const char *host, char *query)
  829. {
  830. const char *period = NULL, *orig = NULL;
  831. int len;
  832. orig = query;
  833. while ((period = strchr(host, '.'))) {
  834. len = period - host;
  835. if (len > 63) return(-1);
  836. *query++ = len;
  837. memcpy(query, host, len);
  838. query += len;
  839. host = period+1;
  840. }
  841. len = strlen(host);
  842. if (len) {
  843. *query++ = len;
  844. memcpy(query, host, len);
  845. query += len;
  846. }
  847. *query++ = 0;
  848. return(query-orig);
  849. }
  850. int reverse_ip(const char *host, char *reverse)
  851. {
  852. const char *period = NULL;
  853. int offset, len;
  854. period = strchr(host, '.');
  855. if (!period) {
  856. len = strlen(host);
  857. memcpy(reverse, host, len);
  858. return(len);
  859. }
  860. else {
  861. len = period - host;
  862. offset = reverse_ip(host+len+1, reverse);
  863. reverse[offset++] = '.';
  864. memcpy(reverse+offset, host, len);
  865. reverse[offset+len] = 0;
  866. return(offset+len);
  867. }
  868. }
  869. int egg_dns_cancel(int id, int issue_callback)
  870. {
  871. dns_query_t *q, *prev = NULL;
  872. for (q = query_head; q; q = q->next) {
  873. if (q->id == id) break;
  874. prev = q;
  875. }
  876. if (!q) return(-1);
  877. if (prev) prev->next = q->next;
  878. else query_head = q->next;
  879. sdprintf("Cancelling query: %s", q->query);
  880. if (issue_callback && q->callback) {
  881. if (q->answer->size() > 0) {
  882. cache_add(q->query, *(q->answer), q->lowest_ttl);
  883. q->callback(q->id, q->client_data, q->query, *(q->answer));
  884. } else {
  885. bd::Array<bd::String> empty;
  886. q->callback(q->id, q->client_data, q->query, empty);
  887. }
  888. }
  889. free_query(q);
  890. return(0);
  891. }
  892. static int skip_name(unsigned char *ptr)
  893. {
  894. int len;
  895. unsigned char *start = ptr;
  896. while ((len = *ptr++) > 0) {
  897. if (len > 63) {
  898. ptr++;
  899. break;
  900. }
  901. else {
  902. ptr += len;
  903. }
  904. }
  905. return(ptr - start);
  906. }
  907. /*
  908. void print_header(dns_header_t &header)
  909. {
  910. #define dofield(_field) sdprintf("%s: %d\n", #_field, _field)
  911. dofield(header.id);
  912. dofield(header.question_count);
  913. dofield(header.answer_count);
  914. dofield(header.ar_count);
  915. dofield(header.ns_count);
  916. #undef dofield
  917. }
  918. void print_reply(dns_rr_t &reply)
  919. {
  920. #define dofield(_field) sdprintf("%s: %d\n", #_field, _field)
  921. dofield(reply.type);
  922. dofield(reply.dclass);
  923. dofield(reply.ttl);
  924. dofield(reply.rdlength);
  925. #undef dofield
  926. }
  927. */
  928. static int parse_reply(char *response, size_t nbytes, const char* server_ip, bool blocking)
  929. {
  930. dns_header_t header;
  931. dns_query_t *q = NULL, *prev = NULL;
  932. char result[512] = "";
  933. short rr;
  934. dns_rr_t reply;
  935. unsigned const char *eop = (unsigned char *) response + nbytes;
  936. unsigned char *ptr = (unsigned char *) response;
  937. int return_code = 0;
  938. memcpy(&header, ptr, HEAD_SIZE);
  939. ptr += HEAD_SIZE;
  940. /* header.id is already in our order, echoed by the server */
  941. header.flags = ntohs(header.flags);
  942. header.question_count = ntohs(header.question_count);
  943. header.answer_count = ntohs(header.answer_count);
  944. header.ar_count = ntohs(header.ar_count);
  945. header.ns_count = ntohs(header.ns_count);
  946. // print_header(header);
  947. /* Find our copy of the query before proceeding. */
  948. for (q = query_head; q; q = q->next) {
  949. if (q->id == header.id) break;
  950. prev = q;
  951. }
  952. sdprintf("Reply(%d) questions: %d answers: %d ar: %d ns: %d from: %s QR: %d OPCODE: %d AA: %d TC: %d RD: %d RA: %d RCODE: %d",
  953. header.id,
  954. header.question_count,
  955. header.answer_count,
  956. header.ar_count,
  957. header.ns_count,
  958. server_ip,
  959. GET_QR(header.flags),
  960. GET_OPCODE(header.flags),
  961. GET_AA(header.flags),
  962. GET_TC(header.flags),
  963. GET_RD(header.flags),
  964. GET_RA(header.flags),
  965. GET_RCODE(header.flags)
  966. );
  967. if (!q) {
  968. sdprintf("Reply(%d) not found??", header.id);
  969. return 0;
  970. }
  971. /* Did this server give us recursion? */
  972. if (!GET_RA(header.flags)) {
  973. sdprintf("Ignoring reply(%d) from %s: no recusion available.", header.id, server_ip);
  974. return_code = 1; /* get a new server */
  975. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  976. errno = EACCES;
  977. goto callback;
  978. }
  979. /* Check for errors */
  980. if (GET_RCODE(header.flags)) {
  981. switch (GET_RCODE(header.flags)) {
  982. case 1: /* Format error */
  983. sdprintf("Ignoring reply(%d) from %s: Format error.", header.id, server_ip);
  984. errno = EINVAL;
  985. break;
  986. case 2: /* Server error */
  987. sdprintf("Ignoring reply(%d) from %s: Server error.", header.id, server_ip);
  988. errno = EINVAL;
  989. return_code = 1; /* get a new server */
  990. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  991. break;
  992. case 3: /* Name error */
  993. sdprintf("Ignoring reply(%d) from %s: NXDOMAIN.", header.id, server_ip);
  994. /* Ignore the incoming AAAA or A reply as it will still be NXDOMAIN */
  995. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  996. errno = ENOENT;
  997. break;
  998. case 4:
  999. sdprintf("Ignoring reply(%d) from %s: Query not supported", header.id, server_ip);
  1000. errno = EINVAL;
  1001. break;
  1002. case 5:
  1003. sdprintf("Ignoring reply(%d) from %s: REFUSED", header.id, server_ip);
  1004. return_code = 1; /* get a new server */
  1005. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1006. errno = EACCES;
  1007. break;
  1008. }
  1009. goto callback;
  1010. }
  1011. // /* destroy our async timeout */
  1012. // timer_destroy(q->timer_id);
  1013. /* Pass over the questions. */
  1014. for (rr = 0; rr < header.question_count; rr++) {
  1015. ptr += skip_name(ptr);
  1016. ptr += 4;
  1017. }
  1018. /* End of questions. */
  1019. // for (rr = 0; rr < header.answer_count + header.ar_count + header.ns_count; rr++) {
  1020. q->answers += header.answer_count;
  1021. for (rr = 0; rr < header.answer_count; rr++) {
  1022. result[0] = 0;
  1023. /* Read in the answer. */
  1024. ptr += skip_name(ptr);
  1025. memcpy(&reply, ptr, RR_SIZE);
  1026. ptr += RR_SIZE;
  1027. reply.type = ntohs(reply.type);
  1028. reply.dclass = ntohs(reply.dclass);
  1029. reply.rdlength = ntohs(reply.rdlength);
  1030. reply.ttl = ntohl(reply.ttl);
  1031. /* Save the lowest ttl */
  1032. if (reply.ttl && ((!q->lowest_ttl) || (q->lowest_ttl > reply.ttl))) q->lowest_ttl = reply.ttl;
  1033. // print_reply(reply);
  1034. switch (reply.type) {
  1035. case DNS_A:
  1036. inet_ntop(AF_INET, ptr, result, 512);
  1037. *(q->answer) << result;
  1038. sdprintf("Reply(%d): %s. \t %d \t IN A \t %s", header.id, q->query, reply.ttl, result);
  1039. break;
  1040. case DNS_AAAA:
  1041. #ifdef USE_IPV6
  1042. inet_ntop(AF_INET6, ptr, result, 512);
  1043. *(q->answer) << result;
  1044. sdprintf("Reply(%d): %s. \t %d \t IN AAAA \t %s", header.id, q->query, reply.ttl, result);
  1045. #endif /* USE_IPV6 */
  1046. break;
  1047. case DNS_PTR:
  1048. if ((my_dn_expand((const unsigned char *) response, eop, ptr, result, sizeof(result)) != -1) && result[0]) {
  1049. *(q->answer) << result;
  1050. sdprintf("Reply(%d): %s. \t %d \t IN PTR \t %s", header.id, q->query, reply.ttl, result);
  1051. }
  1052. break;
  1053. default:
  1054. sdprintf("Unhandled DNS reply type: %d", reply.type);
  1055. break;
  1056. }
  1057. ptr += reply.rdlength;
  1058. if ((size_t) (ptr - (unsigned char*) response) > nbytes) {
  1059. sdprintf("MALFORMED/TRUNCATED DNS PACKET detected (need TCP).");
  1060. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1061. break;
  1062. }
  1063. }
  1064. callback:
  1065. /* Don't continue if we haven't gotten all expected replies. */
  1066. if (--q->remaining > 0) {
  1067. sdprintf("Waiting on query(%d) for more replies.", header.id);
  1068. errno = EAGAIN;
  1069. return 0;
  1070. }
  1071. errno = 0;
  1072. /* Ok, we have, so now issue the callback with the answers. */
  1073. if (prev) prev->next = q->next;
  1074. else query_head = q->next;
  1075. if (q->answer->size() > 0) {
  1076. cache_add(q->query, *(q->answer), q->lowest_ttl);
  1077. if (q->callback)
  1078. q->callback(q->id, q->client_data, q->query, *(q->answer));
  1079. } else if (q->callback) {
  1080. bd::Array<bd::String> empty;
  1081. q->callback(q->id, q->client_data, q->query, empty);
  1082. }
  1083. if (!blocking)
  1084. free_query(q);
  1085. return return_code;
  1086. }
  1087. void tell_dnsdebug(int idx)
  1088. {
  1089. dns_query_t *q = NULL;
  1090. dprintf(idx, "NS: %s\n", dns_ip);
  1091. for (q = query_head; q; q = q->next)
  1092. dprintf(idx, "DNS (%d) (%ds): %s\n", q->id, (int) (q->expiretime - now), q->query);
  1093. // for (i = 0; i < nhosts; i++)
  1094. // dprintf(idx, "HOST #%d: %s/%s\n", i, hosts[i].host, hosts[i].ip);
  1095. for (int i = 0; i < ncache; i++) {
  1096. dprintf(idx, "cache(%d) %s expires in %ds\n", i, cache[i].query, (int) (cache[i].expiretime - now));
  1097. for (size_t n = 0; n < cache[i].answer->size(); n++)
  1098. dprintf(idx, "%zu: %s\n", n, bd::String((*(cache[i].answer))[n]).c_str());
  1099. }
  1100. }
  1101. static void expire_queries()
  1102. {
  1103. dns_query_t *q = NULL, *next = NULL;
  1104. int i = 0;
  1105. /* need to check for expired queries and either:
  1106. a) recheck/change ns
  1107. b) expire due to ttl
  1108. */
  1109. if (query_head) {
  1110. for (q = query_head; q; q = q->next) {
  1111. if (q->expiretime <= now) { /* set in alloc_query */
  1112. if (q->next)
  1113. next = q->next;
  1114. egg_dns_cancel(q->id, 1);
  1115. if (!next) break;
  1116. q = next;
  1117. }
  1118. }
  1119. }
  1120. for (i = 0; i < ncache; i++) {
  1121. if (cache_expired(i)) {
  1122. cache_del(i);
  1123. if (i == ncache) break;
  1124. i--;
  1125. }
  1126. }
  1127. }
  1128. /* Read in .hosts and /etc/hosts and .resolv.conf and /etc/resolv.conf */
  1129. int egg_dns_init()
  1130. {
  1131. /* Set RECURSION DESIRED */
  1132. SET_RD(_dns_header.flags);
  1133. /* Convert flags to network order */
  1134. _dns_header.flags = htons(_dns_header.flags);
  1135. if (!read_resolv(".resolv.conf")) {
  1136. read_resolv("/etc/resolv.conf");
  1137. /* some backup servers, probably will never be used. */
  1138. add_dns_server("4.2.2.2");
  1139. add_dns_server("8.8.8.8");
  1140. add_dns_server("8.8.4.4");
  1141. }
  1142. read_hosts("/etc/hosts");
  1143. read_hosts(".hosts");
  1144. /* root servers for future development (tracing down)
  1145. add_dns_server("198.41.0.4");
  1146. add_dns_server("192.228.79.201");
  1147. add_dns_server("192.33.4.12");
  1148. add_dns_server("128.8.10.90");
  1149. add_dns_server("192.203.230.10");
  1150. add_dns_server("192.5.5.241");
  1151. add_dns_server("192.112.36.4");
  1152. add_dns_server("128.63.2.53");
  1153. add_dns_server("192.36.148.17");
  1154. add_dns_server("192.58.128.30");
  1155. add_dns_server("193.0.14.129");
  1156. add_dns_server("198.32.64.12");
  1157. add_dns_server("202.12.27.33");
  1158. */
  1159. timer_create_secs(3, "adns_check_expires", (Function) expire_queries);
  1160. return(0);
  1161. }
  1162. bool valid_dns_id(int idx, int id)
  1163. {
  1164. if (id == -1)
  1165. return 1;
  1166. if (valid_idx(idx) && dcc[idx].dns_id && dcc[idx].dns_id == id)
  1167. return 1;
  1168. sdprintf("dns_id: %d is not associated with dead idx: %d", id, idx);
  1169. return 0;
  1170. }
  1171. bd::String dns_find_ip(bd::Array<bd::String> ips, int af_type) {
  1172. for (size_t i = 0; i < ips.size(); ++i) {
  1173. if (is_dotted_ip(bd::String(ips[i]).c_str()) == af_type) {
  1174. return ips[i];
  1175. }
  1176. }
  1177. return bd::String();
  1178. }
  1179. /* vim: set sts=4 sw=4 ts=4 noet: */