title: How to query a server and get the MX, A, NS records
body: <p>I'm trying to get the A, MX and NS A server record as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;resolv.h&gt;
  #include &lt;netdb.h&gt;
  
  #define N 4096
  
  int main (int argc, char *argv[])
  {
      u_char nsbuf[N];
      char dispbuf[N];
      ns_msg msg;
      ns_rr rr;
      int i, l;
  
      if (argc &lt; 2) {
          printf ("Usage: %s &lt;domain&gt;\n", argv[0]);
          exit (1);
      }
  
      // HEADER
      printf("Domain : %s\n", argv[1]);
      // ------
  
      // A RECORD
      printf("A records : \n");
      l = res_query(argv[1], ns_c_any, ns_t_a, nsbuf, sizeof(nsbuf));
      if (l &lt; 0)
      {
        perror(argv[1]);
      }
      ns_initparse(nsbuf, l, &amp;msg);
      l = ns_msg_count(msg, ns_s_an);
      for (i = 0; i &lt; l; i++)
      {
        ns_parserr(&amp;msg, ns_s_an, 0, &amp;rr);
        ns_sprintrr(&amp;msg, &amp;rr, NULL, NULL, dispbuf, sizeof(dispbuf));
        printf("\t%s \n", dispbuf);
      }
      //------------
  
  
      // MX RECORD
      printf("MX records : \n");
      l = res_query(argv[1], ns_c_any, ns_t_mx, nsbuf, sizeof(nsbuf));
      if (l &lt; 0)
      {
        perror(argv[1]);
      }
      else
      {
  #ifdef USE_PQUERY
        /* this will give lots of detailed info on the request and reply */
        res_pquery(&amp;_res, nsbuf, l, stdout);
  #else
        /* just grab the MX answer info */
        ns_initparse(nsbuf, l, &amp;msg);
        l = ns_msg_count(msg, ns_s_an);
        for (i = 0; i &lt; l; i++)
        {
          ns_parserr(&amp;msg, ns_s_an, i, &amp;rr);
          ns_sprintrr(&amp;msg, &amp;rr, NULL, NULL, dispbuf, sizeof(dispbuf));
          printf ("\t%s\n", dispbuf);
        }
  #endif
      }
      // ---------
  
      // NS RECORD
      printf("NS records : \n");
      l = res_query(argv[1], ns_c_any, ns_t_ns, nsbuf, sizeof(nsbuf));
      if (l &lt; 0)
      {
        perror(argv[1]);
      }
      ns_initparse(nsbuf, l, &amp;msg);
      l = ns_msg_count(msg, ns_s_an);
      for (i = 0; i &lt; l; i++)
      {
        ns_parserr(&amp;msg, ns_s_an, 0, &amp;rr);
        ns_sprintrr(&amp;msg, &amp;rr, NULL, NULL, dispbuf, sizeof(dispbuf));
        printf("\t%s \n", dispbuf);
      }
      // ---------
      return 0;
  }
  </code></pre>
  
  <p>On request receive a lot of the same, that is, the same A records. The same with the NS records. What am I doing wrong?
  Example :</p>
  
  <pre><code>Domain : mail.ru
  A records :
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
          mail.ru.                22S IN A        94.100.191.207
  MX records :
          mail.ru.                1m57s IN MX     10 mxs.mail.ru.
  NS records :
          mail.ru.                1m50s IN NS     ns.mail.ru.
          mail.ru.                1m50s IN NS     ns.mail.ru.
          mail.ru.                1m50s IN NS     ns.mail.ru.
          mail.ru.                1m50s IN NS     ns.mail.ru.
          mail.ru.                1m50s IN NS     ns.mail.ru.
          mail.ru.                1m50s IN NS     ns.mail.ru.
  </code></pre>
  
  <p>Please help.</p>