From d271b270fca3965a03dc543aeaeda0cc84d10139 Mon Sep 17 00:00:00 2001 From: travisladuke Date: Wed, 9 Aug 2023 08:36:51 -0700 Subject: [PATCH] Add macOs defaultRoute function --- osdep/MacDNSHelper.hpp | 18 ++++++++++-------- osdep/MacDNSHelper.mm | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/osdep/MacDNSHelper.hpp b/osdep/MacDNSHelper.hpp index 69ea58d3b..cbcc82fb9 100644 --- a/osdep/MacDNSHelper.hpp +++ b/osdep/MacDNSHelper.hpp @@ -8,14 +8,16 @@ namespace ZeroTier { -class MacDNSHelper { - public: - static void setDNS(uint64_t nwid, const char* domain, const std::vector& servers); - static void removeDNS(uint64_t nwid); - static bool addIps4(uint64_t nwid, const MAC mac, const char* dev, const std::vector& addrs); - static bool addIps6(uint64_t nwid, const MAC mac, const char* dev, const std::vector& addrs); - static bool removeIps4(uint64_t nwid); - static bool removeIps6(uint64_t nwid); +class MacDNSHelper +{ +public: + static void setDNS(uint64_t nwid, const char *domain, const std::vector &servers); + static void removeDNS(uint64_t nwid); + static bool addIps4(uint64_t nwid, const MAC mac, const char *dev, const std::vector &addrs); + static bool addIps6(uint64_t nwid, const MAC mac, const char *dev, const std::vector &addrs); + static bool removeIps4(uint64_t nwid); + static bool removeIps6(uint64_t nwid); + static bool getDefaultRoute(char *buf); }; } // namespace ZeroTier diff --git a/osdep/MacDNSHelper.mm b/osdep/MacDNSHelper.mm index 5e170e172..74bcb0996 100644 --- a/osdep/MacDNSHelper.mm +++ b/osdep/MacDNSHelper.mm @@ -343,5 +343,40 @@ bool MacDNSHelper::removeIps4(uint64_t nwid) 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; +} + + }