Finding DNS server settings programmatically on Mac OS X
Asked Answered
W

5

6

I have some cross platform DNS client code that I use for doing end to end SMTP and on windows I can find the current DNS server ip addresses by looking in the registry. On the Mac I can probably use the SystemConfiguration framework as mentioned in the first answer, however the exact method of doing so is not immediately obvious.

For instance SCDynamicStoreCopyDHCPInfo returns some of the dynamic DHCP related data but not the DNS server addresses.

Whimsical answered 4/11, 2008 at 1:14 Comment(0)
C
1

They are also available from /etc/resolv.conf

Conklin answered 7/11, 2008 at 1:14 Comment(0)
S
7

I know its very late to answer this question but may be helpful for the others.

This Code will help out for this task ..

SCPreferencesRef prefsDNS = SCPreferencesCreate(NULL, CFSTR("DNSSETTING"), NULL);
CFArrayRef services = SCNetworkServiceCopyAll(prefsDNS);
long servicesCount = CFArrayGetCount(services);
for (long i = 0; i < servicesCount; i++) {
    const SCNetworkServiceRef service = (const SCNetworkServiceRef)CFArrayGetValueAtIndex(services, i);
    CFStringRef interfaceServiceID = SCNetworkServiceGetServiceID(service);
    CFStringRef primaryservicepath = CFStringCreateWithFormat(NULL,NULL,CFSTR("State:/Network/Service/%@/DNS"),interfaceServiceID);
    SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, CFSTR("DNSSETTING"), NULL, NULL);
    CFPropertyListRef propList = SCDynamicStoreCopyValue(dynRef,primaryservicepath);
    if (propList) {
        CFDictionaryRef dict = (CFDictionaryRef)propList;
        CFArrayRef addresses = (CFArrayRef)CFDictionaryGetValue(dict, CFSTR("ServerAddresses"));
        long addressesCount = CFArrayGetCount(addresses);
        for (long j = 0; j < addressesCount; j++) {
            CFStringRef address = (CFStringRef)CFArrayGetValueAtIndex(addresses, j);
            // Print address
            CFShow(address);
        }
        CFRelease(propList);
    }
    CFRelease(dynRef);
    CFRelease(primaryservicepath);
}
CFRelease(services);
CFRelease(prefsDNS);
Septilateral answered 22/1, 2016 at 7:3 Comment(0)
T
6

I know it's been a long time since you needed this, but there is nothing worse than a old unsolved answer. You can't access them from "/etc/resolv.conf" because of permission issues. After much searching, and a little luck I discovered you can get it via res_ninit() function.

// Get native iOS System Resolvers
res_ninit(&_res);
res_state res = &_res;

for (int i = 0; i < res->nscount; i++) {
  sa_family_t family = res->nsaddr_list[i].sin_family;
  int port = ntohs(res->nsaddr_list[i].sin_port);
  if (family == AF_INET) { // IPV4 address
    char str[INET_ADDRSTRLEN]; // String representation of address
    inet_ntop(AF_INET, & (res->nsaddr_list[i].sin_addr.s_addr), str, INET_ADDRSTRLEN);
  } else if (family == AF_INET6) { // IPV6 address
    char str[INET6_ADDRSTRLEN]; // String representation of address
    inet_ntop(AF_INET6, &(res->nsaddr_list [i].sin_addr.s_addr), str, INET6_ADDRSTRLEN);
  }
}
res_ndestroy(res);
Travers answered 17/11, 2014 at 21:19 Comment(2)
good code. minor improvement: don't forget to link with libresolv.dylibMonogamous
alternative version, without peeking into res_state structure internals: https://mcmap.net/q/1021290/-get-dns-server-ip-from-iphone-settingsImpressionable
O
5

You can use the SystemConfiguration framework. It's in C.

Update: apparently the rest of the web is harder to use than I thought. Search for the key "State:/Network/Service/ServiceID/DNS" where ServiceID is the ID of the service.

Osanna answered 4/11, 2008 at 1:22 Comment(1)
Ok, I've initially trying SCDynamicStoreCopyDHCPInfo and that gets me the current IP address and netmask + a few other things I don't understand, but not the DNS servers. Is there any chance you can be more specific?Whimsical
C
1

They are also available from /etc/resolv.conf

Conklin answered 7/11, 2008 at 1:14 Comment(0)
L
-1

You could read from /etc/resolv.conf.

Limonite answered 7/11, 2008 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.