6 changed files with 166 additions and 2 deletions
@ -0,0 +1,20 @@
|
||||
#ifndef MAC_DNS_HELPER |
||||
#define MAC_DNS_HELPER |
||||
|
||||
#include <vector> |
||||
#include "../node/InetAddress.hpp" |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
class MacDNSHelper |
||||
{ |
||||
public: |
||||
static void doTheThing(); |
||||
|
||||
static void setDNS(uint64_t nwid, const char *domain, const std::vector<InetAddress> &servers); |
||||
static void removeDNS(uint64_t nwid); |
||||
}; |
||||
|
||||
} |
||||
|
||||
#endif |
||||
@ -0,0 +1,72 @@
|
||||
#include "MacDNSHelper.hpp" |
||||
|
||||
#include <stdio.h> |
||||
|
||||
#include <SystemConfiguration/SystemConfiguration.h> |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
void MacDNSHelper::doTheThing() |
||||
{ |
||||
fprintf(stderr, "\n\nDOING THE THING!!\n\n"); |
||||
} |
||||
|
||||
void MacDNSHelper::setDNS(uint64_t nwid, const char *domain, const std::vector<InetAddress> &servers) |
||||
{ |
||||
SCDynamicStoreRef ds = SCDynamicStoreCreate(NULL, CFSTR("zerotier"), NULL, NULL); |
||||
|
||||
CFStringRef *s = new CFStringRef[4]; |
||||
for (unsigned int i = 0; i < servers.size(); ++i) { |
||||
char buf[64]; |
||||
ZeroTier::InetAddress a = servers[i]; |
||||
const char *ipStr = a.toIpString(buf); |
||||
s[i] = CFStringCreateWithCString(NULL, ipStr, kCFStringEncodingUTF8); |
||||
} |
||||
|
||||
CFArrayRef serverArray = CFArrayCreate(NULL, (const void**)s, servers.size(), &kCFTypeArrayCallBacks); |
||||
|
||||
CFStringRef keys[2]; |
||||
keys[0] = CFSTR("SupplementalMatchDomains"); |
||||
keys[1] = CFSTR("ServerAddresses"); |
||||
|
||||
CFStringRef cfdomain = CFStringCreateWithCString(NULL, domain, kCFStringEncodingUTF8); |
||||
CFArrayRef domainArray = CFArrayCreate(NULL, (const void**)&cfdomain, 1, &kCFTypeArrayCallBacks); |
||||
|
||||
CFTypeRef values[2]; |
||||
values[0] = domainArray; |
||||
values[1] = serverArray; |
||||
|
||||
CFDictionaryRef dict = CFDictionaryCreate(NULL, |
||||
(const void**)keys, (const void**)values, 2, &kCFCopyStringDictionaryKeyCallBacks, |
||||
&kCFTypeDictionaryValueCallBacks); |
||||
CFShow(dict); |
||||
|
||||
char buf[256] = {0}; |
||||
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) { |
||||
fprintf(stderr, "Key '%s' does not exist. Creating.\n", buf); |
||||
ret &= SCDynamicStoreAddValue(ds, key, dict); |
||||
} else { |
||||
fprintf(stderr, "Key '%s' already exists. Updating DNS config.\n", buf); |
||||
ret &= SCDynamicStoreSetValue(ds, (CFStringRef)CFArrayGetValueAtIndex(list, i), dict); |
||||
} |
||||
if (ret) { |
||||
fprintf(stderr, "DNS written successfully\n"); |
||||
} else { |
||||
fprintf(stderr, "Error writing DNS configuration\n"); |
||||
} |
||||
|
||||
delete[] s; |
||||
} |
||||
|
||||
void MacDNSHelper::removeDNS(uint64_t nwid) |
||||
{ |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue