Finding DNS server settings programmatically on Ma

2020-02-06 08:39发布

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.

5条回答
够拽才男人
2楼-- · 2020-02-06 09:12

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.

查看更多
Luminary・发光体
3楼-- · 2020-02-06 09:16

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);
查看更多
叼着烟拽天下
4楼-- · 2020-02-06 09:21

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);

if (services) {
long count = CFArrayGetCount(services);
for (int i = 0; i < count; i++) {
service = CFArrayGetValueAtIndex(services, i);
interface = SCNetworkServiceGetInterface(service);
NSString *interfaceServiceID = (__bridge NSString*)SCNetworkServiceGetServiceID(service);
CFStringRef primaryservicepath = CFStringCreateWithFormat(NULL,NULL,CFSTR("State:/Network/Service/%@/DNS"),interfaceServiceID);
        //    NSLog(@"%@",primaryservicepath);


SCDynamicStoreRef dynRef=SCDynamicStoreCreate(kCFAllocatorSystemDefault, CFSTR("DNSSETTING"), NULL, NULL);
                // NSLog(@"%@",dynRef);
CFDictionaryRef dnskey = SCDynamicStoreCopyValue(dynRef,primaryservicepath);

           //     NSLog(@"%@",dnskey);
//dnskey will give you the DNS server address.
查看更多
再贱就再见
5楼-- · 2020-02-06 09:27

You could read from /etc/resolv.conf.

查看更多
看我几分像从前
6楼-- · 2020-02-06 09:34

They are also available from /etc/resolv.conf

查看更多
登录 后发表回答