diff --git a/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard b/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard index fea90f5..6bc9fee 100644 --- a/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard +++ b/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + diff --git a/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift b/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift index 4454b41..7c766fa 100644 --- a/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift +++ b/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift @@ -183,12 +183,11 @@ class ViewController: UIViewController { while(true) { sleep(1) - dispatch_async(dispatch_get_main_queue()) { - //var str_buf = [Int8](count: 16, repeatedValue: 0) - //print(self.zt.get_address(self.txtNWID.text!)) //, &str_buf); - //self.lblAddress.text = String.fromCString(str_buf) - // print("IPV4 = ", String.fromCString(str_buf)) + var str_buf = [Int8](count: 16, repeatedValue: 0) + self.zt.get_ipv6_address(self.txtNWID.text!, &str_buf) + self.lblAddress.text = String.fromCString(str_buf) + print("addr = ", String.fromCString(str_buf)) } diff --git a/src/service.cpp b/src/service.cpp index 893b435..caa2593 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -165,12 +165,14 @@ void zts_get_ipv4_address(const char *nwid, char *addrstr) uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int]; if(tap && tap->_ips.size()){ - for(int i=0; i_ips.size(); i++) - { - std::string addr = tap->_ips[0].toString(); - DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length()); + for(int i=0; i_ips.size(); i++) { + if(tap->_ips[i].isV4()) { + std::string addr = tap->_ips[i].toString(); + // DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length()); + memcpy(addrstr, addr.c_str(), addr.length()); // first address found that matches protocol version 4 + return; + } } - //memcpy(addrstr, addr.c_str(), addr.length()); } else { memcpy(addrstr, "-1.-1.-1.-1/-1", 14); @@ -182,9 +184,14 @@ void zts_get_ipv6_address(const char *nwid, char *addrstr) uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int]; if(tap && tap->_ips.size()){ - std::string addr = tap->_ips[0].toString(); - DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length()); - memcpy(addrstr, addr.c_str(), addr.length()); + for(int i=0; i_ips.size(); i++){ + if(tap->_ips[i].isV6()) { + std::string addr = tap->_ips[i].toString(); + // DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length()); + memcpy(addrstr, addr.c_str(), addr.length()); // first address found that matches protocol version 6 + return; + } + } } else { memcpy(addrstr, "-1.-1.-1.-1/-1", 14); diff --git a/src/tap.cpp b/src/tap.cpp index e0606bb..fd32441 100644 --- a/src/tap.cpp +++ b/src/tap.cpp @@ -755,7 +755,6 @@ bool NetconEthernetTap::enabled() const void NetconEthernetTap::lwIP_init_interface(const InetAddress &ip) { #if defined(SDK_LWIP) - DEBUG_INFO("local_addr=%s", ip.toString().c_str()); Mutex::Lock _l(_ips_m); if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) { @@ -764,7 +763,7 @@ void NetconEthernetTap::lwIP_init_interface(const InetAddress &ip) #if defined(SDK_IPV4) if (ip.isV4()) { - DEBUG_INFO("IPV4"); + DEBUG_INFO("local_addr=%s", ip.toString().c_str()); // convert address static ip_addr_t ipaddr, netmask, gw; IP4_ADDR((ip4_addr_t *)&gw,127,0,0,1); @@ -789,7 +788,7 @@ void NetconEthernetTap::lwIP_init_interface(const InetAddress &ip) #if defined(SDK_IPV6) if(ip.isV6()) { - DEBUG_INFO("IPV6"); + DEBUG_INFO("local_addr=%s", ip.toString().c_str()); // convert address static ip6_addr_t addr6; struct sockaddr_in6 in6; @@ -862,7 +861,7 @@ std::vector NetconEthernetTap::ips() const void NetconEthernetTap::lwIP_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) { #if defined(SDK_LWIP) - DEBUG_INFO(); + // DEBUG_EXTRA(); struct pbuf *p,*q; if (!_enabled) return; diff --git a/src/wrappers/swift/ztsdk.swift b/src/wrappers/swift/ztsdk.swift index 6d8979d..9dc69b4 100644 --- a/src/wrappers/swift/ztsdk.swift +++ b/src/wrappers/swift/ztsdk.swift @@ -84,18 +84,13 @@ class ZTSDK : NSObject } // Returns the IPV4 address of this device on a given ZeroTier network - func get_ipv4_address(nwid: String) -> String? { - var str_buf = [Int8](count: 16, repeatedValue: 0) - zt_get_ipv4_address(nwid,&str_buf) - return String.fromCString(str_buf) - //return "IPV4" + func get_ipv4_address(nwid: String, inout _ addrbuf: [Int8]) { + zt_get_ipv4_address(nwid,&addrbuf) } // Returns the IPV6 address of this device on a given ZeroTier network - func get_ipv6_address(nwid: String) -> String? { - var str_buf = [Int8](count: 16, repeatedValue: 0) - zt_get_ipv6_address(nwid,&str_buf) - return String.fromCString(str_buf) + func get_ipv6_address(nwid: String, inout _ addrbuf: [Int8]) { + zt_get_ipv6_address(nwid,&addrbuf) } @@ -127,6 +122,9 @@ class ZTSDK : NSObject */ + // TODO: Verify this hasn't been broken. Should check for interface addresses based on + // protocol version. (important) + // SOCKET API func socket(socket_family: Int32, _ socket_type: Int32, _ socket_protocol: Int32) -> Int32 { return zt_socket(socket_family, socket_type, socket_protocol); @@ -137,7 +135,10 @@ class ZTSDK : NSObject return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); } while(true) { // politely wait until an address is provided. simulates a blocking call - if(self.get_ipv4_address(nwid!) != nil) { + var addrbuf = [Int8](count: 16, repeatedValue: 0) + self.get_ipv4_address(nwid!, &addrbuf) + var addr_str:String = String.fromCString(addrbuf)! + if(addr_str != "-1.-1.-1.-1/-1") { return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); } } @@ -147,7 +148,10 @@ class ZTSDK : NSObject return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); } while(true) { // politely wait until an address is provided. simulates a blocking call - if(self.get_ipv4_address(nwid!) != nil) { + var addrbuf = [Int8](count: 16, repeatedValue: 0) + self.get_ipv4_address(nwid!, &addrbuf) + var addr_str:String = String.fromCString(addrbuf)! + if(addr_str != "-1.-1.-1.-1/-1") { return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); } }