Add macOs defaultRoute function

This commit is contained in:
travisladuke 2023-08-09 08:36:51 -07:00 committed by Travis LaDuke
commit d271b270fc
2 changed files with 45 additions and 8 deletions

View file

@ -8,7 +8,8 @@
namespace ZeroTier { namespace ZeroTier {
class MacDNSHelper { class MacDNSHelper
{
public: public:
static void setDNS(uint64_t nwid, const char *domain, const std::vector<InetAddress> &servers); static void setDNS(uint64_t nwid, const char *domain, const std::vector<InetAddress> &servers);
static void removeDNS(uint64_t nwid); static void removeDNS(uint64_t nwid);
@ -16,6 +17,7 @@ class MacDNSHelper {
static bool addIps6(uint64_t nwid, const MAC mac, const char *dev, const std::vector<InetAddress> &addrs); static bool addIps6(uint64_t nwid, const MAC mac, const char *dev, const std::vector<InetAddress> &addrs);
static bool removeIps4(uint64_t nwid); static bool removeIps4(uint64_t nwid);
static bool removeIps6(uint64_t nwid); static bool removeIps6(uint64_t nwid);
static bool getDefaultRoute(char *buf);
}; };
} // namespace ZeroTier } // namespace ZeroTier

View file

@ -343,5 +343,40 @@ bool MacDNSHelper::removeIps4(uint64_t nwid)
return res; return res;
} }
bool MacDNSHelper::getDefaultRoute(char *buf) {
SCDynamicStoreRef ds = SCDynamicStoreCreate(NULL, CFSTR("ZeroTierOne"), NULL, NULL);
CFPropertyListRef propertyList = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4"));
if (!propertyList && CFGetTypeID(propertyList) != CFDictionaryGetTypeID()) {
if (propertyList != NULL) {CFRelease(propertyList);}
CFRelease(ds);
return false;
}
CFDictionaryRef dict = (CFDictionaryRef)propertyList;
CFTypeRef routerObject = CFDictionaryGetValue(dict, CFSTR("Router"));
if (!routerObject && CFGetTypeID(routerObject) != CFStringGetTypeID()) {
if (propertyList != NULL) {CFRelease(propertyList);}
CFRelease(ds);
return false;
}
CFStringRef router = (CFStringRef)routerObject;
CFIndex length = CFStringGetLength(router);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
CFStringGetCString(router, buf, maxSize, kCFStringEncodingUTF8);
if (propertyList != NULL) {CFRelease(propertyList);}
CFRelease(ds);
return true;
}
} }