3

I'm trying to get the my dns server IP using using libresolv framework , but all I get is "0.0.0.0"

-(void) getDns
{
    res_init();

    for (int i=0;i<MAXNS;i++)
    {       
        printf("%s",inet_ntoa(_res.nsaddr_list[i].sin_addr));

    }
}

Anything wrong with my code here ?

Thanks

Edit

Tried to use the code in SO's other question, but this is what I get when the host is "apple.com"

2011-03-16 15:07:21.689 MobileMax[5876:207] RESOLVED 0:<17.149.160.49>
2011-03-16 15:07:21.691 MobileMax[5876:207] RESOLVED 1:<17.251.200.70>
2011-03-16 15:07:21.691 MobileMax[5876:207] RESOLVED 2:<17.112.152.57>

While when running the command : cat /etc/resolv.conf I get nameserver 10.0.0.138 which is what I want.

Any idea how to get that ?

Edit2

I tried this as well, but still getting the "0.0.0.0" response.

u_char buf[NS_PACKETSZ];
int responseLen;             

    res_query("www.google.com",ns_c_in,ns_t_ns,buf,sizeof(buf));
Idan
  • 5,717
  • 10
  • 47
  • 84
  • Oh now I get what you want... only way I can see right now is to actually parse `/etc/resolv.conf`. I'm curious, why do you need the IP of your DNS server ? – DarkDust Mar 16 '11 at 14:04
  • 2
    See http://stackoverflow.com/questions/10999612/iphone-get-3g-dns-host-name-and-ip-address – AriX Jul 20 '12 at 23:02
  • I also get the 0.0.0.0 information when running the simulator. But when I try the code on a real device it actually returns the right dns ip-address. So there is nothing wrong with your code. – user2061448 Feb 11 '13 at 13:32

2 Answers2

0

Try below code to get DNS Server Address . Don't forget to link libresolv.lib

    #include <arpa/inet.h>
    #include <ifaddrs.h>
    #include <resolv.h>
    #include <dns.h>

    - (NSString *) getDNSServers
    {
    // dont forget to link libresolv.lib
    NSMutableString *addresses = [[NSMutableString alloc]initWithString:@"DNS Addresses \n"];

    res_state res = malloc(sizeof(struct __res_state));

    int result = res_ninit(res);

    if ( result == 0 )
    {
        for ( int i = 0; i < res->nscount; i++ )
        {
            NSString *s = [NSString stringWithUTF8String :  inet_ntoa(res->nsaddr_list[i].sin_addr)];
            [addresses appendFormat:@"%@\n",s];
            NSLog(@"%@",s);
        }
    }
    else
        [addresses appendString:@" res_init result != 0"];

    return addresses;
}
Ramkumar chintala
  • 958
  • 11
  • 24
  • This will fail to detect IPv6 DNS servers (and will actually break if you have some). I have some Swift 3 code in my answer for another question at http://stackoverflow.com/a/41303040/6782 – Alnitak Jan 03 '17 at 13:59
  • either way, you shouldn't be peeking into the internals of `res->nsaddr_list` - the `res_getservers()` function returns the list – Alnitak Jan 03 '17 at 16:08
  • Objective-C approach: http://stackoverflow.com/questions/31256024/get-dns-server-ip-from-iphone-settings/43344910#43344910 inet_ntoa is not a best approach since it is supporting only IPv4 – Aleksei Minaev Apr 12 '17 at 09:52
0

You are not resolving anything, you are just printing the address stored in some variable. You actually need to call res_query or res_search. But on iOS, you're better off with using the CFHost* methods, like in this question. There's also sample code from Apple (search the MyResolveNameToAddress function).

Update after understanding the question: According to this question you cannot access the /etc/resolv.conf file (permissions). But according to this question you might succeed by using the SystemConfigFramework but I've got no idea whether it works on iOS (the framework exists, but whether that information is exposed I don't know).

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224