We use NetworkExtension to develop a Mac VPN application with the includedRoutes attribute set to defaultRoute. We hope that when the DNS of the physical port changes or the network is switched in the VPN connection state, the DNS information of the current "en" port can be obtained. And optimize accordingly.
We have found two ways to obtain DNS
SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL);
CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/DNS"));
NSArray *routerArray;
if (dr) {
CFArrayRef router = CFDictionaryGetValue(dr, CFSTR("ServerAddresses"));
routerArray = [NSArray arrayWithArray:(__bridge NSArray *)router];
CFRelease(dr);
}
CFRelease(ds);
AND
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)];
[dnsArray addObject:s];
}
}
res_nclose(res);
They work perfectly when there is no VPN connection, but when the VPN connected is established, they can only get the DNS of the "utun" port, which does not meet our needs. We hope to get the DNS of the "en" port.
Can we get the DNS information of the specified port?
This is very important to us.
please help us
We have found two ways to obtain DNS
SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL);
CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/DNS"));
NSArray *routerArray;
if (dr) {
CFArrayRef router = CFDictionaryGetValue(dr, CFSTR("ServerAddresses"));
routerArray = [NSArray arrayWithArray:(__bridge NSArray *)router];
CFRelease(dr);
}
CFRelease(ds);
AND
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)];
[dnsArray addObject:s];
}
}
res_nclose(res);
They work perfectly when there is no VPN connection, but when the VPN connected is established, they can only get the DNS of the "utun" port, which does not meet our needs. We hope to get the DNS of the "en" port.
Can we get the DNS information of the specified port?
This is very important to us.
please help us