Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ jobs:
cmake --build build
cmake --install build

- name: Install zeromq
run: |
# the distro libzmq.a needs gssapi, which has no static archive, so build it without optional dependencies
wget https://github.com/zeromq/libzmq/releases/download/v4.3.5/zeromq-4.3.5.tar.gz
echo "6653ef5910f17954861fe72332e68b03ca6e4d9c7160eb3a8de5a5a913bfab43 zeromq-4.3.5.tar.gz" | sha256sum -c
tar xzf zeromq-4.3.5.tar.gz
cd zeromq-4.3.5
cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED=OFF -DBUILD_STATIC=ON -DZMQ_BUILD_TESTS=OFF -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DWITH_DOCS=OFF -DWITH_PERF_TOOL=OFF -DENABLE_DRAFTS=OFF -DENABLE_CPACK=OFF -DCMAKE_POLICY_VERSION_MINIMUM=3.5
cmake --build build -j3
cmake --install build

- name: Install boost
run: |
# PIC static boost, same version on all platforms (no distro boost so headers and archives match)
Expand Down
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ find_path(SODIUM_INCLUDE_DIR sodium/crypto_verify_32.h REQUIRED)
include_directories(${SODIUM_INCLUDE_DIR})
message(STATUS "Using libsodium library at ${SODIUM_LIBRARY}")

############
# libzmq
############

# link before libsodium, which static libzmq depends on
find_path(ZMQ_INCLUDE_DIR zmq.h REQUIRED)
find_library(ZMQ_LIBRARY zmq REQUIRED)
include_directories(${ZMQ_INCLUDE_DIR})
set(ZMQ_LIBRARIES ${ZMQ_LIBRARY})
if (STATIC)
add_definitions(-DZMQ_STATIC) # as monero-project, else windows expects a zmq dll
# optional transports of static libzmq
foreach(zmq_dep pgm norm gssapi_krb5 protolib protokit bsd md)
string(TOUPPER ${zmq_dep} zmq_dep_var)
find_library(ZMQ_DEP_${zmq_dep_var} ${zmq_dep})
if (ZMQ_DEP_${zmq_dep_var})
list(APPEND ZMQ_LIBRARIES ${ZMQ_DEP_${zmq_dep_var}})
endif()
endforeach()
endif()
if (WIN32)
list(APPEND ZMQ_LIBRARIES ws2_32 rpcrt4 iphlpapi)
endif()
message(STATUS "Using libzmq libraries: ${ZMQ_LIBRARIES}")

############
# HIDAPI
############
Expand Down Expand Up @@ -423,6 +448,7 @@ if (BUILD_LIBRARY)
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZMQ_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${EXTRA_LIBRARIES}
Expand Down Expand Up @@ -547,6 +573,7 @@ if (BUILD_SAMPLE)
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZMQ_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${EXTRA_LIBRARIES}
Expand Down Expand Up @@ -619,6 +646,7 @@ if (BUILD_SCRATCHPAD)
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZMQ_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${EXTRA_LIBRARIES}
Expand Down Expand Up @@ -691,6 +719,7 @@ if (BUILD_TESTS)
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZMQ_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${EXTRA_LIBRARIES}
Expand Down
130 changes: 129 additions & 1 deletion src/common/monero_rpc_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include "utils/monero_utils.h"
#include "daemon/monero_daemon_rpc_model.h"
#include "monero_error.h"
#include "net/net_parse_helpers.h"
#include "net/zmq.h"

static const uint32_t DEFAULT_TIMEOUT_MS = 3 * 60 * 1000; // 3 minutes
static const char* ZMQ_MONITOR_ENDPOINT = "inproc://monero_zmq_subscriber_monitor";
static const int ZMQ_RECONNECT_INTERVAL_MS = 1000;

namespace monero {

Expand Down Expand Up @@ -72,6 +76,115 @@ namespace monero {
throw monero_rpc_error(err_code, err_message);
}

// --------------------------- MONERO ZMQ SUBSCRIBER ---------------------------

struct monero_zmq_subscriber::impl {
net::zmq::context m_context; // destroyed after the sockets
net::zmq::socket m_socket;
net::zmq::socket m_monitor;
};

monero_zmq_subscriber::monero_zmq_subscriber(const std::string& uri, const std::vector<std::string>& topics): m_impl(new impl()) {
m_impl->m_context.reset(zmq_ctx_new());
if (!m_impl->m_context) throw monero_error("Failed to create zmq context: " + net::zmq::get_error_code().message());

m_impl->m_socket.reset(zmq_socket(m_impl->m_context.get(), ZMQ_SUB));
if (!m_impl->m_socket) throw monero_error("Failed to create zmq subscriber: " + net::zmq::get_error_code().message());
int linger_ms = 0;
int reconnect_ms = ZMQ_RECONNECT_INTERVAL_MS;
zmq_setsockopt(m_impl->m_socket.get(), ZMQ_LINGER, &linger_ms, sizeof(linger_ms));
zmq_setsockopt(m_impl->m_socket.get(), ZMQ_RECONNECT_IVL, &reconnect_ms, sizeof(reconnect_ms));

// connection state comes from a monitor, which must be created before connecting
if (zmq_socket_monitor(m_impl->m_socket.get(), ZMQ_MONITOR_ENDPOINT, ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED) < 0) {
throw monero_error("Failed to monitor zmq subscriber: " + net::zmq::get_error_code().message());
}
m_impl->m_monitor.reset(zmq_socket(m_impl->m_context.get(), ZMQ_PAIR));
if (!m_impl->m_monitor) throw monero_error("Failed to create zmq monitor: " + net::zmq::get_error_code().message());
zmq_setsockopt(m_impl->m_monitor.get(), ZMQ_LINGER, &linger_ms, sizeof(linger_ms));
if (zmq_connect(m_impl->m_monitor.get(), ZMQ_MONITOR_ENDPOINT) < 0) {
throw monero_error("Failed to connect zmq monitor: " + net::zmq::get_error_code().message());
}

for (const std::string& topic : topics) {
if (zmq_setsockopt(m_impl->m_socket.get(), ZMQ_SUBSCRIBE, topic.c_str(), topic.size()) < 0) {
throw monero_error("Failed to subscribe to zmq topic '" + topic + "': " + net::zmq::get_error_code().message());
}
}
if (zmq_connect(m_impl->m_socket.get(), uri.c_str()) < 0) {
throw monero_error("Failed to connect to zmq uri '" + uri + "': " + net::zmq::get_error_code().message());
}
}

monero_zmq_subscriber::~monero_zmq_subscriber() { }

boost::optional<monero_zmq_publication> monero_zmq_subscriber::wait_for_publication(int timeout_ms) {
zmq_pollitem_t items[2] = {
{ m_impl->m_monitor.get(), 0, ZMQ_POLLIN, 0 },
{ m_impl->m_socket.get(), 0, ZMQ_POLLIN, 0 }
};
if (zmq_poll(items, 2, timeout_ms) < 0) {
if (zmq_errno() == EINTR) return boost::none;
throw monero_error("Failed to wait for zmq publication: " + net::zmq::get_error_code().message());
}
if (items[0].revents & ZMQ_POLLIN) read_events();
if (!(items[1].revents & ZMQ_POLLIN)) return boost::none;

auto message = net::zmq::receive(m_impl->m_socket.get(), ZMQ_DONTWAIT);
if (!message) {
if (message.error() == net::zmq::make_error_code(EAGAIN)) return boost::none;
throw monero_error("Failed to receive zmq publication: " + message.error().message());
}

// "topic:json"
size_t sep = message->find(':');
if (sep == std::string::npos) return boost::none;
return monero_zmq_publication{ message->substr(0, sep), message->substr(sep + 1) };
}

void monero_zmq_subscriber::discard_publications() {
read_events();
while (true) {
zmq_msg_t msg;
zmq_msg_init(&msg);
int rc = zmq_msg_recv(&msg, m_impl->m_socket.get(), ZMQ_DONTWAIT);
zmq_msg_close(&msg);
if (rc < 0) return;
}
}

void monero_zmq_subscriber::read_events() {
while (true) {
zmq_msg_t msg;
zmq_msg_init(&msg);
if (zmq_msg_recv(&msg, m_impl->m_monitor.get(), ZMQ_DONTWAIT) < 0) {
zmq_msg_close(&msg);
return;
}

// event id is in the first frame, the rest is the address
uint16_t event = 0;
if (zmq_msg_size(&msg) >= sizeof(event)) memcpy(&event, zmq_msg_data(&msg), sizeof(event));
bool more = zmq_msg_more(&msg) != 0;
zmq_msg_close(&msg);
while (more) {
zmq_msg_init(&msg);
if (zmq_msg_recv(&msg, m_impl->m_monitor.get(), ZMQ_DONTWAIT) < 0) { zmq_msg_close(&msg); break; }
more = zmq_msg_more(&msg) != 0;
zmq_msg_close(&msg);
}

if (event == ZMQ_EVENT_CONNECTED) {
MDEBUG("Connected to zmq publisher");
m_is_connected = true;
m_num_connections++;
} else if (event == ZMQ_EVENT_DISCONNECTED) {
MDEBUG("Disconnected from zmq publisher");
m_is_connected = false;
}
}
}

// --------------------------- MONERO RPC CONNECTION ---------------------------

bool monero_rpc_connection::compare(int p1, int p2) {
Expand Down Expand Up @@ -204,6 +317,16 @@ namespace monero {
return i->second;
}

std::unique_ptr<monero_zmq_subscriber> monero_rpc_connection::subscribe_zmq(const std::vector<std::string>& topics) const {
std::string uri;
{
boost::lock_guard<boost::recursive_mutex> lock(m_mutex);
if (m_zmq_uri == boost::none || m_zmq_uri.get().empty()) throw monero_error("Connection has no zmq uri");
uri = m_zmq_uri.get();
}
return std::unique_ptr<monero_zmq_subscriber>(new monero_zmq_subscriber(uri, topics));
}

boost::optional<bool> monero_rpc_connection::is_online() const {
boost::lock_guard<boost::recursive_mutex> lock(m_mutex);
return m_is_online;
Expand Down Expand Up @@ -355,6 +478,11 @@ namespace monero {
if (!m_http_client->set_server(uri, login, std::move(ssl))) throw monero_error("Could not set uri: " + uri);
if (!m_http_client->connect(std::chrono::milliseconds(DEFAULT_TIMEOUT_MS))) MWARNING("Could not connect to server: " + uri);

// the http client only keeps host and port, so requests are sent under the path of the uri
epee::net_utils::http::url_content parsed;
m_path_prefix = epee::net_utils::parse_url(uri, parsed) ? parsed.m_uri_content.m_path : "";
while (!m_path_prefix.empty() && m_path_prefix.back() == '/') m_path_prefix.pop_back();

m_applied = key;
}

Expand All @@ -373,7 +501,7 @@ namespace monero {

// invoke http json
const uint64_t received_before = m_http_client->get_bytes_received();
if (!m_http_client->invoke_post(uri, body, std::chrono::milliseconds(timeout), std::addressof(pri))) {
if (!m_http_client->invoke_post(m_path_prefix + uri.to_string(), body, std::chrono::milliseconds(timeout), std::addressof(pri))) {

// epee never surfaces the 401 status, but a rejected login means the server answered;
// a dead or idle-closed socket yields no response bytes at all
Expand Down
65 changes: 64 additions & 1 deletion src/common/monero_rpc_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,59 @@ namespace monero {
boost::optional<std::string> m_binary;
};

/**
* Models a publication of a Monero daemon's zmq publisher.
*/
struct monero_zmq_publication {
std::string m_topic;
std::string m_json;
};

/**
* Receives the publications of a Monero daemon's zmq publisher, can only be used by one thread at a time.
*/
class monero_zmq_subscriber {
public:
~monero_zmq_subscriber();

/**
* Indicates if connected to the publisher, which is updated while waiting for publications.
*
* @return true if connected
*/
bool is_connected() const { return m_is_connected; }

/**
* Get the number of times connected to the publisher, which is updated while waiting for publications.
*
* @return the number of connections
*/
uint64_t get_num_connections() const { return m_num_connections; }

/**
* Wait for a publication.
*
* @param timeout_ms the maximum time to wait
* @return the publication, or none if there is none before the timeout
*/
boost::optional<monero_zmq_publication> wait_for_publication(int timeout_ms);

/**
* Discard the publications which are pending.
*/
void discard_publications();

private:
friend class monero_rpc_connection;
struct impl;
std::unique_ptr<impl> m_impl;
bool m_is_connected = false;
uint64_t m_num_connections = 0;

monero_zmq_subscriber(const std::string& uri, const std::vector<std::string>& topics);
void read_events();
};

/**
* Maintains a connection and sends requests to a Monero RPC API.
*/
Expand All @@ -114,7 +167,7 @@ namespace monero {
boost::optional<std::string> m_password;
boost::optional<std::string> m_proxy_uri;
bool m_ssl_verify = true; // verify TLS certificates and hostnames
boost::optional<std::string> m_zmq_uri; // TODO: implement zmq listener
boost::optional<std::string> m_zmq_uri; // monerod/monero-wallet-rpc --zmq-pub uri
boost::optional<uint32_t> m_timeout_ms; // RPC request timeout in milliseconds
boost::optional<uint64_t> m_response_time; // automatically set by calling check_connection()
int m_priority; // priority relative to other connections where 1 is highest, then 2, etc, and 0 is lowest (default)
Expand Down Expand Up @@ -217,6 +270,15 @@ namespace monero {
*/
bool check_connection(const boost::optional<uint32_t>& timeout_ms = boost::none);

/**
* Subscribe to the publications of the connection's zmq uri.
*
* @param topics are the topics to receive, e.g. "json-minimal-chain_main"
* @return the subscriber
* @throws if the connection has no zmq uri or the subscription fails
*/
std::unique_ptr<monero_zmq_subscriber> subscribe_zmq(const std::vector<std::string>& topics) const;

/**
* Send a request to the RPC API.
*
Expand Down Expand Up @@ -274,6 +336,7 @@ namespace monero {
mutable std::unique_ptr<epee::net_utils::http::abstract_http_client> m_http_client;
std::unordered_map<std::string, std::string> m_attributes;
mutable std::tuple<std::string, std::string, std::string, std::string, bool> m_applied{"", "", "", "", true};
mutable std::string m_path_prefix; // path of the uri, prepended to request paths (e.g. lws rest server prefix)

void ensure_configured() const;
std::string invoke_post(const boost::string_ref uri, const std::string& body, const boost::optional<uint32_t>& timeout_ms = boost::none) const;
Expand Down
Loading
Loading