diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f485e11c..dc3feb367 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set(HEADERS src/App.h src/interfaces/IClientListener.h src/net/Client.h + src/net/Job.h src/net/Network.h src/net/Url.h src/Options.h @@ -15,6 +16,7 @@ set(HEADERS set(SOURCES src/App.cpp src/net/Client.cpp + src/net/Job.cpp src/net/Network.cpp src/net/Url.cpp src/Options.cpp diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 53b2504bf..ee2c68732 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -25,6 +25,7 @@ #include "Console.h" #include "interfaces/IClientListener.h" #include "net/Client.h" +#include "net/Job.h" #include "net/Url.h" @@ -136,6 +137,35 @@ void Client::send(char *data) } +bool Client::parseJob(const json_t *params, int *code) +{ + if (!json_is_object(params)) { + *code = 2; + return false; + } + + Job job; + if (!job.setId(json_string_value(json_object_get(params, "job_id")))) { + *code = 3; + return false; + } + + if (!job.setBlob(json_string_value(json_object_get(params, "blob")))) { + *code = 4; + return false; + } + + if (!job.setTarget(json_string_value(json_object_get(params, "target")))) { + *code = 5; + return false; + } + + LOG_NOTICE("PARSE JOB %d %lld %lld", job.size(), job.target(), job.diff()); + + return true; +} + + bool Client::parseLogin(const json_t *result, int *code) { const char *id = json_string_value(json_object_get(result, "id")); @@ -147,7 +177,7 @@ bool Client::parseLogin(const json_t *result, int *code) memset(m_rpcId, 0, sizeof(m_rpcId)); memcpy(m_rpcId, id, strlen(id)); - return true; + return parseJob(json_object_get(result, "job"), code); } diff --git a/src/net/Client.h b/src/net/Client.h index 7db3ae3f4..2eecb0449 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -58,6 +58,7 @@ public: private: constexpr static size_t kRecvBufSize = 4096; + bool parseJob(const json_t *params, int *code); bool parseLogin(const json_t *result, int *code); int resolve(const char *host); void close(); diff --git a/src/net/Job.cpp b/src/net/Job.cpp new file mode 100644 index 000000000..aeb466850 --- /dev/null +++ b/src/net/Job.cpp @@ -0,0 +1,158 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 +//#include + + +#include "net/Job.h" +#include "Console.h" + + +static inline unsigned char hf_hex2bin(char c, bool &err) +{ + if (c >= '0' && c <= '9') { + return c - '0'; + } + else if (c >= 'a' && c <= 'f') { + return c - 'a' + 0xA; + } + else if (c >= 'A' && c <= 'F') { + return c - 'A' + 0xA; + } + + err = true; + return 0; +} + + +static inline char hf_bin2hex(unsigned char c) +{ + if (c <= 0x9) { + return '0' + c; + } + + return 'a' - 0xA + c; +} + + +Job::Job() : + m_size(0), + m_diff(0), + m_target(0) +{ +} + + +bool Job::setBlob(const char *blob) +{ + if (!blob) { + return false; + } + + m_size = strlen(blob); + if (m_size % 2 != 0) { + return false; + } + + m_size /= 2; + if (m_size < 76 || m_size >= sizeof(m_blob)) { + return false; + } + + return fromHex(blob, m_size * 2, m_blob); +} + + +bool Job::setId(const char *id) +{ + if (!id || strlen(id) >= sizeof(m_id)) { + return false; + } + + memset(m_id, 0, sizeof(m_id)); + memcpy(m_id, id, strlen(id)); + return true; +} + + +bool Job::setTarget(const char *target) +{ + if (!target) { + return false; + } + + const size_t len = strlen(target); + + if (len <= 8) { + uint32_t tmp = 0; + char str[8]; + memcpy(str, target, len); + + if (!fromHex(str, 8, reinterpret_cast(&tmp)) || tmp == 0) { + return false; + } + + m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / static_cast(tmp)); + } + else if (len <= 16) { + m_target = 0; + char str[16]; + memcpy(str, target, len); + + if (!fromHex(str, 16, reinterpret_cast(&m_target)) || m_target == 0) { + return false; + } + } + else { + return false; + } + + + m_diff = toDiff(m_target); + return true; +} + + +bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) +{ + bool error = false; + for (unsigned int i = 0; i < len; i += 2) { + out[i / 2] = (hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error); + + if (error) { + return false; + } + } + return true; +} + + +void Job::toHex(const unsigned char* in, unsigned int len, char* out) +{ + for (unsigned int i = 0; i < len; i++) { + out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4); + out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); + } +} diff --git a/src/net/Job.h b/src/net/Job.h new file mode 100644 index 000000000..d213ec34a --- /dev/null +++ b/src/net/Job.h @@ -0,0 +1,57 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 __JOB_H__ +#define __JOB_H__ + + +#include + + +class Job +{ +public: + Job(); + bool setBlob(const char *blob); + bool setId(const char *id); + bool setTarget(const char *target); + + inline const uint8_t *blob() const { return m_blob; } + inline const char *id() const { return m_id; } + inline uint32_t size() const { return m_size; } + inline uint32_t diff() const { return m_diff; } + inline uint64_t target() const { return m_target; } + + static bool fromHex(const char* in, unsigned int len, unsigned char* out); + static void toHex(const unsigned char* in, unsigned int len, char* out); + inline static uint64_t toDiff(uint64_t target) { return 0xFFFFFFFFFFFFFFFFULL / target; } + +private: + char m_id[64] __attribute__((aligned(16))); + uint8_t m_blob[84] __attribute__((aligned(16))); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + uint32_t m_size; + uint64_t m_diff; + uint64_t m_target; +}; + +#endif /* __JOB_H__ */