You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

759 lines
21 KiB

  1. /* dnsget.c
  2. simple host/dig-like application using UDNS library
  3. Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
  4. This file is part of UDNS library, an async DNS stub resolver.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library, in file named COPYING.LGPL; if not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place,
  16. Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #ifdef __MINGW32__
  22. #include <windows.h>
  23. #include <winsock2.h>
  24. #else
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <sys/time.h>
  29. #include <unistd.h>
  30. #endif
  31. #include <time.h>
  32. #include <stdarg.h>
  33. #include <errno.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "udns.h"
  38. #ifndef HAVE_GETOPT
  39. # include "getopt.c"
  40. #endif
  41. #ifndef AF_INET6
  42. # define AF_INET6 10
  43. #endif
  44. static char *progname;
  45. static int verbose = 1;
  46. static int errors;
  47. static int notfound;
  48. /* verbosity level:
  49. * <0 - bare result
  50. * 0 - bare result and error messages
  51. * 1 - readable result
  52. * 2 - received packet contents and `trying ...' stuff
  53. * 3 - sent and received packet contents
  54. */
  55. static void die(int errnum, const char *fmt, ...) {
  56. va_list ap;
  57. fprintf(stderr, "%s: ", progname);
  58. va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap);
  59. if (errnum) fprintf(stderr, ": %s\n", strerror(errnum));
  60. else putc('\n', stderr);
  61. fflush(stderr);
  62. exit(1);
  63. }
  64. static const char *dns_xntop(int af, const void *src) {
  65. static char buf[6*5+4*4];
  66. return dns_ntop(af, src, buf, sizeof(buf));
  67. }
  68. struct query {
  69. const char *name; /* original query string */
  70. unsigned char *dn; /* the DN being looked up */
  71. enum dns_type qtyp; /* type of the query */
  72. };
  73. static void query_free(struct query *q) {
  74. free(q->dn);
  75. free(q);
  76. }
  77. static struct query *
  78. query_new(const char *name, const unsigned char *dn, enum dns_type qtyp) {
  79. struct query *q = malloc(sizeof(*q));
  80. unsigned l = dns_dnlen(dn);
  81. unsigned char *cdn = malloc(l);
  82. if (!q || !cdn) die(0, "out of memory");
  83. memcpy(cdn, dn, l);
  84. q->name = name;
  85. q->dn = cdn;
  86. q->qtyp = qtyp;
  87. return q;
  88. }
  89. static enum dns_class qcls = DNS_C_IN;
  90. static void
  91. dnserror(struct query *q, int errnum) {
  92. if (verbose >= 0)
  93. fprintf(stderr, "%s: unable to lookup %s record for %s: %s\n", progname,
  94. dns_typename(q->qtyp), dns_dntosp(q->dn), dns_strerror(errnum));
  95. if (errnum == DNS_E_NXDOMAIN || errnum == DNS_E_NODATA)
  96. ++notfound;
  97. else
  98. ++errors;
  99. query_free(q);
  100. }
  101. static const unsigned char *
  102. printtxt(const unsigned char *c) {
  103. unsigned n = *c++;
  104. const unsigned char *e = c + n;
  105. if (verbose > 0) while(c < e) {
  106. if (*c < ' ' || *c >= 127) printf("\\%03u", *c);
  107. else if (*c == '\\' || *c == '"') printf("\\%c", *c);
  108. else putchar(*c);
  109. ++c;
  110. }
  111. else
  112. fwrite(c, n, 1, stdout);
  113. return e;
  114. }
  115. static void
  116. printhex(const unsigned char *c, const unsigned char *e) {
  117. while(c < e)
  118. printf("%02x", *c++);
  119. }
  120. static unsigned char to_b64[] =
  121. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  122. static void
  123. printb64(const unsigned char *c, const unsigned char *e) {
  124. while(c < e) {
  125. putchar(to_b64[c[0] >> 2]);
  126. if (c+1 < e) {
  127. putchar(to_b64[(c[0] & 0x3) << 4 | c[1] >> 4]);
  128. if (c+2 < e) {
  129. putchar(to_b64[(c[1] & 0xf) << 2 | c[2] >> 6]);
  130. putchar(to_b64[c[2] & 0x3f]);
  131. }
  132. else {
  133. putchar(to_b64[(c[1] & 0xf) << 2]);
  134. putchar('=');
  135. break;
  136. }
  137. }
  138. else {
  139. putchar(to_b64[(c[0] & 0x3) << 4]);
  140. putchar('=');
  141. putchar('=');
  142. break;
  143. }
  144. c += 3;
  145. }
  146. }
  147. static void
  148. printdate(time_t time) {
  149. struct tm *tm = gmtime(&time);
  150. printf("%04d%02d%02d%02d%02d%02d",
  151. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  152. tm->tm_hour, tm->tm_min, tm->tm_sec);
  153. }
  154. static void
  155. printrr(const struct dns_parse *p, struct dns_rr *rr) {
  156. const unsigned char *pkt = p->dnsp_pkt;
  157. const unsigned char *end = p->dnsp_end;
  158. const unsigned char *dptr = rr->dnsrr_dptr;
  159. const unsigned char *dend = rr->dnsrr_dend;
  160. unsigned char *dn = rr->dnsrr_dn;
  161. const unsigned char *c;
  162. unsigned n;
  163. if (verbose > 0) {
  164. if (verbose > 1) {
  165. if (!p->dnsp_rrl && !rr->dnsrr_dn[0] && rr->dnsrr_typ == DNS_T_OPT) {
  166. printf(";EDNS%d OPT record (UDPsize: %d, ERcode: %d, Flags: 0x%02x): %d bytes\n",
  167. (rr->dnsrr_ttl>>16) & 0xff, /* version */
  168. rr->dnsrr_cls, /* udp size */
  169. (rr->dnsrr_ttl>>24) & 0xff, /* extended rcode */
  170. rr->dnsrr_ttl & 0xffff, /* flags */
  171. rr->dnsrr_dsz);
  172. return;
  173. }
  174. n = printf("%s.", dns_dntosp(rr->dnsrr_dn));
  175. printf("%s%u\t%s\t%s\t",
  176. n > 15 ? "\t" : n > 7 ? "\t\t" : "\t\t\t",
  177. rr->dnsrr_ttl,
  178. dns_classname(rr->dnsrr_cls),
  179. dns_typename(rr->dnsrr_typ));
  180. }
  181. else
  182. printf("%s. %s ", dns_dntosp(rr->dnsrr_dn), dns_typename(rr->dnsrr_typ));
  183. }
  184. switch(rr->dnsrr_typ) {
  185. case DNS_T_CNAME:
  186. case DNS_T_PTR:
  187. case DNS_T_NS:
  188. case DNS_T_MB:
  189. case DNS_T_MD:
  190. case DNS_T_MF:
  191. case DNS_T_MG:
  192. case DNS_T_MR:
  193. if (dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN) <= 0) goto xperr;
  194. printf("%s.", dns_dntosp(dn));
  195. break;
  196. case DNS_T_A:
  197. if (rr->dnsrr_dsz != 4) goto xperr;
  198. printf("%d.%d.%d.%d", dptr[0], dptr[1], dptr[2], dptr[3]);
  199. break;
  200. case DNS_T_AAAA:
  201. if (rr->dnsrr_dsz != 16) goto xperr;
  202. printf("%s", dns_xntop(AF_INET6, dptr));
  203. break;
  204. case DNS_T_MX:
  205. c = dptr + 2;
  206. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 || c != dend) goto xperr;
  207. printf("%d %s.", dns_get16(dptr), dns_dntosp(dn));
  208. break;
  209. case DNS_T_TXT:
  210. /* first verify it */
  211. for(c = dptr; c < dend; c += n) {
  212. n = *c++;
  213. if (c + n > dend) goto xperr;
  214. }
  215. c = dptr; n = 0;
  216. while (c < dend) {
  217. if (verbose > 0) printf(n++ ? "\" \"":"\"");
  218. c = printtxt(c);
  219. }
  220. if (verbose > 0) putchar('"');
  221. break;
  222. case DNS_T_HINFO: /* CPU, OS */
  223. c = dptr;
  224. n = *c++; if ((c += n) >= dend) goto xperr;
  225. n = *c++; if ((c += n) != dend) goto xperr;
  226. c = dptr;
  227. if (verbose > 0) putchar('"');
  228. c = printtxt(c);
  229. if (verbose > 0) printf("\" \""); else putchar(' ');
  230. printtxt(c);
  231. if (verbose > 0) putchar('"');
  232. break;
  233. case DNS_T_WKS:
  234. c = dptr;
  235. if (dptr + 4 + 2 >= end) goto xperr;
  236. printf("%s %d", dns_xntop(AF_INET, dptr), dptr[4]);
  237. c = dptr + 5;
  238. for (n = 0; c < dend; ++c, n += 8) {
  239. if (*c) {
  240. unsigned b;
  241. for (b = 0; b < 8; ++b)
  242. if (*c & (1 << (7-b))) printf(" %d", n + b);
  243. }
  244. }
  245. break;
  246. case DNS_T_SRV: /* prio weight port targetDN */
  247. c = dptr;
  248. c += 2 + 2 + 2;
  249. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 || c != dend) goto xperr;
  250. c = dptr;
  251. printf("%d %d %d %s.",
  252. dns_get16(c+0), dns_get16(c+2), dns_get16(c+4),
  253. dns_dntosp(dn));
  254. break;
  255. case DNS_T_NAPTR: /* order pref flags serv regexp repl */
  256. c = dptr;
  257. c += 4; /* order, pref */
  258. for (n = 0; n < 3; ++n)
  259. if (c >= dend) goto xperr;
  260. else c += *c + 1;
  261. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 || c != dend) goto xperr;
  262. c = dptr;
  263. printf("%u %u", dns_get16(c+0), dns_get16(c+2));
  264. c += 4;
  265. for(n = 0; n < 3; ++n) {
  266. putchar(' ');
  267. if (verbose > 0) putchar('"');
  268. c = printtxt(c);
  269. if (verbose > 0) putchar('"');
  270. }
  271. printf(" %s.", dns_dntosp(dn));
  272. break;
  273. case DNS_T_KEY:
  274. case DNS_T_DNSKEY:
  275. /* flags(2) proto(1) algo(1) pubkey */
  276. case DNS_T_DS:
  277. case DNS_T_DLV:
  278. /* ktag(2) proto(1) algo(1) pubkey */
  279. c = dptr;
  280. if (c + 2 + 1 + 1 > dend) goto xperr;
  281. printf("%d %d %d", dns_get16(c), c[2], c[3]);
  282. c += 2 + 1 + 1;
  283. if (c < dend) {
  284. putchar(' ');
  285. printb64(c, dend);
  286. }
  287. break;
  288. case DNS_T_SIG:
  289. case DNS_T_RRSIG:
  290. /* type(2) algo(1) labels(1) ottl(4) sexp(4) sinc(4) tag(2) sdn sig */
  291. c = dptr;
  292. c += 2 + 1 + 1 + 4 + 4 + 4 + 2;
  293. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0) goto xperr;
  294. printf("%s %u %u %u ",
  295. dns_typename(dns_get16(dptr)), dptr[2], dptr[3], dns_get32(dptr+4));
  296. printdate(dns_get32(dptr+8));
  297. putchar(' ');
  298. printdate(dns_get32(dptr+12));
  299. printf(" %d %s. ", dns_get16(dptr+10), dns_dntosp(dn));
  300. printb64(c, dend);
  301. break;
  302. case DNS_T_SSHFP: /* algo(1), fp type(1), fp... */
  303. if (dend < dptr + 3) goto xperr;
  304. printf("%u %u ", dptr[0], dptr[1]); /* algo, fp type */
  305. printhex(dptr + 2, dend);
  306. break;
  307. #if 0 /* unused RR types? */
  308. case DNS_T_NSEC: /* nextDN bitmaps */
  309. c = dptr;
  310. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0) goto xperr;
  311. printf("%s.", dns_dntosp(dn));
  312. unfinished.
  313. break;
  314. #endif
  315. case DNS_T_SOA:
  316. c = dptr;
  317. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 ||
  318. dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 ||
  319. c + 4*5 != dend)
  320. goto xperr;
  321. dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN);
  322. printf("%s. ", dns_dntosp(dn));
  323. dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN);
  324. printf("%s. ", dns_dntosp(dn));
  325. printf("%u %u %u %u %u",
  326. dns_get32(dptr), dns_get32(dptr+4), dns_get32(dptr+8),
  327. dns_get32(dptr+12), dns_get32(dptr+16));
  328. break;
  329. case DNS_T_MINFO:
  330. c = dptr;
  331. if (dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 ||
  332. dns_getdn(pkt, &c, end, dn, DNS_MAXDN) <= 0 ||
  333. c != dend)
  334. goto xperr;
  335. dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN);
  336. printf("%s. ", dns_dntosp(dn));
  337. dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN);
  338. printf("%s.", dns_dntosp(dn));
  339. break;
  340. case DNS_T_NULL:
  341. default:
  342. printhex(dptr, dend);
  343. break;
  344. }
  345. putchar('\n');
  346. return;
  347. xperr:
  348. printf("<parse error>\n");
  349. ++errors;
  350. }
  351. static int
  352. printsection(struct dns_parse *p, int nrr, const char *sname) {
  353. struct dns_rr rr;
  354. int r;
  355. if (!nrr) return 0;
  356. if (verbose > 1) printf("\n;; %s section (%d):\n", sname, nrr);
  357. p->dnsp_rrl = nrr;
  358. while((r = dns_nextrr(p, &rr)) > 0)
  359. printrr(p, &rr);
  360. if (r < 0) printf("<<ERROR>>\n");
  361. return r;
  362. }
  363. /* dbgcb will only be called if verbose > 1 */
  364. static void
  365. dbgcb(int code, const struct sockaddr *sa, unsigned slen,
  366. const unsigned char *pkt, int r,
  367. const struct dns_query *unused_q, void *unused_data) {
  368. struct dns_parse p;
  369. const unsigned char *cur, *end;
  370. int numqd;
  371. if (code > 0) {
  372. printf(";; trying %s.\n", dns_dntosp(dns_payload(pkt)));
  373. printf(";; sending %d bytes query to ", r);
  374. }
  375. else
  376. printf(";; received %d bytes response from ", r);
  377. if (sa->sa_family == AF_INET && slen >= sizeof(struct sockaddr_in))
  378. printf("%s port %d\n",
  379. dns_xntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr),
  380. htons(((struct sockaddr_in*)sa)->sin_port));
  381. #ifdef HAVE_IPv6
  382. else if (sa->sa_family == AF_INET6 && slen >= sizeof(struct sockaddr_in6))
  383. printf("%s port %d\n",
  384. dns_xntop(AF_INET6, &((struct sockaddr_in6*)sa)->sin6_addr),
  385. htons(((struct sockaddr_in6*)sa)->sin6_port));
  386. #endif
  387. else
  388. printf("<<unknown socket type %d>>\n", sa->sa_family);
  389. if (code > 0 && verbose < 3) {
  390. putchar('\n');
  391. return;
  392. }
  393. if (code == -2) printf(";; reply from unexpected source\n");
  394. if (code == -5) printf(";; reply to a query we didn't sent (or old)\n");
  395. if (r < DNS_HSIZE) {
  396. printf(";; short packet (%d bytes)\n", r);
  397. return;
  398. }
  399. if (dns_opcode(pkt) != 0)
  400. printf(";; unexpected opcode %d\n", dns_opcode(pkt));
  401. if (dns_tc(pkt) != 0)
  402. printf(";; warning: TC bit set, probably incomplete reply\n");
  403. printf(";; ->>HEADER<<- opcode: ");
  404. switch(dns_opcode(pkt)) {
  405. case 0: printf("QUERY"); break;
  406. case 1: printf("IQUERY"); break;
  407. case 2: printf("STATUS"); break;
  408. default: printf("UNKNOWN(%u)", dns_opcode(pkt)); break;
  409. }
  410. printf(", status: %s, id: %d, size: %d\n;; flags:",
  411. dns_rcodename(dns_rcode(pkt)), dns_qid(pkt), r);
  412. if (dns_qr(pkt)) printf(" qr");
  413. if (dns_aa(pkt)) printf(" aa");
  414. if (dns_tc(pkt)) printf(" tc");
  415. if (dns_rd(pkt)) printf(" rd");
  416. if (dns_ra(pkt)) printf(" ra");
  417. /* if (dns_z(pkt)) printf(" z"); only one reserved bit left */
  418. if (dns_ad(pkt)) printf(" ad");
  419. if (dns_cd(pkt)) printf(" cd");
  420. numqd = dns_numqd(pkt);
  421. printf("; QUERY: %d, ANSWER: %d, AUTHORITY: %d, ADDITIONAL: %d\n",
  422. numqd, dns_numan(pkt), dns_numns(pkt), dns_numar(pkt));
  423. if (numqd != 1)
  424. printf(";; unexpected number of entries in QUERY section: %d\n",
  425. numqd);
  426. printf("\n;; QUERY SECTION (%d):\n", numqd);
  427. cur = dns_payload(pkt);
  428. end = pkt + r;
  429. while(numqd--) {
  430. if (dns_getdn(pkt, &cur, end, p.dnsp_dnbuf, DNS_MAXDN) <= 0 ||
  431. cur + 4 > end) {
  432. printf("; invalid query section\n");
  433. return;
  434. }
  435. r = printf(";%s.", dns_dntosp(p.dnsp_dnbuf));
  436. printf("%s%s\t%s\n",
  437. r > 23 ? "\t" : r > 15 ? "\t\t" : r > 7 ? "\t\t\t" : "\t\t\t\t",
  438. dns_classname(dns_get16(cur+2)), dns_typename(dns_get16(cur)));
  439. cur += 4;
  440. }
  441. p.dnsp_pkt = pkt;
  442. p.dnsp_cur = p.dnsp_ans = cur;
  443. p.dnsp_end = end;
  444. p.dnsp_qdn = NULL;
  445. p.dnsp_qcls = p.dnsp_qtyp = 0;
  446. p.dnsp_ttl = 0xffffffffu;
  447. p.dnsp_nrr = 0;
  448. r = printsection(&p, dns_numan(pkt), "ANSWER");
  449. if (r == 0)
  450. r = printsection(&p, dns_numns(pkt), "AUTHORITY");
  451. if (r == 0)
  452. r = printsection(&p, dns_numar(pkt), "ADDITIONAL");
  453. putchar('\n');
  454. }
  455. static void dnscb(struct dns_ctx *ctx, void *result, void *data) {
  456. int r = dns_status(ctx);
  457. struct query *q = data;
  458. struct dns_parse p;
  459. struct dns_rr rr;
  460. unsigned nrr;
  461. unsigned char dn[DNS_MAXDN];
  462. const unsigned char *pkt, *cur, *end;
  463. if (!result) {
  464. dnserror(q, r);
  465. return;
  466. }
  467. pkt = result; end = pkt + r; cur = dns_payload(pkt);
  468. dns_getdn(pkt, &cur, end, dn, sizeof(dn));
  469. dns_initparse(&p, NULL, pkt, cur, end);
  470. p.dnsp_qcls = p.dnsp_qtyp = 0;
  471. nrr = 0;
  472. while((r = dns_nextrr(&p, &rr)) > 0) {
  473. if (!dns_dnequal(dn, rr.dnsrr_dn)) continue;
  474. if ((qcls == DNS_C_ANY || qcls == rr.dnsrr_cls) &&
  475. (q->qtyp == DNS_T_ANY || q->qtyp == rr.dnsrr_typ))
  476. ++nrr;
  477. else if (rr.dnsrr_typ == DNS_T_CNAME && !nrr) {
  478. if (dns_getdn(pkt, &rr.dnsrr_dptr, end,
  479. p.dnsp_dnbuf, sizeof(p.dnsp_dnbuf)) <= 0 ||
  480. rr.dnsrr_dptr != rr.dnsrr_dend) {
  481. r = DNS_E_PROTOCOL;
  482. break;
  483. }
  484. else {
  485. if (verbose == 1) {
  486. printf("%s.", dns_dntosp(dn));
  487. printf(" CNAME %s.\n", dns_dntosp(p.dnsp_dnbuf));
  488. }
  489. dns_dntodn(p.dnsp_dnbuf, dn, sizeof(dn));
  490. }
  491. }
  492. }
  493. if (!r && !nrr)
  494. r = DNS_E_NODATA;
  495. if (r < 0) {
  496. dnserror(q, r);
  497. free(result);
  498. return;
  499. }
  500. if (verbose < 2) { /* else it is already printed by dbgfn */
  501. dns_rewind(&p, NULL);
  502. p.dnsp_qtyp = q->qtyp == DNS_T_ANY ? 0 : q->qtyp;
  503. p.dnsp_qcls = qcls == DNS_C_ANY ? 0 : qcls;
  504. while(dns_nextrr(&p, &rr))
  505. printrr(&p, &rr);
  506. }
  507. free(result);
  508. query_free(q);
  509. }
  510. int main(int argc, char **argv) {
  511. int i;
  512. int fd;
  513. fd_set fds;
  514. struct timeval tv;
  515. time_t now;
  516. char *ns[DNS_MAXSERV];
  517. int nns = 0;
  518. struct query *q;
  519. enum dns_type qtyp = 0;
  520. struct dns_ctx *nctx = NULL;
  521. int flags = 0;
  522. if (!(progname = strrchr(argv[0], '/'))) progname = argv[0];
  523. else argv[0] = ++progname;
  524. if (argc <= 1)
  525. die(0, "try `%s -h' for help", progname);
  526. if (dns_init(NULL, 0) < 0 || !(nctx = dns_new(NULL)))
  527. die(errno, "unable to initialize dns library");
  528. /* we keep two dns contexts: one may be needed to resolve
  529. * nameservers if given as names, using default options.
  530. */
  531. while((i = getopt(argc, argv, "vqt:c:an:o:f:h")) != EOF) switch(i) {
  532. case 'v': ++verbose; break;
  533. case 'q': --verbose; break;
  534. case 't':
  535. if (optarg[0] == '*' && !optarg[1])
  536. i = DNS_T_ANY;
  537. else if ((i = dns_findtypename(optarg)) <= 0)
  538. die(0, "unrecognized query type `%s'", optarg);
  539. qtyp = i;
  540. break;
  541. case 'c':
  542. if (optarg[0] == '*' && !optarg[1])
  543. i = DNS_C_ANY;
  544. else if ((i = dns_findclassname(optarg)) < 0)
  545. die(0, "unrecognized query class `%s'", optarg);
  546. qcls = i;
  547. break;
  548. case 'a':
  549. qtyp = DNS_T_ANY;
  550. ++verbose;
  551. break;
  552. case 'n':
  553. if (nns >= DNS_MAXSERV)
  554. die(0, "too many nameservers, %d max", DNS_MAXSERV);
  555. ns[nns++] = optarg;
  556. break;
  557. case 'o':
  558. case 'f': {
  559. char *opt;
  560. const char *const delim = " \t,;";
  561. for(opt = strtok(optarg, delim); opt != NULL; opt = strtok(NULL, delim)) {
  562. if (dns_set_opts(NULL, optarg) == 0)
  563. ;
  564. else if (strcmp(opt, "aa") == 0) flags |= DNS_AAONLY;
  565. else if (strcmp(optarg, "nord") == 0) flags |= DNS_NORD;
  566. else if (strcmp(optarg, "dnssec") == 0) flags |= DNS_SET_DO;
  567. else if (strcmp(optarg, "do") == 0) flags |= DNS_SET_DO;
  568. else if (strcmp(optarg, "cd") == 0) flags |= DNS_SET_CD;
  569. else
  570. die(0, "invalid option: `%s'", opt);
  571. }
  572. break;
  573. }
  574. case 'h':
  575. printf(
  576. "%s: simple DNS query tool (using udns version %s)\n"
  577. "Usage: %s [options] domain-name...\n"
  578. "where options are:\n"
  579. " -h - print this help and exit\n"
  580. " -v - be more verbose\n"
  581. " -q - be less verbose\n"
  582. " -t type - set query type (A, AAA, PTR etc)\n"
  583. " -c class - set query class (IN (default), CH, HS, *)\n"
  584. " -a - equivalent to -t ANY -v\n"
  585. " -n ns - use given nameserver(s) instead of default\n"
  586. " (may be specified multiple times)\n"
  587. " -o opt,opt,... (comma- or space-separated list,\n"
  588. " may be specified more than once):\n"
  589. " set resovler options (the same as setting $RES_OPTIONS):\n"
  590. " timeout:sec - initial query timeout\n"
  591. " attempts:num - number of attempt to resovle a query\n"
  592. " ndots:num - if name has more than num dots, lookup it before search\n"
  593. " port:num - port number for queries instead of default 53\n"
  594. " udpbuf:num - size of UDP buffer (use EDNS0 if >512)\n"
  595. " or query flags:\n"
  596. " aa,nord,dnssec,do,cd - set query flag (auth-only, no recursion,\n"
  597. " enable DNSSEC (DNSSEC Ok), check disabled)\n"
  598. , progname, dns_version(), progname);
  599. return 0;
  600. default:
  601. die(0, "try `%s -h' for help", progname);
  602. }
  603. argc -= optind; argv += optind;
  604. if (!argc)
  605. die(0, "no name(s) to query specified");
  606. if (nns) {
  607. /* if nameservers given as names, resolve them.
  608. * We only allow IPv4 nameservers as names for now.
  609. * Ok, it is easy enouth to try both AAAA and A,
  610. * but the question is what to do by default.
  611. */
  612. struct sockaddr_in sin;
  613. int j, r = 0, opened = 0;
  614. memset(&sin, 0, sizeof(sin));
  615. sin.sin_family = AF_INET;
  616. sin.sin_port = htons(dns_set_opt(NULL, DNS_OPT_PORT, -1));
  617. dns_add_serv(NULL, NULL);
  618. for(i = 0; i < nns; ++i) {
  619. if (dns_pton(AF_INET, ns[i], &sin.sin_addr) <= 0) {
  620. struct dns_rr_a4 *rr;
  621. if (!opened) {
  622. if (dns_open(nctx) < 0)
  623. die(errno, "unable to initialize dns context");
  624. opened = 1;
  625. }
  626. rr = dns_resolve_a4(nctx, ns[i], 0);
  627. if (!rr)
  628. die(0, "unable to resolve nameserver %s: %s",
  629. ns[i], dns_strerror(dns_status(nctx)));
  630. for(j = 0; j < rr->dnsa4_nrr; ++j) {
  631. sin.sin_addr = rr->dnsa4_addr[j];
  632. if ((r = dns_add_serv_s(NULL, (struct sockaddr *)&sin)) < 0)
  633. break;
  634. }
  635. free(rr);
  636. }
  637. else
  638. r = dns_add_serv_s(NULL, (struct sockaddr *)&sin);
  639. if (r < 0)
  640. die(errno, "unable to add nameserver %s",
  641. dns_xntop(AF_INET, &sin.sin_addr));
  642. }
  643. }
  644. dns_free(nctx);
  645. fd = dns_open(NULL);
  646. if (fd < 0)
  647. die(errno, "unable to initialize dns context");
  648. if (verbose > 1)
  649. dns_set_dbgfn(NULL, dbgcb);
  650. if (flags)
  651. dns_set_opt(NULL, DNS_OPT_FLAGS, flags);
  652. for (i = 0; i < argc; ++i) {
  653. char *name = argv[i];
  654. union {
  655. struct in_addr addr;
  656. struct in6_addr addr6;
  657. } a;
  658. unsigned char dn[DNS_MAXDN];
  659. enum dns_type l_qtyp = 0;
  660. int abs;
  661. if (dns_pton(AF_INET, name, &a.addr) > 0) {
  662. dns_a4todn(&a.addr, 0, dn, sizeof(dn));
  663. l_qtyp = DNS_T_PTR;
  664. abs = 1;
  665. }
  666. #ifdef HAVE_IPv6
  667. else if (dns_pton(AF_INET6, name, &a.addr6) > 0) {
  668. dns_a6todn(&a.addr6, 0, dn, sizeof(dn));
  669. l_qtyp = DNS_T_PTR;
  670. abs = 1;
  671. }
  672. #endif
  673. else if (!dns_ptodn(name, strlen(name), dn, sizeof(dn), &abs))
  674. die(0, "invalid name `%s'\n", name);
  675. else
  676. l_qtyp = DNS_T_A;
  677. if (qtyp) l_qtyp = qtyp;
  678. q = query_new(name, dn, l_qtyp);
  679. if (abs) abs = DNS_NOSRCH;
  680. if (!dns_submit_dn(NULL, dn, qcls, l_qtyp, abs, 0, dnscb, q))
  681. dnserror(q, dns_status(NULL));
  682. }
  683. FD_ZERO(&fds);
  684. now = 0;
  685. while((i = dns_timeouts(NULL, -1, now)) > 0) {
  686. FD_SET(fd, &fds);
  687. tv.tv_sec = i;
  688. tv.tv_usec = 0;
  689. i = select(fd+1, &fds, 0, 0, &tv);
  690. now = time(NULL);
  691. if (i > 0) dns_ioevent(NULL, now);
  692. }
  693. return errors ? 1 : notfound ? 100 : 0;
  694. }