home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / tools / dnsquery.c < prev    next >
C/C++ Source or Header  |  1994-06-11  |  5KB  |  203 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/nameser.h>
  6. #include <netdb.h>
  7. #include <resolv.h>
  8. #include <errno.h>
  9.  
  10. #include "../conf/portability.h"
  11.  
  12. extern int errno;
  13. extern int h_errno;
  14. extern char *h_errlist[];
  15.  
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     char name[MAXDNAME];
  21.     u_char answer[8*1024];
  22.     register int c, i = 0;
  23.     unsigned long ul;
  24.     int nameservers = 0, class, type, len;
  25.     struct in_addr q_nsaddr[MAXNS];
  26.     struct hostent *q_nsname;
  27.     extern int optind, opterr;
  28.     extern char *optarg;
  29.     HEADER *hp;
  30.     int stream = 0, debug = 0;
  31.  
  32.     /* set defaults */
  33.     len = MAXDNAME;
  34.     gethostname(name, len);
  35.     class = C_IN;
  36.     type = T_ANY;
  37.  
  38.     /* if no args, exit */
  39.     if (argc == 1) {
  40.         fprintf(stderr, "Usage:  %s [-h] host [-n ns] [-t type] [-c class] [-r retry] [-p period] [-s] [-v] [-d] [-a]\n", argv[0]);
  41.         exit(-1);
  42.     }
  43.  
  44.     /* handle args */
  45.     while ((c = getopt(argc, argv, "c:dh:n:p:r:st:u:v")) != EOF) {
  46.         switch (c) {
  47.  
  48.         case 'r' :    _res.retry = atoi(optarg);
  49.                 break;
  50.  
  51.         case 'p' :    _res.retrans = atoi(optarg);
  52.                 break;
  53.  
  54.         case 'h' :    strcpy(name, optarg);
  55.                 break;
  56.  
  57.         case 'c' :    if (!strcasecmp(optarg, "IN"))
  58.                     class = C_IN;
  59.                 else if (!strcasecmp(optarg, "HS"))
  60.                     class = C_HS;
  61.                 else if (!strcasecmp(optarg, "CHAOS"))
  62.                     class = C_CHAOS;
  63.                 else if (!strcasecmp(optarg, "ANY"))
  64.                     class = C_ANY;
  65.                 else {
  66.                     class = T_ANY;
  67.                     fprintf(stderr, "optarg=%s\n", optarg);
  68.                 }
  69.                 break;
  70.  
  71.         case 't' :    if (!strcasecmp(optarg, "A"))
  72.                     type = T_A;
  73.                 else if (!strcasecmp(optarg, "NS"))
  74.                     type = T_NS;
  75.                 else if (!strcasecmp(optarg, "CNAME"))
  76.                     type = T_CNAME;
  77.                 else if (!strcasecmp(optarg, "SOA"))
  78.                     type = T_SOA;
  79.                 else if (!strcasecmp(optarg, "WKS"))
  80.                     type = T_WKS;
  81.                 else if (!strcasecmp(optarg, "PTR"))
  82.                     type = T_PTR;
  83.                 else if (!strcasecmp(optarg, "HINFO"))
  84.                     type = T_HINFO;
  85.                 else if (!strcasecmp(optarg, "MINFO"))
  86.                     type = T_MINFO;
  87.                 else if (!strcasecmp(optarg, "MX"))
  88.                     type = T_MX;
  89.                 else if (!strcasecmp(optarg, "MG"))
  90.                     type = T_MG;
  91.                 else if (!strcasecmp(optarg, "RP"))
  92.                     type = T_RP;
  93.                 else if (!strcasecmp(optarg, "TXT"))
  94.                     type = T_TXT;
  95.                 else if (!strcasecmp(optarg, "AFSDB"))
  96.                     type = T_AFSDB;
  97.                 else if (!strcasecmp(optarg, "ANY"))
  98.                     type = T_ANY;
  99.                 else {
  100.                     fprintf(stderr, "Bad type (%s)\n", optarg);
  101.                     exit(-1);
  102.                 }
  103.                 break;
  104.  
  105.         case 'd' :    debug++;
  106.                 break;
  107.  
  108.         case 's' :    
  109.         case 'v' :    stream++;
  110.                 break;
  111.  
  112.         case 'n' :    
  113.                 /*
  114.                  *  If we set some nameservers here without
  115.                  *  using gethostbyname() first, then they will
  116.                  *  get overwritten when we do the first query.
  117.                  *  So, we must init the resolver before any 
  118.                  *  of this.
  119.                  */
  120.                 if (!(_res.options & RES_INIT))
  121.                     if (res_init() == -1) {
  122.                         fprintf(stderr,
  123.                             "res_init() failed\n");
  124.                         exit(-1);
  125.                 }
  126.                 if (nameservers >= MAXNS) break;
  127.                 (void) inet_aton(optarg,
  128.                          &q_nsaddr[nameservers]);
  129.                 if (!inet_aton(optarg, &ul)) {
  130.                     q_nsname = gethostbyname(optarg);
  131.                     if (q_nsname == 0) {
  132.                         fprintf(stderr,
  133.                                "Bad nameserver (%s)\n",
  134.                             optarg);
  135.                         exit(-1);
  136.                     }
  137.                     bcopy((char *) q_nsname->h_addr,
  138.                           (char *) &q_nsaddr[nameservers],
  139.                           INADDRSZ);
  140.                 }
  141.                 else
  142.                     q_nsaddr[nameservers].s_addr = ul;
  143.                 nameservers++;
  144.                 break;
  145.  
  146.         default :     fprintf(stderr, 
  147.                 "\tUsage:  %s [-n ns] [-h host] [-t type] [-c class] [-r retry] [-p period] [-s] [-v] [-d] [-a]\n", argv[0]);
  148.                 exit(-1);
  149.         }
  150.     }
  151.     if (optind < argc)
  152.         strcpy(name, argv[optind]);
  153.  
  154.     len = sizeof(answer);
  155.  
  156.     /* 
  157.      * set these here so they aren't set for a possible call to
  158.      * gethostbyname above
  159.     */
  160.     if (debug) 
  161.         _res.options |= RES_DEBUG;
  162.     if (stream)
  163.          _res.options |= RES_USEVC;
  164.  
  165.     /* if the -n flag was used, add them to the resolver's list */
  166.     if (nameservers != 0) {
  167.         _res.nscount = nameservers;
  168.         for (i = nameservers - 1; i >= 0; i--) {
  169.             _res.nsaddr_list[i].sin_addr.s_addr = q_nsaddr[i].s_addr;
  170.             _res.nsaddr_list[i].sin_family = AF_INET;
  171.             _res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  172.         }
  173.     }
  174.  
  175.     /*
  176.      * if the -h arg is fully-qualified, use res_query() since
  177.      * using res_search() will lead to use of res_querydomain()
  178.      * which will strip the trailing dot
  179.      */
  180.     if (name[strlen(name) - 1] == '.') {
  181.         if (res_query(name, class, type, answer, len) < 0) {
  182.             hp = (HEADER *) answer;
  183.             if ((hp->rcode == 0) && (hp->ancount > 0))
  184.                 __p_query(answer);
  185.             else
  186.                 fprintf(stderr, "Query failed (h_errno = %d) : %s\n", 
  187.                         h_errno, h_errlist[h_errno]);
  188.             exit(-1);
  189.         }
  190.     }
  191.     else if (res_search(name, class, type, answer, len) < 0) {
  192.         hp = (HEADER *) answer;
  193.         if ((hp->rcode == 0) && (hp->ancount > 0))
  194.             __p_query(answer);
  195.         else
  196.             fprintf(stderr, "Query failed (h_errno = %d) : %s\n", 
  197.                         h_errno, h_errlist[h_errno]);
  198.         exit(-1);
  199.     }
  200.     __p_query(answer);
  201.     exit(0);
  202. }
  203.