Browse Source

Add macOs defaultRoute function

travisladuke 2 years ago
parent
commit
d271b270fc
2 changed files with 45 additions and 8 deletions
  1. 10 8
      osdep/MacDNSHelper.hpp
  2. 35 0
      osdep/MacDNSHelper.mm

+ 10 - 8
osdep/MacDNSHelper.hpp

@@ -8,14 +8,16 @@
 
 namespace ZeroTier {
 
-class MacDNSHelper {
-  public:
-	static void setDNS(uint64_t nwid, const char* domain, const std::vector<InetAddress>& servers);
-	static void removeDNS(uint64_t nwid);
-	static bool addIps4(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 removeIps6(uint64_t nwid);
+class MacDNSHelper
+{
+public:
+    static void setDNS(uint64_t nwid, const char *domain, const std::vector<InetAddress> &servers);
+    static void removeDNS(uint64_t nwid);
+    static bool addIps4(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 removeIps6(uint64_t nwid);
+    static bool getDefaultRoute(char *buf);
 };
 
 }	// namespace ZeroTier

+ 35 - 0
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;
+}
+
+
 
 }