adns.cc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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. free(p->cbuf);
  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. free(q->ip);
  253. delete q->answer;
  254. free(q->query);
  255. free(q);
  256. q = NULL;
  257. }
  258. static int get_dns_id() {
  259. while (1) {
  260. int id = randint(65534);
  261. bool found = 0;
  262. // Make sure this ID is not already in use, avoid a race condition
  263. for (dns_query_t *q = query_head; q; q = q->next) {
  264. if (q->id == id) {
  265. found = 1;
  266. break;
  267. }
  268. }
  269. if (found) continue;
  270. return id;
  271. }
  272. }
  273. static dns_query_t *alloc_query(void *client_data, dns_callback_t callback, const char *query)
  274. {
  275. dns_query_t *q = (dns_query_t *) my_calloc(1, sizeof(*q));
  276. q->id = get_dns_id();
  277. q->query = strdup(query);
  278. q->answers = 0;
  279. q->answer = new bd::Array<bd::String>;
  280. q->lowest_ttl = 0;
  281. q->callback = callback;
  282. q->client_data = client_data;
  283. q->expiretime = now + async_lookup_timeout;
  284. q->next = query_head;
  285. query_head = q;
  286. return q;
  287. }
  288. static void query_transform_ip(dns_query_t* q, const char* ip) {
  289. /* We need to transform the ip address into the proper form
  290. * for reverse lookup. */
  291. if (strchr(ip, ':')) {
  292. char temp[128] = "";
  293. socket_ipv6_to_dots(ip, temp);
  294. size_t iplen = strlen(temp) + 9 + 1;
  295. q->ip = (char *) my_calloc(1, iplen);
  296. // reverse_ip(temp, q->ip);
  297. strlcat(q->ip, temp, iplen);
  298. strlcat(q->ip, "ip6.arpa", iplen);
  299. }
  300. else {
  301. size_t iplen = strlen(ip) + 13 + 1;
  302. q->ip = (char *) my_calloc(1, iplen);
  303. reverse_ip(ip, q->ip);
  304. strlcat(q->ip, ".in-addr.arpa", iplen);
  305. }
  306. }
  307. static int get_dns_idx()
  308. {
  309. int i, sock;
  310. sock = -1;
  311. for (i = 0; i < nservers; i++) {
  312. if (!dns_ip) dns_ip = dns_next_server();
  313. sock = socket_create(dns_ip, DNS_PORT, NULL, 0, SOCKET_CLIENT | SOCKET_NONBLOCK | SOCKET_UDP);
  314. if (sock < 0) {
  315. /* Try the next server. */
  316. dns_ip = NULL;
  317. }
  318. else break;
  319. }
  320. if (i == nservers) return 1;
  321. // dns_idx = sockbuf_new();
  322. // sockbuf_set_handler(dns_idx, &dns_handler, NULL);
  323. // sockbuf_set_sock(dns_idx, sock, 0);
  324. // allocsock(sock, SOCK_CONNECT);
  325. if (sock >= 0 && dns_ip) {
  326. dns_idx = new_dcc(&dns_handler, 0);
  327. if (dns_idx < 0) {
  328. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create dns connection.");
  329. killsock(sock);
  330. return 1;
  331. }
  332. sdprintf("dns_idx: %d", dns_idx);
  333. dcc[dns_idx].sock = sock;
  334. dns_sock = sock;
  335. sdprintf("dns_sock: %d", dcc[dns_idx].sock);
  336. strlcpy(dcc[dns_idx].host, dns_ip, sizeof(dcc[dns_idx].host));
  337. strlcpy(dcc[dns_idx].nick, "(adns)", sizeof(dcc[dns_idx].nick));
  338. sdprintf("dns_ip: %s", dns_ip);
  339. dcc[dns_idx].timeval = now;
  340. dns_handler.timeout_val = 0;
  341. return 0;
  342. }
  343. return 1;
  344. }
  345. static void egg_dns_send(char *query, int len, bool blocking)
  346. {
  347. if (dns_idx >= 0 && dcc[dns_idx].sock == -1) {
  348. lostdcc(dns_idx);
  349. dns_idx = -1;
  350. }
  351. if (dns_idx < 0) {
  352. if (get_dns_idx()) {
  353. sdprintf("get_dns_idx() failed in egg_dns_send");
  354. return;
  355. }
  356. }
  357. if (!dns_handler.timeout_val) {
  358. dns_handler.timeout_val = &async_server_timeout;
  359. #ifdef DEBUG
  360. sdprintf("SETTING TIMEOUT to %d", async_server_timeout);
  361. #endif
  362. dcc[dns_idx].timeval = now;
  363. }
  364. if (blocking)
  365. socket_set_nonblock(dcc[dns_idx].sock, 0);
  366. if (write(dcc[dns_idx].sock, query, len) == -1) {
  367. ;
  368. }
  369. // sockbuf_write(dns_idx, query, len);
  370. }
  371. dns_query_t *find_query(const char *host)
  372. {
  373. dns_query_t *q = NULL;
  374. for (q = query_head; q; q = q->next)
  375. if (!strcasecmp(q->query, host))
  376. return q;
  377. return NULL;
  378. }
  379. #define DEFAULT_DNS_TYPE (DNS_LOOKUP_A|DNS_LOOKUP_AAAA)
  380. static void dns_send_query(dns_query_t *q, int type = DEFAULT_DNS_TYPE, bool blocking = 0)
  381. {
  382. char buf[512] = "";
  383. int len;
  384. q->remaining = 0;
  385. if (!q->ip) {
  386. if (type & DNS_LOOKUP_A) {
  387. /* Send the ipv4 query. */
  388. q->remaining++;
  389. len = make_header(buf, q->id);
  390. len += cut_host(q->query, buf + len);
  391. buf[len] = 0; len++; buf[len] = DNS_A; len++;
  392. buf[len] = 0; len++; buf[len] = 1; len++;
  393. egg_dns_send(buf, len, blocking);
  394. }
  395. #ifdef USE_IPV6
  396. if (type & DNS_LOOKUP_AAAA) {
  397. /* Now send the ipv6 query. */
  398. q->remaining++;
  399. len = make_header(buf, q->id);
  400. len += cut_host(q->query, buf + len);
  401. buf[len] = 0; len++; buf[len] = DNS_AAAA; len++;
  402. buf[len] = 0; len++; buf[len] = 1; len++;
  403. egg_dns_send(buf, len, blocking);
  404. }
  405. #endif
  406. } else if (q->ip) {
  407. q->remaining++;
  408. len = make_header(buf, q->id);
  409. len += cut_host(q->ip, buf + len);
  410. buf[len] = 0; len++; buf[len] = DNS_PTR; len++;
  411. buf[len] = 0; len++; buf[len] = 1; len++;
  412. egg_dns_send(buf, len, blocking);
  413. }
  414. }
  415. static void dns_send_query_blocking(dns_query_t* q, int type = DEFAULT_DNS_TYPE) {
  416. dns_send_query(q, type, 1);
  417. }
  418. /*
  419. void dns_resend_queries()
  420. {
  421. dns_query_t *q = NULL;
  422. for (q = query_head; q; q = q->next) {
  423. if (now >= q->expiretime) {
  424. sdprintf("RESENDING: %s", q->query);
  425. dns_send_query(q);
  426. }
  427. }
  428. }
  429. */
  430. /*
  431. void dns_create_timeout_timer(dns_query_t **qm, const char *query, int timeout)
  432. {
  433. dns_query_t *q = *qm;
  434. egg_timeval_t howlong;
  435. howlong.sec = timeout;
  436. howlong.usec = 0;
  437. q->timer_id = timer_create_complex(&howlong, query, (Function) async_timeout, (void *) q->id, 0);
  438. }
  439. */
  440. /* Perform an async dns lookup. This is host -> ip. For ip -> host, use
  441. * egg_dns_reverse(). We return a dns id that you can use to cancel the
  442. * lookup. */
  443. int egg_dns_lookup(const char *host, interval_t timeout, dns_callback_t callback, void *client_data, int type)
  444. {
  445. dns_query_t *q = NULL;
  446. int i, cache_id;
  447. if (is_dotted_ip(host)) {
  448. /* If it's already an ip, we're done. */
  449. bd::Array<bd::String> answer;
  450. sdprintf("egg_dns_lookup(%s, %d): Already an ip.", host, timeout);
  451. answer << host;
  452. callback(-1, client_data, host, answer);
  453. return(-1);
  454. }
  455. /* Ok, now see if it's in our host cache. */
  456. for (i = 0; i < nhosts; i++) {
  457. if (!strcasecmp(host, hosts[i].host)) {
  458. bd::Array<bd::String> answer;
  459. sdprintf("egg_dns_lookup(%s, %d): Found in hosts -> %s", host, timeout, hosts[i].ip);
  460. answer << hosts[i].ip;
  461. callback(-1, client_data, host, answer);
  462. return(-1);
  463. }
  464. }
  465. cache_id = cache_find(host);
  466. if (cache_id >= 0) {
  467. // cache[cache_id].answer->shuffle();
  468. sdprintf("egg_dns_lookup(%s, %d): Found in cache -> %s", host, timeout, cache[cache_id].answer->join(',').c_str());
  469. callback(-1, client_data, host, *(cache[cache_id].answer));
  470. return(-1);
  471. }
  472. /* check if the query was already made */
  473. if ((q = find_query(host))) {
  474. sdprintf("egg_dns_lookup(%s, %d): Already querying -> %d", host, timeout, q->id);
  475. return(-2);
  476. }
  477. /* Allocate our query struct. */
  478. q = alloc_query(client_data, callback, host);
  479. sdprintf("egg_dns_lookup(%s, %d) -> %d", host, timeout, q->id);
  480. dns_send_query(q, type);
  481. // /* setup a timer to detect dead ns */
  482. // dns_create_timeout_timer(&q, host, timeout);
  483. /* Send the ipv4 query. */
  484. return(q->id);
  485. }
  486. bd::Array<bd::String> dns_send_blocking(dns_query_t* q, interval_t timeout, int type = DEFAULT_DNS_TYPE) {
  487. bd::Array<bd::String> answer;
  488. dns_send_query_blocking(q, type);
  489. char buf[512] = "";
  490. int atr;
  491. bool read_error = 0;
  492. /* Now the key difference, call read() on the socket to block until it returns */
  493. if (!setjmp(alarmret)) {
  494. read_more:
  495. alarm(timeout);
  496. dns_read(dns_idx, buf, atr, 1);
  497. read_error = errno ? 1 : 0;
  498. alarm(0);
  499. if (!read_error) {
  500. dcc[dns_idx].timeval = now;
  501. if (parse_reply(buf, atr, dns_ip, 1)) {
  502. dns_on_eof(dns_idx);
  503. } else if (errno == EAGAIN) {
  504. goto read_more;
  505. } else if (q->answer) {
  506. answer = *(q->answer);
  507. }
  508. }
  509. }
  510. if (q) {
  511. dns_query_t *prev = NULL, *cur = NULL;
  512. for (cur = query_head; cur; cur = cur->next) {
  513. if (cur->id == q->id)
  514. break;
  515. prev = cur;
  516. }
  517. if (cur) {
  518. if (prev)
  519. prev->next = cur->next;
  520. else
  521. 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, {});
  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. #ifdef DEBUG
  662. sdprintf("SETTING TIMEOUT to 0: Received reply.");
  663. #endif
  664. dns_handler.timeout_val = 0;
  665. errno = 0;
  666. return;
  667. }
  668. static void dns_on_read(int idx, char* buf, int atr)
  669. {
  670. dcc[idx].timeval = now;
  671. // if (resend_on_read) {
  672. // resend_on_read = 0;
  673. // dns_resend_queries();
  674. // return;
  675. // }
  676. dns_read(idx, buf, atr);
  677. if (parse_reply(buf, atr, dns_ip))
  678. dns_on_eof(idx);
  679. return;
  680. }
  681. static void dns_on_eof(int idx)
  682. {
  683. sdprintf("EOF on dns idx: %d sock: %d (%s)", idx, dcc[idx].sock, dcc[idx].host);
  684. dns_reinit(idx);
  685. return;
  686. }
  687. /* for .restart
  688. int egg_dns_shutdown(void)
  689. {
  690. int i;
  691. if (nservers > 0) {
  692. for (i = 0; i < nservers; i++) {
  693. free(servers[i].ip);
  694. }
  695. free(servers);
  696. servers = NULL;
  697. nservers = 0;
  698. }
  699. if (nhosts > 0) {
  700. for (i = 0; i < nhosts; i++) {
  701. free(hosts[i].host);
  702. free(hosts[i].ip);
  703. }
  704. free(hosts);
  705. hosts = NULL;
  706. nhosts = 0;
  707. }
  708. return (0);
  709. }
  710. */
  711. static const char *dns_next_server()
  712. {
  713. if (!servers || nservers < 1)
  714. return("127.0.0.1");
  715. cur_server++;
  716. if (cur_server >= nservers)
  717. cur_server = 0;
  718. return (servers[cur_server].ip);
  719. }
  720. static void add_dns_server(char *ip)
  721. {
  722. servers = (dns_server_t *) my_realloc(servers, (nservers+1) *
  723. sizeof(*servers));
  724. servers[nservers].ip = strdup(ip);
  725. nservers++;
  726. sdprintf("Added NS: %s", ip);
  727. }
  728. static void add_host(char *host, char *ip)
  729. {
  730. hosts = (dns_host_t *) my_realloc(hosts, (nhosts+1) * sizeof(*hosts));
  731. hosts[nhosts].host = strdup(host);
  732. hosts[nhosts].ip = strdup(ip);
  733. nhosts++;
  734. }
  735. static int cache_expired(int id)
  736. {
  737. if (cache[id].expiretime && (now >= cache[id].expiretime))
  738. return(1);
  739. return (0);
  740. }
  741. static void cache_del(int id)
  742. {
  743. delete cache[id].answer;
  744. free(cache[id].query);
  745. cache[id].expiretime = 0;
  746. ncache--;
  747. if (id < ncache)
  748. memcpy(&cache[id], &cache[ncache], sizeof(dns_cache_t));
  749. else
  750. bzero(&cache[id], sizeof(dns_cache_t));
  751. cache = (dns_cache_t *) my_realloc(cache, (ncache+1) *
  752. sizeof(*cache));
  753. }
  754. static void cache_add(const char *query, bd::Array<bd::String> answer, int ttl)
  755. {
  756. cache = (dns_cache_t *) my_realloc(cache, (ncache+1) * sizeof(*cache));
  757. bzero(&cache[ncache], sizeof(cache[ncache]));
  758. cache[ncache].query = strdup(query);
  759. cache[ncache].answer = new bd::Array<bd::String>;
  760. *(cache[ncache].answer) = answer;
  761. cache[ncache].expiretime = now + ttl;
  762. ncache++;
  763. }
  764. static int cache_find(const char *query)
  765. {
  766. int i;
  767. for (i = 0; i < ncache; i++)
  768. if (!strcasecmp(cache[i].query, query))
  769. return (i);
  770. return (-1);
  771. }
  772. void dns_cache_flush()
  773. {
  774. int i = 0;
  775. for (i = 0; i < ncache; i++) {
  776. cache_del(i);
  777. if (i == ncache)
  778. break;
  779. i--;
  780. }
  781. }
  782. static int read_thing(char *buf, char *ip)
  783. {
  784. int skip, len;
  785. skip = strspn(buf, separators);
  786. buf += skip;
  787. len = strcspn(buf, separators);
  788. memcpy(ip, buf, len);
  789. ip[len] = 0;
  790. return (skip + len);
  791. }
  792. static int read_resolv(char *fname)
  793. {
  794. FILE *fp;
  795. char buf[512], ip[512];
  796. int count = 0;
  797. fp = fopen(fname, "r");
  798. if (!fp) return 0;
  799. while (fgets(buf, sizeof(buf), fp)) {
  800. if (!strncasecmp(buf, "nameserver", 10)) {
  801. read_thing(buf+10, ip);
  802. if (strlen(ip)) {
  803. add_dns_server(ip);
  804. ++count;
  805. }
  806. }
  807. }
  808. fclose(fp);
  809. return count;
  810. }
  811. static void read_hosts(char *fname)
  812. {
  813. FILE *fp;
  814. char buf[512], ip[512], host[512];
  815. int skip, n;
  816. fp = fopen(fname, "r");
  817. if (!fp) return;
  818. while (fgets(buf, sizeof(buf), fp)) {
  819. if (strchr(buf, '#')) continue;
  820. skip = read_thing(buf, ip);
  821. if (!strlen(ip)) continue;
  822. while ((n = read_thing(buf+skip, host))) {
  823. skip += n;
  824. if (strlen(host)) add_host(host, ip);
  825. }
  826. }
  827. fclose(fp);
  828. }
  829. static int make_header(char *buf, int id)
  830. {
  831. _dns_header.question_count = htons(1);
  832. // _dns_header.id = htons(id);
  833. _dns_header.id = id;
  834. memcpy(buf, &_dns_header, HEAD_SIZE);
  835. return(HEAD_SIZE);
  836. }
  837. static int cut_host(const char *host, char *query)
  838. {
  839. const char *period = NULL, *orig = NULL;
  840. int len;
  841. orig = query;
  842. while ((period = strchr(host, '.'))) {
  843. len = period - host;
  844. if (len > 63) return(-1);
  845. *query++ = len;
  846. memcpy(query, host, len);
  847. query += len;
  848. host = period+1;
  849. }
  850. len = strlen(host);
  851. if (len) {
  852. *query++ = len;
  853. memcpy(query, host, len);
  854. query += len;
  855. }
  856. *query++ = 0;
  857. return(query-orig);
  858. }
  859. int reverse_ip(const char *host, char *reverse)
  860. {
  861. const char *period = NULL;
  862. int offset, len;
  863. period = strchr(host, '.');
  864. if (!period) {
  865. len = strlen(host);
  866. memcpy(reverse, host, len);
  867. return (len);
  868. }
  869. else {
  870. len = period - host;
  871. offset = reverse_ip(host+len+1, reverse);
  872. reverse[offset++] = '.';
  873. memcpy(reverse+offset, host, len);
  874. reverse[offset+len] = 0;
  875. return (offset+len);
  876. }
  877. }
  878. int egg_dns_cancel(int id, int issue_callback)
  879. {
  880. dns_query_t *q, *prev = NULL;
  881. for (q = query_head; q; q = q->next) {
  882. if (q->id == id)
  883. break;
  884. prev = q;
  885. }
  886. if (!q)
  887. return(-1);
  888. if (prev)
  889. prev->next = q->next;
  890. else
  891. query_head = q->next;
  892. sdprintf("Cancelling query: %s", q->query);
  893. if (issue_callback && q->callback) {
  894. if (q->answer->size() > 0) {
  895. cache_add(q->query, *(q->answer), q->lowest_ttl);
  896. q->callback(q->id, q->client_data, q->query, *(q->answer));
  897. } else {
  898. bd::Array<bd::String> empty;
  899. q->callback(q->id, q->client_data, q->query, empty);
  900. }
  901. }
  902. free_query(q);
  903. return(0);
  904. }
  905. static int skip_name(unsigned char *ptr)
  906. {
  907. int len;
  908. unsigned char *start = ptr;
  909. while ((len = *ptr++) > 0) {
  910. if (len > 63) {
  911. ptr++;
  912. break;
  913. }
  914. else {
  915. ptr += len;
  916. }
  917. }
  918. return(ptr - start);
  919. }
  920. /*
  921. void print_header(dns_header_t &header)
  922. {
  923. #define dofield(_field) sdprintf("%s: %d\n", #_field, _field)
  924. dofield(header.id);
  925. dofield(header.question_count);
  926. dofield(header.answer_count);
  927. dofield(header.ar_count);
  928. dofield(header.ns_count);
  929. #undef dofield
  930. }
  931. void print_reply(dns_rr_t &reply)
  932. {
  933. #define dofield(_field) sdprintf("%s: %d\n", #_field, _field)
  934. dofield(reply.type);
  935. dofield(reply.dclass);
  936. dofield(reply.ttl);
  937. dofield(reply.rdlength);
  938. #undef dofield
  939. }
  940. */
  941. static int parse_reply(char *response, size_t nbytes, const char* server_ip, bool blocking)
  942. {
  943. dns_header_t header;
  944. dns_query_t *q = NULL, *prev = NULL;
  945. char result[512] = "";
  946. short rr;
  947. dns_rr_t reply;
  948. unsigned const char *eop = (unsigned char *) response + nbytes;
  949. unsigned char *ptr = (unsigned char *) response;
  950. int return_code = 0;
  951. memcpy(&header, ptr, HEAD_SIZE);
  952. ptr += HEAD_SIZE;
  953. /* header.id is already in our order, echoed by the server */
  954. header.flags = ntohs(header.flags);
  955. header.question_count = ntohs(header.question_count);
  956. header.answer_count = ntohs(header.answer_count);
  957. header.ar_count = ntohs(header.ar_count);
  958. header.ns_count = ntohs(header.ns_count);
  959. // print_header(header);
  960. /* Find our copy of the query before proceeding. */
  961. for (q = query_head; q; q = q->next) {
  962. if (q->id == header.id)
  963. break;
  964. prev = q;
  965. }
  966. 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",
  967. header.id,
  968. header.question_count,
  969. header.answer_count,
  970. header.ar_count,
  971. header.ns_count,
  972. server_ip,
  973. GET_QR(header.flags),
  974. GET_OPCODE(header.flags),
  975. GET_AA(header.flags),
  976. GET_TC(header.flags),
  977. GET_RD(header.flags),
  978. GET_RA(header.flags),
  979. GET_RCODE(header.flags)
  980. );
  981. if (!q) {
  982. sdprintf("Reply(%d) not found??", header.id);
  983. return 0;
  984. }
  985. /* Did this server give us recursion? */
  986. if (!GET_RA(header.flags)) {
  987. sdprintf("Ignoring reply(%d) from %s: no recusion available.", header.id, server_ip);
  988. return_code = 1; /* get a new server */
  989. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  990. errno = EACCES;
  991. goto callback;
  992. }
  993. /* Check for errors */
  994. if (GET_RCODE(header.flags)) {
  995. switch (GET_RCODE(header.flags)) {
  996. case 1: /* Format error */
  997. sdprintf("Ignoring reply(%d) from %s: Format error.", header.id, server_ip);
  998. errno = EINVAL;
  999. break;
  1000. case 2: /* Server error */
  1001. sdprintf("Ignoring reply(%d) from %s: Server error.", header.id, server_ip);
  1002. errno = EINVAL;
  1003. return_code = 1; /* get a new server */
  1004. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1005. break;
  1006. case 3: /* Name error */
  1007. sdprintf("Ignoring reply(%d) from %s: NXDOMAIN.", header.id, server_ip);
  1008. /* Ignore the incoming AAAA or A reply as it will still be NXDOMAIN */
  1009. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1010. errno = ENOENT;
  1011. break;
  1012. case 4:
  1013. sdprintf("Ignoring reply(%d) from %s: Query not supported", header.id, server_ip);
  1014. errno = EINVAL;
  1015. break;
  1016. case 5:
  1017. sdprintf("Ignoring reply(%d) from %s: REFUSED", header.id, server_ip);
  1018. return_code = 1; /* get a new server */
  1019. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1020. errno = EACCES;
  1021. break;
  1022. }
  1023. goto callback;
  1024. }
  1025. // /* destroy our async timeout */
  1026. // timer_destroy(q->timer_id);
  1027. /* Pass over the questions. */
  1028. for (rr = 0; rr < header.question_count; rr++) {
  1029. ptr += skip_name(ptr);
  1030. ptr += 4;
  1031. }
  1032. /* End of questions. */
  1033. // for (rr = 0; rr < header.answer_count + header.ar_count + header.ns_count; rr++) {
  1034. q->answers += header.answer_count;
  1035. for (rr = 0; rr < header.answer_count; rr++) {
  1036. result[0] = 0;
  1037. /* Read in the answer. */
  1038. ptr += skip_name(ptr);
  1039. memcpy(&reply, ptr, RR_SIZE);
  1040. ptr += RR_SIZE;
  1041. reply.type = ntohs(reply.type);
  1042. reply.dclass = ntohs(reply.dclass);
  1043. reply.rdlength = ntohs(reply.rdlength);
  1044. reply.ttl = ntohl(reply.ttl);
  1045. /* Save the lowest ttl */
  1046. if (reply.ttl && ((!q->lowest_ttl) || (q->lowest_ttl > reply.ttl))) q->lowest_ttl = reply.ttl;
  1047. // print_reply(reply);
  1048. switch (reply.type) {
  1049. case DNS_A:
  1050. inet_ntop(AF_INET, ptr, result, 512);
  1051. *(q->answer) << result;
  1052. sdprintf("Reply(%d): %s. \t %d \t IN A \t %s", header.id, q->query, reply.ttl, result);
  1053. break;
  1054. case DNS_AAAA:
  1055. #ifdef USE_IPV6
  1056. inet_ntop(AF_INET6, ptr, result, 512);
  1057. *(q->answer) << result;
  1058. sdprintf("Reply(%d): %s. \t %d \t IN AAAA \t %s", header.id, q->query, reply.ttl, result);
  1059. #endif /* USE_IPV6 */
  1060. break;
  1061. case DNS_PTR:
  1062. if ((my_dn_expand((const unsigned char *) response, eop, ptr, result, sizeof(result)) != -1) && result[0]) {
  1063. *(q->answer) << result;
  1064. sdprintf("Reply(%d): %s. \t %d \t IN PTR \t %s", header.id, q->query, reply.ttl, result);
  1065. }
  1066. break;
  1067. default:
  1068. sdprintf("Unhandled DNS reply type: %d", reply.type);
  1069. break;
  1070. }
  1071. ptr += reply.rdlength;
  1072. if ((size_t) (ptr - (unsigned char*) response) > nbytes) {
  1073. sdprintf("MALFORMED/TRUNCATED DNS PACKET detected (need TCP).");
  1074. q->remaining = 0; /* Force this query to be removed, any further answers are ignored */
  1075. break;
  1076. }
  1077. }
  1078. callback:
  1079. /* Don't continue if we haven't gotten all expected replies. */
  1080. if (--q->remaining > 0) {
  1081. sdprintf("Waiting on query(%d) for more replies.", header.id);
  1082. errno = EAGAIN;
  1083. return 0;
  1084. }
  1085. errno = 0;
  1086. /* Ok, we have, so now issue the callback with the answers. */
  1087. if (prev)
  1088. prev->next = q->next;
  1089. else
  1090. query_head = q->next;
  1091. if (q->answer->size() > 0) {
  1092. cache_add(q->query, *(q->answer), q->lowest_ttl);
  1093. if (q->callback)
  1094. q->callback(q->id, q->client_data, q->query, *(q->answer));
  1095. } else if (q->callback) {
  1096. bd::Array<bd::String> empty;
  1097. q->callback(q->id, q->client_data, q->query, empty);
  1098. }
  1099. if (!blocking)
  1100. free_query(q);
  1101. return return_code;
  1102. }
  1103. void tell_dnsdebug(int idx)
  1104. {
  1105. dns_query_t *q = NULL;
  1106. dprintf(idx, "NS: %s\n", dns_ip);
  1107. for (q = query_head; q; q = q->next)
  1108. dprintf(idx, "DNS (%d) (%ds): %s\n", q->id, (int) (q->expiretime - now), q->query);
  1109. // for (i = 0; i < nhosts; i++)
  1110. // dprintf(idx, "HOST #%d: %s/%s\n", i, hosts[i].host, hosts[i].ip);
  1111. for (int i = 0; i < ncache; i++) {
  1112. dprintf(idx, "cache(%d) %s expires in %ds\n", i, cache[i].query, (int) (cache[i].expiretime - now));
  1113. for (size_t n = 0; n < cache[i].answer->size(); n++)
  1114. dprintf(idx, "%zu: %s\n", n, bd::String((*(cache[i].answer))[n]).c_str());
  1115. }
  1116. }
  1117. static void expire_queries()
  1118. {
  1119. dns_query_t *q = NULL, *next = NULL;
  1120. int i = 0;
  1121. /* need to check for expired queries and either:
  1122. a) recheck/change ns
  1123. b) expire due to ttl
  1124. */
  1125. if (query_head) {
  1126. for (q = query_head; q; q = q->next) {
  1127. if (q->expiretime <= now) { /* set in alloc_query */
  1128. if (q->next)
  1129. next = q->next;
  1130. egg_dns_cancel(q->id, 1);
  1131. if (!next)
  1132. break;
  1133. q = next;
  1134. }
  1135. }
  1136. }
  1137. for (i = 0; i < ncache; i++) {
  1138. if (cache_expired(i)) {
  1139. cache_del(i);
  1140. if (i == ncache)
  1141. break;
  1142. i--;
  1143. }
  1144. }
  1145. }
  1146. /* Read in .hosts and /etc/hosts and .resolv.conf and /etc/resolv.conf */
  1147. int egg_dns_init()
  1148. {
  1149. /* Set RECURSION DESIRED */
  1150. SET_RD(_dns_header.flags);
  1151. /* Convert flags to network order */
  1152. _dns_header.flags = htons(_dns_header.flags);
  1153. if (!read_resolv(".resolv.conf")) {
  1154. read_resolv("/etc/resolv.conf");
  1155. /* some backup servers, probably will never be used. */
  1156. add_dns_server("4.2.2.2");
  1157. add_dns_server("8.8.8.8");
  1158. add_dns_server("8.8.4.4");
  1159. }
  1160. read_hosts("/etc/hosts");
  1161. read_hosts(".hosts");
  1162. /* root servers for future development (tracing down)
  1163. add_dns_server("198.41.0.4");
  1164. add_dns_server("192.228.79.201");
  1165. add_dns_server("192.33.4.12");
  1166. add_dns_server("128.8.10.90");
  1167. add_dns_server("192.203.230.10");
  1168. add_dns_server("192.5.5.241");
  1169. add_dns_server("192.112.36.4");
  1170. add_dns_server("128.63.2.53");
  1171. add_dns_server("192.36.148.17");
  1172. add_dns_server("192.58.128.30");
  1173. add_dns_server("193.0.14.129");
  1174. add_dns_server("198.32.64.12");
  1175. add_dns_server("202.12.27.33");
  1176. */
  1177. timer_create_secs(3, "adns_check_expires", (Function) expire_queries);
  1178. return(0);
  1179. }
  1180. bool valid_dns_id(int idx, int id)
  1181. {
  1182. if (id == -1)
  1183. return 1;
  1184. if (valid_idx(idx) && dcc[idx].dns_id && dcc[idx].dns_id == id)
  1185. return 1;
  1186. sdprintf("dns_id: %d is not associated with dead idx: %d", id, idx);
  1187. return 0;
  1188. }
  1189. bd::String dns_find_ip(bd::Array<bd::String> ips, int af_type) {
  1190. for (size_t i = 0; i < ips.size(); ++i) {
  1191. if (is_dotted_ip(bd::String(ips[i]).c_str()) == af_type) {
  1192. return ips[i];
  1193. }
  1194. }
  1195. return bd::String();
  1196. }
  1197. /* vim: set sts=0 sw=8 ts=8 noet: */