diff --git a/examples/cpp/dummy.cpp b/examples/cpp/dummy.cpp index 450da11..e85b7ed 100644 --- a/examples/cpp/dummy.cpp +++ b/examples/cpp/dummy.cpp @@ -5,7 +5,7 @@ int main() { - printf("Starting ZT service"); + printf("Starting ZT service\n"); zts_startjoin("my_config_path",0x0000000000000000); printf("Dummy. Going into infinite loop. Ping me or something\n"); diff --git a/examples/cpp/ipv4simple/ipv4client.cpp b/examples/cpp/ipv4simple/ipv4client.cpp index f9f84ab..9d0100b 100644 --- a/examples/cpp/ipv4simple/ipv4client.cpp +++ b/examples/cpp/ipv4simple/ipv4client.cpp @@ -66,29 +66,29 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) { - DEBUG_ERROR("error connecting to remote host (%d)", err); + printf("error connecting to remote host (%d)\n", err); } - DEBUG_TEST("sending to server..."); + printf("sending to server...\n"); w = zts_write(sockfd, msg, strlen(msg)); - DEBUG_TEST("reading from server..."); + printf("reading from server...\n"); r = zts_read(sockfd, rbuf, strlen(msg)); - DEBUG_TEST("Sent : %s", msg); - DEBUG_TEST("Received : %s", rbuf); + printf("Sent : %s\n", msg); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); diff --git a/examples/cpp/ipv4simple/ipv4client_udp.cpp b/examples/cpp/ipv4simple/ipv4client_udp.cpp index d172e8c..273c797 100644 --- a/examples/cpp/ipv4simple/ipv4client_udp.cpp +++ b/examples/cpp/ipv4simple/ipv4client_udp.cpp @@ -72,19 +72,19 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } int flags = 0; int len = strlen(msg); @@ -92,16 +92,16 @@ int main(int argc, char **argv) while(true) { sleep(1); if ((err = zts_sendto(sockfd, msg, len, flags, (const struct sockaddr *)&remote4, sizeof(remote4))) < 0) { - DEBUG_ERROR("error sending message to remote host (%d)", err); + printf("error sending message to remote host (%d)\n", err); } - DEBUG_TEST("sent=%d", err); + printf("sent=%d\n", err); } /* - DEBUG_TEST("reading from server..."); + printf("reading from server...\n"); r = zts_read(sockfd, rbuf, strlen(msg)); - DEBUG_TEST("Sent : %s", msg); - DEBUG_TEST("Received : %s", rbuf); + printf("Sent : %s\n", msg); + printf("Received : %s\n", rbuf); */ err = zts_close(sockfd); diff --git a/examples/cpp/ipv4simple/ipv4server.cpp b/examples/cpp/ipv4simple/ipv4server.cpp index e0249c5..9744b5b 100644 --- a/examples/cpp/ipv4simple/ipv4server.cpp +++ b/examples/cpp/ipv4simple/ipv4server.cpp @@ -62,41 +62,41 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } if ((err = zts_listen(sockfd, 100)) < 0) { - DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + printf("error placing socket in LISTENING state (%d)\n", err); } socklen_t client_addrlen = sizeof(sockaddr_in); if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) { - DEBUG_ERROR("error accepting connection (%d)", err); + printf("error accepting connection (%d)\n", err); } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen); - DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port)); + printf("accepted connection from %s : %d\n", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port)); - DEBUG_TEST("reading from client..."); + printf("reading from client...\n"); r = zts_read(accfd, rbuf, sizeof rbuf); - DEBUG_TEST("sending to client..."); + printf("sending to client...\n"); w = zts_write(accfd, rbuf, strlen(rbuf)); - DEBUG_TEST("Received : %s", rbuf); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); err = zts_close(accfd); diff --git a/examples/cpp/ipv4simple/ipv4server_udp.cpp b/examples/cpp/ipv4simple/ipv4server_udp.cpp index 755d969..1a7f5eb 100644 --- a/examples/cpp/ipv4simple/ipv4server_udp.cpp +++ b/examples/cpp/ipv4simple/ipv4server_udp.cpp @@ -62,18 +62,18 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } /* @@ -82,7 +82,7 @@ int main(int argc, char **argv) DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port)); */ - DEBUG_TEST("reading from client..."); + printf("reading from client...\n"); socklen_t addrlen = sizeof(acc_in4); memset(&acc_in4, 0, sizeof acc_in4); @@ -91,12 +91,12 @@ int main(int argc, char **argv) r = zts_recvfrom(accfd, rbuf, sizeof(rbuf), flags, (struct sockaddr *)&acc_in4, &addrlen); if (r >= 0) { char *ip = inet_ntoa(acc_in4.sin_addr); - DEBUG_TEST("Received : r=%d, %s -- from: %s : %d", r, rbuf, ip, ntohs(acc_in4.sin_port)); + printf("Received : r=%d, %s -- from: %s : %d\n", r, rbuf, ip, ntohs(acc_in4.sin_port)); } } /* - DEBUG_TEST("sending to client..."); + printf("sending to client...\n"); w = zts_write(accfd, rbuf, strlen(rbuf)); */ diff --git a/examples/cpp/ipv6adhoc/ipv6adhocclient.cpp b/examples/cpp/ipv6adhoc/ipv6adhocclient.cpp index d62da2d..ab1ee0e 100644 --- a/examples/cpp/ipv6adhoc/ipv6adhocclient.cpp +++ b/examples/cpp/ipv6adhoc/ipv6adhocclient.cpp @@ -84,29 +84,29 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = generate_adhoc_nwid_from_port(remote_port); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) { - DEBUG_ERROR("error connecting to remote host (%d)", err); + printf("error connecting to remote host (%d)\n", err); } - DEBUG_TEST("sending to server..."); + printf("sending to server...\n"); w = zts_write(sockfd, msg, strlen(msg)); - DEBUG_TEST("reading from server..."); + printf("reading from server...\n"); r = zts_read(sockfd, rbuf, strlen(msg)); - DEBUG_TEST("Sent : %s", msg); - DEBUG_TEST("Received : %s", rbuf); + printf("Sent : %s\n", msg); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); diff --git a/examples/cpp/ipv6adhoc/ipv6adhocserver.cpp b/examples/cpp/ipv6adhoc/ipv6adhocserver.cpp index 5892b07..512a568 100644 --- a/examples/cpp/ipv6adhoc/ipv6adhocserver.cpp +++ b/examples/cpp/ipv6adhoc/ipv6adhocserver.cpp @@ -84,41 +84,41 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); nwid = generate_adhoc_nwid_from_port(bind_port); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } if ((err = zts_listen(sockfd, 100)) < 0) { - DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + printf("error placing socket in LISTENING state (%d)\n", err); } socklen_t client_addrlen = sizeof(sockaddr_in6); if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) { - DEBUG_ERROR("error accepting connection (%d)", err); + printf("error accepting connection (%d)\n", err); } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen); - //DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port)); + //printf("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port)); - DEBUG_TEST("reading from client..."); + printf("reading from client...\n"); r = zts_read(accfd, rbuf, sizeof rbuf); - DEBUG_TEST("sending to client..."); + printf("sending to client...\n"); w = zts_write(accfd, rbuf, strlen(rbuf)); - DEBUG_TEST("Received : %s", rbuf); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); err = zts_close(accfd); diff --git a/examples/cpp/ipv6simple/ipv6client.cpp b/examples/cpp/ipv6simple/ipv6client.cpp index 3edbe1d..3bf3099 100644 --- a/examples/cpp/ipv6simple/ipv6client.cpp +++ b/examples/cpp/ipv6simple/ipv6client.cpp @@ -66,29 +66,29 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) { - DEBUG_ERROR("error connecting to remote host (%d)", err); + printf("error connecting to remote host (%d)\n", err); } - DEBUG_TEST("sending to server..."); + printf("sending to server...\n"); w = zts_write(sockfd, msg, strlen(msg)); - DEBUG_TEST("reading from server..."); + printf("reading from server...\n"); r = zts_read(sockfd, rbuf, strlen(msg)); - DEBUG_TEST("Sent : %s", msg); - DEBUG_TEST("Received : %s", rbuf); + printf("Sent : %s\n", msg); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); diff --git a/examples/cpp/ipv6simple/ipv6server.cpp b/examples/cpp/ipv6simple/ipv6server.cpp index c488185..7062d4f 100644 --- a/examples/cpp/ipv6simple/ipv6server.cpp +++ b/examples/cpp/ipv6simple/ipv6server.cpp @@ -63,41 +63,41 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); zts_startjoin(path.c_str(), nwid); uint64_t nodeId = zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } if ((err = zts_listen(sockfd, 100)) < 0) { - DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + printf("error placing socket in LISTENING state (%d)\n", err); } socklen_t client_addrlen = sizeof(sockaddr_in6); if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) { - DEBUG_ERROR("error accepting connection (%d)", err); + printf("error accepting connection (%d)\n", err); } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen); //DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port)); - DEBUG_TEST("reading from client..."); + printf("reading from client...\n"); r = zts_read(accfd, rbuf, sizeof rbuf); - DEBUG_TEST("sending to client..."); + printf("sending to client...\n"); w = zts_write(accfd, rbuf, strlen(rbuf)); - DEBUG_TEST("Received : %s", rbuf); + printf("Received : %s\n", rbuf); err = zts_close(sockfd); err = zts_close(accfd); diff --git a/examples/cpp/sharedlib/ipv4client_shared.cpp b/examples/cpp/sharedlib/ipv4client_shared.cpp index 103b837..b369387 100644 --- a/examples/cpp/sharedlib/ipv4client_shared.cpp +++ b/examples/cpp/sharedlib/ipv4client_shared.cpp @@ -24,25 +24,21 @@ * of your own application. */ +#if !defined(_MSC_VER) + #include #include #include #include - -#if defined(_WIN32) -#include -#include -#else #include #include #include #include #include -#endif +#include #include "libzt.h" -#include // function pointers which will have values assigned once the dynamic library is loaded @@ -179,31 +175,33 @@ int main(int argc, char **argv) // --- BEGIN EXAMPLE CODE - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); _zts_startjoin(path.c_str(), nwid); uint64_t nodeId = _zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } if ((err = _zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) { - DEBUG_ERROR("error connecting to remote host (%d)", err); + printf("error connecting to remote host (%d)\n", err); } - DEBUG_TEST("sending to server..."); + printf("sending to server...\n"); w = _zts_write(sockfd, msg, strlen(msg)); - DEBUG_TEST("reading from server..."); + printf("reading from server...\n"); r = _zts_read(sockfd, rbuf, strlen(msg)); - DEBUG_TEST("Sent : %s", msg); - DEBUG_TEST("Received : %s", rbuf); + printf("Sent : %s\n", msg); + printf("Received : %s\n", rbuf); err = _zts_close(sockfd); return err; -} \ No newline at end of file +} + +#endif // _MSC_VER \ No newline at end of file diff --git a/examples/cpp/sharedlib/ipv4server_shared.cpp b/examples/cpp/sharedlib/ipv4server_shared.cpp index fd4d9c6..244b6fe 100644 --- a/examples/cpp/sharedlib/ipv4server_shared.cpp +++ b/examples/cpp/sharedlib/ipv4server_shared.cpp @@ -24,25 +24,21 @@ * of your own application. */ +#if !defined(_MSC_VER) + #include #include #include #include - -#if defined(_WIN32) -#include -#include -#else #include #include #include #include #include -#endif -#include "libzt.h" - #include +#include "libzt.h" + // function pointers which will have values assigned once the dynamic library is loaded int (*_zts_set_service_port)(int portno); @@ -179,44 +175,43 @@ int main(int argc, char **argv) char *library_path = (char*)"bin/lib/libzt.dylib"; load_library_symbols(library_path); - DEBUG_TEST("Waiting for libzt to come online...\n"); + printf("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); printf("nwid=%llx\n", (unsigned long long)nwid); _zts_startjoin(path.c_str(), nwid); uint64_t nodeId = _zts_get_node_id(); - DEBUG_TEST("I am %llx", (unsigned long long)nodeId); + printf("I am %llx\n", (unsigned long long)nodeId); if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { - DEBUG_ERROR("error creating ZeroTier socket"); + printf("error creating ZeroTier socket\n"); } - if ((err = _zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) { - DEBUG_ERROR("error binding to interface (%d)", err); + printf("error binding to interface (%d)\n", err); } - if ((err = _zts_listen(sockfd, 100)) < 0) { - DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + printf("error placing socket in LISTENING state (%d)\n", err); } - socklen_t client_addrlen = sizeof(sockaddr_in); if ((accfd = _zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) { - DEBUG_ERROR("error accepting connection (%d)", err); + printf("error accepting connection (%d)\n", err); } socklen_t peer_addrlen = sizeof(struct sockaddr_storage); _zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen); - DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port)); + DEBUG_INFO("accepted connection from %s : %d\n", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port)); - DEBUG_TEST("reading from client..."); + printf("reading from client...\n"); r = _zts_read(accfd, rbuf, sizeof rbuf); - DEBUG_TEST("sending to client..."); + printf("sending to client...\n"); w = _zts_write(accfd, rbuf, strlen(rbuf)); - DEBUG_TEST("Received : %s", rbuf); + printf("Received : %s\n", rbuf); err = _zts_close(sockfd); err = _zts_close(accfd); return err; -} \ No newline at end of file +} + +#endif // _MSC_VER diff --git a/include/libztDebug.h b/include/libztDebug.h index 13e82e2..8d7949f 100644 --- a/include/libztDebug.h +++ b/include/libztDebug.h @@ -112,9 +112,9 @@ #if defined(__ANDROID__) #define DEBUG_TEST(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "TEST : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) - #elif defined(_WIN32) - #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__) + #elif defined(_WIN32) + #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) @@ -130,7 +130,7 @@ "ERROR: %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #elif defined(_WIN32) #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__) + 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) @@ -144,12 +144,12 @@ #if defined(__ANDROID__) #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) - #elif defined(_WIN32) - #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__) + #elif defined(_WIN32) + #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) + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_INFO(fmt, args...) @@ -160,12 +160,12 @@ #if defined(__ANDROID__) #define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "TRANS: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) - #elif defined(_WIN32) - #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__) + #elif defined(_WIN32) + #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) + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_TRANS(fmt, args...) @@ -178,7 +178,7 @@ "EXTRA: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) #elif defined(_WIN32) #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) + 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/libzt.cpp b/src/libzt.cpp index 40a13a4..3498772 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -36,8 +36,6 @@ #include "lwip/ip_addr.h" #include "lwip/netdb.h" -#include "libztDebug.h" - #include #ifdef __cplusplus @@ -50,139 +48,76 @@ bool zts_ready(); int zts_socket(int socket_family, int socket_type, int protocol) { - DEBUG_EXTRA("family=%d, type=%d, proto=%d", socket_family, socket_type, protocol); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } int socket_family_adj = platform_adjusted_socket_family(socket_family); - int err = lwip_socket(socket_family_adj, socket_type, protocol); - return err; + return !zts_ready() ? -1 : lwip_socket(socket_family_adj, socket_type, protocol); } int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { - DEBUG_EXTRA("fd=%d",fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } struct sockaddr_storage ss; memcpy(&ss, addr, addrlen); fix_addr_socket_family((struct sockaddr*)&ss); - return lwip_connect(fd, (struct sockaddr*)&ss, addrlen); + return !zts_ready() ? -1 : lwip_connect(fd, (struct sockaddr*)&ss, addrlen); } int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } struct sockaddr_storage ss; memcpy(&ss, addr, addrlen); fix_addr_socket_family((struct sockaddr*)&ss); - return lwip_bind(fd, (struct sockaddr*)&ss, addrlen); + return !zts_ready() ? -1 : lwip_bind(fd, (struct sockaddr*)&ss, addrlen); } int zts_listen(int fd, int backlog) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_listen(fd, backlog); + return !zts_ready() ? -1 : lwip_listen(fd, backlog); } int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_accept(fd, addr, addrlen); + return !zts_ready() ? -1 : lwip_accept(fd, addr, addrlen); } #if defined(__linux__) int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - // lwip_accept4(fd, addr, addrlen, flags); - return -1; + return !zts_ready() ? -1 : -1; // lwip_accept4(fd, addr, addrlen, flags); } #endif int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) { - DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_setsockopt(fd, level, optname, optval, optlen); + return !zts_ready() ? -1 : lwip_setsockopt(fd, level, optname, optval, optlen); } int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen) { - DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_getsockopt(fd, level, optname, optval, optlen); + return !zts_ready() ? -1 : lwip_getsockopt(fd, level, optname, optval, optlen); } int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_getsockname(fd, addr, addrlen); + return !zts_ready() ? -1 : lwip_getsockname(fd, addr, addrlen); } int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_getpeername(fd, addr, addrlen); + return !zts_ready() ? -1 : lwip_getpeername(fd, addr, addrlen); } int zts_gethostname(char *name, size_t len) { - DEBUG_EXTRA(""); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return -1; + return !zts_ready() ? -1 : -1; // TODO } int zts_sethostname(const char *name, size_t len) { - DEBUG_EXTRA(""); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return -1; + return !zts_ready() ? -1 : -1; // TODO } struct hostent *zts_gethostbyname(const char *name) { if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); return NULL; } // TODO: Test thread safety @@ -208,45 +143,17 @@ struct hostent *zts_gethostbyname(const char *name) int zts_close(int fd) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_close(fd); + return !zts_ready() ? -1 : lwip_close(fd); } -#if defined(__linux__) -/* -int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout) -{ - DEBUG_ERROR("warning, this is not implemented"); - if (zts_ready() == false) { - DEBUG_ERROR("service not started yet, call zts_startjoin()"); - return -1; - } - return poll(fds, nfds, timeout); -} -*/ -#endif - int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_select(nfds, readfds, writefds, exceptfds, timeout); + return !zts_ready() ? -1 : lwip_select(nfds, readfds, writefds, exceptfds, timeout); } int zts_fcntl(int fd, int cmd, int flags) { - DEBUG_EXTRA("fd=%d, cmd=%d, flags=%d", fd, cmd, flags); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } // translation from platform flag values to stack flag values int translated_flags = 0; #if defined(__linux__) @@ -259,142 +166,79 @@ int zts_fcntl(int fd, int cmd, int flags) translated_flags = 1; } #endif - return lwip_fcntl(fd, cmd, translated_flags); + return !zts_ready() ? -1 : lwip_fcntl(fd, cmd, translated_flags); } int zts_ioctl(int fd, unsigned long request, void *argp) { - DEBUG_EXTRA("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_ioctl(fd, request, argp); + return !zts_ready() ? -1 : lwip_ioctl(fd, request, argp); } -ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags, +ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } struct sockaddr_storage ss; memcpy(&ss, addr, addrlen); fix_addr_socket_family((struct sockaddr*)&ss); - return lwip_sendto(fd, buf, len, flags, (struct sockaddr*)&ss, addrlen); + return !zts_ready() ? -1 : lwip_sendto(fd, buf, len, flags, (struct sockaddr*)&ss, addrlen); } ssize_t zts_send(int fd, const void *buf, size_t len, int flags) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_send(fd, buf, len, flags); + return !zts_ready() ? -1 : lwip_send(fd, buf, len, flags); } ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags) { - DEBUG_TRANS("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_sendmsg(fd, msg, flags); + return !zts_ready() ? -1 : lwip_sendmsg(fd, msg, flags); } ssize_t zts_recv(int fd, void *buf, size_t len, int flags) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_recv(fd, buf, len, flags); + return !zts_ready() ? -1 : lwip_recv(fd, buf, len, flags); } -ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags, +ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_recvfrom(fd, buf, len, flags, addr, addrlen); + return !zts_ready() ? -1 : lwip_recvfrom(fd, buf, len, flags, addr, addrlen); } ssize_t zts_recvmsg(int fd, struct msghdr *msg, int flags) { - DEBUG_TRANS("fd=%d", fd); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return -1; // lwip_recvmsg(fd, msg, flags); - // Not currently implemented by stack + return !zts_ready() ? -1 : -1; // Not currently implemented by stack } int zts_read(int fd, void *buf, size_t len) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_read(fd, buf, len); + return !zts_ready() ? -1 : lwip_read(fd, buf, len); } int zts_write(int fd, const void *buf, size_t len) { - DEBUG_TRANS("fd=%d, len=%zu", fd, len); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_write(fd, buf, len); + return !zts_ready() ? -1 : lwip_write(fd, buf, len); } int zts_shutdown(int fd, int how) { - DEBUG_EXTRA("fd=%d, how=%d", fd, how); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - return lwip_shutdown(fd, how); + return !zts_ready() ? -1 : lwip_shutdown(fd, how); } int zts_add_dns_nameserver(struct sockaddr *addr) { - DEBUG_EXTRA(""); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - // TODO - return -1; + return !zts_ready() ? -1 : -1; // TODO } int zts_del_dns_nameserver(struct sockaddr *addr) { - DEBUG_EXTRA(""); - if (zts_ready() == false) { - DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR); - return -1; - } - // TODO - return -1; + return !zts_ready() ? -1 : -1; // TODO } /* The rationale for the following correctional methods is as follows: Since we don't want the user of this library to worry about naming conflicts with their native OS/platform's socket facilities we deliberately isolate what - is used by the userspace network stack and stack drivers from the user's + is used by the user-space network stack and stack drivers from the user's application. As a result of this, we must compensate for a few things on our side. For instance, differing values for AF_INET6 on major operating systems, and differing structure definitions for sockaddr.