1
0

adns.cc 33 KB

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