adns.c 35 KB

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