From 195c993a1434619a4941d1359eb02b90346f0623 Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Fri, 9 Sep 2016 10:46:27 -0700 Subject: [PATCH] removed old debug code --- src/SDK_Debug.h | 52 +------------------------------------------------ src/ZTSDK.swift | 39 ++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 60 deletions(-) diff --git a/src/SDK_Debug.h b/src/SDK_Debug.h index a0321dc..8c30204 100644 --- a/src/SDK_Debug.h +++ b/src/SDK_Debug.h @@ -102,54 +102,4 @@ extern "C" { } // extern "C" #endif -#endif // _SDK_DEBUG_H_ - - -/* -void dwr(int level, const char *fmt, ... ) -{ -#if defined(SDK_DEBUG) - if(level > DEBUG_LEVEL) - return; - int saveerr; - saveerr = errno; - va_list ap; - va_start(ap, fmt); - char timestring[20]; - time_t timestamp; - timestamp = time(NULL); - strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(×tamp)); -#if defined(__ANDROID__) - pid_t tid = gettid(); -#elif defined(__linux__) - pid_t tid = 5;//syscall(SYS_gettid); -#elif defined(__APPLE__) - pid_t tid = pthread_mach_thread_np(pthread_self()); -#endif - #if defined(SDK_DEBUG_LOG_TO_FILE) - if(!debug_logfile) { // Try to get logfile from env - debug_logfile = getenv("ZT_SDK_LOGFILE"); - } - if(debug_logfile) { - FILE *file = fopen(debug_logfile,"a"); - fprintf(file, "%s [tid=%7d] ", timestring, tid); - vfprintf(file, fmt, ap); - fclose(file); - va_end(ap); - } - #endif - va_start(ap, fmt); - fprintf(stderr, "%s [tid=%7d] ", timestring, tid); - vfprintf(stderr, fmt, ap); - -// Outputs to Android debug console -#if defined(__ANDROID__) - __android_log_vprint(ANDROID_LOG_VERBOSE, "ZT-JNI", fmt, ap); -#endif - - fflush(stderr); - errno = saveerr; - va_end(ap); -#endif // _SDK_DEBUG -} -*/ \ No newline at end of file +#endif // _SDK_DEBUG_H_ \ No newline at end of file diff --git a/src/ZTSDK.swift b/src/ZTSDK.swift index 78a7ec5..3deb1cb 100644 --- a/src/ZTSDK.swift +++ b/src/ZTSDK.swift @@ -93,11 +93,18 @@ class ZTSDK : NSObject zt_leave_network(nwid); } - // Returns the address of this device on a given ZeroTier network - func get_address(nwid: String) -> (String, String) - { - // zts_get_addresses(nwid, addrstr); - return ("ipv4", "ipv6") + // 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(str_buf); + } + + // 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(str_buf); } @@ -134,11 +141,25 @@ class ZTSDK : NSObject return zt_socket(socket_family, socket_type, socket_protocol); } - func connect(fd: Int32, _ addr: ZTAddress) -> Int32 { - return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); + func connect(fd: Int32, _ addr: ZTAddress, _ nwid: String? = nil) -> Int32 { + if(nwid == nil) { // no nwid is provided to check for address, try once and fail + 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) { + return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); + } + } } - func bind(fd: Int32, _ addr: ZTAddress) -> Int32 { - return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); + func bind(fd: Int32, _ addr: ZTAddress, _ nwid: String? = nil) -> Int32 { + if(nwid == nil) { // no nwid is provided to check for address, try once and fail + 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) { + return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len())); + } + } } func accept(fd: Int32, _ addr: ZTAddress) -> Int32 {