Finding DNS server settings programmatically on Mac OS X

前端 未结 5 1674
醉话见心
醉话见心 2020-12-10 18:55

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

5条回答
  •  盖世英雄少女心
    2020-12-10 19:29

    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.
    

提交回复
热议问题