PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/commands/host/host.c

http://www.minix3.org/
C | 1532 lines | 1304 code | 118 blank | 110 comment | 352 complexity | 3209796276a2b9f1f6680cad74e0afc1 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /*
  2. * Copyright (c) 1986 Regents of the University of California
  3. * All Rights Reserved
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that this notice is preserved and that due credit is given
  7. * to the University of California at Berkeley. The name of the University
  8. * may not be used to endorse or promote products derived from this
  9. * software without specific prior written permission. This software
  10. * is provided ``as is'' without express or implied warranty.
  11. */
  12. /*
  13. * Actually, this program is from Rutgers University, however it is
  14. * based on nslookup and other pieces of named tools, so it needs
  15. * that copyright notice.
  16. */
  17. #if _MINIX && !defined(__NBSD_LIBC)
  18. #include <sys/types.h>
  19. #include <sys/ioctl.h>
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <net/hton.h>
  28. #include <net/netlib.h>
  29. #include <net/gen/in.h>
  30. #include <net/gen/inet.h>
  31. #include <net/gen/netdb.h>
  32. #include <net/gen/nameser.h>
  33. #include <net/gen/resolv.h>
  34. #include <net/gen/socket.h>
  35. #include <net/gen/tcp.h>
  36. #include <net/gen/tcp_io.h>
  37. #undef ERROR
  38. #else
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <arpa/nameser.h>
  42. #include <netdb.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <resolv.h>
  46. #include <sys/param.h>
  47. #include <strings.h>
  48. #include <ctype.h>
  49. #include <assert.h>
  50. #include <errno.h>
  51. #include <fcntl.h>
  52. #include <sys/ioctl.h>
  53. #include <sys/ioc_net.h>
  54. #include <net/netlib.h>
  55. #include <net/gen/in.h>
  56. #include <net/gen/tcp.h>
  57. #include <net/gen/tcp_io.h>
  58. #endif
  59. extern int h_errno;
  60. #define NUMMX 50
  61. #define SUCCESS 0
  62. #define TIME_OUT -1
  63. #define NO_INFO -2
  64. #define ERROR -3
  65. #define NONAUTH -4
  66. #define NAME_LEN 256
  67. #ifndef T_TXT
  68. #define T_TXT 16
  69. #endif
  70. #ifndef NO_DATA
  71. #define NO_DATA NO_ADDRESS
  72. #endif
  73. #ifndef C_HS
  74. #define C_HS 4
  75. #endif
  76. #ifndef NOCHANGE
  77. #define NOCHANGE 0xf
  78. #endif
  79. FILE *filePtr;
  80. #ifdef __NBSD_LIBC
  81. static struct __res_state orig;
  82. #else
  83. struct state orig;
  84. extern struct state _res;
  85. #endif
  86. static u8_t *cname = NULL;
  87. int getclass = C_IN;
  88. int gettype, getdeftype = T_A;
  89. int verbose = 0;
  90. int list = 0;
  91. int server_specified = 0;
  92. union querybuf;
  93. int main( int c, char *v[] );
  94. static int parsetype( char *s );
  95. static int parseclass( char *s );
  96. static void hperror( int err_no );
  97. static void printanswer( struct hostent *hp );
  98. static int ListHosts( char *namePtr, int queryType );
  99. static int gethostinfo( char *name );
  100. static int getdomaininfo( char *name, char *domain );
  101. static int getinfo( char *name, char *domain, int type );
  102. static int printinfo( union querybuf *answer, u8_t *eom, int filter, int
  103. isls );
  104. static char *DecodeError( int result );
  105. static u8_t *pr_rr( u8_t *cp, u8_t *msg, FILE *file, int filter );
  106. static u8_t * pr_cdname( u8_t *cp, u8_t *msg, u8_t *name, int namelen );
  107. static char *pr_class( int class );
  108. static char *pr_type( int type );
  109. static int tcpip_writeall( int fd, char *buf, unsigned siz );
  110. int
  111. main(c, v)
  112. int c;
  113. char **v;
  114. {
  115. char *domain;
  116. #ifdef __NBSD_LIBC
  117. struct in_addr addr;
  118. #else
  119. ipaddr_t addr;
  120. #endif
  121. register struct hostent *hp;
  122. register char *s, *p;
  123. register int inverse = 0;
  124. register int waitmode = 0;
  125. u8_t *oldcname;
  126. int ncnames;
  127. int isaddr;
  128. res_init();
  129. _res.retrans = 5;
  130. if (c < 2) {
  131. fprintf(stderr, "Usage: host [-w] [-v] [-r] [-d] [-V] [-t querytype] [-c class] [-a] host [server]\n -w to wait forever until reply\n -v for verbose output\n -r to disable recursive processing\n -d to turn on debugging output\n -t querytype to look for a specific type of information\n -c class to look for non-Internet data\n -a is equivalent to '-v -t *'\n -V to always use a virtual circuit\n");
  132. exit(1);
  133. }
  134. while (c > 2 && v[1][0] == '-') {
  135. if (strcmp (v[1], "-w") == 0) {
  136. _res.retry = 1;
  137. _res.retrans = 15;
  138. waitmode = 1;
  139. v++;
  140. c--;
  141. }
  142. else if (strcmp (v[1], "-r") == 0) {
  143. _res.options &= ~RES_RECURSE;
  144. v++;
  145. c--;
  146. }
  147. else if (strcmp (v[1], "-d") == 0) {
  148. _res.options |= RES_DEBUG;
  149. v++;
  150. c--;
  151. }
  152. else if (strcmp (v[1], "-v") == 0) {
  153. verbose = 1;
  154. v++;
  155. c--;
  156. }
  157. else if (strcmp (v[1], "-l") == 0) {
  158. list = 1;
  159. v++;
  160. c--;
  161. }
  162. else if (strncmp (v[1], "-t", 2) == 0) {
  163. v++;
  164. c--;
  165. gettype = parsetype(v[1]);
  166. v++;
  167. c--;
  168. }
  169. else if (strncmp (v[1], "-c", 2) == 0) {
  170. v++;
  171. c--;
  172. getclass = parseclass(v[1]);
  173. v++;
  174. c--;
  175. }
  176. else if (strcmp (v[1], "-a") == 0) {
  177. verbose = 1;
  178. gettype = T_ANY;
  179. v++;
  180. c--;
  181. }
  182. else if (strcmp (v[1], "-V") == 0) {
  183. _res.options |= RES_USEVC;
  184. v++;
  185. c--;
  186. }
  187. }
  188. if (c > 2) {
  189. s = v[2];
  190. server_specified++;
  191. if ((p = strchr(s, ':')) != NULL) *p++ = 0;
  192. isaddr = inet_aton(s, &addr);
  193. if (p != NULL) p[-1] = ':';
  194. if (!isaddr) {
  195. hp = gethostbyname(s);
  196. if (hp == NULL) {
  197. fprintf(stderr,"Error in looking up server name:\n");
  198. hperror(h_errno);
  199. exit(1);
  200. }
  201. #ifdef __NBSD_LIBC
  202. memcpy(&_res.nsaddr.sin_addr, hp->h_addr, NS_INADDRSZ);
  203. #else
  204. _res.nsaddr= *(ipaddr_t *)hp->h_addr;
  205. #endif
  206. printf("Using domain server:\n");
  207. printanswer(hp);
  208. }
  209. else {
  210. #ifdef __NBSD_LIBC
  211. _res.nsaddr.sin_family = AF_INET;
  212. _res.nsaddr.sin_addr = addr;
  213. _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  214. printf("Using domain server %s:\n",
  215. inet_ntoa(_res.nsaddr.sin_addr));
  216. #else
  217. _res.nsaddr_list[0]= addr;
  218. _res.nsport_list[0]= htons(NAMESERVER_PORT);
  219. printf("Using domain server %s",
  220. inet_ntoa(_res.nsaddr));
  221. if (p != NULL) {
  222. printf(" on port %d", atoi(p));
  223. _res.nsport_list[0]= htons(atoi(p));
  224. }
  225. printf(":\n");
  226. #endif
  227. }
  228. }
  229. domain = v[1];
  230. if (strcmp (domain, ".") != 0 && inet_aton(domain, &addr)) {
  231. static char ipname[sizeof("255.255.255.255.in-addr.arpa.")];
  232. sprintf(ipname, "%d.%d.%d.%d.in-addr.arpa.",
  233. ((unsigned char *) &addr)[3],
  234. ((unsigned char *) &addr)[2],
  235. ((unsigned char *) &addr)[1],
  236. ((unsigned char *) &addr)[0]);
  237. domain = ipname;
  238. getdeftype = T_PTR;
  239. }
  240. hp = NULL;
  241. h_errno = TRY_AGAIN;
  242. /*
  243. * we handle default domains ourselves, thank you
  244. */
  245. _res.options &= ~RES_DEFNAMES;
  246. if (list)
  247. exit(ListHosts(domain, gettype ? gettype : getdeftype));
  248. oldcname = NULL;
  249. ncnames = 5;
  250. while (hp == NULL && h_errno == TRY_AGAIN) {
  251. cname = NULL;
  252. if (oldcname == NULL)
  253. hp = (struct hostent *)gethostinfo(domain);
  254. else
  255. hp = (struct hostent *)gethostinfo((char *)oldcname);
  256. if (cname) {
  257. if (ncnames-- == 0) {
  258. printf("Too many cnames. Possible loop.\n");
  259. exit(1);
  260. }
  261. oldcname = cname;
  262. hp = NULL;
  263. h_errno = TRY_AGAIN;
  264. continue;
  265. }
  266. if (!waitmode)
  267. break;
  268. }
  269. if (hp == NULL) {
  270. hperror(h_errno);
  271. exit(1);
  272. }
  273. exit(0);
  274. }
  275. static int
  276. parsetype(s)
  277. char *s;
  278. {
  279. if (strcmp(s,"a") == 0)
  280. return(1);
  281. if (strcmp(s,"ns") == 0)
  282. return(2);
  283. if (strcmp(s,"md") == 0)
  284. return(3);
  285. if (strcmp(s,"mf") == 0)
  286. return(4);
  287. if (strcmp(s,"cname") == 0)
  288. return(5);
  289. if (strcmp(s,"soa") == 0)
  290. return(6);
  291. if (strcmp(s,"mb") == 0)
  292. return(7);
  293. if (strcmp(s,"mg") == 0)
  294. return(8);
  295. if (strcmp(s,"mr") == 0)
  296. return(9);
  297. if (strcmp(s,"null") == 0)
  298. return(10);
  299. if (strcmp(s,"wks") == 0)
  300. return(11);
  301. if (strcmp(s,"ptr") == 0)
  302. return(12);
  303. if (strcmp(s,"hinfo") == 0)
  304. return(13);
  305. if (strcmp(s,"minfo") == 0)
  306. return(14);
  307. if (strcmp(s,"mx") == 0)
  308. return(15);
  309. if (strcmp(s,"txt") == 0) /* Roy */
  310. return(T_TXT); /* Roy */
  311. if (strcmp(s,"uinfo") == 0)
  312. return(100);
  313. if (strcmp(s,"uid") == 0)
  314. return(101);
  315. if (strcmp(s,"gid") == 0)
  316. return(102);
  317. if (strcmp(s,"unspec") == 0)
  318. return(103);
  319. if (strcmp(s,"any") == 0)
  320. return(255);
  321. if (strcmp(s,"*") == 0)
  322. return(255);
  323. if (atoi(s))
  324. return(atoi(s));
  325. fprintf(stderr, "Invalid query type: %s\n", s);
  326. exit(2);
  327. }
  328. static int
  329. parseclass(s)
  330. char *s;
  331. {
  332. if (strcmp(s,"in") == 0)
  333. return(C_IN);
  334. if (strcmp(s,"chaos") == 0)
  335. return(C_CHAOS);
  336. if (strcmp(s,"hs") == 0)
  337. return(C_HS);
  338. if (strcmp(s,"any") == 0)
  339. return(C_ANY);
  340. if (atoi(s))
  341. return(atoi(s));
  342. fprintf(stderr, "Invalid query class: %s\n", s);
  343. exit(2);
  344. }
  345. static void
  346. printanswer(hp)
  347. register struct hostent *hp;
  348. {
  349. register char **cp;
  350. #ifdef __NBSD_LIBC
  351. struct in_addr **hptr;
  352. #else
  353. register ipaddr_t **hptr;
  354. #endif
  355. printf("Name: %s\n", hp->h_name);
  356. printf("Address:");
  357. #ifdef __NBSD_LIBC
  358. for (hptr = (struct in_addr **)hp->h_addr_list; *hptr; hptr++)
  359. printf(" %s", inet_ntoa(**hptr));
  360. #else
  361. for (hptr = (ipaddr_t **)hp->h_addr_list; *hptr; hptr++)
  362. printf(" %s", inet_ntoa(*(ipaddr_t *)*hptr));
  363. #endif
  364. printf("\nAliases:");
  365. for (cp = hp->h_aliases; cp && *cp && **cp; cp++)
  366. printf(" %s", *cp);
  367. printf("\n\n");
  368. }
  369. static void
  370. hperror(err_no)
  371. int err_no;
  372. {
  373. switch(err_no) {
  374. case HOST_NOT_FOUND:
  375. fprintf(stderr,"Host not found.\n");
  376. break;
  377. case TRY_AGAIN:
  378. fprintf(stderr,"Host not found, try again.\n");
  379. break;
  380. case NO_RECOVERY:
  381. fprintf(stderr,"No recovery, Host not found.\n");
  382. break;
  383. case NO_ADDRESS:
  384. fprintf(stderr,"There is an entry for this host, but it doesn't have what you requested.\n");
  385. break;
  386. }
  387. }
  388. typedef union querybuf {
  389. HEADER qb1;
  390. u8_t qb2[PACKETSZ];
  391. } querybuf_t;
  392. static u8_t hostbuf[BUFSIZ+1];
  393. static int
  394. gethostinfo(name)
  395. char *name;
  396. {
  397. register char *cp, **domain;
  398. int n;
  399. int hp;
  400. int nDomain;
  401. if (strcmp(name, ".") == 0)
  402. return(getdomaininfo(name, NULL));
  403. for (cp = name, n = 0; *cp; cp++)
  404. if (*cp == '.')
  405. n++;
  406. if (n && cp[-1] == '.') {
  407. if (cp[-1] == '.')
  408. cp[-1] = 0;
  409. hp = getdomaininfo(name, (char *)NULL);
  410. if (cp[-1] == 0)
  411. cp[-1] = '.';
  412. return (hp);
  413. }
  414. if (n == 0 && (cp = __hostalias(name))) {
  415. if (verbose)
  416. printf("Aliased to \"%s\"\n", cp);
  417. _res.options |= RES_DEFNAMES;
  418. return (getdomaininfo(cp, (char *)NULL));
  419. }
  420. #ifdef MAXDS
  421. for (nDomain = 0;
  422. _res.defdname_list[nDomain][0] != 0;
  423. nDomain++) {
  424. for (domain = _res.dnsrch_list[nDomain]; *domain; domain++) {
  425. if (verbose)
  426. printf("Trying domain \"%s\"\n", *domain);
  427. hp = getdomaininfo(name, *domain);
  428. if (hp)
  429. return (hp);
  430. }
  431. }
  432. #else
  433. for (domain = _res.dnsrch; *domain; domain++) {
  434. if (verbose)
  435. printf("Trying domain \"%s\"\n", *domain);
  436. hp = getdomaininfo(name, *domain);
  437. if (hp)
  438. return (hp);
  439. }
  440. #endif
  441. if (h_errno != HOST_NOT_FOUND ||
  442. (_res.options & RES_DNSRCH) == 0)
  443. return (0);
  444. if (verbose)
  445. printf("Trying null domain\n");
  446. return (getdomaininfo(name, (char *)NULL));
  447. }
  448. static int
  449. getdomaininfo(name, domain)
  450. char *name, *domain;
  451. {
  452. return getinfo(name, domain, gettype ? gettype : getdeftype);
  453. }
  454. static int
  455. getinfo(name, domain, type)
  456. char *name, *domain;
  457. {
  458. HEADER *hp;
  459. u8_t *eom, *bp, *cp;
  460. querybuf_t buf, answer;
  461. int n, n1, i, j, nmx, ancount, nscount, arcount, qdcount, buflen;
  462. char host[2*MAXDNAME+2];
  463. if (domain == NULL)
  464. (void)sprintf(host, "%.*s", MAXDNAME, name);
  465. else
  466. (void)sprintf(host, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain);
  467. n = res_mkquery(QUERY, host, getclass, type, (char *)NULL, 0, NULL,
  468. (char *)&buf, sizeof(buf));
  469. if (n < 0) {
  470. if (_res.options & RES_DEBUG)
  471. printf("res_mkquery failed\n");
  472. h_errno = NO_RECOVERY;
  473. return(0);
  474. }
  475. n = res_send((char *)&buf, n, (char *)&answer, sizeof(answer));
  476. if (n < 0) {
  477. if (_res.options & RES_DEBUG)
  478. printf("res_send failed\n");
  479. h_errno = TRY_AGAIN;
  480. return (0);
  481. }
  482. eom = (u8_t *)&answer + n;
  483. return(printinfo(&answer, eom, T_ANY, 0));
  484. }
  485. static int
  486. printinfo(answer, eom, filter, isls)
  487. querybuf_t *answer;
  488. u8_t *eom;
  489. int filter;
  490. int isls;
  491. {
  492. HEADER *hp;
  493. u8_t *bp, *cp;
  494. int nmx, ancount, nscount, arcount, qdcount, buflen;
  495. /*
  496. * find first satisfactory answer
  497. */
  498. hp = (HEADER *) answer;
  499. ancount = ntohs(hp->dh_ancount);
  500. qdcount = ntohs(hp->dh_qdcount);
  501. nscount = ntohs(hp->dh_nscount);
  502. arcount = ntohs(hp->dh_arcount);
  503. if (_res.options & RES_DEBUG || (verbose && isls == 0))
  504. printf("rcode = %d (%s), ancount=%d\n",
  505. #ifdef __NBSD_LIBC
  506. hp->rcode,
  507. DecodeError(hp->rcode),
  508. #else
  509. hp->dh_flag2 & DHF_RCODE,
  510. DecodeError(hp->dh_flag2 & DHF_RCODE),
  511. #endif
  512. ancount);
  513. if (
  514. #ifdef __NBSD_LIBC
  515. hp->rcode != NOERROR ||
  516. #else
  517. hp->dh_flag2 & DHF_RCODE != NOERROR ||
  518. #endif
  519. (ancount+nscount+arcount) == 0) {
  520. switch (
  521. #ifdef __NBSD_LIBC
  522. hp->rcode
  523. #else
  524. hp->dh_flag2 & DHF_RCODE
  525. #endif
  526. ) {
  527. case NXDOMAIN:
  528. /* Check if it's an authoritive answer */
  529. if (
  530. #ifdef __NBSD_LIBC
  531. hp->aa
  532. #else
  533. hp->dh_flag1 & DHF_AA
  534. #endif
  535. ) {
  536. h_errno = HOST_NOT_FOUND;
  537. return(0);
  538. } else {
  539. h_errno = TRY_AGAIN;
  540. return(0);
  541. }
  542. case SERVFAIL:
  543. h_errno = TRY_AGAIN;
  544. return(0);
  545. #ifdef OLDJEEVES
  546. /*
  547. * Jeeves (TOPS-20 server) still does not
  548. * support MX records. For the time being,
  549. * we must accept FORMERRs as the same as
  550. * NOERROR.
  551. */
  552. case FORMERR:
  553. #endif /* OLDJEEVES */
  554. case NOERROR:
  555. /* TpB - set a return error for this case. NO_DATA */
  556. h_errno = NO_DATA;
  557. return(0); /* was 1,but now indicates exception */
  558. #ifndef OLDJEEVES
  559. case FORMERR:
  560. #endif /* OLDJEEVES */
  561. case NOTIMP:
  562. case REFUSED:
  563. h_errno = NO_RECOVERY;
  564. return(0);
  565. }
  566. return (0);
  567. }
  568. bp = hostbuf;
  569. nmx = 0;
  570. buflen = sizeof(hostbuf);
  571. cp = (u8_t *)answer + sizeof(HEADER);
  572. if (qdcount) {
  573. cp += dn_skipname((u8_t *)cp,(u8_t *)eom) + QFIXEDSZ;
  574. while (--qdcount > 0)
  575. cp += dn_skipname((u8_t *)cp,(u8_t *)eom) + QFIXEDSZ;
  576. }
  577. if (ancount) {
  578. if (!
  579. #ifdef __NBSD_LIBC
  580. hp->aa
  581. #else
  582. (hp->dh_flag1 & DHF_AA)
  583. #endif
  584. )
  585. if (verbose && isls == 0)
  586. printf("The following answer is not authoritative:\n");
  587. while (--ancount >= 0 && cp && cp < eom) {
  588. cp = pr_rr(cp, (u8_t *)answer, stdout, filter);
  589. /*
  590. * When we ask for address and there is a CNAME, it seems to return
  591. * both the CNAME and the address. Since we trace down the CNAME
  592. * chain ourselves, we don't really want to print the address at
  593. * this point.
  594. */
  595. if (cname && ! verbose)
  596. return (1);
  597. }
  598. }
  599. if (! verbose)
  600. return (1);
  601. if (nscount) {
  602. printf("For authoritative answers, see:\n");
  603. while (--nscount >= 0 && cp && cp < eom) {
  604. cp = pr_rr(cp, (u8_t *)answer, stdout, filter);
  605. }
  606. }
  607. if (arcount) {
  608. printf("Additional information:\n");
  609. while (--arcount >= 0 && cp && cp < eom) {
  610. cp = pr_rr(cp, (u8_t *)answer, stdout, filter);
  611. }
  612. }
  613. return(1);
  614. }
  615. static u8_t cnamebuf[MAXDNAME];
  616. /*
  617. * Print resource record fields in human readable form.
  618. */
  619. static u8_t *
  620. pr_rr(cp, msg, file, filter)
  621. u8_t *cp, *msg;
  622. FILE *file;
  623. int filter;
  624. {
  625. int type, class, dlen, n, c, proto, ttl;
  626. #ifdef __NBSD_LIBC
  627. struct in_addr inaddr;
  628. #else
  629. ipaddr_t inaddr;
  630. #endif
  631. u8_t *cp1;
  632. struct protoent *protop;
  633. struct servent *servp;
  634. char punc;
  635. int doprint;
  636. u8_t name[MAXDNAME];
  637. if ((cp = pr_cdname(cp, msg, name, sizeof(name))) == NULL)
  638. return (NULL); /* compression error */
  639. type = _getshort(cp);
  640. cp += sizeof(u_short);
  641. class = _getshort(cp);
  642. cp += sizeof(u_short);
  643. ttl = _getlong(cp);
  644. cp += sizeof(u_long);
  645. if (filter == type || filter == T_ANY ||
  646. (filter == T_A && (type == T_PTR || type == T_NS)))
  647. doprint = 1;
  648. else
  649. doprint = 0;
  650. if (doprint)
  651. if (verbose)
  652. fprintf(file,"%s\t%d%s\t%s",
  653. name, ttl, pr_class(class), pr_type(type));
  654. else {
  655. fprintf(file,"%s%s %s",name, pr_class(class), pr_type(type));
  656. }
  657. if (verbose)
  658. punc = '\t';
  659. else
  660. punc = ' ';
  661. dlen = _getshort(cp);
  662. cp += sizeof(u_short);
  663. cp1 = cp;
  664. /*
  665. * Print type specific data, if appropriate
  666. */
  667. switch (type) {
  668. case T_A:
  669. switch (class) {
  670. case C_IN:
  671. bcopy((char *)cp, (char *)&inaddr, sizeof(inaddr));
  672. if (dlen == 4) {
  673. if (doprint)
  674. fprintf(file,"%c%s", punc,
  675. inet_ntoa(inaddr));
  676. cp += dlen;
  677. } else if (dlen == 7) {
  678. if (doprint) {
  679. fprintf(file,"%c%s", punc,
  680. inet_ntoa(inaddr));
  681. fprintf(file,", protocol = %d", cp[4]);
  682. fprintf(file,", port = %d",
  683. (cp[5] << 8) + cp[6]);
  684. }
  685. cp += dlen;
  686. }
  687. break;
  688. }
  689. break;
  690. case T_CNAME:
  691. if (dn_expand(msg, msg + 512, cp, cnamebuf,
  692. sizeof(cnamebuf)-1) >= 0) {
  693. strcat((char *) cnamebuf, ".");
  694. if (gettype != T_CNAME && gettype != T_ANY)
  695. cname = cnamebuf;
  696. }
  697. case T_MB:
  698. #ifdef OLDRR
  699. case T_MD:
  700. case T_MF:
  701. #endif /* OLDRR */
  702. case T_MG:
  703. case T_MR:
  704. case T_NS:
  705. case T_PTR:
  706. cp = pr_cdname(cp, msg, name, sizeof(name));
  707. if (doprint)
  708. fprintf(file,"%c%s",punc, name);
  709. break;
  710. case T_HINFO:
  711. if ((n = *cp++)) {
  712. if (doprint)
  713. fprintf(file,"%c%.*s", punc, n, cp);
  714. cp += n;
  715. }
  716. if ((n = *cp++)) {
  717. if (doprint)
  718. fprintf(file,"%c%.*s", punc, n, cp);
  719. cp += n;
  720. }
  721. break;
  722. case T_SOA:
  723. cp = pr_cdname(cp, msg, name, sizeof(name));
  724. if (doprint)
  725. fprintf(file,"\t%s", name);
  726. cp = pr_cdname(cp, msg, name, sizeof(name));
  727. if (doprint)
  728. fprintf(file," %s", name);
  729. if (doprint)
  730. fprintf(file,"(\n\t\t\t%d\t;serial (version)", _getlong(cp));
  731. cp += sizeof(u_long);
  732. if (doprint)
  733. fprintf(file,"\n\t\t\t%d\t;refresh period", _getlong(cp));
  734. cp += sizeof(u_long);
  735. if (doprint)
  736. fprintf(file,"\n\t\t\t%d\t;retry refresh this often", _getlong(cp));
  737. cp += sizeof(u_long);
  738. if (doprint)
  739. fprintf(file,"\n\t\t\t%d\t;expiration period", _getlong(cp));
  740. cp += sizeof(u_long);
  741. if (doprint)
  742. fprintf(file,"\n\t\t\t%d\t;minimum TTL\n\t\t\t)", _getlong(cp));
  743. cp += sizeof(u_long);
  744. break;
  745. case T_MX:
  746. if (doprint) {
  747. if (verbose)
  748. fprintf(file,"\t%d ",_getshort(cp));
  749. else
  750. fprintf(file," (pri=%d) by ",_getshort(cp));
  751. }
  752. cp += sizeof(u_short);
  753. cp = pr_cdname(cp, msg, name, sizeof(name));
  754. if (doprint)
  755. fprintf(file, "%s", name);
  756. break;
  757. case T_MINFO:
  758. cp = pr_cdname(cp, msg, name, sizeof(name));
  759. if (doprint)
  760. fprintf(file,"%c%s",punc, name);
  761. cp = pr_cdname(cp, msg, name, sizeof(name));
  762. if (doprint)
  763. fprintf(file," %s", name);
  764. break;
  765. /* Roy start */
  766. case T_TXT:
  767. if ((n = *cp++)) {
  768. if (doprint)
  769. fprintf(file,"%c%.*s", punc, n, cp);
  770. cp += n;
  771. }
  772. break;
  773. /* Roy end */
  774. #ifndef __NBSD_LIBC
  775. case T_UINFO:
  776. if (doprint)
  777. fprintf(file,"%c%s", punc, cp);
  778. cp += dlen;
  779. break;
  780. case T_UID:
  781. case T_GID:
  782. if (dlen == 4) {
  783. if (doprint)
  784. fprintf(file,"%c%ld", punc, _getlong(cp));
  785. cp += sizeof(int);
  786. }
  787. break;
  788. #endif
  789. case T_WKS:
  790. if (dlen < sizeof(u_long) + 1)
  791. break;
  792. bcopy((char *)cp, (char *)&inaddr, sizeof(inaddr));
  793. cp += sizeof(u_long);
  794. proto = *cp++;
  795. protop = getprotobynumber(proto);
  796. if (doprint)
  797. if (protop)
  798. fprintf(file,"%c%s %s", punc,
  799. inet_ntoa(inaddr), protop->p_name);
  800. else
  801. fprintf(file,"%c%s %d", punc,
  802. inet_ntoa(inaddr), proto);
  803. n = 0;
  804. while (cp < cp1 + dlen) {
  805. c = *cp++;
  806. do {
  807. if (c & 0200) {
  808. servp = NULL;
  809. if (protop)
  810. servp = getservbyport (htons(n),
  811. protop->p_name);
  812. if (doprint)
  813. if (servp)
  814. fprintf(file, " %s", servp->s_name);
  815. else
  816. fprintf(file, " %d", n);
  817. }
  818. c <<= 1;
  819. } while (++n & 07);
  820. }
  821. break;
  822. default:
  823. if (doprint)
  824. fprintf(file,"%c???", punc);
  825. cp += dlen;
  826. }
  827. if (cp != cp1 + dlen)
  828. fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
  829. if (doprint)
  830. fprintf(file,"\n");
  831. return (cp);
  832. }
  833. static char nbuf[20];
  834. /*
  835. * Return a string for the type
  836. */
  837. static char *
  838. pr_type(type)
  839. int type;
  840. {
  841. switch (type) {
  842. case T_A:
  843. return(verbose? "A" : "has address");
  844. case T_NS: /* authoritative server */
  845. return("NS");
  846. #ifdef OLDRR
  847. case T_MD: /* mail destination */
  848. return("MD");
  849. case T_MF: /* mail forwarder */
  850. return("MF");
  851. #endif /* OLDRR */
  852. case T_CNAME: /* connonical name */
  853. return(verbose? "CNAME" : "is a nickname for");
  854. case T_SOA: /* start of authority zone */
  855. return("SOA");
  856. case T_MB: /* mailbox domain name */
  857. return("MB");
  858. case T_MG: /* mail group member */
  859. return("MG");
  860. case T_MX: /* mail routing info */
  861. return(verbose? "MX" : "mail is handled");
  862. /* Roy start */
  863. case T_TXT: /* TXT - descriptive info */
  864. return(verbose? "TXT" : "descriptive text");
  865. /* Roy end */
  866. case T_MR: /* mail rename name */
  867. return("MR");
  868. case T_NULL: /* null resource record */
  869. return("NULL");
  870. case T_WKS: /* well known service */
  871. return("WKS");
  872. case T_PTR: /* domain name pointer */
  873. return("PTR");
  874. case T_HINFO: /* host information */
  875. return("HINFO");
  876. case T_MINFO: /* mailbox information */
  877. return("MINFO");
  878. case T_AXFR: /* zone transfer */
  879. return("AXFR");
  880. case T_MAILB: /* mail box */
  881. return("MAILB");
  882. case T_MAILA: /* mail address */
  883. return("MAILA");
  884. case T_ANY: /* matches any type */
  885. return("ANY");
  886. #ifndef __NBSD_LIBC
  887. case T_UINFO:
  888. return("UINFO");
  889. case T_UID:
  890. return("UID");
  891. case T_GID:
  892. return("GID");
  893. #endif
  894. default:
  895. return (sprintf(nbuf, "%d", type) == EOF ? NULL : nbuf);
  896. }
  897. }
  898. /*
  899. * Return a mnemonic for class
  900. */
  901. static char *
  902. pr_class(class)
  903. int class;
  904. {
  905. switch (class) {
  906. case C_IN: /* internet class */
  907. return(verbose? " IN" : "");
  908. case C_CHAOS: /* chaos class */
  909. return(verbose? " CHAOS" : "");
  910. case C_HS: /* Hesiod class */
  911. return(verbose? " HS" : "");
  912. case C_ANY: /* matches any class */
  913. return(" ANY");
  914. default:
  915. return (sprintf(nbuf," %d", class) == EOF ? NULL : nbuf);
  916. }
  917. }
  918. static u8_t *
  919. pr_cdname(cp, msg, name, namelen)
  920. u8_t *cp, *msg;
  921. u8_t *name;
  922. int namelen;
  923. {
  924. int n;
  925. if ((n = dn_expand(msg, msg + 512, cp, name, namelen - 2)) < 0)
  926. return (NULL);
  927. if (name[0] == '\0') {
  928. name[0] = '.';
  929. name[1] = '\0';
  930. }
  931. return (cp + n);
  932. }
  933. char *resultcodes[] = {
  934. "NOERROR",
  935. "FORMERR",
  936. "SERVFAIL",
  937. "NXDOMAIN",
  938. "NOTIMP",
  939. "REFUSED",
  940. "6",
  941. "7",
  942. "8",
  943. "9",
  944. "10",
  945. "11",
  946. "12",
  947. "13",
  948. "14",
  949. "NOCHANGE",
  950. };
  951. /*
  952. ******************************************************************************
  953. *
  954. * ListHosts --
  955. *
  956. * Requests the name server to do a zone transfer so we
  957. * find out what hosts it knows about.
  958. *
  959. * Results:
  960. * SUCCESS the listing was successful.
  961. * ERROR the server could not be contacted because
  962. * a socket could not be obtained or an error
  963. * occured while receiving, or the output file
  964. * could not be opened.
  965. *
  966. ******************************************************************************
  967. */
  968. static int
  969. ListHosts(namePtr, queryType)
  970. char *namePtr;
  971. int queryType; /* e.g. T_A */
  972. {
  973. querybuf_t buf, answer;
  974. HEADER *headerPtr;
  975. int msglen;
  976. int amtToRead;
  977. int numRead;
  978. int i;
  979. int numAnswers = 0;
  980. int result;
  981. int soacnt = 0;
  982. u_short len;
  983. int dlen;
  984. int type;
  985. int nscount;
  986. u8_t *cp, *nmp;
  987. u8_t name[NAME_LEN];
  988. char dname[2][NAME_LEN];
  989. u8_t domain[NAME_LEN];
  990. /* names and addresses of name servers to try */
  991. #define NUMNS 8
  992. char nsname[NUMNS][NAME_LEN];
  993. int nshaveaddr[NUMNS];
  994. #define IPADDRSIZE 4
  995. #define NUMNSADDR 16
  996. char nsipaddr[NUMNSADDR][IPADDRSIZE];
  997. int numns;
  998. int numnsaddr;
  999. int thisns;
  1000. struct hostent *hp;
  1001. enum {
  1002. NO_ERRORS,
  1003. ERR_READING_LEN,
  1004. ERR_READING_MSG,
  1005. ERR_PRINTING
  1006. } error = NO_ERRORS;
  1007. char *tcp_serv_name;
  1008. int tcp_fd;
  1009. nwio_tcpconf_t tcpconf;
  1010. nwio_tcpcl_t clopt;
  1011. int terrno;
  1012. /*
  1013. * normalize to not have trailing dot. We do string compares below
  1014. * of info from name server, and it won't have trailing dots.
  1015. */
  1016. i = strlen(namePtr);
  1017. if (namePtr[i-1] == '.')
  1018. namePtr[i-1] = 0;
  1019. if (server_specified) {
  1020. bcopy((char *)&_res.nsaddr, nsipaddr[0], IPADDRSIZE);
  1021. numnsaddr = 1;
  1022. }
  1023. else {
  1024. /*
  1025. * First we have to find out where to look. This needs a NS query,
  1026. * possibly followed by looking up addresses for some of the names.
  1027. */
  1028. msglen = res_mkquery(QUERY, namePtr, C_IN, T_NS,
  1029. (char *)0, 0, (struct rrec *)0,
  1030. (char *)&buf, sizeof(buf));
  1031. if (msglen < 0) {
  1032. printf("res_mkquery failed\n");
  1033. return (ERROR);
  1034. }
  1035. msglen = res_send((char *)&buf,msglen,(char *)&answer, sizeof(answer));
  1036. if (msglen < 0) {
  1037. printf("Unable to get to nameserver -- try again later\n");
  1038. return (ERROR);
  1039. }
  1040. if (_res.options & RES_DEBUG || verbose)
  1041. printf("rcode = %d (%s), ancount=%d\n",
  1042. #ifdef __NBSD_LIBC
  1043. answer.qb1.rcode,
  1044. DecodeError(answer.qb1.rcode),
  1045. ntohs(answer.qb1.ancount)
  1046. #else
  1047. answer.qb1.dh_flag2 & DHF_RCODE,
  1048. DecodeError(answer.qb1.dh_flag2 & DHF_RCODE),
  1049. ntohs(answer.qb1.dh_ancount)
  1050. #endif
  1051. );
  1052. /*
  1053. * Analyze response to our NS lookup
  1054. */
  1055. #ifdef __NBSD_LIBC
  1056. nscount = ntohs(answer.qb1.ancount) +
  1057. ntohs(answer.qb1.nscount) +
  1058. ntohs(answer.qb1.arcount);
  1059. #else
  1060. nscount = ntohs(answer.qb1.dh_ancount) + ntohs(answer.qb1.dh_nscount) +
  1061. ntohs(answer.qb1.dh_arcount);
  1062. #endif
  1063. if (
  1064. #ifdef __NBSD_LIBC
  1065. answer.qb1.rcode != NOERROR || nscount == 0
  1066. #else
  1067. answer.qb1.dh_flag2 & DHF_RCODE != NOERROR || nscount == 0
  1068. #endif
  1069. ) {
  1070. switch (
  1071. #ifdef __NBSD_LIBC
  1072. answer.qb1.rcode
  1073. #else
  1074. answer.qb1.dh_flag2 & DHF_RCODE
  1075. #endif
  1076. ) {
  1077. case NXDOMAIN:
  1078. /* Check if it's an authoritive answer */
  1079. if (
  1080. #ifdef __NBSD_LIBC
  1081. answer.qb1.aa
  1082. #else
  1083. answer.qb1.dh_flag1 & DHF_AA
  1084. #endif
  1085. ) {
  1086. printf("No such domain\n");
  1087. } else {
  1088. printf("Unable to get information about domain -- try again later.\n");
  1089. }
  1090. break;
  1091. case SERVFAIL:
  1092. printf("Unable to get information about that domain -- try again later.\n");
  1093. break;
  1094. case NOERROR:
  1095. printf("That domain exists, but seems to be a leaf node.\n");
  1096. break;
  1097. case FORMERR:
  1098. case NOTIMP:
  1099. case REFUSED:
  1100. printf("Unrecoverable error looking up domain name.\n");
  1101. break;
  1102. }
  1103. return (0);
  1104. }
  1105. cp = answer.qb2 + sizeof(HEADER);
  1106. if (ntohs(answer.qb1.dh_qdcount) > 0)
  1107. cp += dn_skipname(cp, answer.qb2 + msglen) + QFIXEDSZ;
  1108. numns = 0;
  1109. numnsaddr = 0;
  1110. /*
  1111. * Look at response from NS lookup for NS and A records.
  1112. */
  1113. for (;nscount; nscount--) {
  1114. cp += dn_expand(answer.qb2, answer.qb2 + msglen, cp,
  1115. domain, sizeof(domain));
  1116. type = _getshort(cp);
  1117. cp += sizeof(u_short) + sizeof(u_short) + sizeof(u_long);
  1118. dlen = _getshort(cp);
  1119. cp += sizeof(u_short);
  1120. if (type == T_NS) {
  1121. if (dn_expand(answer.qb2, answer.qb2 + msglen, cp,
  1122. name, sizeof(name)) >= 0) {
  1123. if (numns < NUMNS && strcasecmp((char *)domain, namePtr) == 0) {
  1124. for (i = 0; i < numns; i++)
  1125. if (strcasecmp(nsname[i], (char *)name) == 0)
  1126. break; /* duplicate */
  1127. if (i >= numns) {
  1128. strncpy(nsname[numns], (char *)name, sizeof(name));
  1129. nshaveaddr[numns] = 0;
  1130. numns++;
  1131. }
  1132. }
  1133. }
  1134. }
  1135. else if (type == T_A) {
  1136. if (numnsaddr < NUMNSADDR)
  1137. for (i = 0; i < numns; i++) {
  1138. if (strcasecmp(nsname[i], (char *)domain) == 0) {
  1139. nshaveaddr[i]++;
  1140. bcopy((char *)cp, nsipaddr[numnsaddr],IPADDRSIZE);
  1141. numnsaddr++;
  1142. break;
  1143. }
  1144. }
  1145. }
  1146. cp += dlen;
  1147. }
  1148. /*
  1149. * Usually we'll get addresses for all the servers in the additional
  1150. * info section. But in case we don't, look up their addresses.
  1151. */
  1152. for (i = 0; i < numns; i++) {
  1153. if (! nshaveaddr[i]) {
  1154. register long **hptr;
  1155. int numaddrs = 0;
  1156. hp = gethostbyname(nsname[i]);
  1157. if (hp) {
  1158. for (hptr = (long **)hp->h_addr_list; *hptr; hptr++)
  1159. if (numnsaddr < NUMNSADDR) {
  1160. bcopy((char *)*hptr, nsipaddr[numnsaddr],IPADDRSIZE);
  1161. numnsaddr++;
  1162. numaddrs++;
  1163. }
  1164. }
  1165. if (_res.options & RES_DEBUG || verbose)
  1166. printf("Found %d addresses for %s by extra query\n",
  1167. numaddrs, nsname[i]);
  1168. }
  1169. else
  1170. if (_res.options & RES_DEBUG || verbose)
  1171. printf("Found %d addresses for %s\n",
  1172. nshaveaddr[i], nsname[i]);
  1173. }
  1174. }
  1175. /*
  1176. * Now nsipaddr has numnsaddr addresses for name servers that
  1177. * serve the requested domain. Now try to find one that will
  1178. * accept a zone transfer.
  1179. */
  1180. thisns = 0;
  1181. again:
  1182. numAnswers = 0;
  1183. soacnt = 0;
  1184. /*
  1185. * Create a query packet for the requested domain name.
  1186. *
  1187. */
  1188. msglen = res_mkquery(QUERY, namePtr, getclass, T_AXFR,
  1189. (char *)0, 0, (struct rrec *)0,
  1190. (char *) &buf, sizeof(buf));
  1191. if (msglen < 0) {
  1192. if (_res.options & RES_DEBUG) {
  1193. fprintf(stderr, "ListHosts: Res_mkquery failed\n");
  1194. }
  1195. return (ERROR);
  1196. }
  1197. /*
  1198. * Set up a virtual circuit to the server.
  1199. */
  1200. tcp_serv_name= getenv("TCP_DEVICE");
  1201. if (!tcp_serv_name)
  1202. tcp_serv_name= TCP_DEVICE;
  1203. for (;thisns < numnsaddr; thisns++)
  1204. {
  1205. tcp_fd= open(tcp_serv_name, O_RDWR);
  1206. if (tcp_fd == -1)
  1207. {
  1208. fprintf(stderr, "unable to open '%s': %s\n", tcp_serv_name,
  1209. strerror(errno));
  1210. return ERROR;
  1211. }
  1212. tcpconf.nwtc_flags= NWTC_EXCL | NWTC_LP_SEL | NWTC_SET_RA |
  1213. NWTC_SET_RP;
  1214. tcpconf.nwtc_remaddr= *(ipaddr_t *)nsipaddr[thisns];
  1215. #ifdef __NBSD_LIBC
  1216. tcpconf.nwtc_remport= _res.nsaddr.sin_port;
  1217. #else
  1218. tcpconf.nwtc_remport= _res.nsport_list[0];
  1219. #endif
  1220. result= ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
  1221. if (result == -1)
  1222. {
  1223. fprintf(stderr, "tcp_ioc_setconf failed: %s\n",
  1224. strerror(errno));
  1225. close(tcp_fd);
  1226. return ERROR;
  1227. }
  1228. if (_res.options & RES_DEBUG || verbose)
  1229. printf("Trying %s\n", inet_ntoa(tcpconf.nwtc_remaddr));
  1230. clopt.nwtcl_flags= 0;
  1231. result= ioctl(tcp_fd, NWIOTCPCONN, &clopt);
  1232. if (result == 0)
  1233. break;
  1234. terrno= errno;
  1235. if (verbose)
  1236. fprintf(stderr,
  1237. "Connection failed, trying next server: %s\n",
  1238. strerror(errno));
  1239. close(tcp_fd);
  1240. }
  1241. if (thisns >= numnsaddr) {
  1242. printf("No server for that domain responded\n");
  1243. if (!verbose)
  1244. fprintf(stderr, "Error from the last server was: %s\n",
  1245. strerror(terrno));
  1246. return(ERROR);
  1247. }
  1248. /*
  1249. * Send length & message for zone transfer
  1250. */
  1251. len = htons(msglen);
  1252. result= tcpip_writeall(tcp_fd, (char *)&len, sizeof(len));
  1253. if (result != sizeof(len))
  1254. {
  1255. fprintf(stderr, "write failed: %s\n", strerror(errno));
  1256. close(tcp_fd);
  1257. return ERROR;
  1258. }
  1259. result= tcpip_writeall(tcp_fd, (char *)&buf, msglen);
  1260. if (result != msglen)
  1261. {
  1262. fprintf(stderr, "write failed: %s\n",
  1263. strerror(errno));
  1264. close(tcp_fd);
  1265. return ERROR;
  1266. }
  1267. filePtr = stdout;
  1268. while (1) {
  1269. /*
  1270. * Read the length of the response.
  1271. */
  1272. cp = (u8_t *) &buf;
  1273. amtToRead = sizeof(u_short);
  1274. while(amtToRead > 0)
  1275. {
  1276. result = read(tcp_fd, (char *)cp, amtToRead);
  1277. if (result <= 0)
  1278. break;
  1279. cp += result;
  1280. amtToRead -= result;
  1281. }
  1282. if (amtToRead) {
  1283. error = ERR_READING_LEN;
  1284. break;
  1285. }
  1286. if ((len = htons(*(u_short *)&buf)) == 0) {
  1287. break; /* nothing left to read */
  1288. }
  1289. /*
  1290. * Read the response.
  1291. */
  1292. amtToRead = len;
  1293. cp = (u8_t *) &buf;
  1294. while(amtToRead > 0)
  1295. {
  1296. result = read(tcp_fd, (char *)cp, amtToRead);
  1297. if (result<= 0)
  1298. break;
  1299. cp += result;
  1300. amtToRead -= result;
  1301. }
  1302. if (amtToRead) {
  1303. error = ERR_READING_MSG;
  1304. break;
  1305. }
  1306. #ifdef __NBSD_LIBC
  1307. i = buf.qb1.rcode;
  1308. #else
  1309. i = buf.qb1.dh_flag2 & DHF_RCODE;
  1310. #endif
  1311. if (i != NOERROR || ntohs(buf.qb1.dh_ancount) == 0) {
  1312. if ((thisns+1) < numnsaddr &&
  1313. (i == SERVFAIL || i == NOTIMP || i == REFUSED)) {
  1314. if (_res.options & RES_DEBUG || verbose)
  1315. printf("Server failed, trying next server: %s\n",
  1316. i != NOERROR ?
  1317. DecodeError(i) : "Premature end of data");
  1318. close(tcp_fd);
  1319. thisns++;
  1320. goto again;
  1321. }
  1322. printf("Server failed: %s\n",
  1323. i != NOERROR ? DecodeError(i) : "Premature end of data");
  1324. break;
  1325. }
  1326. result = printinfo(&buf, cp, queryType, 1);
  1327. if (! result) {
  1328. error = ERR_PRINTING;
  1329. break;
  1330. }
  1331. numAnswers++;
  1332. cp = buf.qb2 + sizeof(HEADER);
  1333. if (ntohs(buf.qb1.dh_qdcount) > 0)
  1334. cp += dn_skipname(cp, buf.qb2 + len) + QFIXEDSZ;
  1335. nmp = cp;
  1336. cp += dn_skipname(cp, (u_char *)&buf + len);
  1337. if ((_getshort(cp) == T_SOA)) {
  1338. dn_expand(buf.qb2, buf.qb2 + len, nmp, (u8_t *)dname[soacnt],
  1339. sizeof(dname[0]));
  1340. if (soacnt) {
  1341. if (strcmp(dname[0], dname[1]) == 0)
  1342. break;
  1343. } else
  1344. soacnt++;
  1345. }
  1346. }
  1347. close(tcp_fd);
  1348. switch (error) {
  1349. case NO_ERRORS:
  1350. return (SUCCESS);
  1351. case ERR_READING_LEN:
  1352. return(ERROR);
  1353. case ERR_PRINTING:
  1354. fprintf(stderr,"*** Error during listing of %s: %s\n",
  1355. namePtr, DecodeError(result));
  1356. return(result);
  1357. case ERR_READING_MSG:
  1358. headerPtr = (HEADER *) &buf;
  1359. fprintf(stderr,"ListHosts: error receiving zone transfer:\n");
  1360. fprintf(stderr,
  1361. " result: %s, answers = %d, authority = %d, additional = %d\n",
  1362. #ifdef __NBSD_LIBC
  1363. resultcodes[headerPtr->rcode],
  1364. #else
  1365. resultcodes[headerPtr->dh_flag2 & DHF_RCODE],
  1366. #endif
  1367. ntohs(headerPtr->dh_ancount),
  1368. ntohs(headerPtr->dh_nscount),
  1369. ntohs(headerPtr->dh_arcount));
  1370. return(ERROR);
  1371. default:
  1372. return(ERROR);
  1373. }
  1374. }
  1375. static char *
  1376. DecodeError(result)
  1377. int result;
  1378. {
  1379. switch(result) {
  1380. case NOERROR: return("Success"); break;
  1381. case FORMERR: return("Format error"); break;
  1382. case SERVFAIL: return("Server failed"); break;
  1383. case NXDOMAIN: return("Non-existent domain"); break;
  1384. case NOTIMP: return("Not implemented"); break;
  1385. case REFUSED: return("Query refused"); break;
  1386. case NOCHANGE: return("No change"); break;
  1387. case NO_INFO: return("No information"); break;
  1388. case ERROR: return("Unspecified error"); break;
  1389. case TIME_OUT: return("Timed out"); break;
  1390. case NONAUTH: return("Non-authoritative answer"); break;
  1391. default: break;
  1392. }
  1393. return("BAD ERROR VALUE");
  1394. }
  1395. static int tcpip_writeall(fd, buf, siz)
  1396. int fd;
  1397. char *buf;
  1398. unsigned siz;
  1399. {
  1400. unsigned siz_org;
  1401. int nbytes;
  1402. siz_org= siz;
  1403. while (siz)
  1404. {
  1405. nbytes= write(fd, buf, siz);
  1406. if (nbytes == -1)
  1407. return nbytes;
  1408. assert(siz >= nbytes);
  1409. buf += nbytes;
  1410. siz -= nbytes;
  1411. }
  1412. return siz_org;
  1413. }