From a76243a65ec4b38aa887de7ea4ebc0bd928dd417 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Nov 2018 00:50:28 +0700 Subject: [PATCH] Sync changes with proxy. --- CMakeLists.txt | 2 + src/base/tools/String.cpp | 225 +++++++++++++++++++++++++++++ src/base/tools/String.h | 103 +++++++++++++ src/common/config/CommonConfig.cpp | 51 +++++-- src/common/config/CommonConfig.h | 4 +- src/common/interfaces/IConfig.h | 27 ++-- src/common/log/Log.h | 8 +- src/common/net/Tls.h | 6 +- src/common/utils/c_str.h | 73 +--------- 9 files changed, 402 insertions(+), 97 deletions(-) create mode 100644 src/base/tools/String.cpp create mode 100644 src/base/tools/String.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ebfdbd3..4d910e6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ include (cmake/cpu.cmake) set(HEADERS src/api/NetworkState.h src/App.h + src/base/tools/String.h src/common/config/CommonConfig.h src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h @@ -93,6 +94,7 @@ endif() set(SOURCES src/api/NetworkState.cpp src/App.cpp + src/base/tools/String.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp new file mode 100644 index 00000000..fe2792c7 --- /dev/null +++ b/src/base/tools/String.cpp @@ -0,0 +1,225 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include "base/tools/String.h" +#include "rapidjson/document.h" + + +xmrig::String::String(const char *str) : + m_data(nullptr), + m_size(str == nullptr ? 0 : strlen(str)) +{ + if (m_size == 0) { + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, str, m_size + 1); +} + + +xmrig::String::String(const char *str, size_t size) : + m_data(nullptr), + m_size(size) +{ + if (str == nullptr) { + m_size = 0; + + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, str, m_size); + m_data[m_size] = '\0'; +} + + +xmrig::String::String(const String &other) : + m_data(nullptr), + m_size(other.m_size) +{ + if (other.m_data == nullptr) { + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, other.m_data, m_size + 1); +} + + +bool xmrig::String::isEqual(const char *str) const +{ + return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && str == nullptr); +} + + +bool xmrig::String::isEqual(const String &other) const +{ + if (m_size != other.m_size) { + return false; + } + + return (m_data != nullptr && other.m_data != nullptr && memcmp(m_data, other.m_data, m_size) == 0) || (m_data == nullptr && other.m_data == nullptr); +} + + +rapidjson::Value xmrig::String::toJSON() const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(StringRef(m_data)); +} + + +rapidjson::Value xmrig::String::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(m_data, doc.GetAllocator()); +} + + +std::vector xmrig::String::split(char sep) const +{ + std::vector out; + if (m_size == 0) { + return out; + } + + size_t start = 0; + size_t pos = 0; + + for (pos = 0; pos < m_size; ++pos) { + if (m_data[pos] == sep) { + if ((pos - start) > 0) { + out.push_back(std::move(String(m_data + start, pos - start))); + } + + start = pos + 1; + } + } + + if ((pos - start) > 0) { + out.push_back(std::move(String(m_data + start, pos - start))); + } + + return out; +} + + +xmrig::String xmrig::String::join(const std::vector &vec, char sep) +{ + if (vec.empty()) { + return String(); + } + + size_t size = vec.size(); + for (const String &str : vec) { + size += str.size(); + } + + size_t offset = 0; + char *buf = new char[size]; + + for (const String &str : vec) { + memcpy(buf + offset, str.data(), str.size()); + + offset += str.size() + 1; + + if (offset < size) { + buf[offset - 1] = sep; + } + } + + buf[size - 1] = '\0'; + + return String(buf); +} + + +void xmrig::String::copy(const char *str) +{ + delete [] m_data; + + if (str == nullptr) { + m_size = 0; + m_data = nullptr; + + return; + } + + m_size = strlen(str); + m_data = new char[m_size + 1]; + + memcpy(m_data, str, m_size + 1); +} + + +void xmrig::String::copy(const String &other) +{ + if (m_size > 0) { + if (m_size == other.m_size) { + memcpy(m_data, other.m_data, m_size + 1); + + return; + } + + delete [] m_data; + } + + delete [] m_data; + + if (other.m_data == nullptr) { + m_size = 0; + m_data = nullptr; + + return; + } + + m_size = other.m_size; + m_data = new char[m_size + 1]; + + memcpy(m_data, other.m_data, m_size + 1); +} + + +void xmrig::String::move(char *str) +{ + delete [] m_data; + + m_size = str == nullptr ? 0 : strlen(str); + m_data = str; +} + + +void xmrig::String::move(String &&other) +{ + delete [] m_data; + + m_data = other.m_data; + m_size = other.m_size; + + other.m_data = nullptr; + other.m_size = 0; +} diff --git a/src/base/tools/String.h b/src/base/tools/String.h new file mode 100644 index 00000000..b2da0940 --- /dev/null +++ b/src/base/tools/String.h @@ -0,0 +1,103 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_STRING_H +#define XMRIG_STRING_H + + +#include +#include + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +/** + * @brief Simple C string wrapper. + * + * 1. I know about std:string. + * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. + * 3. nullptr and JSON conversion supported. + */ +class String +{ +public: + inline String() : m_data(nullptr), m_size(0) {} + inline String(char *str) : m_data(str), m_size(str == nullptr ? 0 : strlen(str)) {} + inline String(String &&other) : m_data(other.m_data), m_size(other.m_size) { other.m_data = nullptr; other.m_size = 0; } + + String(const char *str); + String(const char *str, size_t size); + String(const String &other); + + inline ~String() { delete [] m_data; } + + + bool isEqual(const char *str) const; + bool isEqual(const String &other) const; + + + inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; } + + + inline bool isEmpty() const { return size() == 0; } + inline bool isNull() const { return m_data == nullptr; } + inline char *data() { return m_data; } + inline const char *data() const { return m_data; } + inline size_t size() const { return m_size; } + + + inline bool operator!=(const char *str) const { return !isEqual(str); } + inline bool operator!=(const String &other) const { return !isEqual(other); } + inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; } + inline bool operator==(const char *str) const { return isEqual(str); } + inline bool operator==(const String &other) const { return isEqual(other); } + inline String &operator=(char *str) { move(str); return *this; } + inline String &operator=(const char *str) { copy(str); return *this; } + inline String &operator=(const String &str) { copy(str); return *this; } + inline String &operator=(String &&other) { move(std::move(other)); return *this; } + + rapidjson::Value toJSON() const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + std::vector split(char sep) const; + + static String join(const std::vector &vec, char sep); + +private: + void copy(const char *str); + void copy(const String &other); + void move(char *str); + void move(String &&other); + + char *m_data; + size_t m_size; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_STRING_H */ diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 01fdfa21..94399d7d 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -280,16 +280,16 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) break; case KeepAliveKey: /* --keepalive */ - m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); + currentPool().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); break; case TlsKey: /* --tls */ - m_pools.back().setTLS(enable); + currentPool().setTLS(enable); break; # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ - m_pools.back().setNicehash(enable); + currentPool().setNicehash(enable); break; # endif @@ -333,13 +333,15 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserpassKey: /* --userpass */ - if (!m_pools.back().setUserpass(arg)) { + if (!currentPool().setUserpass(arg)) { return false; } break; case UrlKey: /* --url */ + fixup(); + if (m_pools.size() > 1 || m_pools[0].isValid()) { Pool pool(arg); @@ -358,23 +360,23 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserKey: /* --user */ - m_pools.back().setUser(arg); + currentPool().setUser(arg); break; case PasswordKey: /* --pass */ - m_pools.back().setPassword(arg); + currentPool().setPassword(arg); break; case RigIdKey: /* --rig-id */ - m_pools.back().setRigId(arg); + currentPool().setRigId(arg); break; case FingerprintKey: /* --tls-fingerprint */ - m_pools.back().setFingerprint(arg); + currentPool().setFingerprint(arg); break; case VariantKey: /* --variant */ - m_pools.back().algorithm().parseVariant(arg); + currentPool().algorithm().parseVariant(arg); break; case LogFileKey: /* --log-file */ @@ -462,11 +464,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case KeepAliveKey: /* --keepalive */ - m_pools.back().setKeepAlive(arg); + currentPool().setKeepAlive(arg); break; case VariantKey: /* --variant */ - m_pools.back().algorithm().parseVariant(arg); + currentPool().algorithm().parseVariant(arg); break; case DonateLevelKey: /* --donate-level */ @@ -493,3 +495,30 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) return true; } + + +Pool &xmrig::CommonConfig::currentPool() +{ + fixup(); + + return m_pools.back(); +} + + +void xmrig::CommonConfig::fixup() +{ + if (m_state == NoneState) { + return; + } + + if (m_pools.empty()) { + if (!m_activePools.empty()) { + std::swap(m_pools, m_activePools); + } + else { + m_pools.push_back(Pool()); + } + + m_state = NoneState; + } +} diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 422a6bb2..a864033b 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -112,9 +112,11 @@ protected: private: bool parseInt(int key, int arg); + Pool ¤tPool(); + void fixup(); }; } /* namespace xmrig */ -#endif /* __COMMONCONFIG_H__ */ +#endif /* XMRIG_COMMONCONFIG_H */ diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 69f2ffab..0c8cfc28 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -97,16 +97,23 @@ public: OclCompModeKey = 1410, // xmrig-proxy - AccessLogFileKey = 'A', - BindKey = 'b', - CoinKey = 1104, - CustomDiffKey = 1102, - DebugKey = 1101, - ModeKey = 'm', - PoolCoinKey = 'C', - ReuseTimeoutKey = 1106, - WorkersKey = 1103, - WorkersAdvKey = 1107, + AccessLogFileKey = 'A', + BindKey = 'b', + CoinKey = 1104, + CustomDiffKey = 1102, + DebugKey = 1101, + ModeKey = 'm', + PoolCoinKey = 'C', + ReuseTimeoutKey = 1106, + WorkersKey = 1103, + WorkersAdvKey = 1107, + TlsBindKey = 1108, + TlsCertKey = 1109, + TlsCertKeyKey = 1110, + TlsDHparamKey = 1111, + TlsCiphersKey = 1112, + TlsCipherSuitesKey = 1113, + TlsProtocolsKey = 1114, // xmrig nvidia CudaMaxThreadsKey = 1200, diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 2774ae0c..788ad263 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __LOG_H__ -#define __LOG_H__ +#ifndef XMRIG_LOG_H +#define XMRIG_LOG_H #include @@ -39,7 +39,7 @@ public: static inline Log* i() { if (!m_self) { defaultInit(); } return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { new Log(); } } - static inline void release() { assert(m_self != nullptr); delete m_self; } + static inline void release() { delete m_self; } void message(ILogBackend::Level level, const char* fmt, ...); void text(const char* fmt, ...); @@ -98,4 +98,4 @@ private: # define LOG_DEBUG_WARN(x, ...) #endif -#endif /* __LOG_H__ */ +#endif /* XMRIG_LOG_H */ diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h index 6e38f32f..083adfc4 100644 --- a/src/common/net/Tls.h +++ b/src/common/net/Tls.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef XMRIG_TLS_H -#define XMRIG_TLS_H +#ifndef XMRIG_CLIENT_TLS_H +#define XMRIG_CLIENT_TLS_H #include @@ -59,4 +59,4 @@ private: }; -#endif /* XMRIG_TLS_H */ +#endif /* XMRIG_CLIENT_TLS_H */ diff --git a/src/common/utils/c_str.h b/src/common/utils/c_str.h index 7ce63754..fe0164b9 100644 --- a/src/common/utils/c_str.h +++ b/src/common/utils/c_str.h @@ -21,82 +21,19 @@ * along with this program. If not, see . */ -#ifndef __C_STR_H__ -#define __C_STR_H__ +#ifndef XMRIG_C_STR_H +#define XMRIG_C_STR_H -#include -#include - -#include +#include "base/tools/String.h" namespace xmrig { -/** - * @brief Simple C string wrapper. - * - * 1. I know about std:string. - * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. - */ -class c_str -{ -public: - inline c_str() : m_data(nullptr) {} - inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; } - inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); } - inline c_str(const char *str) : m_data(nullptr) { set(str); } - inline ~c_str() { free(m_data); } - - - inline void set(const char *str) - { - free(m_data); - - m_data = str != nullptr ? strdup(str) : nullptr; - } - - - inline void set(char *str) - { - free(m_data); - - m_data = str; - } - - - inline bool isEqual(const char *str) const - { - return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); - } - - - inline bool contains(const char *str) const - { - return strstr(m_data, str) != nullptr; - } - - - inline bool isNull() const { return m_data == nullptr; } - inline const char *data() const { return m_data; } - inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); } - - - inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); } - inline bool operator!=(const char *str) const { return !isEqual(str); } - inline bool operator==(const c_str &str) const { return isEqual(str.data()); } - inline bool operator==(const char *str) const { return isEqual(str); } - inline c_str &operator=(char *str) { set(str); return *this; } - inline c_str &operator=(const c_str &str) { set(str.data()); return *this; } - inline c_str &operator=(const char *str) { set(str); return *this; } - - -private: - char *m_data; -}; +typedef String c_str; } /* namespace xmrig */ -#endif /* __C_STR_H__ */ +#endif /* XMRIG_C_STR_H */