From 972f542901be5b2e7007a56e6c0acd7ac0706895 Mon Sep 17 00:00:00 2001 From: BenDr0id Date: Thu, 1 Mar 2018 17:44:15 +0100 Subject: [PATCH] Fixed TLS build on Windows GCC/MVSC Cleanup TLS compile --- CMakeLists.txt | 19 +++++++--- src/3rdparty/clib-net/deps/buffer/buffer.h | 5 +++ src/3rdparty/clib-net/include/net.h | 43 +++++++--------------- src/3rdparty/clib-net/src/net.c | 8 ++-- src/Cpu_win.cpp | 2 +- src/Mem_win.cpp | 2 +- src/Options.cpp | 10 ++--- src/cc/Httpd.cpp | 2 +- src/cc/Summary.cpp | 5 +-- src/net/Client.cpp | 2 +- src/net/Client.h | 6 +++ src/net/Url.cpp | 1 + src/version.h | 12 +++++- 13 files changed, 66 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36e02a55..534162f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ if (WIN32) ) add_definitions(/DWIN32) - set(EXTRA_LIBS ws2_32 psapi iphlpapi userenv) + set(EXTRA_LIBS ws2_32 psapi iphlpapi userenv crypt32) elseif (APPLE) set(SOURCES_OS src/App_unix.cpp @@ -113,7 +113,7 @@ find_package(UV REQUIRED) include(cmake/flags.cmake) if (WITH_TLS) - find_package(OpenSSL REQUIRED) + find_package(OpenSSL) add_definitions(/DCPPHTTPLIB_OPENSSL_SUPPORT) @@ -218,21 +218,30 @@ endif (WITH_CC_SERVER OR WITH_CC_CLIENT) add_executable(xmrigMiner ${SOURCES} ${SOURCES_CRYPTO} ${HTTPD_SOURCES} ${SOURCES_CC_CLIENT} res/app.rc) -target_link_libraries(xmrigMiner xmrig_tls xmrig_common xmrig_os_dependencies xmrig_cpuid - ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB} ${OPENSSL_LIBRARIES}) +target_link_libraries(xmrigMiner xmrig_common xmrig_os_dependencies xmrig_cpuid + ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) if (WITH_CC_CLIENT) target_link_libraries(xmrigMiner xmrig_cc_common) endif (WITH_CC_CLIENT) +if (WITH_TLS) + target_link_libraries(xmrigMiner xmrig_tls OpenSSL::SSL OpenSSL::Crypto) +endif (WITH_TLS) + add_executable(xmrigDaemon src/cc/XMRigd.cpp res/app.rc) if (WITH_CC_SERVER AND MHD_FOUND) add_library(xmrig_common_cc STATIC ${SOURCES_COMMON}) add_executable(xmrigCCServer ${SOURCES_CC_SERVER} res/app.rc) target_link_libraries(xmrigCCServer - xmrig_common_cc xmrig_os_dependencies xmrig_cpuid xmrig_cc_common xmrig_tls + xmrig_common_cc xmrig_os_dependencies xmrig_cpuid xmrig_cc_common ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) + + if (WITH_TLS) + target_link_libraries(xmrigCCServer xmrig_tls OpenSSL::SSL OpenSSL::Crypto) + endif (WITH_TLS) + set_target_properties(xmrig_common_cc PROPERTIES COMPILE_FLAGS "-DXMRIG_CC_SERVER ${SHARED_FLAGS}") set_target_properties(xmrigCCServer PROPERTIES COMPILE_FLAGS "-DXMRIG_CC_SERVER ${SHARED_FLAGS}") endif() diff --git a/src/3rdparty/clib-net/deps/buffer/buffer.h b/src/3rdparty/clib-net/deps/buffer/buffer.h index 5cecbb0d..f2784e13 100644 --- a/src/3rdparty/clib-net/deps/buffer/buffer.h +++ b/src/3rdparty/clib-net/deps/buffer/buffer.h @@ -10,6 +10,11 @@ #include +#if defined(_MSC_VER) +#include +typedef SSIZE_T ssize_t; +#endif + /* * Default buffer size. */ diff --git a/src/3rdparty/clib-net/include/net.h b/src/3rdparty/clib-net/include/net.h index d9f1ce6d..3ed2a9c3 100644 --- a/src/3rdparty/clib-net/include/net.h +++ b/src/3rdparty/clib-net/include/net.h @@ -26,37 +26,22 @@ typedef struct net_s net_t; typedef struct addrinfo net_ai; typedef struct sockaddr_in socketPair_t; -#define NET_FIELDS \ - NET_CONNECTION_FIELDS \ - NET_UV_FIELDS \ - NET_TLS_FIELDS \ - -#define NET_CONNECTION_FIELDS \ - char *hostname; \ - int port; \ - int connected; \ - - -#define NET_UV_FIELDS \ - uv_getaddrinfo_t *resolver; \ - uv_loop_t *loop; \ - uv_tcp_t *handle; \ - uv_connect_t *conn; \ - -#ifndef XMRIG_NO_TLS - #define NET_TLS_FIELDS \ - int use_ssl; \ - int tls_established; \ - tls_t *tls; -#else - #define NET_TLS_FIELDS \ - int use_ssl; \ - int tls_established; -#endif - struct net_s { - NET_FIELDS; + char *hostname; + int port; + int connected; + uv_getaddrinfo_t *resolver; + uv_loop_t *loop; + uv_tcp_t *handle; + uv_connect_t *conn; + int use_ssl; + int tls_established; + +#ifndef XMRIG_NO_TLS + tls_t *tls; +#endif + void *data; void (*conn_cb)(net_t*); void (*read_cb)(net_t*, size_t, char*); diff --git a/src/3rdparty/clib-net/src/net.c b/src/3rdparty/clib-net/src/net.c index e992c229..69a7f596 100644 --- a/src/3rdparty/clib-net/src/net.c +++ b/src/3rdparty/clib-net/src/net.c @@ -213,14 +213,15 @@ net_connect_cb(uv_connect_t *conn, int err) { do { read = tls_bio_read(net->tls, 0); if (read > 0) { - char buf[read]; + char* buf = (char *) calloc(read, 1); uv_write_t * req = malloc(sizeof(uv_write_t)); req->data = net; memset(buf, 0, read); memcpy(buf, net->tls->buf, read); uv_buf_t uvbuf = uv_buf_init(buf, read); uv_write(req, (uv_stream_t*)net->handle, &uvbuf, 1, net_write_cb); - } + free(buf); + } } while (read > 0); } #endif @@ -267,13 +268,14 @@ net_read(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { do { read = tls_bio_read(net->tls, 0); if (read > 0) { - char buf2[read]; + char* buf2 = (char *) calloc(read, 1); uv_write_t * req = malloc(sizeof(uv_write_t)); req->data = net; memset(buf2, 0, read); memcpy(buf2, net->tls->buf, read); uv_buf_t uvbuf = uv_buf_init(buf2, read); uv_write(req, (uv_stream_t*)net->handle, &uvbuf, 1, net_write_cb); + free(buf2); } } while (read > 0); diff --git a/src/Cpu_win.cpp b/src/Cpu_win.cpp index d967421c..1560dd64 100644 --- a/src/Cpu_win.cpp +++ b/src/Cpu_win.cpp @@ -51,7 +51,7 @@ void CpuImpl::setAffinity(int id, uint64_t mask) int threadCount = 0; - for (int i = 0; i < m_totalThreads; i++) { + for (size_t i = 0; i < m_totalThreads; i++) { if (threadAffinityMask.test(i)) { if (threadCount == id) { SetThreadAffinityMask(GetCurrentThread(), 1ULL << i); diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index dff6f3be..0f46b6b3 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -153,7 +153,7 @@ bool Mem::allocate(const Options* options) m_memorySize = 0; size_t scratchPadSize = m_algo == Options::ALGO_CRYPTONIGHT ? MEMORY : MEMORY_LITE; - for (int i=0; i < m_threads; i++) { + for (size_t i=0; i < m_threads; i++) { m_memorySize += sizeof(cryptonight_ctx); m_memorySize += scratchPadSize * getThreadHashFactor(i); } diff --git a/src/Options.cpp b/src/Options.cpp index 6767b6c1..9e75a676 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -91,10 +91,10 @@ Options:\n" # ifndef XMRIG_NO_CC "\ --cc-url=URL url of the CC Server\n\ - --cc-use-tls enable tls encryption for CC communication\ + --cc-use-tls enable tls encryption for CC communication\n\ --cc-access-token=T access token for CC Server\n\ --cc-worker-id=ID custom worker-id for CC Server\n\ - --cc-update-interval-s status update interval in seconds (default: 10 min: 1)\n" + --cc-update-interval-s=N status update interval in seconds (default: 10 min: 1)\n" # endif # endif @@ -104,9 +104,9 @@ Options:\n" --cc-pass=PASSWORD CC Server admin pass\n\ --cc-access-token=T CC Server access token for CC Client\n\ --cc-port=N CC Server port\n\ - --cc-use-tls enable tls encryption for CC communication \ - --cc-cert-file=FILE when tls is turned on, use this to point to the right cert file (default: server.pem) \ - --cc-key-file when tls is turned on, use this to point to the right key file (default: server.key) \ + --cc-use-tls enable tls encryption for CC communication\n\ + --cc-cert-file=FILE when tls is turned on, use this to point to the right cert file (default: server.pem) \n\ + --cc-key-file=FILE when tls is turned on, use this to point to the right key file (default: server.key) \n\ --cc-client-config-folder=FOLDER Folder contains the client config files\n\ --cc-custom-dashboard=FILE loads a custom dashboard and serve it to '/'\n" # endif diff --git a/src/cc/Httpd.cpp b/src/cc/Httpd.cpp index 80790c5e..7eb23656 100644 --- a/src/cc/Httpd.cpp +++ b/src/cc/Httpd.cpp @@ -51,7 +51,7 @@ bool Httpd::start() m_keyPem = readFile(m_options->ccKeyFile()); m_certPem = readFile(m_options->ccCertFile()); - if (m_keyPem.length() == 0 || m_certPem.length() == 0) { + if (m_keyPem.empty() || m_certPem.empty()) { LOG_ERR("HTTPS Daemon failed to start. Unable to load Key/Cert."); return false; } diff --git a/src/cc/Summary.cpp b/src/cc/Summary.cpp index 4c3cc784..a1544e03 100644 --- a/src/cc/Summary.cpp +++ b/src/cc/Summary.cpp @@ -45,9 +45,8 @@ static void print_versions() buf[0] = '\0'; # endif - - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36m%s/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: %s/%s libuv/%s%s", - APP_NAME, APP_VERSION, uv_version_string(), buf); + Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36m%s/%s\x1B[01;37m libuv/%s%s \x1B[01;36m(%s)" : " * VERSIONS: %s/%s libuv/%s%s (%s)", + APP_NAME, APP_VERSION, uv_version_string(), buf, BUILD_TYPE); } static void print_commands() diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 45daf2f1..86a1d2ea 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -282,7 +282,7 @@ void Client::onRead(net_t *net, size_t size, char *buf) { auto client = getClient(net->data); - if (size < 0) { + if (size == 0) { if (size != UV_EOF && !client->m_quiet) { LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) size)); } diff --git a/src/net/Client.h b/src/net/Client.h index 363f0d1b..b2b422f6 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -36,14 +36,20 @@ #include "net/Url.h" #include "rapidjson/fwd.h" +#ifdef __cplusplus extern "C" { +#endif + #include "net.h" #ifndef XMRIG_NO_TLS #include "tls.h" #endif + +#ifdef __cplusplus } +#endif class IClientListener; class JobResult; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index c020d4f7..10442236 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -36,6 +36,7 @@ Url::Url() : + m_useTls(false), m_keepAlive(false), m_nicehash(false), m_host(nullptr), diff --git a/src/version.h b/src/version.h index ad2635ad..d4a48ac6 100644 --- a/src/version.h +++ b/src/version.h @@ -47,9 +47,17 @@ #define APP_VER_REV 0 #ifndef NDEBUG -#define BUILD_TYPE "DEBUG" + #ifndef XMRIG_NO_TLS + #define BUILD_TYPE "DEBUG with TLS" + #else + #define BUILD_TYPE "DEBUG" + #endif #else -#define BUILD_TYPE "RELEASE" + #ifndef XMRIG_NO_TLS + #define BUILD_TYPE "RELEASE with TLS" + #else + #define BUILD_TYPE "RELEASE" + #endif #endif #ifdef _MSC_VER