From 4c121d0df35567ae4a5696243cbdbfc076e308b2 Mon Sep 17 00:00:00 2001 From: SwimmingTiger Date: Mon, 15 Apr 2019 15:44:11 +0800 Subject: [PATCH] simulator: add DNS resolver for ss_ip --- src/StratumClient.cc | 29 ++++++++++++++++++----------- src/StratumClient.h | 9 ++++++--- src/simulator/simulator.cfg | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/StratumClient.cc b/src/StratumClient.cc index b65fa0578..703fea28a 100644 --- a/src/StratumClient.cc +++ b/src/StratumClient.cc @@ -50,6 +50,11 @@ StratumClient::StratumClient( , isMining_(false) { inBuf_ = evbuffer_new(); + evdnsBase_ = evdns_base_new(base, 1); + if (evdnsBase_ == nullptr) { + LOG(FATAL) << "DNS init failed"; + } + if (enableTLS_) { LOG(INFO) << "<" << workerFullName_ << "> TLS enabled"; @@ -95,16 +100,20 @@ StratumClient::StratumClient( StratumClient::~StratumClient() { evbuffer_free(inBuf_); bufferevent_free(bev_); + evdns_base_free(evdnsBase_, 0); } -bool StratumClient::connect(struct sockaddr_in &sin) { - // bufferevent_socket_connect(): This function returns 0 if the connect - // was successfully launched, and -1 if an error occurred. - int res = - bufferevent_socket_connect(bev_, (struct sockaddr *)&sin, sizeof(sin)); +bool StratumClient::connect(const string &host, uint16_t port) { + LOG(INFO) << "Connection request to " << host << ":" << port; + + // bufferevent_socket_connect_hostname(): This function returns 0 if the + // connect was successfully launched, and -1 if an error occurred. + int res = bufferevent_socket_connect_hostname( + bev_, evdnsBase_, AF_INET, host.c_str(), (int)port); if (res == 0) { return true; } + return false; } @@ -253,7 +262,7 @@ void StratumClient::sendData(const char *data, size_t len) { ////////////////////////////// StratumClientWrapper //////////////////////////// StratumClientWrapper::StratumClientWrapper( bool enableTLS, - const char *host, + const string &host, const uint32_t port, const uint32_t numConnections, const string &userName, @@ -263,6 +272,8 @@ StratumClientWrapper::StratumClientWrapper( const libconfig::Config &config) : running_(true) , enableTLS_(enableTLS) + , host_(host) + , port_(port) , base_(event_base_new()) , numConnections_(numConnections) , userName_(userName) @@ -270,10 +281,6 @@ StratumClientWrapper::StratumClientWrapper( , passwd_(passwd) , type_(type) , config_(config) { - memset(&sin_, 0, sizeof(sin_)); - sin_.sin_family = AF_INET; - inet_pton(AF_INET, host, &(sin_.sin_addr)); - sin_.sin_port = htons(port); if (minerNamePrefix_.empty()) minerNamePrefix_ = "simulator"; @@ -345,7 +352,7 @@ void StratumClientWrapper::run() { Strings::Format("%s.%s-%05d", userName_, minerNamePrefix_, i); auto client = createClient(enableTLS_, base_, workerFullName, passwd_); - if (!client->connect(sin_)) { + if (!client->connect(host_, port_)) { LOG(ERROR) << "client connnect failure: " << workerFullName; return; } diff --git a/src/StratumClient.h b/src/StratumClient.h index 1ac76bb31..8b55e8141 100644 --- a/src/StratumClient.h +++ b/src/StratumClient.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,7 @@ class StratumClient { bool enableTLS_; struct bufferevent *bev_; struct evbuffer *inBuf_; + struct evdns_base *evdnsBase_; uint32_t sessionId_; // session ID int32_t extraNonce2Size_; @@ -118,7 +120,7 @@ class StratumClient { const string &workerPasswd); virtual ~StratumClient(); - bool connect(struct sockaddr_in &sin); + bool connect(const string &host, uint16_t port); virtual void sendHelloData(); void sendData(const char *data, size_t len); @@ -133,8 +135,9 @@ class StratumClient { class StratumClientWrapper { bool running_; bool enableTLS_; + string host_; + uint16_t port_; struct event_base *base_; - struct sockaddr_in sin_; struct event *timer_; struct event *sigterm_; struct event *sigint_; @@ -151,7 +154,7 @@ class StratumClientWrapper { public: StratumClientWrapper( bool enableTLS, - const char *host, + const string &host, const uint32_t port, const uint32_t numConnections, const string &userName, diff --git a/src/simulator/simulator.cfg b/src/simulator/simulator.cfg index 99167ad85..c9437e4a7 100644 --- a/src/simulator/simulator.cfg +++ b/src/simulator/simulator.cfg @@ -10,7 +10,7 @@ simulator = { number_clients = 1; # stratum sever host & port - ss_ip = "127.0.0.1"; + ss_ip = "localhost"; ss_port = 3333; enable_tls = false;