Solved

Setting nameservers after res_init fails doing res_query

Posted on 2016-09-01
2
107 Views
Last Modified: 2016-09-07
I have an Apache module which needs to find the "real" IP address of its host.  This became an issue after my own server went behind a firewall; the previous use of gethostbyname then returned 192.168.0.x rather than the "real" address.

I found some code that looked likely and wrote a short test program, below.

Unfortunately the results indicate that it is using the default nameservers from resolv.conf, not the nameservers loaded into _res.nsaddr_list after the call to res_init.  This was called out as an issue in the original code, but the original solution (a "dummy" call to res_query) does not work on my system.

Can someone suggest where the problem lies?


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>
#include <netdb.h>

#define N 4096

/* --------------------------------- */
/* CODE BEGINS                       */
/* --------------------------------- */

int main (int argc, char *argv[]) {
    u_char nsbuf[N];
    char dispbuf[N];
    ns_msg msg;
    ns_rr rr;
    int i, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>\n", argv[0]);
        exit (1);
    }

/* Initialize to use the Google nameservers */
     res_init();
     _res.nscount = 2;
     _res.nsaddr_list[0].sin_family = AF_INET;
     _res.nsaddr_list[0].sin_addr.s_addr = inet_addr ("8.8.8.8");
     _res.nsaddr_list[0].sin_port = htons(53);
     _res.nsaddr_list[1].sin_family = AF_INET;
     _res.nsaddr_list[1].sin_addr.s_addr = inet_addr ("8.8.4.4");
     _res.nsaddr_list[1].sin_port = htons(53);

    printf("Domain : %s\n", argv[1]);

 /* Print the A record return or the error code */
    printf("A records : \n");
    l = res_query(argv[1], ns_c_any, ns_t_a, nsbuf, sizeof(nsbuf));
    if (l < 0) perror(argv[1]);
    ns_initparse(nsbuf, l, &msg);
    l = ns_msg_count(msg, ns_s_an);
    for (i = 0; i < l; i++)
    {
      ns_parserr(&msg, ns_s_an, i, &rr);
      ns_sprintrr(&msg, &rr, NULL, NULL, dispbuf, sizeof(dispbuf));
      printf("\t%s \n", dispbuf);
    }


    // NS RECORD
    printf("NS records : \n");
    l = res_query(argv[1], ns_c_any, ns_t_ns, nsbuf, sizeof(nsbuf));
    if (l < 0)
    {
      perror(argv[1]);
    }
    ns_initparse(nsbuf, l, &msg);
    l = ns_msg_count(msg, ns_s_an);
    for (i = 0; i < l; i++)
    {
      ns_parserr(&msg, ns_s_an, 0, &rr);
      ns_sprintrr(&msg, &rr, NULL, NULL, dispbuf, sizeof(dispbuf));
      printf("\t%s \n", dispbuf);
    }
    // ---------
    return 0;
}

Open in new window


sysmgr@www:~$ ./a.out www.*******.com
Domain : www.*******.com
A records :
        www.*******.com.           6H IN A         192.168.0.209
NS records :
        www.*******.com.           6H IN NS        localhost.

Open in new window

0
Comment
Question by:Dr. Klahn
  • 2
2 Comments
 
LVL 26

Accepted Solution

by:
Dr. Klahn earned 0 total points
ID: 41788573
Update:  There is no solution to this problem other than correction of the Debian resolver library.  The bug will therefore also be present in (at least) Ubuntu.

The bug was introduced 10 years ago into debian and was first reported in 2015.

Given that the bug is 10 years old, it took ten years to notice it, the original report is now a year old and the bug priority has been reduced to "normal", it seems unlikely that there will be a solution in the near future.
0
 
LVL 26

Author Closing Comment

by:Dr. Klahn
ID: 41788918
Manual close.  No solution found.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MPLS Network Question 2 65
OSPF Design NSSA 5 67
Can't remote with RDC through ASUS RT-N66W Router 3 68
svi stops eigrp advertisement 13 31
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
I know for anybody starting from Beginner to Expert in Networking knows what OSI model. But this tutorial is for freshers or those who are new to networking world. Why I am putting OSI in such simple and compact manner is because it enables you to k…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question