Check if DNS servers need to be Applied on macOS

This commit is contained in:
Robert.Schreib 2021-04-21 12:02:31 +02:00
parent e8f7d5ef9e
commit 31ffe4403c
No known key found for this signature in database
GPG Key ID: C5D4FEC8240C0777

View File

@ -39,18 +39,22 @@ void MacDNSHelper::setDNS(uint64_t nwid, const char *domain, const std::vector<I
sprintf(buf, "State:/Network/Service/%.16llx/DNS", nwid);
CFStringRef key = CFStringCreateWithCString(NULL, buf, kCFStringEncodingUTF8);
CFArrayRef list = SCDynamicStoreCopyKeyList(ds, key);
CFIndex i = 0, j = CFArrayGetCount(list);
bool ret = TRUE;
if (j <= 0) {
ret &= SCDynamicStoreAddValue(ds, key, dict);
} else {
ret &= SCDynamicStoreSetValue(ds, (CFStringRef)CFArrayGetValueAtIndex(list, i), dict);
}
if (!ret) {
fprintf(stderr, "Error writing DNS configuration\n");
CFPropertyListRef oldDNSServers = SCDynamicStoreCopyValue(ds, (CFStringRef)CFArrayGetValueAtIndex(list, i));
if (!CFEqual(oldDNSServers,dict)) {
bool ret = TRUE;
if (j <= 0) {
ret &= SCDynamicStoreAddValue(ds, key, dict);
} else {
ret &= SCDynamicStoreSetValue(ds, (CFStringRef)CFArrayGetValueAtIndex(list, i), dict);
}
if (!ret) {
fprintf(stderr, "Error writing DNS configuration\n");
}
}
CFRelease(oldDNSServers);
CFRelease(list);
CFRelease(key);
CFRelease(dict);