Files
xmrig/src/base/net/stratum/Client.h

166 lines
5.6 KiB
C
Raw Normal View History

2017-06-04 20:52:21 +03:00
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
2019-01-09 16:43:36 +07:00
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
2017-06-04 20:52:21 +03:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
2018-09-16 03:06:54 +03:00
#ifndef XMRIG_CLIENT_H
#define XMRIG_CLIENT_H
2017-06-04 20:52:21 +03:00
2019-03-15 01:06:10 +07:00
#include <bitset>
#include <map>
2017-06-04 20:52:21 +03:00
#include <uv.h>
2018-03-07 16:38:58 +07:00
#include <vector>
2017-06-04 20:52:21 +03:00
#include "base/kernel/interfaces/IDnsListener.h"
2019-03-15 03:44:02 +07:00
#include "base/kernel/interfaces/ILineListener.h"
2019-04-10 18:29:33 +07:00
#include "base/net/stratum/BaseClient.h"
2019-03-15 01:50:35 +07:00
#include "base/net/stratum/Job.h"
#include "base/net/stratum/Pool.h"
#include "base/net/stratum/SubmitResult.h"
2019-03-15 03:44:02 +07:00
#include "base/net/tools/RecvBuf.h"
2019-03-15 01:50:35 +07:00
#include "base/net/tools/Storage.h"
2018-05-19 14:44:50 +07:00
#include "common/crypto/Algorithm.h"
2017-06-07 01:19:59 +03:00
typedef struct bio_st BIO;
2017-06-04 20:52:21 +03:00
namespace xmrig {
class IClientListener;
class JobResult;
2018-09-16 03:06:54 +03:00
2019-04-10 18:29:33 +07:00
class Client : public BaseClient, public IDnsListener, public ILineListener
2017-06-04 20:52:21 +03:00
{
public:
2018-09-16 03:06:54 +03:00
constexpr static int kResponseTimeout = 20 * 1000;
# ifdef XMRIG_FEATURE_TLS
2018-09-16 03:06:54 +03:00
constexpr static int kInputBufferSize = 1024 * 16;
# else
constexpr static int kInputBufferSize = 1024 * 2;
# endif
2017-06-07 06:48:00 +03:00
Client(int id, const char *agent, IClientListener *listener);
2019-03-15 03:44:02 +07:00
~Client() override;
2017-06-04 20:52:21 +03:00
2019-04-10 18:29:33 +07:00
bool disconnect() override;
bool isTLS() const override;
const char *tlsFingerprint() const override;
const char *tlsVersion() const override;
int64_t submit(const JobResult &result) override;
void connect() override;
void connect(const Pool &pool) override;
void deleteLater() override;
void tick(uint64_t now) override;
inline bool hasExtension(Extension extension) const noexcept override { return m_extensions.test(extension); }
2019-04-11 00:18:33 +07:00
inline const char *mode() const override { return "pool"; }
2019-03-15 01:06:10 +07:00
2019-03-15 03:44:02 +07:00
protected:
inline void onLine(char *line, size_t size) override { parse(line, size); }
void onResolved(const Dns &dns, int status) override;
2017-06-04 20:52:21 +03:00
private:
2019-04-10 18:29:33 +07:00
enum SocketState {
UnconnectedState,
HostLookupState,
ConnectingState,
ConnectedState,
ClosingState
};
2018-09-16 03:06:54 +03:00
class Tls;
2018-03-07 16:38:58 +07:00
bool close();
bool isCriticalError(const char *message);
bool parseJob(const rapidjson::Value &params, int *code);
bool parseLogin(const rapidjson::Value &result, int *code);
2018-09-16 03:06:54 +03:00
bool send(BIO *bio);
bool verifyAlgorithm(const Algorithm &algorithm) const;
int resolve(const String &host);
2018-04-26 23:27:53 +07:00
int64_t send(const rapidjson::Document &doc);
int64_t send(size_t size);
2018-03-07 16:38:58 +07:00
void connect(sockaddr *addr);
2018-09-16 03:06:54 +03:00
void handshake();
void login();
2018-03-26 11:47:01 +07:00
void onClose();
2017-06-04 20:52:21 +03:00
void parse(char *line, size_t len);
2019-03-15 01:06:10 +07:00
void parseExtensions(const rapidjson::Value &result);
void parseNotification(const char *method, const rapidjson::Value &params, const rapidjson::Value &error);
void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error);
2017-06-07 06:48:00 +03:00
void ping();
2019-03-15 03:44:02 +07:00
void read(ssize_t nread);
2017-06-07 04:19:32 +03:00
void reconnect();
2017-06-04 20:52:21 +03:00
void setState(SocketState state);
2017-06-07 06:48:00 +03:00
void startTimeout();
2017-06-04 20:52:21 +03:00
2019-03-15 01:06:10 +07:00
inline bool isQuiet() const { return m_quiet || m_failures >= m_retries; }
inline const char *url() const { return m_pool.url(); }
2019-04-10 18:29:33 +07:00
inline SocketState state() const { return m_state; }
2019-03-15 01:06:10 +07:00
inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); }
2019-04-10 18:29:33 +07:00
template<Extension ext> inline bool has() const noexcept { return m_extensions.test(ext); }
2017-06-04 20:52:21 +03:00
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void onClose(uv_handle_t *handle);
static void onConnect(uv_connect_t *req, int status);
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
2018-03-31 18:12:52 +07:00
static inline Client *getClient(void *data) { return m_storage.get(data); }
2017-06-04 20:52:21 +03:00
char m_sendBuf[2048];
const char *m_agent;
Dns *m_dns;
2017-06-07 04:19:32 +03:00
int64_t m_failures;
2019-03-15 03:44:02 +07:00
RecvBuf<kInputBufferSize> m_recvBuf;
2017-06-04 20:52:21 +03:00
SocketState m_state;
2019-03-15 01:06:10 +07:00
std::bitset<EXT_MAX> m_extensions;
std::map<int64_t, SubmitResult> m_results;
2019-03-15 02:03:01 +07:00
String m_rpcId;
2018-09-16 03:06:54 +03:00
Tls *m_tls;
uint64_t m_expire;
2018-03-17 16:30:41 +07:00
uint64_t m_jobs;
2018-03-31 18:12:52 +07:00
uint64_t m_keepAlive;
uintptr_t m_key;
2017-06-04 20:52:21 +03:00
uv_stream_t *m_stream;
uv_tcp_t *m_socket;
2018-03-31 18:12:52 +07:00
static int64_t m_sequence;
static Storage<Client> m_storage;
2017-06-04 20:52:21 +03:00
};
2019-03-15 01:06:10 +07:00
template<> inline bool Client::has<Client::EXT_NICEHASH>() const noexcept { return m_extensions.test(EXT_NICEHASH) || m_pool.isNicehash(); }
template<> inline bool Client::has<Client::EXT_KEEPALIVE>() const noexcept { return m_extensions.test(EXT_KEEPALIVE) || m_pool.keepAlive() > 0; }
} /* namespace xmrig */
2018-09-16 03:06:54 +03:00
#endif /* XMRIG_CLIENT_H */