diff --git a/integrations/android/example_app/app/src/main/java/ZeroTier/ZTSDK.java b/integrations/android/example_app/app/src/main/java/ZeroTier/ZTSDK.java index 7cd32c4..f8d44ea 100644 --- a/integrations/android/example_app/app/src/main/java/ZeroTier/ZTSDK.java +++ b/integrations/android/example_app/app/src/main/java/ZeroTier/ZTSDK.java @@ -102,9 +102,9 @@ public class ZTSDK { return zt_get_proxy_port(nwid); } - public native boolean zt_running(); - public boolean running() { - return zt_running(); + public native boolean zt_service_is_running(); + public boolean service_is_running() { + return zt_service_is_running(); } @@ -272,4 +272,11 @@ public class ZTSDK { //public static native int zt_getsockopt(int fd, int type, int protocol); //public static native int zt_setsockopt(int fd, int type, int protocol); //public static native int zt_getsockname(int fd, int type, int protocol); + + + // PROXY SERVER CONTROLS + public native boolean zt_proxy_is_running(); + public boolean proxy_is_running() { + return zt_proxy_is_running(); + } } \ No newline at end of file diff --git a/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java b/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java index 3114421..73cdfff 100644 --- a/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java +++ b/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java @@ -34,7 +34,14 @@ public class MainActivity extends AppCompatActivity { zt.start_service(homeDir); } }).start(); - while(!zt.running()) { } + while(!zt.service_is_running()) { } + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } // client/server mode toggle int mode = 4, err; diff --git a/integrations/apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.pbxproj b/integrations/apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.pbxproj index d9e6116..28086b2 100644 --- a/integrations/apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.pbxproj +++ b/integrations/apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.pbxproj @@ -12,7 +12,6 @@ 7C9587951D6FB4C80014AF6B /* SDK_Service.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CAA03DE1D628E88003F9B1A /* SDK_Service.cpp */; }; 7CAA03DF1D628E88003F9B1A /* SDK_Service.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CAA03DE1D628E88003F9B1A /* SDK_Service.cpp */; }; 7CC003261D1216E3003E68DC /* ZeroTierSDK_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CC003251D1216E3003E68DC /* ZeroTierSDK_iOS.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 7CC003501D12178D003E68DC /* SDK_Debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003421D12178D003E68DC /* SDK_Debug.c */; }; 7CC003511D12178D003E68DC /* SDK_EthernetTap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */; }; 7CC003521D12178D003E68DC /* SDK_Intercept.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003461D12178D003E68DC /* SDK_Intercept.c */; }; 7CC003531D12178D003E68DC /* SDK_Proxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003481D12178D003E68DC /* SDK_Proxy.cpp */; }; @@ -84,7 +83,6 @@ 7CC004351D121840003E68DC /* Utils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC0041C1D121840003E68DC /* Utils.cpp */; }; 7CC0043A1D12184F003E68DC /* ControlPlane.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC004361D12184F003E68DC /* ControlPlane.cpp */; }; 7CC0043B1D12184F003E68DC /* OneService.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC004381D12184F003E68DC /* OneService.cpp */; }; - 7CC0043C1D1316BF003E68DC /* SDK_Debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003421D12178D003E68DC /* SDK_Debug.c */; }; 7CC0043D1D1316BF003E68DC /* SDK_EthernetTap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */; }; 7CC0043E1D1316BF003E68DC /* SDK_Intercept.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003461D12178D003E68DC /* SDK_Intercept.c */; }; 7CC0043F1D1316BF003E68DC /* SDK_Proxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003481D12178D003E68DC /* SDK_Proxy.cpp */; }; @@ -156,7 +154,6 @@ 7CC004841D131704003E68DC /* tcpip.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003641D1217C3003E68DC /* tcpip.c */; }; 7CC004851D1318DA003E68DC /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC0035A1D1217B2003E68DC /* lz4.c */; }; 7CC004861D1318DA003E68DC /* http_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003571D1217A1003E68DC /* http_parser.c */; }; - 7CC004871D131E21003E68DC /* SDK_Debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003421D12178D003E68DC /* SDK_Debug.c */; }; 7CC004881D131E21003E68DC /* SDK_EthernetTap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */; }; 7CC004891D131E21003E68DC /* SDK_Intercept.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003461D12178D003E68DC /* SDK_Intercept.c */; }; 7CC0048A1D131E21003E68DC /* SDK_Proxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003481D12178D003E68DC /* SDK_Proxy.cpp */; }; @@ -228,7 +225,6 @@ 7CC004D01D131E37003E68DC /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC0035A1D1217B2003E68DC /* lz4.c */; }; 7CC004D11D131E37003E68DC /* http_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003571D1217A1003E68DC /* http_parser.c */; }; 7CC004D21D131F76003E68DC /* ip_addr.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003921D1217DD003E68DC /* ip_addr.c */; }; - 7CC004D71D13247A003E68DC /* SDK_Debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003421D12178D003E68DC /* SDK_Debug.c */; }; 7CC004D81D13247A003E68DC /* SDK_EthernetTap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */; }; 7CC004D91D13247A003E68DC /* SDK_Intercept.c in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003461D12178D003E68DC /* SDK_Intercept.c */; }; 7CC004DA1D13247A003E68DC /* SDK_Proxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CC003481D12178D003E68DC /* SDK_Proxy.cpp */; }; @@ -314,7 +310,6 @@ 7CC003341D1216F8003E68DC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 7CC0033C1D121741003E68DC /* ZeroTierSDK_Unity3D_OSX.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ZeroTierSDK_Unity3D_OSX.bundle; sourceTree = BUILT_PRODUCTS_DIR; }; 7CC0033E1D121742003E68DC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 7CC003421D12178D003E68DC /* SDK_Debug.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDK_Debug.c; path = ../../../src/SDK_Debug.c; sourceTree = ""; }; 7CC003431D12178D003E68DC /* SDK_Debug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDK_Debug.h; path = ../../../src/SDK_Debug.h; sourceTree = ""; }; 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SDK_EthernetTap.cpp; path = ../../../src/SDK_EthernetTap.cpp; sourceTree = ""; }; 7CC003451D12178D003E68DC /* SDK_EthernetTap.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = SDK_EthernetTap.hpp; path = ../../../src/SDK_EthernetTap.hpp; sourceTree = ""; }; @@ -733,7 +728,6 @@ isa = PBXGroup; children = ( 7CAA03DE1D628E88003F9B1A /* SDK_Service.cpp */, - 7CC003421D12178D003E68DC /* SDK_Debug.c */, 7CC003431D12178D003E68DC /* SDK_Debug.h */, 7CC003441D12178D003E68DC /* SDK_EthernetTap.cpp */, 7CC003451D12178D003E68DC /* SDK_EthernetTap.hpp */, @@ -958,7 +952,6 @@ 7CC003821D1217D1003E68DC /* memp.c in Sources */, 7CC003661D1217C3003E68DC /* api_msg.c in Sources */, 7CC004291D121840003E68DC /* NetworkConfig.cpp in Sources */, - 7CC003501D12178D003E68DC /* SDK_Debug.c in Sources */, 7CC004321D121840003E68DC /* SHA512.cpp in Sources */, 7CC003531D12178D003E68DC /* SDK_Proxy.cpp in Sources */, 7CC003961D1217DD003E68DC /* icmp.c in Sources */, @@ -1101,7 +1094,6 @@ 7CC0045D1D1316F5003E68DC /* BackgroundResolver.cpp in Sources */, 7CC0045E1D1316F5003E68DC /* Http.cpp in Sources */, 7CC0045F1D1316F5003E68DC /* OSUtils.cpp in Sources */, - 7CC0043C1D1316BF003E68DC /* SDK_Debug.c in Sources */, 7CC0043D1D1316BF003E68DC /* SDK_EthernetTap.cpp in Sources */, 7CC0043E1D1316BF003E68DC /* SDK_Intercept.c in Sources */, 7CC0043F1D1316BF003E68DC /* SDK_Proxy.cpp in Sources */, @@ -1152,7 +1144,6 @@ 7CC004CD1D131E2D003E68DC /* netifapi.c in Sources */, 7CC004CE1D131E2D003E68DC /* sockets.c in Sources */, 7CC004CF1D131E2D003E68DC /* tcpip.c in Sources */, - 7CC004871D131E21003E68DC /* SDK_Debug.c in Sources */, 7CC004881D131E21003E68DC /* SDK_EthernetTap.cpp in Sources */, 7CC004891D131E21003E68DC /* SDK_Intercept.c in Sources */, 7CC0048A1D131E21003E68DC /* SDK_Proxy.cpp in Sources */, @@ -1261,7 +1252,6 @@ 7CC004F81D1324A2003E68DC /* BackgroundResolver.cpp in Sources */, 7CC004F91D1324A2003E68DC /* Http.cpp in Sources */, 7CC004FA1D1324A2003E68DC /* OSUtils.cpp in Sources */, - 7CC004D71D13247A003E68DC /* SDK_Debug.c in Sources */, 7CC004D81D13247A003E68DC /* SDK_EthernetTap.cpp in Sources */, 7CC004D91D13247A003E68DC /* SDK_Intercept.c in Sources */, 7CC004DA1D13247A003E68DC /* SDK_Proxy.cpp in Sources */, diff --git a/src/SDK.h b/src/SDK.h index bfa8522..054dc50 100644 --- a/src/SDK.h +++ b/src/SDK.h @@ -86,15 +86,16 @@ extern char *debug_logfile; // the specific way needed for that platform, but will be implemented // in terms of zts_* -// NOTE: Each platform specific exposed API will be implemented in terms of zts_* // SOCKS5 Proxy Controls int zts_start_proxy_server(const char *homepath, const char * nwid, struct sockaddr_storage * addr); int zts_stop_proxy_server(const char *nwid); int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage *addr); +bool zts_proxy_is_running(); + // ZT Service Controls void *zts_start_service(void *thread_id); void zts_stop_service(); -bool zts_is_running(); +bool zts_service_is_running(); void zts_join_network(const char * nwid); void zts_leave_network(const char * nwid); void zts_get_addresses(const char * nwid, char * addrstr); @@ -144,44 +145,43 @@ ssize_t zts_recvmsg(RECVMSG_SIG); // Android JNI Direct-call API // JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME #if defined(__ANDROID__) - // Exported JNI : ZT SERVICE CONTROLS - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path); - JNIEXPORT jboolean JNICALL Java_ZeroTier_SDK_zt_1stop_service(); - JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1join_1network(JNIEnv *env, jobject thisObj, jstring nwid); - JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1leave_1network(JNIEnv *env, jobject thisObj, jstring nwid); - JNIEXPORT jboolean JNICALL Java_ZeroTier_SDK_zt_1running(JNIEnv *env, jobject thisObj); - JNIEXPORT jobject JNICALL Java_ZeroTier_SDK_zt_1get_1addresses(JNIEnv *env, jobject thisObj, jstring nwid); - JNIEXPORT jboolean JNICALL Java_ZeroTier_SDK_zt_1is_1relayed(); - // Returns the homepath - JNIEXPORT jstring JNICALL Java_ZeroTier_SDK_zt_1get_1homepath(JNIEnv *env, jobject thisObj); + // ZT SERVICE CONTROLS + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path); + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1stop_service(); + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1service_1is_1running(JNIEnv *env, jobject thisObj); + JNIEXPORT jstring JNICALL Java_ZeroTier_ZTSDK_zt_1get_1homepath(JNIEnv *env, jobject thisObj); + JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1join_1network(JNIEnv *env, jobject thisObj, jstring nwid); + JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1leave_1network(JNIEnv *env, jobject thisObj, jstring nwid); + JNIEXPORT jobject JNICALL Java_ZeroTier_ZTSDK_zt_1get_1addresses(JNIEnv *env, jobject thisObj, jstring nwid); + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1is_1relayed(); - // Exported JNI : SOCKS5 PROXY SERVER CONTROLS - // Stops the SOCKS5 proxy server for a given ZeroTier network - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1start_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid, jobject zaddr); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1stop_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1get_1proxy_1server_1address(JNIEnv *env, jobject thisObj, jstring nwid, jobject zaddr); - - // Exported JNI : SOCKET API - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1socket(JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog); + // SOCKS5 PROXY SERVER CONTROLS + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1start_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid, jobject zaddr); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1stop_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1get_1proxy_1server_1address(JNIEnv *env, jobject thisObj, jstring nwid, jobject zaddr); + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1proxy_1is_1running(JNIEnv *env, jobject thisObj, jstring nwid); + + // SOCKET API + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1socket(JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog); // TCP - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags); // UDP - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1sendto(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1recvfrom(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1sendto(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1recvfrom(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr); // GENERAL UTILITY - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1close(JNIEnv *env, jobject thisObj, jint fd); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint socket, jint cmd, jint flags); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1setsockopt(JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockopt(JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1close(JNIEnv *env, jobject thisObj, jint fd); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint socket, jint cmd, jint flags); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1setsockopt(JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getsockopt(JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr); + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr); #endif diff --git a/src/SDK_Debug.c b/src/SDK_Debug.c deleted file mode 100644 index 2f8bc1a..0000000 --- a/src/SDK_Debug.c +++ /dev/null @@ -1,131 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2015 ZeroTier, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * -- - * - * ZeroTier may be used and distributed under the terms of the GPLv3, which - * are available at: http://www.gnu.org/licenses/gpl-3.0.html - * - * If you would like to embed ZeroTier into a commercial application or - * redistribute it in a modified binary form, please contact ZeroTier Networks - * LLC. Start here: http://www.zerotier.com/ - */ - -#ifndef _COMMON_DEBUG_ -#define _COMMON_DEBUG_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef SDK_DEBUG_H -#define SDK_DEBUG_H - -// Set during make (e.g. make SDK_DEBUG=2) -#define DEBUG_LEVEL 4 - -#define MSG_ERROR 1 // Errors -#define MSG_TRANSFER 2 // RX/TX specific statements -#define MSG_INFO 3 // Information which is generally useful to any user -#define MSG_DEBUG 4 // Information which is only useful to someone debugging -#define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense - -#ifdef __cplusplus -extern "C" { -#endif - #if __ANDROID__ - #include - #include - #define LOG_TAG "ZTSDK" - #define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) - #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) - #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) - #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) - #else - #define LOGV(...) fprintf(stdout, __VA_ARGS__) - #define LOGI(...) fprintf(stdout, __VA_ARGS__) - #define LOGD(...) fprintf(stdout, __VA_ARGS__) - #define LOGE(...) fprintf(stdout, __VA_ARGS__) - #endif -#ifdef __cplusplus -} // extern "C" -#endif - -//char *debug_logfile = (char*)0; -void dwr(int level, const char *fmt, ... ); - -//#if !defined(__ANDROID__) -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 -} -//#endif - -#endif // -#endif // \ No newline at end of file diff --git a/src/SDK_Debug.h b/src/SDK_Debug.h index bc0abe7..6c5d7dc 100644 --- a/src/SDK_Debug.h +++ b/src/SDK_Debug.h @@ -26,33 +26,129 @@ * LLC. Start here: http://www.zerotier.com/ */ + +#ifndef _SDK_DEBUG_H_ +#define _SDK_DEBUG_H_ + #define DEBUG_LEVEL 4 // Set this to adjust what you'd like to see in the debug traces #define MSG_ERROR 1 // Errors #define MSG_TRANSFER 2 // RX/TX specific statements -#define MSG_INFO 3 // Information which is generally useful to any user -#define MSG_DEBUG 4 // Information which is only useful to someone debugging -#define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense +#define MSG_INFO 3 // Information which is generally useful to any developer +#define MSG_EXTRA 4 // If nothing in your world makes sense -extern char *debug_logfile; +#define __SHOW_FILENAMES__ true + +// filenames +#if __SHOW_FILENAMES__ + #if __SHOW_FULL_FILENAME_PATH__ + #define __FILENAME__ __FILE__ // show the entire mess + #else + #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // shorten + #endif +#else + #define __FILENAME__ // omit filename +#endif #ifdef __cplusplus extern "C" { -#endif - #if __ANDROID__ +#endif + +#if defined(__ANDROID__) #include #include #define LOG_TAG "ZTSDK" - #define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) - #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) - #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) - #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) +#endif + +//#if defined(SDK_DEBUG) + #if DEBUG_LEVEL >= MSG_ERROR + #define DEBUG_ERROR(fmt, args...) fprintf(stderr, "\nZT_ERROR: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args) + #else + #define DEBUG_ERROR(fmt, args...) + #endif + #if DEBUG_LEVEL >= MSG_INFO + #if defined(__ANDROID__) + #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)) #else - #define LOGV(...) fprintf(stdout, __VA_ARGS__) - #define LOGI(...) fprintf(stdout, __VA_ARGS__) - #define LOGD(...) fprintf(stdout, __VA_ARGS__) - #define LOGE(...) fprintf(stdout, __VA_ARGS__) + #define DEBUG_INFO(fmt, args...) fprintf(stderr, "\nZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args) + #define DEBUG_BLANK(fmt, args...) fprintf(stderr, "\nZT_INFO : %s:%d:" fmt "\n", __FILENAME__, __LINE__, ##args) #endif + #else + #define DEBUG_INFO(fmt, args...) + #define DEBUG_BLANK(fmt, args...) + #endif + #if DEBUG_LEVEL >= MSG_TRANSFER + #if defined(__ANDROID__) + #define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_TRANS : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)) + #else + #define DEBUG_TRANS(fmt, args...) fprintf(stderr, "\nZT_TRANS: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args) + #endif + #else + #define DEBUG_TRANS(fmt, args...) + #endif + #if DEBUG_LEVEL >= MSG_EXTRA + #if defined(__ANDROID__) + #define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_EXTRA : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)) + #else + #define DEBUG_EXTRA(fmt, args...) fprintf(stderr, "\nZT_EXTRA: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args) + #endif + #else + #define DEBUG_EXTRA(fmt, args...) + #endif +//#endif + #ifdef __cplusplus } // extern "C" -#endif \ No newline at end of file +#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 diff --git a/src/SDK_EthernetTap.cpp b/src/SDK_EthernetTap.cpp index 5b3ff5e..36a5037 100644 --- a/src/SDK_EthernetTap.cpp +++ b/src/SDK_EthernetTap.cpp @@ -58,8 +58,8 @@ #include "lwip/netif.h" #include "lwip/udp.h" -#include "SDK_Debug.c" #include "SDK.h" +#include "SDK_Debug.h" #if !defined(__IOS__) && !defined(__ANDROID__) && !defined(__UNITY_3D__) && !defined(__XCODE__) const ip_addr_t ip_addr_any = { IPADDR_ANY }; @@ -142,9 +142,9 @@ NetconEthernetTap::NetconEthernetTap( lwipstack->__lwip_init(); _unixListenSocket = _phy.unixListen(sockPath,(void *)this); - dwr(MSG_DEBUG, " NetconEthernetTap initialized on: %s\n", sockPath); + DEBUG_INFO(" NetconEthernetTap initialized on: %s\n", sockPath); if (!_unixListenSocket) - dwr(MSG_ERROR, "unable to bind to: %s\n", sockPath); + DEBUG_ERROR("unable to bind to: %s\n", sockPath); _thread = Thread::start(this); } @@ -170,7 +170,7 @@ bool NetconEthernetTap::enabled() const bool NetconEthernetTap::addIp(const InetAddress &ip) { - dwr(MSG_DEBUG, "addIp(): ZT address = %s", ip.toString().c_str()); + DEBUG_INFO("addIp(): ZT address = %s", ip.toString().c_str()); Mutex::Lock _l(_ips_m); if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) { _ips.push_back(ip); @@ -222,7 +222,7 @@ std::vector NetconEthernetTap::ips() const void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) { - dwr(MSG_DEBUG_EXTRA, "RX packet: len = %d\n", len); + DEBUG_EXTRA("RX packet: len = %d\n", len); struct pbuf *p,*q; if (!_enabled) return; @@ -240,7 +240,7 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType // First pbuf gets ethernet header at start q = p; if (q->len < sizeof(ethhdr)) { - dwr(MSG_ERROR,"_put(): Dropped packet: first pbuf smaller than ethernet header\n"); + DEBUG_ERROR("_put(): Dropped packet: first pbuf smaller than ethernet header\n"); return; } memcpy(q->payload,ðhdr,sizeof(ethhdr)); @@ -253,13 +253,13 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType dataptr += q->len; } } else { - dwr(MSG_ERROR,"put(): Dropped packet: no pbufs available\n"); + DEBUG_ERROR("put(): Dropped packet: no pbufs available\n"); return; } { if(interface.input(p, &interface) != ERR_OK) { - dwr(MSG_ERROR,"put(): Error while RXing packet (netif->input)\n"); + DEBUG_ERROR("put(): Error while RXing packet (netif->input)\n"); } } } @@ -276,7 +276,6 @@ void NetconEthernetTap::scanMulticastGroups(std::vector &added,s { std::vector newGroups; Mutex::Lock _l(_multicastGroups_m); - // TODO: get multicast subscriptions from LWIP std::vector allIps(ips()); for(std::vector::iterator ip(allIps.begin());ip!=allIps.end();++ip) @@ -294,7 +293,6 @@ void NetconEthernetTap::scanMulticastGroups(std::vector &added,s removed.push_back(*m); } _multicastGroups.swap(newGroups); - } void NetconEthernetTap::threadMain() @@ -318,7 +316,7 @@ void NetconEthernetTap::threadMain() if(!_Connections[i]->sock || _Connections[i]->type != SOCK_STREAM) continue; int fd = _phy.getDescriptor(_Connections[i]->sock); - // dwr(MSG_DEBUG," tap_thread(): tcp\\jobs = {%d, %d}\n", _Connection.size(), jobmap.size()); + // DEBUG_INFO(" tap_thread(): tcp\\jobs = {%d, %d}\n", _Connection.size(), jobmap.size()); // If there's anything on the RX buf, set to notify in case we stalled if(_Connections[i]->rxsz > 0) _phy.setNotifyWritable(_Connections[i]->sock, true); @@ -327,14 +325,14 @@ void NetconEthernetTap::threadMain() ssize_t n = read(fd,&tmpbuf,BUF_SZ); if(_Connections[i]->TCP_pcb->state == SYN_SENT) { - dwr(MSG_DEBUG_EXTRA," tap_thread(): (sock=%p) state = SYN_SENT, should finish or be removed soon\n", + DEBUG_EXTRA(" tap_thread(): (sock=%p) state = SYN_SENT, should finish or be removed soon\n", (void*)&(_Connections[i]->sock)); } if((n < 0 && errno != EAGAIN) || (n == 0 && errno == EAGAIN)) { - dwr(MSG_DEBUG," tap_thread(): closing sock (%x)\n", _Connections[i]->sock); + DEBUG_INFO(" tap_thread(): closing sock (%x)\n", (void*)_Connections[i]->sock); closeConnection(_Connections[i]->sock); } else if (n > 0) { - dwr(MSG_DEBUG," tap_thread(): data read during connection check (%d bytes)\n", n); + DEBUG_INFO(" tap_thread(): data read during connection check (%ld bytes)\n", n); phyOnUnixData(_Connections[i]->sock,_phy.getuptr(_Connections[i]->sock),&tmpbuf,n); } } @@ -376,11 +374,11 @@ Connection *NetconEthernetTap::getConnection(PhySocket *sock) void NetconEthernetTap::closeConnection(PhySocket *sock) { - dwr(MSG_DEBUG_EXTRA, "closeConnection(sock=%x):\n", sock); + DEBUG_EXTRA("closeConnection(sock=%x):\n", sock); Mutex::Lock _l(_close_m); // Here we assume _tcpconns_m is already locked by caller if(!sock) { - dwr(MSG_DEBUG_EXTRA,"closeConnection(): invalid PhySocket\n"); + DEBUG_EXTRA("closeConnection(): invalid PhySocket\n"); return; } Connection *conn = getConnection(sock); @@ -390,14 +388,14 @@ void NetconEthernetTap::closeConnection(PhySocket *sock) lwipstack->__udp_remove(conn->UDP_pcb); } if(conn->TCP_pcb && conn->TCP_pcb->state != CLOSED) { - dwr(MSG_DEBUG_EXTRA,"closeConnection(conn=%p,sock=%p): PCB->state = %d\n", + DEBUG_EXTRA("closeConnection(conn=%p,sock=%p): PCB->state = %d\n", (void*)&conn, (void*)&sock, conn->TCP_pcb->state); if(conn->TCP_pcb->state == SYN_SENT /*|| conn->TCP_pcb->state == CLOSE_WAIT*/) { - dwr(MSG_DEBUG_EXTRA,"closeConnection(sock=%p): invalid PCB state for this operation. ignoring.\n", + DEBUG_EXTRA("closeConnection(sock=%p): invalid PCB state for this operation. ignoring.\n", (void*)&sock); return; } - dwr(MSG_DEBUG_EXTRA, "__tcp_close(...)\n"); + DEBUG_EXTRA("__tcp_close(...)\n"); if(lwipstack->__tcp_close(conn->TCP_pcb) == ERR_OK) { // Unregister callbacks for this PCB lwipstack->__tcp_arg(conn->TCP_pcb, NULL); @@ -407,7 +405,7 @@ void NetconEthernetTap::closeConnection(PhySocket *sock) lwipstack->__tcp_poll(conn->TCP_pcb, NULL, 1); } else { - dwr(MSG_DEBUG_EXTRA,"closeConnection(sock=%p): error while calling tcp_close()\n", (void*)&sock); + DEBUG_EXTRA("closeConnection(sock=%p): error while calling tcp_close()\n", (void*)&sock); } } for(size_t i=0;i<_Connections.size();++i) { @@ -424,7 +422,7 @@ void NetconEthernetTap::closeConnection(PhySocket *sock) } void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) { - dwr(MSG_DEBUG_EXTRA, "phyOnUnixClose(sock=%p):\n", (void*)&sock); + DEBUG_EXTRA("phyOnUnixClose(sock=%p):\n", (void*)&sock); Mutex::Lock _l(_tcpconns_m); closeConnection(sock); } @@ -432,7 +430,7 @@ void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) { void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwip_invoked) { - //dwr(MSG_DEBUG_EXTRA,"processReceivedData(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked); + //DEBUG_EXTRA("processReceivedData(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked); if(!lwip_invoked) { _tcpconns_m.lock(); _rx_buf_m.lock(); @@ -464,19 +462,19 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_TRANSFER,"UDP RX <--- :: {TX: %.3f%%, RX: %d, sock=%x} :: payload = %d bytes (%d.%d.%d.%d:%d)\n", + DEBUG_TRANS("UDP RX <--- :: {TX: %.3f%%, RX: %d, sock=%x} :: payload = %d bytes (%d.%d.%d.%d:%d)\n", (float)conn->txsz / max, conn->rxsz/* / max*/, conn->sock, payload_sz, d[0],d[1],d[2],d[3], port); #endif } // STREAM - //dwr(MSG_DEBUG, "phyOnUnixWritable(): tid = %d\n", pthread_mach_thread_np(pthread_self())); + //DEBUG_INFO("phyOnUnixWritable(): tid = %d\n", pthread_mach_thread_np(pthread_self())); if(conn->type==SOCK_STREAM) { // Only acknolwedge receipt of TCP packets lwipstack->__tcp_recved(conn->TCP_pcb, n); - dwr(MSG_TRANSFER,"TCP RX <--- :: {TX: %.3f%%, RX: %.3f%%, sock=%x} :: %d bytes\n", + DEBUG_TRANS("TCP RX <--- :: {TX: %.3f%%, RX: %.3f%%, sock=%x} :: %ld bytes\n", (float)conn->txsz / max, (float)conn->rxsz / max, conn->sock, n); } } else { - dwr(MSG_DEBUG_EXTRA," processReceivedData(): errno = %d, rxsz = %d\n", errno, conn->rxsz); + DEBUG_EXTRA(" processReceivedData(): errno = %d, rxsz = %d\n", errno, conn->rxsz); _phy.setNotifyWritable(conn->sock, false); } } @@ -490,16 +488,15 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi } } - void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked) { - //dwr(MSG_DEBUG_EXTRA," phyOnUnixWritable(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked); + //DEBUG_EXTRA(" phyOnUnixWritable(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked); processReceivedData(sock,uptr,lwip_invoked); } void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len) { - dwr(MSG_DEBUG_EXTRA, "phyOnUnixData(%p), len = %d\n", (void*)&sock, len); + DEBUG_EXTRA("phyOnUnixData(%p), len = %d\n", (void*)&sock, (int)len); uint64_t CANARY_num; pid_t pid, tid; ssize_t wlen = len; @@ -521,11 +518,11 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, if(detected_rpc) { unloadRPC(data, pid, tid, timestamp, CANARY, cmd, payload); memcpy(&CANARY_num, CANARY, CANARY_SZ); - dwr(MSG_DEBUG_EXTRA," RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n", + DEBUG_EXTRA(" RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n", (void*)&sock, pid, tid, timestamp, cmd); if(cmd == RPC_SOCKET) { - dwr(MSG_DEBUG," RPC_SOCKET\n", (void*)&sock); + DEBUG_INFO(" RPC_SOCKET\n", (void*)&sock); // Create new lwip socket and associate it with this sock struct socket_st socket_rpc; memcpy(&socket_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct socket_st)); @@ -553,7 +550,7 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, // Find job sockdata = jobmap[CANARY_num]; if(!sockdata.first) { - dwr(MSG_DEBUG," unable to locate job entry for %llu\n", (void*)&sock, CANARY_num); + DEBUG_INFO(" unable to locate job entry for %llu\n", (void*)&sock, CANARY_num); return; } else foundJob = true; @@ -615,36 +612,36 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, rpcSock = sockdata.first; buf = (unsigned char*)sockdata.second; unloadRPC(buf, pid, tid, timestamp, CANARY, cmd, payload); - dwr(MSG_DEBUG_EXTRA," RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n", + DEBUG_EXTRA(" RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n", (void*)&sock, pid, tid, timestamp, cmd); switch(cmd) { case RPC_BIND: - dwr(MSG_DEBUG," RPC_BIND\n", (void*)&sock); + DEBUG_INFO(" RPC_BIND\n", (void*)&sock); struct bind_st bind_rpc; memcpy(&bind_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct bind_st)); handleBind(sock, rpcSock, uptr, &bind_rpc); break; case RPC_LISTEN: - dwr(MSG_DEBUG," RPC_LISTEN\n", (void*)&sock); + DEBUG_INFO(" RPC_LISTEN\n", (void*)&sock); struct listen_st listen_rpc; memcpy(&listen_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct listen_st)); handleListen(sock, rpcSock, uptr, &listen_rpc); break; case RPC_GETSOCKNAME: - dwr(MSG_DEBUG," RPC_GETSOCKNAME\n", (void*)&sock); + DEBUG_INFO(" RPC_GETSOCKNAME\n", (void*)&sock); struct getsockname_st getsockname_rpc; memcpy(&getsockname_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st)); handleGetsockname(sock, rpcSock, uptr, &getsockname_rpc); break; case RPC_GETPEERNAME: - dwr(MSG_DEBUG," RPC_GETPEERNAME\n", (void*)&sock); + DEBUG_INFO(" RPC_GETPEERNAME\n", (void*)&sock); struct getsockname_st getpeername_rpc; memcpy(&getpeername_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st)); handleGetpeername(sock, rpcSock, uptr, &getpeername_rpc); break; case RPC_CONNECT: - dwr(MSG_DEBUG," RPC_CONNECT\n", (void*)&sock); + DEBUG_INFO(" RPC_CONNECT\n", (void*)&sock); struct connect_st connect_rpc; memcpy(&connect_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct connect_st)); handleConnect(sock, rpcSock, conn, &connect_rpc); @@ -661,13 +658,13 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, } int NetconEthernetTap::sendReturnValue(PhySocket *sock, int retval, int _errno = 0){ - dwr(MSG_DEBUG_EXTRA," sendReturnValue(sock=%p)\n", (void*)&sock); + DEBUG_EXTRA(" sendReturnValue(sock=%p)\n", (void*)&sock); return sendReturnValue(_phy.getDescriptor(sock), retval, _errno); } int NetconEthernetTap::sendReturnValue(int fd, int retval, int _errno = 0) { //#if !defined(USE_SOCKS_PROXY) - dwr(MSG_DEBUG_EXTRA," sendReturnValue(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno); + DEBUG_EXTRA(" sendReturnValue(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno); int sz = sizeof(char) + sizeof(retval) + sizeof(errno); char retmsg[sz]; memset(&retmsg, 0, sizeof(retmsg)); @@ -697,7 +694,7 @@ void NetconEthernetTap::unloadRPC(void *data, pid_t &pid, pid_t &tid, err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err) { - dwr(MSG_DEBUG, "nc_accept(pcb=%p)\n", (void*)&newPCB); + DEBUG_INFO("nc_accept(pcb=%p)\n", (void*)&newPCB); Larg *l = (Larg*)arg; Mutex::Lock _l(l->tap->_tcpconns_m); Connection *conn = l->conn; @@ -717,7 +714,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err) if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) { if(errno < 0) { l->tap->sendReturnValue(conn, -1, errno); - dwr(MSG_ERROR," nc_accept(): unable to create socketpair\n"); + DEBUG_ERROR(" nc_accept(): unable to create socketpair\n"); return ERR_MEM; } } @@ -740,14 +737,14 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err) tcp_accepted(conn->TCP_pcb); // Let lwIP know that it can queue additional incoming connections return ERR_OK; } else - dwr(MSG_ERROR," nc_accept(): can't locate Connection object for PCB.\n"); + DEBUG_ERROR(" nc_accept(): can't locate Connection object for PCB.\n"); return -1; } void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, struct ip_addr * addr, u16_t port) { Larg *l = (Larg*)arg; - //dwr(MSG_DEBUG_EXTRA, "nc_udp_recved(conn=%p,pcb=%p,port=%d)\n", (void*)&(l->conn), (void*)&upcb, port); + //DEBUG_EXTRA("nc_udp_recved(conn=%p,pcb=%p,port=%d)\n", (void*)&(l->conn), (void*)&upcb, port); int tot = 0; unsigned char *addr_pos, *sz_pos, *payload_pos; struct pbuf* q = p; @@ -762,7 +759,7 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct if(p) { // Intra-API "packetization" scheme: [addr_len|addr|payload_len|payload] if(l->conn->rxsz == DEFAULT_UDP_RX_BUF_SZ) { // if UDP buffer full - dwr(MSG_DEBUG, "nc_udp_recved(): UDP RX buffer full. Discarding oldest payload segment\n"); + DEBUG_INFO("nc_udp_recved(): UDP RX buffer full. Discarding oldest payload segment\n"); memmove(l->conn->rxbuf, l->conn->rxbuf + ZT_MAX_MTU, DEFAULT_UDP_RX_BUF_SZ - ZT_MAX_MTU); addr_pos = l->conn->rxbuf + (DEFAULT_UDP_RX_BUF_SZ - ZT_MAX_MTU); // TODO: sz_pos = addr_pos + sizeof(struct sockaddr_storage); @@ -788,7 +785,7 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct if(tot) { l->conn->rxsz += ZT_MAX_MTU; memcpy(sz_pos, &tot, sizeof(tot)); - //dwr(MSG_DEBUG_EXTRA, " nc_udp_recved(): data_len = %d, rxsz = %d, addr_info_len = %d\n", + //DEBUG_EXTRA(" nc_udp_recved(): data_len = %d, rxsz = %d, addr_info_len = %d\n", // tot, l->conn->rxsz, sizeof(u32_t) + sizeof(u16_t)); l->tap->phyOnUnixWritable(l->conn->sock, NULL, true); l->tap->_phy.setNotifyWritable(l->conn->sock, true); @@ -800,13 +797,13 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err) { Larg *l = (Larg*)arg; - dwr(MSG_DEBUG_EXTRA, "nc_recved(conn=%p,pcb=%p)\n", (void*)&(l->conn), (void*)&PCB); + DEBUG_EXTRA("nc_recved(conn=%p,pcb=%p)\n", (void*)&(l->conn), (void*)&PCB); int tot = 0; struct pbuf* q = p; Mutex::Lock _l(l->tap->_tcpconns_m); if(!l->conn) { - dwr(MSG_ERROR," nc_recved(): no connection\n"); + DEBUG_ERROR(" nc_recved(): no connection\n"); return ERR_OK; } if(p == NULL) { @@ -825,7 +822,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf * int avail = DEFAULT_TCP_RX_BUF_SZ - l->conn->rxsz; int len = p->len; if(avail < len) - dwr(MSG_ERROR," nc_recved(): not enough room (%d bytes) on RX buffer\n", avail); + DEBUG_ERROR(" nc_recved(): not enough room (%d bytes) on RX buffer\n", avail); memcpy(l->conn->rxbuf + (l->conn->rxsz), p->payload, len); l->conn->rxsz += len; p = p->next; @@ -844,7 +841,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf * err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len) { - dwr(MSG_DEBUG_EXTRA, "nc_sent(pcb=%p)\n", (void*)&PCB); + DEBUG_EXTRA("nc_sent(pcb=%p)\n", (void*)&PCB); Larg *l = (Larg*)arg; Mutex::Lock _l(l->tap->_tcpconns_m); if(l->conn->probation && l->conn->txsz == 0){ @@ -862,13 +859,13 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len) err_t NetconEthernetTap::nc_connected_proxy(void *arg, struct tcp_pcb *PCB, err_t err) { - dwr(MSG_DEBUG, "nc_connected_proxy(pcb=%p)\n", (void*)&PCB); + DEBUG_INFO("nc_connected_proxy(pcb=%p)\n", (void*)&PCB); return ERR_OK; } err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err) { - dwr(MSG_DEBUG, "nc_connected(pcb=%p)\n", (void*)&PCB); + DEBUG_INFO("nc_connected(pcb=%p)\n", (void*)&PCB); Larg *l = (Larg*)arg; if(l && l->conn) l->tap->sendReturnValue(l->tap->_phy.getDescriptor(l->conn->rpcSock), ERR_OK); @@ -882,54 +879,54 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *PCB) void NetconEthernetTap::nc_err(void *arg, err_t err) { - dwr(MSG_DEBUG,"nc_err() = %d\n", err); + DEBUG_INFO("nc_err() = %d\n", err); Larg *l = (Larg*)arg; Mutex::Lock _l(l->tap->_tcpconns_m); if(!l->conn) - dwr(MSG_ERROR," nc_err(): connection is NULL!\n"); + DEBUG_ERROR(" nc_err(): connection is NULL!\n"); int fd = l->tap->_phy.getDescriptor(l->conn->sock); switch(err) { case ERR_MEM: - dwr(MSG_ERROR," nc_err(): ERR_MEM->ENOMEM\n"); + DEBUG_ERROR(" nc_err(): ERR_MEM->ENOMEM\n"); l->tap->sendReturnValue(fd, -1, ENOMEM); break; case ERR_BUF: - dwr(MSG_ERROR," nc_err(): ERR_BUF->ENOBUFS\n"); + DEBUG_ERROR(" nc_err(): ERR_BUF->ENOBUFS\n"); l->tap->sendReturnValue(fd, -1, ENOBUFS); break; case ERR_TIMEOUT: - dwr(MSG_ERROR," nc_err(): ERR_TIMEOUT->ETIMEDOUT\n"); + DEBUG_ERROR(" nc_err(): ERR_TIMEOUT->ETIMEDOUT\n"); l->tap->sendReturnValue(fd, -1, ETIMEDOUT); break; case ERR_RTE: - dwr(MSG_ERROR," nc_err(): ERR_RTE->ENETUNREACH\n"); + DEBUG_ERROR(" nc_err(): ERR_RTE->ENETUNREACH\n"); l->tap->sendReturnValue(fd, -1, ENETUNREACH); break; case ERR_INPROGRESS: - dwr(MSG_ERROR," nc_err(): ERR_INPROGRESS->EINPROGRESS\n"); + DEBUG_ERROR(" nc_err(): ERR_INPROGRESS->EINPROGRESS\n"); l->tap->sendReturnValue(fd, -1, EINPROGRESS); break; case ERR_VAL: - dwr(MSG_ERROR," nc_err(): ERR_VAL->EINVAL\n"); + DEBUG_ERROR(" nc_err(): ERR_VAL->EINVAL\n"); l->tap->sendReturnValue(fd, -1, EINVAL); break; case ERR_WOULDBLOCK: - dwr(MSG_ERROR," nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n"); + DEBUG_ERROR(" nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n"); l->tap->sendReturnValue(fd, -1, EWOULDBLOCK); break; case ERR_USE: - dwr(MSG_ERROR," nc_err(): ERR_USE->EADDRINUSE\n"); + DEBUG_ERROR(" nc_err(): ERR_USE->EADDRINUSE\n"); l->tap->sendReturnValue(fd, -1, EADDRINUSE); break; case ERR_ISCONN: - dwr(MSG_ERROR," nc_err(): ERR_ISCONN->EISCONN\n"); + DEBUG_ERROR(" nc_err(): ERR_ISCONN->EISCONN\n"); l->tap->sendReturnValue(fd, -1, EISCONN); break; case ERR_ABRT: - dwr(MSG_ERROR," nc_err(): ERR_ABRT->ECONNREFUSED\n"); + DEBUG_ERROR(" nc_err(): ERR_ABRT->ECONNREFUSED\n"); l->tap->sendReturnValue(fd, -1, ECONNREFUSED); break; @@ -953,7 +950,7 @@ void NetconEthernetTap::nc_err(void *arg, err_t err) default: break; } - dwr(MSG_ERROR," nc_err(): closing connection\n"); + DEBUG_ERROR(" nc_err(): closing connection\n"); l->tap->closeConnection(l->conn); } @@ -966,7 +963,7 @@ void NetconEthernetTap::handleGetsockname(PhySocket *sock, PhySocket *rpcSock, v Mutex::Lock _l(_tcpconns_m); Connection *conn = getConnection(sock); if(conn->local_addr == NULL){ - dwr(MSG_DEBUG_EXTRA," handleGetsockname(): No address info available. Is it bound?"); + DEBUG_EXTRA(" handleGetsockname(): No address info available. Is it bound?"); struct sockaddr_storage storage; memset(&storage, 0, sizeof(struct sockaddr_storage)); write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage)); @@ -980,7 +977,7 @@ void NetconEthernetTap::handleGetpeername(PhySocket *sock, PhySocket *rpcSock, v Mutex::Lock _l(_tcpconns_m); Connection *conn = getConnection(sock); if(conn->peer_addr == NULL){ - dwr(MSG_DEBUG_EXTRA," handleGetpeername(): No peer address info available. Is it connected?"); + DEBUG_EXTRA(" handleGetpeername(): No peer address info available. Is it connected?"); struct sockaddr_storage storage; memset(&storage, 0, sizeof(struct sockaddr_storage)); write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage)); @@ -997,7 +994,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u ip_addr_t connAddr; if(!_ips.size()) { // We haven't been given an address yet. Binding at this stage is premature - dwr(MSG_ERROR, " handleBind(): ZT address hasn't been provided. Cannot bind yet."); + DEBUG_ERROR(" handleBind(): ZT address hasn't been provided. Cannot bind yet."); sendReturnValue(rpcSock, -1, ENOMEM); return; } @@ -1008,11 +1005,11 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_DEBUG," handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port); + DEBUG_INFO(" handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port); connAddr.addr = *((u32_t *)_ips[0].rawIpData()); Connection *conn = getConnection(sock); - dwr(MSG_DEBUG," handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->fd, port); + DEBUG_INFO(" handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->fd, port); if(conn) { if(conn->type == SOCK_DGRAM) { #if defined(__ANDROID__) @@ -1036,7 +1033,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u if(conn->TCP_pcb->state == CLOSED){ err = lwipstack->__tcp_bind(conn->TCP_pcb, &connAddr, port); if(err != ERR_OK) { - dwr(MSG_ERROR," handleBind(): err = %d\n", err); + DEBUG_ERROR(" handleBind(): err = %d\n", err); if(err == ERR_USE) sendReturnValue(rpcSock, -1, EADDRINUSE); if(err == ERR_MEM) @@ -1048,20 +1045,20 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success } } else { - dwr(MSG_ERROR," handleBind(): PCB (conn=%p,pcb=%p) not in CLOSED state. Ignoring BIND request.\n", + DEBUG_ERROR(" handleBind(): PCB (conn=%p,pcb=%p) not in CLOSED state. Ignoring BIND request.\n", (void*)&conn, (void*)&conn->TCP_pcb); sendReturnValue(rpcSock, -1, EINVAL); } } } else { - dwr(MSG_ERROR," handleBind(): unable to locate Connection.\n"); + DEBUG_ERROR(" handleBind(): unable to locate Connection.\n"); sendReturnValue(rpcSock, -1, EBADF); } } void NetconEthernetTap::handleListen(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc) { - dwr(MSG_DEBUG, "handleListen(sock=%p)\n", (void*)&sock); + DEBUG_INFO("handleListen(sock=%p)\n", (void*)&sock); Mutex::Lock _l(_tcpconns_m); Connection *conn = getConnection(sock); @@ -1071,12 +1068,12 @@ void NetconEthernetTap::handleListen(PhySocket *sock, PhySocket *rpcSock, void * return; } if(!conn) { - dwr(MSG_ERROR," handleListen(): unable to locate Connection.\n"); + DEBUG_ERROR(" handleListen(): unable to locate Connection.\n"); sendReturnValue(rpcSock, -1, EBADF); return; } if(conn->TCP_pcb->state == LISTEN) { - dwr(MSG_ERROR," handleListen(): PCB is already in listening state.\n"); + DEBUG_ERROR(" handleListen(): PCB is already in listening state.\n"); sendReturnValue(rpcSock, ERR_OK, ERR_OK); return; } @@ -1103,19 +1100,19 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty { Connection *conn = getConnection(sock); if(!conn){ - dwr(MSG_DEBUG, "handleSocketProxy(sock=%p): Unable to locate Connection object for this PhySocket\n", (void*)&sock); + DEBUG_INFO("handleSocketProxy(sock=%p): Unable to locate Connection object for this PhySocket\n", (void*)&sock); return NULL; } - dwr(MSG_DEBUG, "handleSocketProxy(sock=%p)\n", (void*)&sock); + DEBUG_INFO("handleSocketProxy(sock=%p)\n", (void*)&sock); struct udp_pcb *new_udp_PCB = NULL; struct tcp_pcb *new_tcp_PCB = NULL; if(socket_type == SOCK_DGRAM) { - dwr(MSG_DEBUG, " handleSocketProxy(): SOCK_DGRAM\n"); + DEBUG_INFO(" handleSocketProxy(): SOCK_DGRAM\n"); Mutex::Lock _l(_tcpconns_m); new_udp_PCB = lwipstack->__udp_new(); } else if(socket_type == SOCK_STREAM) { - dwr(MSG_DEBUG, " handleSocketProxy(): SOCK_STREAM\n"); + DEBUG_INFO(" handleSocketProxy(): SOCK_STREAM\n"); Mutex::Lock _l(_tcpconns_m); new_tcp_PCB = lwipstack->__tcp_new(); } @@ -1126,25 +1123,25 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty conn->peer_addr = NULL; if(conn->type == SOCK_DGRAM) conn->UDP_pcb = new_udp_PCB; if(conn->type == SOCK_STREAM) conn->TCP_pcb = new_tcp_PCB; - dwr(MSG_DEBUG, " handleSocketProxy(): Updated sock=%p\n", (void*)&sock); + DEBUG_INFO(" handleSocketProxy(): Updated sock=%p\n", (void*)&sock); return conn; } - dwr(MSG_ERROR," handleSocketProxy(): Memory not available for new PCB\n"); + DEBUG_ERROR(" handleSocketProxy(): Memory not available for new PCB\n"); return NULL; } Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc) { - dwr(MSG_DEBUG, "handleSocket(sock=%p)\n", (void*)&sock); + DEBUG_INFO("handleSocket(sock=%p)\n", (void*)&sock); struct udp_pcb *new_udp_PCB = NULL; struct tcp_pcb *new_tcp_PCB = NULL; if(socket_rpc->socket_type == SOCK_DGRAM) { - dwr(MSG_DEBUG, "handleSocket(): SOCK_DGRAM\n"); + DEBUG_INFO("handleSocket(): SOCK_DGRAM\n"); Mutex::Lock _l(_tcpconns_m); new_udp_PCB = lwipstack->__udp_new(); } else if(socket_rpc->socket_type == SOCK_STREAM) { - dwr(MSG_DEBUG, "handleSocket(): SOCK_STREAM\n"); + DEBUG_INFO("handleSocket(): SOCK_STREAM\n"); Mutex::Lock _l(_tcpconns_m); new_tcp_PCB = lwipstack->__tcp_new(); } @@ -1160,14 +1157,14 @@ Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struc _Connections.push_back(newConn); return newConn; } - dwr(MSG_ERROR," handleSocket(): Memory not available for new PCB\n"); + DEBUG_ERROR(" handleSocket(): Memory not available for new PCB\n"); sendReturnValue(_phy.getDescriptor(sock), -1, ENOMEM); return NULL; } int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *rawAddr) { - dwr(MSG_DEBUG, "handleConnectProxy(%p)\n", (void*)&sock); + DEBUG_INFO("handleConnectProxy(%p)\n", (void*)&sock); Mutex::Lock _l(_tcpconns_m); int port = rawAddr->sin_port; ip_addr_t connAddr = convert_ip(rawAddr); @@ -1175,13 +1172,13 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r Connection *conn = getConnection(sock); if(!conn) { - dwr(MSG_DEBUG, " handleConnectProxy(): Unable to locate Connection object for (sock=%p)\n", (void*)&sock); + DEBUG_INFO(" handleConnectProxy(): Unable to locate Connection object for (sock=%p)\n", (void*)&sock); return -1; } if(conn->type == SOCK_DGRAM) { // Generates no network traffic if((err = lwipstack->__udp_connect(conn->UDP_pcb,&connAddr,port)) < 0) - dwr(MSG_DEBUG, "handleConnectProxy(): Error while connecting to with UDP\n"); + DEBUG_INFO("handleConnectProxy(): Error while connecting to with UDP\n"); lwipstack->__udp_recv(conn->UDP_pcb, nc_udp_recved, new Larg(this, conn)); errno = ERR_OK; return 0; @@ -1199,10 +1196,10 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_DEBUG," handleConnectProxy(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port); - dwr(MSG_DEBUG," handleConnectProxy(): pcb->state = %x\n", conn->TCP_pcb->state); + DEBUG_INFO(" handleConnectProxy(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port); + DEBUG_INFO(" handleConnectProxy(): pcb->state = %x\n", conn->TCP_pcb->state); if(conn->TCP_pcb->state != CLOSED) { - dwr(MSG_DEBUG," handleConnectProxy(): PCB != CLOSED, cannot connect using this PCB\n"); + DEBUG_INFO(" handleConnectProxy(): PCB != CLOSED, cannot connect using this PCB\n"); errno = EAGAIN; return -1; } @@ -1246,7 +1243,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r // that's it! // - Most instances of a retval for a connect() should happen // in the nc_connect() and nc_err() callbacks! - dwr(MSG_ERROR," handleConnectProxy(): unable to connect\n"); + DEBUG_ERROR(" handleConnectProxy(): unable to connect\n"); errno = EAGAIN; return -1; } @@ -1254,7 +1251,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r conn->listening=true; return 0; } else { - dwr(MSG_ERROR," handleConnectProxy(): could not locate PCB based on their fd\n"); + DEBUG_ERROR(" handleConnectProxy(): could not locate PCB based on their fd\n"); errno = EBADF; return -1; } @@ -1263,7 +1260,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Connection *conn, struct connect_st* connect_rpc) { - dwr(MSG_DEBUG_EXTRA, "handleConnect(%p)\n", (void*)&sock); + DEBUG_EXTRA("handleConnect(%p)\n", (void*)&sock); Mutex::Lock _l(_tcpconns_m); struct sockaddr_in *rawAddr = (struct sockaddr_in *) &connect_rpc->addr; int port = lwipstack->__lwip_ntohs(rawAddr->sin_port); @@ -1275,12 +1272,12 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_DEBUG," handleConnect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port); + DEBUG_INFO(" handleConnect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port); if(conn->type == SOCK_DGRAM) { // Generates no network traffic if((err = lwipstack->__udp_connect(conn->UDP_pcb,&connAddr,port)) < 0) - dwr(MSG_ERROR, "handleConnect(): Error while connecting to with UDP\n"); + DEBUG_ERROR("handleConnect(): Error while connecting to with UDP\n"); lwipstack->__udp_recv(conn->UDP_pcb, nc_udp_recved, new Larg(this, conn)); sendReturnValue(rpcSock, 0, ERR_OK); return; @@ -1292,9 +1289,9 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne lwipstack->__tcp_poll(conn->TCP_pcb, nc_poll, APPLICATION_POLL_FREQ); lwipstack->__tcp_arg(conn->TCP_pcb, new Larg(this, conn)); - dwr(MSG_DEBUG_EXTRA," handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state); + DEBUG_EXTRA(" handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state); if(conn->TCP_pcb->state != CLOSED) { - dwr(MSG_DEBUG," handleConnect(): PCB != CLOSED, cannot connect using this PCB\n"); + DEBUG_INFO(" handleConnect(): PCB != CLOSED, cannot connect using this PCB\n"); sendReturnValue(rpcSock, -1, EAGAIN); return; } @@ -1339,48 +1336,48 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne // that's it! // - Most instances of a retval for a connect() should happen // in the nc_connect() and nc_err() callbacks! - dwr(MSG_ERROR," handleConnect(): unable to connect\n"); + DEBUG_ERROR(" handleConnect(): unable to connect\n"); sendReturnValue(rpcSock, -1, EAGAIN); } // Everything seems to be ok, but we don't have enough info to retval conn->listening=true; conn->rpcSock=rpcSock; // used for return value from lwip CB } else { - dwr(MSG_ERROR," handleConnect(): could not locate PCB based on their fd\n"); + DEBUG_ERROR(" handleConnect(): could not locate PCB based on their fd\n"); sendReturnValue(rpcSock, -1, EBADF); } } void NetconEthernetTap::handleWrite(Connection *conn) { - dwr(MSG_DEBUG_EXTRA, "handleWrite(conn=%p)\n", (void*)&conn); + DEBUG_EXTRA("handleWrite(conn=%p)\n", (void*)&conn); if(!conn || (!conn->TCP_pcb && !conn->UDP_pcb)) { - dwr(MSG_ERROR," handleWrite(): invalid connection\n"); + DEBUG_ERROR(" handleWrite(): invalid connection\n"); return; } if(conn->type == SOCK_DGRAM) { if(!conn->UDP_pcb) { - dwr(MSG_ERROR, " handleWrite(): type = SOCK_DGRAM, invalid UDP_pcb\n"); + DEBUG_ERROR(" handleWrite(): type = SOCK_DGRAM, invalid UDP_pcb\n"); return; } // TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks int udp_trans_len = conn->txsz < ZT_UDP_DEFAULT_PAYLOAD_MTU ? conn->txsz : ZT_UDP_DEFAULT_PAYLOAD_MTU; - dwr(MSG_DEBUG_EXTRA, " handleWrite(): Allocating pbuf chain of size (%d) for UDP packet, TXSZ = %d\n", udp_trans_len, conn->txsz); + DEBUG_EXTRA(" handleWrite(): Allocating pbuf chain of size (%d) for UDP packet, TXSZ = %d\n", udp_trans_len, conn->txsz); struct pbuf * pb = lwipstack->__pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL); if(!pb){ - dwr(MSG_ERROR, " handleWrite(): unable to allocate new pbuf of size (%d)\n", conn->txsz); + DEBUG_ERROR(" handleWrite(): unable to allocate new pbuf of size (%d)\n", conn->txsz); return; } memcpy(pb->payload, conn->txbuf, udp_trans_len); int err = lwipstack->__udp_send(conn->UDP_pcb, pb); if(err == ERR_MEM) { - dwr(MSG_ERROR, " handleWrite(): Error sending packet. Out of memory\n"); + DEBUG_ERROR(" handleWrite(): Error sending packet. Out of memory\n"); } else if(err == ERR_RTE) { - dwr(MSG_ERROR, " handleWrite(): Could not find route to destinations address\n"); + DEBUG_ERROR(" handleWrite(): Could not find route to destinations address\n"); } else if(err != ERR_OK) { - dwr(MSG_ERROR, " handleWrite(): Error sending packet - %d\n", err); + DEBUG_ERROR(" handleWrite(): Error sending packet - %d\n", err); } else { // Success int buf_remaining = (conn->txsz)-udp_trans_len; @@ -1398,7 +1395,7 @@ void NetconEthernetTap::handleWrite(Connection *conn) d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_TRANSFER,"UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes (%d.%d.%d.%d:%d)\n", + DEBUG_TRANS("UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes (%d.%d.%d.%d:%d)\n", conn->sock, udp_trans_len, d[0], d[1], d[2], d[3], port); #endif } @@ -1407,7 +1404,7 @@ void NetconEthernetTap::handleWrite(Connection *conn) } else if(conn->type == SOCK_STREAM) { if(!conn->TCP_pcb) { - dwr(MSG_ERROR, " handleWrite(): type = SOCK_STREAM, invalid TCP_pcb\n"); + DEBUG_ERROR(" handleWrite(): type = SOCK_STREAM, invalid TCP_pcb\n"); return; } // How much we are currently allowed to write to the connection @@ -1419,7 +1416,7 @@ void NetconEthernetTap::handleWrite(Connection *conn) // corresponding PhySocket until nc_sent() is called and confirms that there is // now space on the buffer if(!conn->probation) { - dwr(MSG_ERROR," handleWrite(): sndbuf == 0, LWIP stack is full\n"); + DEBUG_ERROR(" handleWrite(): sndbuf == 0, LWIP stack is full\n"); _phy.setNotifyReadable(conn->sock, false); conn->probation = true; } @@ -1438,9 +1435,9 @@ void NetconEthernetTap::handleWrite(Connection *conn) err = lwipstack->__tcp_write(conn->TCP_pcb, &conn->txbuf, r, TCP_WRITE_FLAG_COPY); lwipstack->__tcp_output(conn->TCP_pcb); if(err != ERR_OK) { - dwr(MSG_ERROR," handleWrite(): error while writing to PCB, (err = %d)\n", err); + DEBUG_ERROR(" handleWrite(): error while writing to PCB, (err = %d)\n", err); if(err == -1) - dwr(MSG_ERROR," handleWrite(): out of memory\n"); + DEBUG_ERROR(" handleWrite(): out of memory\n"); return; } else { sz = (conn->txsz)-r; @@ -1448,7 +1445,7 @@ void NetconEthernetTap::handleWrite(Connection *conn) memmove(&conn->txbuf, (conn->txbuf+r), sz); conn->txsz -= r; int max = conn->type == SOCK_STREAM ? DEFAULT_TCP_TX_BUF_SZ : DEFAULT_UDP_TX_BUF_SZ; - dwr(MSG_TRANSFER,"TCP TX ---> :: {TX: %.3f%%, RX: %.3f%%, sock=%p} :: %d bytes\n", + DEBUG_TRANS("TCP TX ---> :: {TX: %.3f%%, RX: %.3f%%, sock=%p} :: %d bytes\n", (float)conn->txsz / (float)max, (float)conn->rxsz / max, (void*)&conn->sock, r); return; } diff --git a/src/SDK_Intercept.c b/src/SDK_Intercept.c index 3e8b11d..e7339ee 100644 --- a/src/SDK_Intercept.c +++ b/src/SDK_Intercept.c @@ -25,7 +25,7 @@ * LLC. Start here: http://www.zerotier.com/ */ -#if defined(SDK_INTERCEPT) +#if defined(_SDK_INTERCEPT_) #include #include @@ -42,7 +42,7 @@ #include #include #include -#include +//#include #include #include #include @@ -59,9 +59,8 @@ #include "SDK_Debug.h" #include "SDK_RPC.h" -void dwr(int level, const char *fmt, ... ); pthread_key_t thr_id_key; -char *api_netpath; +//char *api_netpath; // externs common between SDK_Intercept and SDK_Socket from SDK.h #if defined(__linux__) @@ -93,7 +92,7 @@ char *api_netpath; extern void load_symbols() { - dwr(MSG_DEBUG_EXTRA,"load_symbols\n"); + DEBUG_EXTRA(""); #if defined(__linux__) realaccept4 = dlsym(RTLD_NEXT, "accept4"); #if !defined(__ANDROID__) @@ -122,7 +121,7 @@ char *api_netpath; // Return whether 'intercept' API is enabled for this thread bool check_intercept_enabled() { - dwr(MSG_DEBUG_EXTRA, "check_intercept_enabled()\n"); + DEBUG_EXTRA(""); if(!realconnect){ load_symbols(); } @@ -157,7 +156,7 @@ char *api_netpath; addr_un = (struct sockaddr_un*)&addr; return strcmp(addr_un->sun_path, api_netpath) == 0; } - dwr(MSG_DEBUG,"connected_to_service(): Not connected to service\n"); + DEBUG_INFO("connected_to_service(): Not connected to service\n"); return 0; } @@ -170,7 +169,7 @@ char *api_netpath; #if !defined(__ANDROID__) ssize_t sendto(SENDTO_SIG) { - dwr(MSG_DEBUG, "sendto(%d, ..., %d)\n", fd, len); + DEBUG_INFO("fd=%d, len=%d\n", fd, (int)len); //if (!check_intercept_enabled()) return realsendto(fd, buf, len, flags, addr, addrlen); return zts_sendto(fd, buf, len, flags, addr, addrlen); @@ -185,7 +184,7 @@ char *api_netpath; #if !defined(__ANDROID__) ssize_t sendmsg(SENDMSG_SIG) { - dwr(MSG_DEBUG, "sendmsg()\n"); + DEBUG_INFO(""); //if(!check_intercept_enabled()) return realsendmsg(fd, msg, flags); zts_sendmsg(fd, msg, flags); @@ -201,7 +200,7 @@ char *api_netpath; #if !defined(__ANDROID__) ssize_t recvfrom(RECVFROM_SIG) { - dwr(MSG_DEBUG, "recvfrom(%d)\n", socket); + DEBUG_INFO("fd=%d", fd); if(!check_intercept_enabled()) return realrecvfrom(fd, buf, len, flags, addr, addrlen); return zts_recvfrom(fd, buf, len, flags, addr, addrlen); @@ -216,7 +215,7 @@ char *api_netpath; #if !defined(__ANDROID__) ssize_t recvmsg(RECVMSG_SIG) { - dwr(MSG_DEBUG, "recvmsg(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); //if(!check_intercept_enabled()) return realrecvmsg(fd, msg, flags); return zts_recvmsg(fd, msg, flags); @@ -230,7 +229,7 @@ char *api_netpath; int setsockopt(SETSOCKOPT_SIG) { - dwr(MSG_DEBUG, "setsockopt(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); if (!check_intercept_enabled()) return realsetsockopt(fd, level, optname, optval, optlen); #if defined(__linux__) @@ -253,7 +252,7 @@ char *api_netpath; int getsockopt(GETSOCKOPT_SIG) { - dwr(MSG_DEBUG, "getsockopt(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); if (!check_intercept_enabled() || !connected_to_service(fd)) return realgetsockopt(fd, level, optname, optval, optlen); return zts_getsockopt(fd, level, optname, optval, optlen); @@ -265,15 +264,15 @@ char *api_netpath; // int socket_family, int socket_type, int protocol int socket(SOCKET_SIG) - { - dwr(MSG_DEBUG, "socket()\n"); + { + DEBUG_INFO(); if (!check_intercept_enabled() && socket_type) { int err = realsocket(socket_family, socket_type, protocol); if(err < 0) { perror("socket:\n"); } else { - dwr(MSG_DEBUG, " socket() = %d\n", err); + DEBUG_INFO("err=%d", err); return err; } } @@ -284,7 +283,7 @@ char *api_netpath; #endif || socket_family == AF_UNIX) { int err = realsocket(socket_family, socket_type, protocol); - dwr(MSG_DEBUG,"realsocket() = %d\n", err); + DEBUG_BLANK("realsocket(): err=%d\n", err); return err; } return zts_socket(socket_family, socket_type, protocol); @@ -297,14 +296,14 @@ char *api_netpath; int connect(CONNECT_SIG) { - dwr(MSG_DEBUG, "connect(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); struct sockaddr_in *connaddr; connaddr = (struct sockaddr_in *)addr; if(addr->sa_family == AF_LOCAL || addr->sa_family == AF_UNIX) { struct sockaddr_storage storage; memcpy(&storage, addr, addrlen); struct sockaddr_un *s_un = (struct sockaddr_un*)&storage; - dwr(MSG_DEBUG, "connect(): address = %s\n", s_un->sun_path); + DEBUG_INFO("addr=%s\n", s_un->sun_path); } int port = connaddr->sin_port; @@ -314,7 +313,7 @@ char *api_netpath; d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_DEBUG,"connect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port)); + DEBUG_INFO("addr=%d.%d.%d.%d:%d\n", d[0],d[1],d[2],d[3], ntohs(port)); if(!check_intercept_enabled()) return realconnect(fd, addr, addrlen); @@ -360,7 +359,7 @@ char *api_netpath; int bind(BIND_SIG) { - dwr(MSG_DEBUG,"bind(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); // make sure we don't touch any standard outputs if(fd == 0 || fd == 1 || fd == 2) return(realbind(fd, addr, addrlen)); @@ -373,7 +372,7 @@ char *api_netpath; #endif || connaddr->sin_family == AF_UNIX) { int err = realbind(fd, addr, addrlen); - dwr(MSG_DEBUG,"realbind, err = %d\n", err); + DEBUG_BLANK("realbind(): err=%d", err); return err; } int port = connaddr->sin_port; @@ -383,7 +382,7 @@ char *api_netpath; d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - dwr(MSG_DEBUG,"bind(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port)); + DEBUG_INFO("addr=%d.%d.%d.%d:%d\n", d[0],d[1],d[2],d[3], ntohs(port)); int sock_type; socklen_t sock_type_len = sizeof(sock_type); @@ -418,7 +417,7 @@ char *api_netpath; #if defined(__linux__) int accept4(ACCEPT4_SIG) { - dwr(MSG_DEBUG,"accept4(%d):\n", fd); + DEBUG_INFO("fd=%d", fd); return zts_accept4(fd, addr, addrlen, flags); } #endif @@ -429,7 +428,7 @@ char *api_netpath; // int fd struct sockaddr *addr, socklen_t *addrlen int accept(ACCEPT_SIG) { - dwr(MSG_DEBUG,"accept(%d):\n", fd); + DEBUG_INFO("fd=%d", fd); if (!check_intercept_enabled()) return realaccept(fd, addr, addrlen); @@ -437,7 +436,7 @@ char *api_netpath; if(fcntl(fd, F_GETFD) < 0) { return -1; errno = EBADF; - dwr(MSG_DEBUG,"EBADF\n"); + DEBUG_INFO("EBADF\n"); return -1; } // Check that it is a socket @@ -445,13 +444,13 @@ char *api_netpath; socklen_t opt_len; if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) { errno = ENOTSOCK; - dwr(MSG_DEBUG,"ENOTSOCK\n"); + DEBUG_INFO("ENOTSOCK\n"); return -1; } // Check that this socket supports accept() if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) { errno = EOPNOTSUPP; - dwr(MSG_DEBUG,"EOPNOTSUPP\n"); + DEBUG_INFO("EOPNOTSUPP\n"); return -1; } // Check that we haven't hit the soft-limit file descriptors allowed @@ -459,18 +458,18 @@ char *api_netpath; getrlimit(RLIMIT_NOFILE, &rl); if(fd >= rl.rlim_cur){ errno = EMFILE; - dwr(MSG_DEBUG,"EMFILE\n"); + DEBUG_INFO("EMFILE\n"); return -1; } // Check address length if(addrlen < 0) { errno = EINVAL; - dwr(MSG_DEBUG,"EINVAL\n"); + DEBUG_INFO("EINVAL\n"); return -1; } // redirect calls for standard I/O descriptors to kernel if(fd == 0 || fd == 1 || fd == 2){ - dwr(MSG_DEBUG,"realaccept():\n"); + DEBUG_BLANK("realaccept(): "); return(realaccept(fd, addr, addrlen)); } @@ -484,7 +483,7 @@ char *api_netpath; int listen(LISTEN_SIG) { - dwr(MSG_DEBUG,"listen(%d):\n", fd); + DEBUG_INFO("listen(%d):\n", fd); if (!check_intercept_enabled()) return reallisten(fd, backlog); @@ -521,7 +520,7 @@ char *api_netpath; // int fd int close(CLOSE_SIG) { - dwr(MSG_DEBUG, " close(%d)\n", fd); + DEBUG_INFO(" close(%d)\n", fd); if(!check_intercept_enabled()) { return realclose(fd); } @@ -535,14 +534,14 @@ char *api_netpath; int getsockname(GETSOCKNAME_SIG) { - dwr(MSG_DEBUG,"getsockname(%d):\n", fd); + DEBUG_INFO("getsockname(%d):\n", fd); #if !defined(__IOS__) if (!check_intercept_enabled()) return realgetsockname(fd, addr, addrlen); #endif - dwr(MSG_DEBUG,"getsockname(%d)\n", fd); + DEBUG_INFO("getsockname(%d)\n", fd); if(!connected_to_service(fd)) { - dwr(MSG_DEBUG,"getsockname(): not used by service\n"); + DEBUG_INFO("getsockname(): not used by service\n"); return realgetsockname(fd, addr, addrlen); } return zts_getsockname(fd, addr, addrlen); @@ -570,7 +569,7 @@ char *api_netpath; if (!check_intercept_enabled()) return realsyscall(number,a,b,c,d,e,f); - dwr(MSG_DEBUG,"syscall(%u, ...)\n", number); + DEBUG_INFO("syscall(%u, ...)\n", number); #if defined(__i386__) // TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args); @@ -597,5 +596,5 @@ char *api_netpath; #endif #endif -#endif // zts_SDK_INTERCEPT +#endif // _SDK_INTERCEPT_ diff --git a/src/SDK_JavaWrapper.java b/src/SDK_JavaWrapper.java index 7cd32c4..f8d44ea 100644 --- a/src/SDK_JavaWrapper.java +++ b/src/SDK_JavaWrapper.java @@ -102,9 +102,9 @@ public class ZTSDK { return zt_get_proxy_port(nwid); } - public native boolean zt_running(); - public boolean running() { - return zt_running(); + public native boolean zt_service_is_running(); + public boolean service_is_running() { + return zt_service_is_running(); } @@ -272,4 +272,11 @@ public class ZTSDK { //public static native int zt_getsockopt(int fd, int type, int protocol); //public static native int zt_setsockopt(int fd, int type, int protocol); //public static native int zt_getsockname(int fd, int type, int protocol); + + + // PROXY SERVER CONTROLS + public native boolean zt_proxy_is_running(); + public boolean proxy_is_running() { + return zt_proxy_is_running(); + } } \ No newline at end of file diff --git a/src/SDK_LocalBuild.h b/src/SDK_LocalBuild.h index 43c11ee..ad4168a 100644 --- a/src/SDK_LocalBuild.h +++ b/src/SDK_LocalBuild.h @@ -1 +1 @@ -#define ZTSDK_BUILD_VERSION 34 +#define ZTSDK_BUILD_VERSION 44 diff --git a/src/SDK_Proxy.cpp b/src/SDK_Proxy.cpp index ad609cd..c7f8237 100644 --- a/src/SDK_Proxy.cpp +++ b/src/SDK_Proxy.cpp @@ -80,12 +80,12 @@ namespace ZeroTier int NetconEthernetTap::stopProxyServer() { - dwr(MSG_DEBUG, "stopProxyServer()"); + DEBUG_INFO("stopProxyServer()"); if(proxyListenPhySocket) { _phy.close(proxyListenPhySocket); return 0; } - dwr(MSG_ERROR, "stopProxyServer(): Invalid proxyListenPhySocket"); + DEBUG_ERROR("stopProxyServer(): Invalid proxyListenPhySocket"); return -1; } @@ -96,25 +96,25 @@ namespace ZeroTier // - If no address, assume 127.0.0.1: // - If no port assignment file, 127.0.0.1:RANDOM_PORT - dwr(MSG_DEBUG, "startProxyServer()\n"); + DEBUG_INFO("startProxyServer()\n"); int portno = -1; if(addr) { - dwr(MSG_DEBUG, "startProxyServer(): Using provided address"); + DEBUG_INFO("startProxyServer(): Using provided address"); // This address pointer may come from a different memory space and might be de-allocated, so we keep a copy memcpy(&proxyServerAddress, addr, sizeof(struct sockaddr_storage)); struct sockaddr_in *in4 = (struct sockaddr_in *)&addr; proxyListenPhySocket = _phy.tcpListen((const struct sockaddr*)&in4,(void *)this); sockstate = SOCKS_OPEN; - dwr(MSG_DEBUG, "SOCKS5 proxy server address for <%.16llx> is: <%s> (sock=%p)\n", nwid, inet_ntoa(in4->sin_addr), /*ntohs(in4->sin_port), */(void*)&proxyListenPhySocket); + DEBUG_INFO("SOCKS5 proxy server address for <%.16llx> is: <%s> (sock=%p)\n", nwid, inet_ntoa(in4->sin_addr), /*ntohs(in4->sin_port), */(void*)&proxyListenPhySocket); return 0; } else { - dwr(MSG_DEBUG, "startProxyServer(): No address provided. Checking port file."); + DEBUG_INFO("startProxyServer(): No address provided. Checking port file."); // Look for a port file for this network's proxy server instance char portFile[4096]; Utils::snprintf(portFile,sizeof(portFile),"%s/networks.d/%.16llx.port",homepath,nwid); std::string portStr; - printf("Proxy(): Reading port from: %s\n", portFile); + DEBUG_INFO("Proxy(): Reading port from: %s\n", portFile); if(ZeroTier::OSUtils::fileExists(portFile,true)) { if(ZeroTier::OSUtils::readFile(portFile, portStr)) { @@ -125,12 +125,12 @@ namespace ZeroTier unsigned int randp = 0; Utils::getSecureRandom(&randp,sizeof(randp)); portno = 1000 + (randp % 1000); - dwr(MSG_DEBUG, "Proxy(): No port specified in networks.d/%.16llx.port, randomly picking port\n", nwid); + DEBUG_INFO("Proxy(): No port specified in networks.d/%.16llx.port, randomly picking port\n", nwid); std::stringstream ss; ss << portno; portStr = ss.str(); if(!ZeroTier::OSUtils::writeFile(portFile, portStr)) { - dwr(MSG_ERROR, "unable to write proxy port file: %s\n", portFile); + DEBUG_ERROR("unable to write proxy port file: %s\n", portFile); } } struct sockaddr_in in4; @@ -140,7 +140,7 @@ namespace ZeroTier in4.sin_port = Utils::hton((uint16_t)portno); proxyListenPhySocket = _phy.tcpListen((const struct sockaddr*)&in4,(void *)this); sockstate = SOCKS_OPEN; - //dwr(MSG_DEBUG, "SOCKS5 proxy server address for <%.16llx> is: <%s:%d> (sock=%p)\n", nwid, , portno, (void*)&proxyListenPhySocket); + //DEBUG_INFO("SOCKS5 proxy server address for <%.16llx> is: <%s:%d> (sock=%p)\n", nwid, , portno, (void*)&proxyListenPhySocket); } return 0; } @@ -168,14 +168,14 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) { - dwr(MSG_DEBUG, "phyOnTcpData(): sock=%p, len=%lu\n", (void*)&sock, len); + DEBUG_INFO("phyOnTcpData(): sock=%p, len=%lu\n", (void*)&sock, len); unsigned char *buf; buf = (unsigned char *)data; // Get connection for this PhySocket Connection *conn = getConnection(sock); if(!conn) { - dwr(MSG_DEBUG, "phyOnTcpData(): Unable to locate Connection for sock=%p\n", (void*)&sock); + DEBUG_INFO("phyOnTcpData(): Unable to locate Connection for sock=%p\n", (void*)&sock); return; } @@ -183,7 +183,7 @@ namespace ZeroTier if(conn->proxy_conn_state == SOCKS_COMPLETE) { if(len) { - dwr(MSG_DEBUG, "len=%lu\n", len); + DEBUG_INFO("len=%lu\n", len); memcpy((&conn->txbuf)+(conn->txsz), buf, len); conn->txsz += len; handleWrite(conn); @@ -192,7 +192,7 @@ namespace ZeroTier if(conn->proxy_conn_state==SOCKS_UDP) { - dwr(MSG_DEBUG, "SOCKS_UDP from client\n"); + DEBUG_INFO("SOCKS_UDP from client\n"); // +----+------+------+----------+----------+----------+ // |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA | // +----+------+------+----------+----------+----------+ @@ -222,7 +222,7 @@ namespace ZeroTier if(firstSupportedMethod == 2) { supportedMethod = firstSupportedMethod; } - dwr(MSG_DEBUG, " INFO \n", version, methodsLength, supportedMethod); + DEBUG_INFO(" INFO \n", version, methodsLength, supportedMethod); // Send METHOD selection msg // +----+--------+ @@ -255,11 +255,11 @@ namespace ZeroTier int cmd = buf[IDX_COMMAND]; int addr_type = buf[IDX_ATYP]; - dwr(MSG_DEBUG, "SOCKS REQUEST = \n", version, cmd, addr_type); + DEBUG_INFO("SOCKS REQUEST = \n", version, cmd, addr_type); // CONNECT request if(cmd == 1) { - dwr(MSG_DEBUG, "CONNECT request\n"); + DEBUG_INFO("CONNECT request\n"); // Ipv4 /* if(addr_type == 144) @@ -269,12 +269,12 @@ namespace ZeroTier memcpy(&raw_addr, &buf[4], 4); char newaddr[16]; inet_ntop(AF_INET, &raw_addr, (char*)newaddr, INET_ADDRSTRLEN); - dwr(MSG_DEBUG, "new addr = %s\n", newaddr); + DEBUG_INFO("new addr = %s\n", newaddr); int rawport, port; memcpy(&rawport, &buf[5], 2); port = Utils::ntoh(rawport); - dwr(MSG_DEBUG, "new port = %d\n", port); + DEBUG_INFO("new port = %d\n", port); // Assemble new address struct sockaddr_in addr; @@ -283,13 +283,13 @@ namespace ZeroTier addr.sin_port = Utils::hton(8080); int fd = socket(AF_INET, SOCK_STREAM, 0); - dwr(MSG_DEBUG, "fd = %d\n", fd); + DEBUG_INFO("fd = %d\n", fd); if(fd < 0) perror("socket"); int err = connect(fd, (struct sockaddr*)&addr, sizeof(addr)); - dwr(MSG_DEBUG, "connect_err = %d\n", err); + DEBUG_INFO("connect_err = %d\n", err); if(err < 0) perror("connect"); } @@ -302,7 +302,7 @@ namespace ZeroTier ExtractAddress(addr_type,buf,&addr); PhySocket * new_sock = handleSocketProxy(sock, SOCK_STREAM); if(!new_sock) - dwr(MSG_ERROR, "Error while creating proxied-socket\n"); + DEBUG_ERROR("Error while creating proxied-socket\n"); handleConnectProxy(sock, &addr); // Convert connection err code into SOCKS-err-code @@ -324,7 +324,7 @@ namespace ZeroTier // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ - dwr(MSG_DEBUG, "REPLY = %d\n", addr.sin_port); + DEBUG_INFO("REPLY = %d\n", addr.sin_port); char reply[len]; // TODO: determine proper length int addr_len = domain_len; memset(reply, 0, len); // Create reply buffer at least as big as incoming SOCKS request data @@ -346,7 +346,7 @@ namespace ZeroTier // BIND Request if(cmd == 2) { - dwr(MSG_DEBUG, "BIND request\n"); + DEBUG_INFO("BIND request\n"); //char raw_addr[15]; //int bind_port; } @@ -355,7 +355,7 @@ namespace ZeroTier if(cmd == 3) { // PORT supplied should be port assigned by server in previous msg - dwr(MSG_DEBUG, "UDP association request\n"); + DEBUG_INFO("UDP association request\n"); // SOCKS_CONNECT (Cont.) // +----+-----+-------+------+----------+----------+ @@ -381,10 +381,10 @@ namespace ZeroTier // Create new lwIP PCB PhySocket * new_sock = handleSocketProxy(sock, SOCK_DGRAM); - dwr(MSG_DEBUG, "new_sock = %p\n", (void*)&sock); - dwr(MSG_DEBUG, "new_sock = %p\n", (void*)&new_sock); + DEBUG_INFO("new_sock = %p\n", (void*)&sock); + DEBUG_INFO("new_sock = %p\n", (void*)&new_sock); if(!new_sock) - dwr(MSG_ERROR, "Error while creating proxied-socket\n"); + DEBUG_ERROR("Error while creating proxied-socket\n"); // Form address struct sockaddr_in addr; @@ -407,7 +407,7 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) { - dwr(MSG_DEBUG, "phyOnTcpAccept(): sock=%p\n", (void*)&sockN); + DEBUG_INFO("phyOnTcpAccept(): sock=%p\n", (void*)&sockN); Connection *newConn = new Connection(); newConn->sock = sockN; _phy.setNotifyWritable(sockN, false); @@ -416,17 +416,17 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) { - dwr(MSG_DEBUG, "phyOnTcpConnect(): sock=%p\n", (void*)&sock); + DEBUG_INFO("phyOnTcpConnect(): sock=%p\n", (void*)&sock); } // Unused -- no UDP or TCP from this thread/Phy<> void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address, const struct sockaddr *from,void *data,unsigned long len) { - dwr(MSG_DEBUG, "phyOnDatagram(): len = %lu\n", len); + DEBUG_INFO("phyOnDatagram(): len = %lu\n", len); if(len) { Connection *conn = getConnection(sock); if(!conn){ - dwr(MSG_ERROR, "unable to locate Connection: sock=%p\n", (void*)sock); + DEBUG_ERROR("unable to locate Connection: sock=%p\n", (void*)sock); return; } unsigned char *buf = (unsigned char*)data; @@ -438,20 +438,20 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) { - dwr(MSG_DEBUG, "phyOnTcpClose(): sock=%p\n", (void*)&sock); + DEBUG_INFO("phyOnTcpClose(): sock=%p\n", (void*)&sock); Mutex::Lock _l(_tcpconns_m); closeConnection(sock); } void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr, bool lwip_invoked) { - dwr(MSG_DEBUG, " phyOnTcpWritable(): sock=%p\n", (void*)&sock); + DEBUG_INFO(" phyOnTcpWritable(): sock=%p\n", (void*)&sock); processReceivedData(sock,uptr,lwip_invoked); } // RX data on stream socks and send back over client sock's underlying fd void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) { - dwr(MSG_DEBUG, "phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock); + DEBUG_INFO("phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock); } } \ No newline at end of file diff --git a/src/SDK_RPC.c b/src/SDK_RPC.c index 98a6cad..37cfda6 100644 --- a/src/SDK_RPC.c +++ b/src/SDK_RPC.c @@ -30,12 +30,7 @@ #endif #if defined(__linux__) -#include -#endif - -// For defining the Android direct-call API -#if defined(__ANDROID__) - #include + #include #endif #include @@ -55,8 +50,8 @@ // externs common between SDK_Intercept and SDK_Socket from SDK.h -int (*realsocket)(SOCKET_SIG); -int (*realconnect)(CONNECT_SIG); +//int (*realsocket)(SOCKET_SIG); +//int (*realconnect)(CONNECT_SIG); void dwr(int level, const char *fmt, ... ); diff --git a/src/SDK_Service.cpp b/src/SDK_Service.cpp index 54e512c..d93b227 100644 --- a/src/SDK_Service.cpp +++ b/src/SDK_Service.cpp @@ -54,7 +54,7 @@ std::string service_path; pthread_t intercept_thread; int * intercept_thread_id; -pthread_key_t thr_id_key; +extern pthread_key_t thr_id_key; static ZeroTier::OneService *volatile zt1Service; std::string localHomeDir; // Local shortened path @@ -75,30 +75,29 @@ void zts_init_rpc(const char * path, const char * nwid); void dwr(int level, const char *fmt, ... ); int zts_start_proxy_server(const char *homepath, const char * nwid, struct sockaddr_storage * addr) { - LOGV("zts_start_proxy_server\n"); - dwr(MSG_DEBUG, "zts_start_proxy_server()\n"); + DEBUG_INFO(); uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int]; if(tap) { if(tap->startProxyServer(homepath, nwid_int, addr) < 0) { - dwr(MSG_ERROR, "zts_start_proxy_server(%s): Problem while starting server.", nwid); + DEBUG_ERROR("zts_start_proxy_server(%s): Problem while starting server.", nwid); return -1; } } - dwr(MSG_ERROR, "zts_start_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid); + DEBUG_ERROR("zts_start_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid); return 0; } int zts_stop_proxy_server(const char *nwid) { - dwr(MSG_DEBUG, "zts_stop_proxy_server()"); + DEBUG_INFO("zts_stop_proxy_server()"); uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int]; if(tap) { if(tap->stopProxyServer() < 0) { - dwr(MSG_ERROR, "zts_stop_proxy_server(%s): Problem while stopping server.", nwid); + DEBUG_ERROR("zts_stop_proxy_server(%s): Problem while stopping server.", nwid); return -1; } } - dwr(MSG_ERROR, "zts_stop_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid); + DEBUG_ERROR("zts_stop_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid); return 0; } int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage * addr) { @@ -114,13 +113,13 @@ int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage * ad // Basic ZT service controls // Will also spin up a SOCKS5 proxy server if USE_SOCKS_PROXY is set void zts_join_network(const char * nwid) { - LOGV("zts_join_network\n"); + DEBUG_INFO(); std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf"; if(!ZeroTier::OSUtils::mkdir(netDir)) { - dwr(MSG_ERROR, "unable to create %s\n", netDir.c_str()); + DEBUG_INFO("unable to create %s\n", netDir.c_str()); } if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) { - dwr(MSG_ERROR, "unable to write network conf file: %s\n", confFile.c_str()); + DEBUG_INFO("unable to write network conf file: %s\n", confFile.c_str()); } zt1Service->join(nwid); // Provide the API with the RPC information @@ -132,7 +131,7 @@ void zts_join_network(const char * nwid) { #endif } void zts_leave_network(const char * nwid) { zt1Service->leave(nwid); } -bool zts_is_running() { return zt1Service->isRunning(); } +bool zts_service_is_running() { return zt1Service->isRunning(); } void zts_stop_service() { zt1Service->terminate(); } // FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations @@ -143,7 +142,7 @@ void zts_get_addresses(const char * nwid, char *addrstr) ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int]; if(tap && tap->_ips.size()){ std::string addr = tap->_ips[0].toString(); - dwr(MSG_DEBUG, "addr.length() = %d, addr = %s\n", addr.length(), addr.c_str()); + DEBUG_INFO("addr.length() = %d, addr = %s\n", addr.length(), addr.c_str()); memcpy(addrstr, addr.c_str(), addr.length()); } else { @@ -222,26 +221,30 @@ char *zts_get_homepath() { /* NOTE: Since on Android devices the sdcard is formatted as fat32, we can't use just any location to set up the RPC unix domain socket. Rather we must use the application's specific data directory given by getApplicationContext().getFilesDir() */ - JNIEXPORT int JNICALL Java_ZeroTier_SDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path) { + JNIEXPORT int JNICALL Java_ZeroTier_ZTSDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path) { if(path) homeDir = env->GetStringUTFChars(path, NULL); zts_start_service(NULL); } // Shuts down ZeroTier service and SOCKS5 Proxy server - JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1stop_1service(JNIEnv *env, jobject thisObj) { + JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1stop_1service(JNIEnv *env, jobject thisObj) { if(zt1Service) zts_stop_service(); // TODO: Also terminate SOCKS5 Proxy // zts_stop_proxy_server(); } // Returns whether the ZeroTier service is running - JNIEXPORT jboolean JNICALL Java_ZeroTier_SDK_zt_1running(JNIEnv *env, jobject thisObj) { + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1service_1is_1running(JNIEnv *env, jobject thisObj) { if(zt1Service) - return zts_is_running(); + return zts_service_is_running(); return false; } + // Returns path for ZT config/data files + JNIEXPORT jstring JNICALL Java_ZeroTier_ZTSDK_zt_1get_1homepath(JNIEnv *env, jobject thisObj) { + return (*env).NewStringUTF(zts_get_homepath()); + } // Join a network - JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1join_1network(JNIEnv *env, jobject thisObj, jstring nwid) { + JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1join_1network(JNIEnv *env, jobject thisObj, jstring nwid) { const char *nwidstr; if(nwid) { nwidstr = env->GetStringUTFChars(nwid, NULL); @@ -249,7 +252,7 @@ char *zts_get_homepath() { } } // Leave a network - JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1leave_1network(JNIEnv *env, jobject thisObj, jstring nwid) { + JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1leave_1network(JNIEnv *env, jobject thisObj, jstring nwid) { const char *nwidstr; if(nwid) { nwidstr = env->GetStringUTFChars(nwid, NULL); @@ -258,7 +261,7 @@ char *zts_get_homepath() { } // FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations // Now only returns first assigned address per network. Shouldn't normally be a problem - JNIEXPORT jobject JNICALL Java_ZeroTier_SDK_zt_1get_1addresses(JNIEnv *env, jobject thisObj, jstring nwid) { + JNIEXPORT jobject JNICALL Java_ZeroTier_ZTSDK_zt_1get_1addresses(JNIEnv *env, jobject thisObj, jstring nwid) { const char *nwid_str = env->GetStringUTFChars(nwid, NULL); char address_string[32]; memset(address_string, 0, 32); @@ -287,15 +290,15 @@ char *zts_get_homepath() { */ } // Returns the device is in integer form - JNIEXPORT jint Java_ZeroTier_SDK_zt_1get_1device_1id() { + JNIEXPORT jint Java_ZeroTier_ZTSDK_zt_1get_1device_1id() { return zts_get_device_id(); } // Returns whether the path to an endpoint is currently relayed by a root server - JNIEXPORT jboolean JNICALL Java_ZeroTier_SDK_zt_1is_1relayed() { + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1is_1relayed() { return zts_is_relayed(); } // Returns the local address of the SOCKS5 Proxy server - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1get_1proxy_1server_1address(JNIEnv *env, jobject thisObj, jstring nwid, jobject ztaddr) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1get_1proxy_1server_1address(JNIEnv *env, jobject thisObj, jstring nwid, jobject ztaddr) { struct sockaddr_in addr; int err = zts_get_proxy_server_address(env->GetStringUTFChars(nwid, NULL), (struct sockaddr_storage*)&addr); // SET ZTAddress fields @@ -308,7 +311,7 @@ char *zts_get_homepath() { return err; } // Starts a SOCKS5 proxy server for a given ZeroTier network - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1start_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid, jobject ztaddr) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1start_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid, jobject ztaddr) { const char *nwidstr = env->GetStringUTFChars(nwid, NULL); struct sockaddr_in addr; // GET ZTAddress fields @@ -320,11 +323,12 @@ char *zts_get_homepath() { return zts_start_proxy_server((char *)zts_get_homepath, nwidstr, (struct sockaddr_storage *)&addr); } // - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1stop_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1stop_1proxy_1server(JNIEnv *env, jobject thisObj, jstring nwid) { return zts_stop_proxy_server((char*)env->GetStringUTFChars(nwid, NULL)); } - JNIEXPORT jstring JNICALL Java_ZeroTier_SDK_zt_1get_1homepath(JNIEnv *env, jobject thisObj) { - return (*env).NewStringUTF(zts_get_homepath()); + // + JNIEXPORT jboolean JNICALL Java_ZeroTier_ZTSDK_zt_1proxy_1is_1running(JNIEnv *env, jobject thisObj, jstring nwid) { + // TODO: implement } #endif @@ -362,8 +366,7 @@ char *zts_get_homepath() { // Starts a ZeroTier service in the background void *zts_start_service(void *thread_id) { #if defined(__ANDROID__) - dwr(MSG_DEBUG, "ZTSDK_BUILD_VERSION = %d\n", ZTSDK_BUILD_VERSION); - LOGV("ZTSDK_BUILD_VERSION = %d\n", ZTSDK_BUILD_VERSION); + DEBUG_INFO("ZTSDK_BUILD_VERSION = %d\n", ZTSDK_BUILD_VERSION); #endif #if defined(SDK_BUNDLED) && !defined(__ANDROID__) @@ -392,7 +395,7 @@ void *zts_start_service(void *thread_id) { localHomeDir = homeDir; // Used for RPC and *can* differ from homeDir on some platforms #endif - dwr(MSG_DEBUG, "homeDir = %s\n", homeDir.c_str()); + DEBUG_INFO("homeDir = %s\n", homeDir.c_str()); // Where network .conf files will be stored netDir = homeDir + "/networks.d"; zt1Service = (ZeroTier::OneService *)0; @@ -409,7 +412,7 @@ void *zts_start_service(void *thread_id) { ptmp.append(*pi); if ((*pi != ".")&&(*pi != "..")) { if (!ZeroTier::OSUtils::mkdir(ptmp)) { - dwr(MSG_ERROR, "startOneService(): home path does not exist, and could not create\n"); + DEBUG_ERROR("startOneService(): home path does not exist, and could not create\n"); } } } diff --git a/src/SDK_Sockets.c b/src/SDK_Sockets.c index cda1e59..3fd32ed 100644 --- a/src/SDK_Sockets.c +++ b/src/SDK_Sockets.c @@ -75,11 +75,10 @@ #include "Constants.hpp" // For Tap's MTU // Prototypes -void dwr(int level, const char *fmt, ... ); char *api_netpath = (char *)0; void load_symbols(); void load_symbols_rpc(); -int (*realclose)(CLOSE_SIG); +//int (*realclose)(CLOSE_SIG); // ------------------------------------------------------------------------------ // ---------------------------------- zt_init_rpc ------------------------------- @@ -88,7 +87,7 @@ int (*realclose)(CLOSE_SIG); // Assembles (and/or) sets the RPC path for communication with the ZeroTier service void zts_init_rpc(const char *path, const char *nwid) { - // dwr(MSG_DEBUG_EXTRA, "zt_init_rpc\n"); + DEBUG_EXTRA("zt_init_rpc\n"); #if !defined(__IOS__) // Since we don't use function interposition in iOS if(!realconnect) { @@ -113,10 +112,10 @@ int (*realclose)(CLOSE_SIG); // This is used when you're dynamically-linking our library into your application at runtime if (!api_netpath) { api_netpath = getenv("ZT_NC_NETWORK"); - dwr(MSG_DEBUG, "$ZT_NC_NETWORK(len=%d) = %s\n", strlen(api_netpath), api_netpath); + DEBUG_INFO("$ZT_NC_NETWORK(len=%d) = %s\n", strlen(api_netpath), api_netpath); } #endif - dwr(MSG_DEBUG_EXTRA, "zt_init_rpc(): api_netpath = %s\n", api_netpath); + DEBUG_EXTRA("zt_init_rpc(): api_netpath = %s\n", api_netpath); } } @@ -128,7 +127,7 @@ int (*realclose)(CLOSE_SIG); // int fd, const void *buf, size_t len #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags) + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags) { jbyte *body = (*env)->GetByteArrayElements(env, buf, 0); char * bufp = (char *)malloc(sizeof(char)*len); @@ -148,7 +147,7 @@ int (*realclose)(CLOSE_SIG); #if defined(__ANDROID__) // TODO: Check result of each JNI call // UDP TX - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1sendto( + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1sendto( JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr) { struct sockaddr_in addr; @@ -177,7 +176,7 @@ int (*realclose)(CLOSE_SIG); ssize_t zts_sendto(SENDTO_SIG) // Used as internal implementation #endif { - dwr(MSG_DEBUG_EXTRA, "zt_sendto(%d, ...)\n", fd); + DEBUG_EXTRA("zt_sendto(%d, ...)\n", fd); if(len > ZT_UDP_DEFAULT_PAYLOAD_MTU) { errno = EMSGSIZE; // Msg is too large return -1; @@ -198,7 +197,7 @@ int (*realclose)(CLOSE_SIG); // This connect call is used to get the address info to the stack for sending the packet int err; if((err = zts_connect(fd, addr, addrlen)) < 0) { - LOGV("sendto(): unknown problem passing address info to stack\n"); + DEBUG_ERROR("unknown problem passing address info to stack"); errno = EISCONN; // double-check this is correct return -1; } @@ -218,7 +217,7 @@ int (*realclose)(CLOSE_SIG); ssize_t zts_sendmsg(SENDMSG_SIG) #endif { - dwr(MSG_DEBUG_EXTRA, "zt_sendmsg(%d)\n", fd); + DEBUG_EXTRA("fd=%d",fd); char * p, * buf; size_t tot_len = 0; size_t err; @@ -253,7 +252,7 @@ int (*realclose)(CLOSE_SIG); #if defined(__ANDROID__) // UDP RX - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1recvfrom( + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1recvfrom( JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint len, jint flags, jobject ztaddr) { struct sockaddr_in addr; @@ -283,7 +282,7 @@ int (*realclose)(CLOSE_SIG); #endif { int tmpsz = 0; // payload size - // dwr(MSG_DEBUG_EXTRA,"zt_recvfrom(%d, ...)\n", fd); + // DEBUG_EXTRA("zt_recvfrom(%d, ...)\n", fd); if(read(fd, buf, ZT_MAX_MTU) > 0) { // TODO: case for address size mismatch? memcpy(addr, buf, addrlen); @@ -308,7 +307,7 @@ int (*realclose)(CLOSE_SIG); ssize_t zts_recvmsg(RECVMSG_SIG) #endif { - dwr(MSG_DEBUG_EXTRA, "zt_recvmsg(%d)\n", fd); + DEBUG_EXTRA("zt_recvmsg(%d)\n", fd); ssize_t err, n, tot_len = 0; char *buf, *p; struct iovec *iov = msg->msg_iov; @@ -366,7 +365,7 @@ int (*realclose)(CLOSE_SIG); #if defined(__ANDROID__) // TCP TX - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len) + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len) { jbyte *body = (*env)->GetByteArrayElements(env, buf, 0); char * bufp = (char *)malloc(sizeof(char)*len); @@ -376,7 +375,7 @@ int (*realclose)(CLOSE_SIG); return written_bytes; } // TCP RX - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len) + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len) { jbyte *body = (*env)->GetByteArrayElements(env, buf, 0); int read_bytes = read(fd, body, len); @@ -391,7 +390,7 @@ int (*realclose)(CLOSE_SIG); // int fd, int level, int optname, const void *optval, socklen_t optlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1setsockopt( + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1setsockopt( JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen) { return zts_setsockopt(fd, level, optname, optval, optlen); } @@ -403,7 +402,7 @@ int (*realclose)(CLOSE_SIG); int zts_setsockopt(SETSOCKOPT_SIG) #endif { - dwr(MSG_DEBUG, "zt_setsockopt(%d)\n", fd); + DEBUG_INFO("zt_setsockopt(%d)\n", fd); return 0; } @@ -413,7 +412,7 @@ int (*realclose)(CLOSE_SIG); // int fd, int level, int optname, void *optval, socklen_t *optlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockopt( + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getsockopt( JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen) { return zts_getsockopt(fd, level, optname, optval, optlen); } @@ -425,7 +424,7 @@ int (*realclose)(CLOSE_SIG); int zts_getsockopt(GETSOCKOPT_SIG) #endif { - dwr(MSG_DEBUG,"zt_getsockopt(%d)\n", fd); + DEBUG_INFO("zt_getsockopt(%d)\n", fd); if(optname == SO_TYPE) { int* val = (int*)optval; *val = 2; @@ -440,7 +439,7 @@ int (*realclose)(CLOSE_SIG); // int socket_family, int socket_type, int protocol #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1socket(JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1socket(JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol) { return zts_socket(family, type, protocol); } #endif @@ -452,7 +451,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG, "zt_socket()\n"); + DEBUG_INFO(""); // Check that type makes sense #if defined(__linux__) int flags = socket_type & ~SOCK_TYPE_MASK; @@ -488,9 +487,9 @@ int (*realclose)(CLOSE_SIG); #endif #endif // -1 is passed since we we're generating the new socket in this call - printf("api_netpath = %s\n", api_netpath); + DEBUG_INFO("api_netpath=%s\n", api_netpath); int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st)); - dwr(MSG_DEBUG," socket() = %d\n", err); + DEBUG_INFO("err=%d\n", err); return err; } @@ -500,10 +499,10 @@ int (*realclose)(CLOSE_SIG); // int fd, const struct sockaddr *addr, socklen_t addrlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { struct sockaddr_in addr; const char *str = (*env)->GetStringUTFChars(env, addrstr, 0); - dwr(MSG_DEBUG, "zt_connect(): fd = %d\naddr = %s\nport=%d", fd, str, port); + DEBUG_INFO("zt_connect(): fd = %d\naddr = %s\nport=%d", fd, str, port); addr.sin_addr.s_addr = inet_addr(str); addr.sin_family = AF_INET; addr.sin_port = htons( port ); @@ -519,7 +518,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG,"zt_connect(%d)\n", fd); + DEBUG_INFO("zt_connect(%d)\n", fd); struct connect_st rpc_st; #if defined(__linux__) #if !defined(__ANDROID__) @@ -540,10 +539,10 @@ int (*realclose)(CLOSE_SIG); // int fd, const struct sockaddr *addr, socklen_t addrlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { struct sockaddr_in addr; const char *str = (*env)->GetStringUTFChars(env, addrstr, 0); - dwr(MSG_DEBUG, "zt_bind(): fd = %d\naddr = %s\nport=%d", fd, str, port); + DEBUG_INFO("zt_bind(): fd = %d\naddr = %s\nport=%d", fd, str, port); addr.sin_addr.s_addr = inet_addr(str); addr.sin_family = AF_INET; addr.sin_port = htons( port ); @@ -559,7 +558,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG,"zt_bind(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); struct bind_st rpc_st; rpc_st.fd = fd; #if defined(__linux__) @@ -581,7 +580,7 @@ int (*realclose)(CLOSE_SIG); // int fd, struct sockaddr *addr, socklen_t *addrlen, int flags #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags) { struct sockaddr_in addr; char *str; // = env->GetStringUTFChars(addrstr, NULL); @@ -601,7 +600,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG,"zt_accept4(%d):\n", fd); + DEBUG_INFO("zt_accept4(%d):\n", fd); #if !defined(__ANDROID__) if ((flags & SOCK_CLOEXEC)) fcntl(fd, F_SETFL, FD_CLOEXEC); @@ -619,7 +618,7 @@ int (*realclose)(CLOSE_SIG); // int fd struct sockaddr *addr, socklen_t *addrlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { struct sockaddr_in addr; // TODO: Send addr info back to Javaland addr.sin_addr.s_addr = inet_addr(""); @@ -636,14 +635,14 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG,"zt_accept(%d):\n", fd); + DEBUG_INFO("fd=%d", fd); // FIXME: Find a better solution for this before production #if !defined(__UNITY_3D__) if(addr) addr->sa_family = AF_INET; #endif int new_fd = get_new_fd(fd); - dwr(MSG_DEBUG,"newfd = %d\n", new_fd); + DEBUG_INFO("newfd=%d", new_fd); if(new_fd > 0) { errno = ERR_OK; @@ -659,7 +658,7 @@ int (*realclose)(CLOSE_SIG); // int fd, int backlog #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog) { return zts_listen(fd, backlog); } #endif @@ -671,7 +670,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG,"zt_listen(%d):\n", fd); + DEBUG_INFO("fd=%d", fd); struct listen_st rpc_st; rpc_st.fd = fd; rpc_st.backlog = backlog; @@ -691,7 +690,7 @@ int (*realclose)(CLOSE_SIG); // int fd #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1close(JNIEnv *env, jobject thisObj, jint fd) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1close(JNIEnv *env, jobject thisObj, jint fd) { return zts_close(fd); } #endif @@ -703,7 +702,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG, "zt_close(%d)\n", fd); + DEBUG_INFO("fd=%d", fd); return realclose(fd); } @@ -713,7 +712,7 @@ int (*realclose)(CLOSE_SIG); // int fd, struct sockaddr *addr, socklen_t *addrlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { struct sockaddr_in addr; int err = zts_getsockname(fd, &addr, sizeof(struct sockaddr)); jfieldID fid; @@ -733,7 +732,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG_EXTRA,"zt_getsockname(%d):\n", fd); + DEBUG_EXTRA("fd=%d", fd); struct getsockname_st rpc_st; rpc_st.fd = fd; memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t)); @@ -751,13 +750,13 @@ int (*realclose)(CLOSE_SIG); } if(!sum) { // RXed a zero-ed address buffer, currently the only way to signal a problem errno = ENOTSOCK; // TODO: general error, needs to be more specific - dwr(MSG_ERROR, "zt_getpeername(): no address info given by service.\n"); + DEBUG_ERROR("no address info given by service."); return -1; } } else { errno = ENOTSOCK; // TODO: general error, needs to be more specific - dwr(MSG_ERROR, "zt_getpeername(): unable to read address info from service.\n", err); + DEBUG_ERROR("unable to read address info from service. err=%d", err); return -1; } } @@ -775,7 +774,7 @@ int (*realclose)(CLOSE_SIG); // int fd, struct sockaddr *addr, socklen_t *addrlen #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { struct sockaddr_in addr; int err = zts_getpeername(fd, &addr, sizeof(struct sockaddr)); jfieldID fid; @@ -795,7 +794,7 @@ int (*realclose)(CLOSE_SIG); #endif { get_api_netpath(); - dwr(MSG_DEBUG_EXTRA,"zt_getpeername(%d):\n", fd); + DEBUG_EXTRA("fd=%d", fd); struct getsockname_st rpc_st; rpc_st.fd = fd; memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t)); @@ -814,13 +813,13 @@ int (*realclose)(CLOSE_SIG); } if(!sum) { // RXed a zero-ed address buffer, currently the only way to signal a problem errno = ENOTSOCK; // TODO: general error, needs to be more specific - dwr(MSG_ERROR, "zt_getpeername(): no address info given by service.\n"); + DEBUG_ERROR("no address info given by service."); return -1; } } else { errno = ENOTSOCK; // TODO: general error, needs to be more specific - dwr(MSG_ERROR, "zt_getpeername(): unable to read address info from service.\n", err); + DEBUG_ERROR("unable to read address info from service. err=%d", err); return -1; } } @@ -838,7 +837,7 @@ int (*realclose)(CLOSE_SIG); // int fd, int cmd, int flags #if defined(__ANDROID__) - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags) { + JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags) { return zts_fcntl(fd,cmd,flags); } #endif @@ -849,7 +848,7 @@ int (*realclose)(CLOSE_SIG); int zts_fcntl(FCNTL_SIG) #endif { - dwr(MSG_DEBUG_EXTRA,"zt_fcntl(%d, %d, %d)\n", fd, cmd, flags); + DEBUG_EXTRA("fd=%d, cmd=%d, flags=%d", fd, cmd, flags); return fcntl(fd,cmd,flags); } diff --git a/src/SDK_XcodeWrapper.cpp b/src/SDK_XcodeWrapper.cpp index cd1e95a..ec50904 100755 --- a/src/SDK_XcodeWrapper.cpp +++ b/src/SDK_XcodeWrapper.cpp @@ -67,6 +67,10 @@ extern "C" void zt_stop_proxy_server(const char *nwid) { zts_stop_proxy_server(nwid); } // +extern "C" void zt_proxy_running(const char *homepath, const char *nwid, struct sockaddr_storage *addr) { + zts_start_proxy_server(homepath, nwid, addr); +} +// extern "C" void zt_get_proxy_server_address(const char *nwid, struct sockaddr_storage *addr) { zts_get_proxy_server_address(nwid, addr); } @@ -84,38 +88,38 @@ extern "C" int zt_socket(SOCKET_SIG) { return zts_socket(socket_family, socket_type, protocol); } extern "C" int zt_connect(CONNECT_SIG) { - return zts_connect(__fd, __addr, __len); + return zts_connect(fd, addr, addrlen); } extern "C" int zt_bind(BIND_SIG){ - return zts_bind(sockfd, addr, addrlen); + return zts_bind(fd, addr, addrlen); } extern "C" int zt_accept(ACCEPT_SIG) { - return zts_accept(sockfd, addr, addrlen); + return zts_accept(fd, addr, addrlen); } extern "C" int zt_listen(LISTEN_SIG) { - return zts_listen(sockfd, backlog); + return zts_listen(fd, backlog); } extern "C" int zt_setsockopt(SETSOCKOPT_SIG) { - return zts_setsockopt(socket, level, optname, optval, optlen); + return zts_setsockopt(fd, level, optname, optval, optlen); } extern "C" int zt_getsockopt(GETSOCKOPT_SIG) { - return zts_getsockopt(sockfd, level, optname, optval, optlen); + return zts_getsockopt(fd, level, optname, optval, optlen); } extern "C" int zt_close(CLOSE_SIG) { return zts_close(fd); } extern "C" int zt_getsockname(GETSOCKNAME_SIG) { - return zts_getsockname(sockfd, addr, addrlen); + return zts_getsockname(fd, addr, addrlen); } extern "C" int zt_getpeername(GETPEERNAME_SIG) { - return zts_getpeername(sockfd, addr, addrlen); + return zts_getpeername(fd, addr, addrlen); } extern "C" int zt_fcntl(FCNTL_SIG) { return zts_fcntl(fd, cmd, flags); } extern "C" ssize_t zt_recvfrom(RECVFROM_SIG) { - return zts_recvfrom(socket, buffer, length, flags, address, address_len); + return zts_recvfrom(fd, buf, len, flags, addr, addrlen); } extern "C" ssize_t zt_sendto(SENDTO_SIG) { - return zts_sendto(sockfd, buf, len, flags, addr, addr_len); + return zts_sendto(fd, buf, len, flags, addr, addrlen); } \ No newline at end of file