C   29
query
Guest on 4th February 2023 01:25:44 PM


  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <arpa/nameser.h>
  4. #include <netdb.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <resolv.h>
  8. #include <sys/param.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. typedef union {
  13.    HEADER qb1;
  14.    char qb2[PACKETSZ];
  15. } querybuf;
  16.  
  17. int getinfo(
  18. char *name,
  19. char *domain,
  20. int   type)
  21. {
  22.  
  23.    HEADER   *hp;
  24.    char     *eom, *cp;
  25.    querybuf buf, answer;
  26.    int      n;
  27.    int      ancount, nscount, arcount, qdcount;
  28.    char     host[2*MAXDNAME+2];
  29.  
  30.    if (domain == NULL)
  31.       (void)sprintf(host, "%.*s", MAXDNAME, name);
  32.    else
  33.       (void)sprintf(host, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain);
  34.  
  35.    printf("Querying name: %s\n", host);
  36.  
  37.    n = res_mkquery(QUERY, host, C_IN, type, (char *)NULL, 0, NULL,
  38.       (char *)&buf, sizeof(buf));
  39.    if (n < 0) {
  40.       if (_res.options & RES_DEBUG)
  41.          printf("res_mkquery failed\n");
  42.       h_errno = NO_RECOVERY;
  43.       return(0);
  44.    }
  45.    n = res_send((char *)&buf, n, (char *)&answer, sizeof(answer));
  46.    if (n < 0) {
  47.       if (_res.options & RES_DEBUG)
  48.          printf("res_send failed\n");
  49.       h_errno = TRY_AGAIN;
  50.       return (0);
  51.    }
  52.  
  53.    eom = (char *)&answer + n;
  54.    hp = (HEADER *)&answer;
  55.    ancount = ntohs(hp->ancount);
  56.    qdcount = ntohs(hp->qdcount);
  57.    nscount = ntohs(hp->nscount);
  58.    arcount = ntohs(hp->arcount);
  59.    printf("Ans %d -  QDs %d - NSs %d - ARs %d \n",
  60.            ancount, qdcount, nscount, arcount);
  61.    return(n);
  62. }
  63.  
  64. void main(
  65. int argc,
  66. char **argv)
  67. {
  68.    int status;
  69.  
  70.    res_init();
  71.  
  72.    _res.options |= RES_USEVC;
  73.    _res.retry = 1;
  74.    _res.retrans = 15;
  75.    _res.options &= ~RES_RECURSE;
  76.    _res.options |= RES_DEBUG;
  77.  
  78.    if (argc < 3)
  79.       status = getinfo(argv[1], "cs.clemson.edu.", T_A);
  80.    else
  81.       status = getinfo(argv[1], argv[2], T_A);
  82.  
  83.    printf("%d bytes returned \n", status);
  84.  
  85.  
  86. }

Raw Paste

Login or Register to edit or fork this paste. It's free.