diff --git a/CMakeLists.txt b/CMakeLists.txt index 1039a60..6d6df74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,8 @@ set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}) set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) if (WIN32) - set (CMAKE_C_FLAGS "-DZT_SDK=1") + add_definitions(-DSDK=1) + add_definitions(-DZT_SDK=1) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc -DNOMINMAX") set (LWIP_PORT_DIR ${PROJ_DIR}/ext/lwip-contrib/ports/win32/include) #set (COMMON_DEBUG_FLAGS "-DLIBZT_TRACE=1 -DLIBZT_DEBUG=1 -DNS_TRACE=1 -DNS_DEBUG=1") @@ -79,7 +80,6 @@ file (GLOB lwip_src_glob ${LWIP_SRC_DIR}/core/ipv6/*.c) add_library (lwip STATIC ${lwip_src_glob}) add_library (lwip_pic STATIC ${lwip_src_glob}) -#target_compile_options (lwip_pic PRIVATE ${NS_DEFS}) set_target_properties (lwip_pic PROPERTIES POSITION_INDEPENDENT_CODE ON) @@ -97,7 +97,6 @@ file (GLOB zto_src_glob ${ZTO_SRC_DIR}/controller/*.cpp ${ZTO_SRC_DIR}/osdep/ManagedRoute.cpp) add_library (zto STATIC ${zto_src_glob}) -#set (zto "--whole-archive") target_link_libraries (zto http) target_link_libraries (zto zt) add_library (zto_pic STATIC ${zto_src_glob}) @@ -119,38 +118,37 @@ include_directories ("${LIBZT_SRC_DIR}") include_directories ("${PROJ_DIR}/include") file (GLOB libzt_src_glob ${LIBZT_SRC_DIR}/*.cpp) add_library (zt ${libzt_src_glob}) -#set (zt "--whole-archive") target_link_libraries (zt lwip zto) -set_target_properties (zt PROPERTIES OUTPUT_NAME zt) +#set_target_properties (zt PROPERTIES OUTPUT_NAME zt) # (libzt) Shared Library variant of ZeroTier paired with userspace stack and virtual tap interface -add_library (ztshared SHARED ${libzt_src_glob}) -target_link_libraries (ztshared zto_pic lwip_pic) -set_target_properties (ztshared PROPERTIES OUTPUT_NAME zt) +#add_library (ztshared SHARED ${libzt_src_glob}) +#target_link_libraries (ztshared zto_pic lwip_pic) +#set_target_properties (ztshared PROPERTIES OUTPUT_NAME zt) # --- Test applications, examples, etc --- -file (GLOB APP_SOURCES ${PROJ_DIR}/examples/bindings/cpp/ipv4simple/* - ${PROJ_DIR}/examples/bindings/cpp/ipv6simple/* - ${PROJ_DIR}/examples/bindings/cpp/ipv6adhoc/* - ${PROJ_DIR}/examples/apps/ztproxy/*.cpp -) -foreach (testsourcefile ${APP_SOURCES}) - string (REPLACE ".cpp" "" testname ${testsourcefile}) - get_filename_component (testname ${testname} NAME) - add_executable (${testname} ${testsourcefile}) - target_link_libraries (${testname} lwip zto zt pthread) -endforeach (testsourcefile ${APP_SOURCES}) +#file (GLOB APP_SOURCES ${PROJ_DIR}/examples/bindings/cpp/ipv4simple/* +# ${PROJ_DIR}/examples/bindings/cpp/ipv6simple/* +# ${PROJ_DIR}/examples/bindings/cpp/ipv6adhoc/* +# ${PROJ_DIR}/examples/apps/ztproxy/*.cpp +#) +#foreach (testsourcefile ${APP_SOURCES}) +# string (REPLACE ".cpp" "" testname ${testsourcefile}) +# get_filename_component (testname ${testname} NAME) +# add_executable (${testname} ${testsourcefile}) +# target_link_libraries (${testname} lwip zto zt pthread) +#endforeach (testsourcefile ${APP_SOURCES}) # selftest add_executable (selftest ${PROJ_DIR}/test/selftest.cpp) -target_compile_options (selftest PRIVATE -D__SELFTEST__) -target_link_libraries (selftest lwip zto zt pthread) +target_compile_options (selftest PRIVATE -std=c++1 -D__SELFTEST__) +target_link_libraries (selftest lwip zto zt) # nativetest -add_executable (nativetest ${PROJ_DIR}/test/selftest.cpp) -target_compile_options (nativetest PRIVATE -D__NATIVETEST__) -target_link_libraries (nativetest lwip zto zt pthread) +#add_executable (nativetest ${PROJ_DIR}/test/selftest.cpp) +#target_compile_options (nativetest PRIVATE -D__NATIVETEST__) +#target_link_libraries (nativetest lwip zto zt pthread) # Clean up intermediate library targets #file (REMOVE ${LIBRARY_OUTPUT_PATH}/libhttp.a) @@ -163,7 +161,7 @@ target_link_libraries (nativetest lwip zto zt pthread) # --- INSTALL -install (TARGETS zt DESTINATION lib) +#install (TARGETS zt DESTINATION lib) # --- CONFIGURATION diff --git a/examples/bindings/cpp/ipv4simple/ipv4client.cpp b/examples/bindings/cpp/ipv4simple/ipv4client.cpp index c8c8203..d44fa76 100644 --- a/examples/bindings/cpp/ipv4simple/ipv4client.cpp +++ b/examples/bindings/cpp/ipv4simple/ipv4client.cpp @@ -72,13 +72,14 @@ int main(int argc, char **argv) zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - sleep(2); - if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) + if ((err = zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) { DEBUG_ERROR("error connecting to remote host (%d)", err); + } DEBUG_TEST("sending to server..."); w = zts_write(sockfd, msg, strlen(msg)); @@ -89,7 +90,6 @@ int main(int argc, char **argv) DEBUG_TEST("Sent : %s", msg); DEBUG_TEST("Received : %s", rbuf); - sleep(2); err = zts_close(sockfd); return err; diff --git a/examples/bindings/cpp/ipv4simple/ipv4server.cpp b/examples/bindings/cpp/ipv4simple/ipv4server.cpp index a2ec509..e161be5 100644 --- a/examples/bindings/cpp/ipv4simple/ipv4server.cpp +++ b/examples/bindings/cpp/ipv4simple/ipv4server.cpp @@ -25,22 +25,20 @@ */ #include -#include #include #include +#include -#if defined(__linux__) || defined(__APPLE__) +#if defined(_WIN32) +#include +#include +#else #include #include #include #include +#include #endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include -#endif - #include "libzt.h" int main(int argc, char **argv) @@ -70,20 +68,23 @@ int main(int argc, char **argv) zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - sleep(2); - if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) + if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); + } - if ((err = zts_listen(sockfd, 100)) < 0) + if ((err = zts_listen(sockfd, 100)) < 0) { DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + } socklen_t client_addrlen = sizeof(sockaddr_in); - if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) + if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) { DEBUG_ERROR("error accepting connection (%d)", err); + } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen); @@ -97,7 +98,6 @@ int main(int argc, char **argv) DEBUG_TEST("Received : %s", rbuf); - sleep(2); err = zts_close(sockfd); err = zts_close(accfd); diff --git a/examples/bindings/cpp/ipv6adhoc/ipv6adhocclient.cpp b/examples/bindings/cpp/ipv6adhoc/ipv6adhocclient.cpp index 9bfc3d7..4bb7d03 100644 --- a/examples/bindings/cpp/ipv6adhoc/ipv6adhocclient.cpp +++ b/examples/bindings/cpp/ipv6adhoc/ipv6adhocclient.cpp @@ -25,21 +25,19 @@ */ #include -#include #include #include #include -#if defined(__linux__) || defined(__APPLE__) +#if defined(_WIN32) +#include +#include +#else #include #include #include #include -#endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include +#include #endif #include "libzt.h" @@ -93,13 +91,13 @@ int main(int argc, char **argv) uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - sleep(5); - - if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) + if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) { DEBUG_ERROR("error connecting to remote host (%d)", err); + } DEBUG_TEST("sending to server..."); w = zts_write(sockfd, msg, strlen(msg)); diff --git a/examples/bindings/cpp/ipv6adhoc/ipv6adhocserver.cpp b/examples/bindings/cpp/ipv6adhoc/ipv6adhocserver.cpp index c631cce..9fc4b75 100644 --- a/examples/bindings/cpp/ipv6adhoc/ipv6adhocserver.cpp +++ b/examples/bindings/cpp/ipv6adhoc/ipv6adhocserver.cpp @@ -25,20 +25,19 @@ */ #include -#include #include #include +#include -#if defined(__linux__) || defined(__APPLE__) +#if defined(_WIN32) +#include +#include +#else #include #include #include #include -#endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include +#include #endif #include "libzt.h" @@ -92,18 +91,22 @@ int main(int argc, char **argv) uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) + if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); + } - if ((err = zts_listen(sockfd, 100)) < 0) + if ((err = zts_listen(sockfd, 100)) < 0) { DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + } socklen_t client_addrlen = sizeof(sockaddr_in6); - if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) + if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) { DEBUG_ERROR("error accepting connection (%d)", err); + } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen); diff --git a/examples/bindings/cpp/ipv6simple/ipv6client.cpp b/examples/bindings/cpp/ipv6simple/ipv6client.cpp index 74e7623..7d47aa0 100644 --- a/examples/bindings/cpp/ipv6simple/ipv6client.cpp +++ b/examples/bindings/cpp/ipv6simple/ipv6client.cpp @@ -25,21 +25,19 @@ */ #include -#include #include #include #include -#if defined(__linux__) || defined(__APPLE__) +#if defined(_WIN32) +#include +#include +#else #include #include #include #include -#endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include +#include #endif #include "libzt.h" @@ -75,13 +73,13 @@ int main(int argc, char **argv) uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - sleep(5); - - if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) + if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) { DEBUG_ERROR("error connecting to remote host (%d)", err); + } DEBUG_TEST("sending to server..."); w = zts_write(sockfd, msg, strlen(msg)); diff --git a/examples/bindings/cpp/ipv6simple/ipv6server.cpp b/examples/bindings/cpp/ipv6simple/ipv6server.cpp index 48a9392..7b26e2d 100644 --- a/examples/bindings/cpp/ipv6simple/ipv6server.cpp +++ b/examples/bindings/cpp/ipv6simple/ipv6server.cpp @@ -25,20 +25,19 @@ */ #include -#include #include #include +#include -#if defined(__linux__) || defined(__APPLE__) +#if defined(_WIN32) +#include +#include +#else #include #include #include #include -#endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include +#include #endif #include "libzt.h" @@ -71,18 +70,22 @@ int main(int argc, char **argv) uint64_t nodeId = zts_get_node_id(); DEBUG_TEST("I am %llx", nodeId); - if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); + } - if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) + if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); + } - if ((err = zts_listen(sockfd, 100)) < 0) + if ((err = zts_listen(sockfd, 100)) < 0) { DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + } socklen_t client_addrlen = sizeof(sockaddr_in6); - if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) + if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) { DEBUG_ERROR("error accepting connection (%d)", err); + } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen); diff --git a/include/libzt.h b/include/libzt.h index ac94603..a2aab38 100644 --- a/include/libzt.h +++ b/include/libzt.h @@ -480,6 +480,10 @@ ZT_SOCKET_API int ZTCALL zts_select( * @param flags * @return */ +#if defined(_WIN32) +#define F_SETFL 0 +#define O_NONBLOCK 0 +#endif ZT_SOCKET_API int ZTCALL zts_fcntl(int fd, int cmd, int flags); /** diff --git a/include/libztDebug.h b/include/libztDebug.h index 6a97c8c..fb93fa6 100644 --- a/include/libztDebug.h +++ b/include/libztDebug.h @@ -101,8 +101,8 @@ extern unsigned int gettid(); "STACK[%ld]: %17s:%5d:%20s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_STACK(fmt, args, ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_STACK(fmt, ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_STACK(fmt, args ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) @@ -117,8 +117,8 @@ extern unsigned int gettid(); "TEST : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_TEST(fmt, args, ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt "\n" \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_TEST(fmt, ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt "\n" \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_TEST(fmt, args ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt "\n" \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) @@ -134,8 +134,8 @@ extern unsigned int gettid(); "ERROR: %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_ERROR(fmt, args, ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt "\n" \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_ERROR(fmt, ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt "\n" \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_ERROR(fmt, args ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt "\n" \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) @@ -151,8 +151,8 @@ extern unsigned int gettid(); "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_INFO(fmt, args, ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_INFO(fmt, ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_INFO(fmt, args ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) @@ -168,8 +168,8 @@ extern unsigned int gettid(); "TRANS: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_TRANS(fmt, args, ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt "\n" \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_TRANS(fmt, ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt "\n" \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_TRANS(fmt, args ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt "\n" \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) @@ -185,8 +185,8 @@ extern unsigned int gettid(); "EXTRA: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #endif #if defined(_WIN32) - #define DEBUG_EXTRA(fmt, args, ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt "\n" \ - ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args, (long)0) + #define DEBUG_EXTRA(fmt, ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt "\n" \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__, (long)0) #else #define DEBUG_EXTRA(fmt, args ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt "\n" \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index 4ae02ce..fcc039f 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -150,7 +150,7 @@ bool VirtualTap::addIp(const InetAddress &ip) bool VirtualTap::removeIp(const InetAddress &ip) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); Mutex::Lock _l(_ips_m); std::vector::iterator i(std::find(_ips.begin(),_ips.end(),ip)); //if (i == _ips.end()) { @@ -257,12 +257,12 @@ void VirtualTap::threadMain() void VirtualTap::phyOnUnixClose(PhySocket *sock, void **uptr) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); } void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); #if defined(LIBZT_RAW) VirtualSocket *vs = (VirtualSocket*)*uptr; if (vs == NULL) { @@ -276,7 +276,7 @@ void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t void VirtualTap::phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invoked) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); #if defined(LIBZT_RAW) if (sock) { Read(sock,uptr,stack_invoked); @@ -289,7 +289,7 @@ void VirtualTap::phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invo bool VirtualTap::routeAdd(const InetAddress &ip, const InetAddress &nm, const InetAddress &gw) { bool err = false; - DEBUG_EXTRA(); + DEBUG_EXTRA(""); #if defined(STACK_LWIP) // general_lwip_init_interface(this, NULL, "n1", _mac, ip, nm, gw); // general_turn_on_interface(NULL); @@ -307,7 +307,7 @@ bool VirtualTap::routeAdd(const InetAddress &ip, const InetAddress &nm, const In bool VirtualTap::routeDelete(const InetAddress &ip, const InetAddress &nm) { bool err = false; - DEBUG_EXTRA(); + DEBUG_EXTRA(""); #if defined(STACK_LWIP) // general_lwip_init_interface(this, NULL, "n1", _mac, ip, nm, gw); // general_turn_on_interface(NULL); @@ -325,7 +325,7 @@ bool VirtualTap::routeDelete(const InetAddress &ip, const InetAddress &nm) void VirtualTap::addVirtualSocket(VirtualSocket *vs) { #if defined(LIBZT_RAW) - DEBUG_EXTRA(); + DEBUG_EXTRA(""); Mutex::Lock _l(_tcpconns_m); _VirtualSockets.push_back(vs); #endif @@ -334,7 +334,7 @@ void VirtualTap::addVirtualSocket(VirtualSocket *vs) void VirtualTap::removeVirtualSocket() { #if defined(LIBZT_RAW) - DEBUG_EXTRA(); + DEBUG_EXTRA(""); Mutex::Lock _l(_tcpconns_m); for (int i=0; i<_VirtualSockets.size(); i++) { if (vs == _VirtualSockets[i]) { diff --git a/src/ZT1Service.cpp b/src/ZT1Service.cpp index 9e1ab16..ff2cf78 100644 --- a/src/ZT1Service.cpp +++ b/src/ZT1Service.cpp @@ -170,7 +170,7 @@ VirtualTap *getAnyTap() uint64_t zts_get_node_id_from_file(const char *filepath) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); std::string fname("identity.public"); std::string fpath(filepath); std::string oldid; @@ -362,7 +362,7 @@ void zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, co int zts_join(const uint64_t nwid) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (nwid == 0) { return -1; } @@ -382,7 +382,7 @@ int zts_join(const uint64_t nwid) int zts_leave(const uint64_t nwid) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (nwid == 0) { return -1; } @@ -413,7 +413,7 @@ int zts_ready() int zts_start(const char *path, bool blocking = false) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zt1Service) { return 0; // already initialized, ok } @@ -448,7 +448,7 @@ int zts_start(const char *path, bool blocking = false) int zts_startjoin(const char *path, const uint64_t nwid) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); int err = zts_start(path, true); while (true) { try { @@ -468,7 +468,7 @@ int zts_startjoin(const char *path, const uint64_t nwid) void zts_stop() { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zt1Service) { zt1Service->terminate(); // disableTaps(); @@ -480,7 +480,7 @@ void zts_stop() void zts_get_path(char *homePath, size_t len) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (homeDir.length()) { memset(homePath, 0, len); size_t buf_len = len < homeDir.length() ? len : homeDir.length(); @@ -490,7 +490,7 @@ void zts_get_path(char *homePath, size_t len) uint64_t zts_get_node_id() { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zt1Service) { return zt1Service->getNode()->address(); } @@ -499,7 +499,7 @@ uint64_t zts_get_node_id() unsigned long zts_get_peer_count() { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zt1Service) { return zt1Service->getNode()->peers()->peerCount; } @@ -510,7 +510,7 @@ unsigned long zts_get_peer_count() int zts_get_peer_address(char *peer, const uint64_t nodeId) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zt1Service) { ZT_PeerList *pl = zt1Service->getNode()->peers(); // uint64_t addr; @@ -527,7 +527,7 @@ int zts_get_peer_address(char *peer, const uint64_t nodeId) void zts_allow_http_control(bool allowed) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); // TODO } diff --git a/src/libzt.cpp b/src/libzt.cpp index a8f25e1..ca82135 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -257,7 +257,7 @@ int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen) int zts_gethostname(char *name, size_t len) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zts_ready() == false) { DEBUG_ERROR("service not started yet, call zts_startjoin()"); return -1; @@ -275,7 +275,7 @@ int zts_gethostname(char *name, size_t len) int zts_sethostname(const char *name, size_t len) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zts_ready() == false) { DEBUG_ERROR("service not started yet, call zts_startjoin()"); return -1; @@ -373,7 +373,7 @@ int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout) int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { - //DEBUG_EXTRA(); + //DEBUG_EXTRA(""); /* if (zts_ready() == false) { DEBUG_ERROR("service not started yet, call zts_startjoin()"); @@ -611,7 +611,7 @@ int zts_shutdown(int fd, int how) int zts_add_dns_nameserver(struct sockaddr *addr) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zts_ready() == false) { DEBUG_ERROR("service not started yet, call zts_startjoin()"); return -1; @@ -634,7 +634,7 @@ int zts_add_dns_nameserver(struct sockaddr *addr) int zts_del_dns_nameserver(struct sockaddr *addr) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); if (zts_ready() == false) { DEBUG_ERROR("service not started yet, call zts_startjoin()"); return -1; diff --git a/src/lwIP.cpp b/src/lwIP.cpp index f26e22f..a5494f0 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -90,7 +90,7 @@ err_t tapif_init(struct netif *netif) /* static void tcp_timeout(void *data) { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); LWIP_UNUSED_ARG(data); #if TCP_DEBUG && LWIP_TCP // tcp_debug_print_pcbs(); @@ -129,7 +129,7 @@ static void main_thread(void *arg) // initialize the lwIP stack void lwip_driver_init() { - DEBUG_EXTRA(); + DEBUG_EXTRA(""); driver_m.lock(); // unlocked from callback indicating completion of driver init if (lwip_driver_initialized == true) { return; diff --git a/test/selftest.cpp b/test/selftest.cpp index 89a76f1..8d11720 100644 --- a/test/selftest.cpp +++ b/test/selftest.cpp @@ -24,21 +24,6 @@ * of your own application. */ -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#include -#include -#include -#include -#endif - -#if defined(__MINGW32__) || defined(__MINGW64__) -#include -#include -#endif - -#include #include #include #include @@ -51,10 +36,10 @@ #include #include #include -#include -#include #include #include +#include +#include #include "libzt.h" @@ -62,15 +47,94 @@ #include "Utils.hpp" #endif +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#if defined(_WIN32) +#include +#include +#include +#include +#include +#include + +void sleep(unsigned long ms) +{ + Sleep(ms); +} + +void micro_sleep(unsigned long us) +{ + std::this_thread::sleep_for(std::chrono::microseconds(us)); +} + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; + int tz_dsttime; +}; + + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tmpres /= 10; /*convert into microseconds*/ + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} +#endif + + #define EXIT_ON_FAIL false -#define PASSED 1 -#define FAILED 0 +#define TEST_PASSED 1 +#define TEST_FAILED 0 #define ECHO_INTERVAL 1000000 // microseconds #define SLAM_INTERVAL 500000 // microseconds -#define WAIT_FOR_TEST_TO_CONCLUDE 0 #define ARTIFICIAL_SOCKET_LINGER 1 #define STR_SIZE 32 @@ -90,21 +154,15 @@ #define MIN_PORT 5000 #define MAX_PORT 50000 -#define TCP_UNIT_TEST_SIG_4 struct sockaddr_in *addr, int op, int cnt, char *details, \ - bool *passed -#define UDP_UNIT_TEST_SIG_4 struct sockaddr_in *local_addr, struct sockaddr_in *remote_addr, \ - int op, int cnt, char *details, bool *passed +#define TCP_UNIT_TEST_SIG_4 struct sockaddr_in *addr, int op, int cnt, char *details, bool *passed +#define UDP_UNIT_TEST_SIG_4 struct sockaddr_in *local_addr, struct sockaddr_in *remote_addr, int op, int cnt, char *details, bool *passed -#define TCP_UNIT_TEST_SIG_6 struct sockaddr_in6 *addr, int op, int cnt, char *details, \ - bool *passed -#define UDP_UNIT_TEST_SIG_6 struct sockaddr_in6 *local_addr, struct sockaddr_in6 *remote_addr, \ - int op, int cnt, char *details, bool *passed +#define TCP_UNIT_TEST_SIG_6 struct sockaddr_in6 *addr, int op, int cnt, char *details, bool *passed +#define UDP_UNIT_TEST_SIG_6 struct sockaddr_in6 *local_addr, struct sockaddr_in6 *remote_addr, int op, int cnt, char *details, bool *passed #define ECHOTEST_MODE_RX 333 #define ECHOTEST_MODE_TX 666 -#define DATA_BUF_SZ 1024*32 - #define MAX_RX_BUF_SZ 2048 #define MAX_TX_BUF_SZ 2048 @@ -112,10 +170,9 @@ #define DETAILS_STR_LEN 128 - // If running a self test, use libzt calls #if defined(__SELFTEST__) -#define SOCKET zts_socket +//#define SOCKET zts_socket #define BIND zts_bind #define LISTEN zts_listen #define ACCEPT zts_accept @@ -157,8 +214,6 @@ inline unsigned int gettid() #define LISTEN listen #define ACCEPT accept #define CONNECT connect -#define READ read -#define WRITE write #define RECV recvmsg #define SEND send #define RECVFROM recvfrom @@ -170,7 +225,15 @@ inline unsigned int gettid() #define IOCTL ioctl #define FCNTL fcntl #define SELECT select +#if defined(_WIN32) +#define READ recv +#define WRITE send +#define CLOSE closesocket +#else +#define READ read +#define WRITE write #define CLOSE close +#endif #define GETPEERNAME getpeername #endif @@ -220,6 +283,21 @@ std::map testConf; /* Helper Functions */ /****************************************************************************/ +/** Returns true on success, or false if there was an error */ +bool SetSocketBlockingEnabled(int fd, bool blocking) +{ + if (fd < 0) return false; +#ifdef _WIN32 + unsigned long mode = blocking ? 0 : 1; + return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false; +#else + int flags = FCNTL(fd, F_GETFL, 0); + if (flags == -1) return false; + flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); + return (FCNTL(fd, F_SETFL, flags) == 0) ? true : false; +#endif +} + void displayResults(int *results, int size) { int success = 0, failure = 0; @@ -322,7 +400,7 @@ void RECORD_RESULTS(bool passed, char *details, std::vector *result { char *ok_str = (char*)"[ OK ]"; char *fail_str = (char*)"[ FAIL ]"; - if (passed == PASSED) { + if (passed == TEST_PASSED) { DEBUG_TEST("%s", ok_str); results->push_back(std::string(ok_str) + " " + std::string(details)); } else { @@ -345,7 +423,7 @@ void wait_until_everyone_is_ready(struct sockaddr *local_addr, struct sockaddr * int accepted_fd; // listen socket setup int listen_fd; - if ((listen_fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((listen_fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(0); } if ((err = BIND(listen_fd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in)) < 0)) { @@ -354,19 +432,15 @@ void wait_until_everyone_is_ready(struct sockaddr *local_addr, struct sockaddr * } if ((err = LISTEN(listen_fd, 0)) < 0) { perror("listen"); exit(0); - } if ((err = FCNTL(listen_fd, F_SETFL, O_NONBLOCK) < 0)) { - perror("fcntl"); - exit(0); - } + } + SetSocketBlockingEnabled(listen_fd, true); // connect socket setup int conn_fd; - if ((conn_fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((conn_fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(0); - } if ((err = FCNTL(conn_fd, F_SETFL, O_NONBLOCK) < 0)) { - perror("fcntl"); - exit(0); } + SetSocketBlockingEnabled(conn_fd, true); while(connected == false) { if ((err = CONNECT(conn_fd, (const struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { if (errno == EISCONN) { @@ -409,7 +483,7 @@ void tcp_select_server(TCP_UNIT_TEST_SIG_4) int w=0, r=0, fd, client_fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -498,7 +572,7 @@ void tcp_select_client(TCP_UNIT_TEST_SIG_4) int r, w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -580,7 +654,7 @@ void tcp_client_4(TCP_UNIT_TEST_SIG_4) int r, w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -627,7 +701,7 @@ void tcp_server_4(TCP_UNIT_TEST_SIG_4) int w=0, r=0, fd, client_fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -687,7 +761,7 @@ void tcp_client_6(TCP_UNIT_TEST_SIG_6) int r, w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -734,7 +808,7 @@ void tcp_server_6(TCP_UNIT_TEST_SIG_6) int w=0, r=0, fd, client_fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -799,18 +873,13 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4) int r, w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; return; } - if ((err = FCNTL(fd, F_SETFL, O_NONBLOCK) < 0)) { - fprintf(stderr, "error setting O_NONBLOCK (errno=%d)", errno); - perror("fcntl"); - *passed = false; - return; - } + SetSocketBlockingEnabled(fd, true); DEBUG_TEST("sending UDP packets until I get a single response..."); if ((err = BIND(fd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); @@ -855,7 +924,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4) int r, w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -932,19 +1001,13 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6) char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; return; } - if ((err = FCNTL(fd, F_SETFL, O_NONBLOCK) < 0)) { - std::cout << "error setting O_NONBLOCK (errno=" << strerror(errno) << ")" << std::endl; - perror("fcntl"); - *passed = false; - return; - } - + SetSocketBlockingEnabled(fd, true); DEBUG_TEST("[1] binding and sending UDP packets until I get a single response..."); if ((err = BIND(fd, (struct sockaddr*)local_addr, sizeof(struct sockaddr_in6)) < 0)) { DEBUG_ERROR("error binding to interface (err=%d, errno=%d)", err, errno); @@ -959,7 +1022,7 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6) if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { DEBUG_ERROR("error sending packet, err=%d", errno); } - usleep(100000); + micro_sleep(100000); memset(rbuf, 0, sizeof(rbuf)); int serverlen = sizeof(struct sockaddr_storage); // rx @@ -990,7 +1053,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6) char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating socket"); perror("socket"); *passed = false; @@ -1022,7 +1085,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6) // tx long int tx_ti = get_now_ts(); while (true) { - usleep(100000); + micro_sleep(100000); //DEBUG_TEST("sending UDP packet"); if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { DEBUG_ERROR("error sending packet, err=%d", errno); @@ -1065,7 +1128,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4) char *txbuf = (char*)malloc(cnt*sizeof(char)); generate_random_data(txbuf, cnt, 0, 9); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1083,7 +1146,9 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4) long int tx_ti = get_now_ts(); while (wrem) { int next_write = std::min(4096, wrem); +#if !defined(_WIN32) signal(SIGPIPE, SIG_IGN); +#endif DEBUG_TEST("writing..."); n = WRITE(fd, &txbuf[w], next_write); DEBUG_TEST("wrote=%d", n); @@ -1148,7 +1213,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6) char *rxbuf = (char*)malloc(cnt*sizeof(char)); char *txbuf = (char*)malloc(cnt*sizeof(char)); generate_random_data(txbuf, cnt, 0, 9); - if ((fd = SOCKET(AF_INET6, SOCK_STREAM, 0)) < 0){ + if ((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0){ DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1228,7 +1293,7 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4) char *rxbuf = (char*)malloc(cnt*sizeof(char)); memset(rxbuf, 0, cnt); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1312,7 +1377,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6) char *rxbuf = (char*)malloc(cnt*sizeof(char)); memset(rxbuf, 0, cnt); - if ((fd = SOCKET(AF_INET6, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1399,18 +1464,13 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4) int w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; return; } - if ((err = FCNTL(fd, F_SETFL, O_NONBLOCK) < 0)) { - fprintf(stderr, "error setting O_NONBLOCK (errno=%d)", errno); - perror("fcntl"); - *passed = false; - return; - } + SetSocketBlockingEnabled(fd, true); DEBUG_TEST("sending UDP packets until I get a single response..."); if ((err = BIND(fd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); @@ -1447,7 +1507,7 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4) int r, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1495,18 +1555,13 @@ void udp_client_sustained_6(UDP_UNIT_TEST_SIG_6) int w, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; return; } - if ((err = FCNTL(fd, F_SETFL, O_NONBLOCK) < 0)) { - fprintf(stderr, "error setting O_NONBLOCK (errno=%d)", errno); - perror("fcntl"); - *passed = false; - return; - } + SetSocketBlockingEnabled(fd, true); DEBUG_TEST("sending UDP packets until I get a single response..."); if ((err = BIND(fd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in6)) < 0)) { DEBUG_ERROR("error binding to interface (%d)", err); @@ -1544,7 +1599,7 @@ void udp_server_sustained_6(UDP_UNIT_TEST_SIG_6) int r, fd, err, len = strlen(msg.c_str()); char rbuf[STR_SIZE]; memset(rbuf, 0, sizeof rbuf); - if ((fd = SOCKET(AF_INET6, SOCK_DGRAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); perror("socket"); *passed = false; @@ -1597,7 +1652,7 @@ void tcp_client_perf_4(TCP_UNIT_TEST_SIG_4) for (int i=arbitrary_chunk_sz_min; (i*2) < arbitrary_chunk_sz_max; i*=2) { - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) DEBUG_ERROR("error creating ZeroTier socket"); if ((err = CONNECT(fd, (const struct sockaddr *)addr, sizeof(addr))) < 0) DEBUG_ERROR("error connecting to remote host (%d)", err); @@ -1618,7 +1673,7 @@ void tcp_client_perf_4(TCP_UNIT_TEST_SIG_4) sprintf(details, "tot=%d, dt=%.2f, rate=%.2f MB/s", w, ts_delta, (rate / float(ONE_MEGABYTE) )); CLOSE(fd); } - *passed = (w == total_test_sz && !err) ? PASSED : FAILED; + *passed = (w == total_test_sz && !err) ? TEST_PASSED : TEST_FAILED; */ } @@ -1636,7 +1691,7 @@ void tcp_server_perf_4(TCP_UNIT_TEST_SIG_4) for (int i=arbitrary_chunk_sz_min; (i*2) < arbitrary_chunk_sz_max; i*=2) { DEBUG_ERROR("TESTING chunk size = %d", i); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) DEBUG_ERROR("error creating ZeroTier socket"); if ((err = BIND(fd, (struct sockaddr *)addr, (socklen_t)sizeof(struct sockaddr_in)) < 0)) DEBUG_ERROR("error binding to interface (%d)", err); @@ -1665,7 +1720,7 @@ void tcp_server_perf_4(TCP_UNIT_TEST_SIG_4) CLOSE(fd); CLOSE(client_fd); } - *passed = (r == total_test_sz && !err) ? PASSED : FAILED; + *passed = (r == total_test_sz && !err) ? TEST_PASSED : TEST_FAILED; */ } @@ -1693,7 +1748,7 @@ void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4) mode = ECHOTEST_MODE_TX; // connect to remote echotest host - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); return; } @@ -1742,7 +1797,7 @@ void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4) sleep(ARTIFICIAL_SOCKET_LINGER); err = CLOSE(fd); - *passed = (tot == cnt && !err) ? PASSED : FAILED; + *passed = (tot == cnt && !err) ? TEST_PASSED : TEST_FAILED; } @@ -1763,7 +1818,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4) mode = ECHOTEST_MODE_RX; // connect to remote echotest host - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { DEBUG_ERROR("error creating ZeroTier socket"); return; } @@ -1785,7 +1840,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4) // begin DEBUG_TEST("beginning test, as soon as bytes are read we will start keeping time..."); - if ((r = read(fd, tbuf, sizeof tbuf)) < 0) { + if ((r = READ(fd, tbuf, sizeof tbuf)) < 0) { DEBUG_ERROR("there was an error reading the test stream. aborting (err=%d, errno=%s)", r, strerror(errno)); return; } @@ -1796,7 +1851,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4) DEBUG_TEST("Received first set of bytes in test stream. now keeping time"); while (tot < cnt) { - if ((r = read(fd, tbuf, sizeof tbuf)) < 0) { + if ((r = READ(fd, tbuf, sizeof tbuf)) < 0) { DEBUG_ERROR("there was an error reading the test stream. aborting (err=%d)", r); return; } @@ -1810,7 +1865,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4) sleep(ARTIFICIAL_SOCKET_LINGER); err = CLOSE(fd); - *passed = (tot == cnt && !err) ? PASSED : FAILED; + *passed = (tot == cnt && !err) ? TEST_PASSED : TEST_FAILED; } /****************************************************************************/ @@ -1828,7 +1883,7 @@ int obscure_api_test(bool *passed) int fd, client_fd; // after accept() - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) DEBUG_ERROR("error creating ZeroTier socket"); if ((err = BIND(fd, (struct sockaddr *)addr, sizeof(struct sockaddr_in)) < 0)) DEBUG_ERROR("error binding to interface (%d)", err); @@ -1849,7 +1904,7 @@ int obscure_api_test(bool *passed) // compate getpeername() result to address returned by accept() // after connect - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) DEBUG_ERROR("error creating ZeroTier socket"); if ((err = CONNECT(fd, (const struct sockaddr *)addr, sizeof(*addr))) < 0) DEBUG_ERROR("error connecting to remote host (%d)", err); @@ -1887,7 +1942,7 @@ int levels[] = { int optname = TCP_NODELAY; int optval = 1; socklen_t flag_len = sizeof(optval); - int fd = SOCKET(AF_INET, SOCK_STREAM, 0); + int fd = zts_socket(AF_INET, SOCK_STREAM, 0); DEBUG_TEST("setting level=%d, optname=%d, optval=%d...", level, optname, optval); err = SETSOCKOPT(fd, level, optname, (char *)&optval, sizeof(int)); if (err < 0) { @@ -2012,6 +2067,7 @@ int ZT_control_semantics_test(bool *passed) #define SLAM_NUMBER 16 #define SLAM_REPEAT 1 +/* int slam_api_test() { int err = 0; @@ -2031,11 +2087,11 @@ int slam_api_test() // open and close SLAM_NUMBER*SLAM_REPEAT sockets for (int j=0; j 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) > 0) { if ((err = BIND(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) { - usleep(100000); + micro_sleep(100000); if ((err = CLOSE(fd)) < 0) { DEBUG_ERROR("error closing socket (%d)", err); } @@ -2627,9 +2690,9 @@ void bind_to_localhost_test(int port) // ipv4, 127.0.0.1 DEBUG_TEST("binding to 127.0.0.1"); str2addr("127.0.0.1", port, 4, (struct sockaddr *)&bind_addr); - if ((fd = SOCKET(AF_INET, SOCK_STREAM, 0)) > 0) { + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) > 0) { if ((err = BIND(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) { - usleep(100000); + micro_sleep(100000); if ((err = CLOSE(fd)) < 0) { DEBUG_ERROR("error closing socket (%d)", err); } @@ -2648,9 +2711,9 @@ void bind_to_localhost_test(int port) // ipv6, [::] DEBUG_TEST("binding to [::]"); str2addr("::", port, 6, (struct sockaddr *)&bind_addr); - if ((fd = SOCKET(AF_INET6, SOCK_STREAM, 0)) > 0) { + if ((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) > 0) { if ((err = BIND(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) { - usleep(100000); + micro_sleep(100000); if ((err = CLOSE(fd)) < 0) { DEBUG_ERROR("error closing socket (%d)", err); } @@ -2857,6 +2920,7 @@ for (int i=0; i