From 77986023c37737e298fc7c3e9938cce0e10d8b80 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Tue, 5 Sep 2017 12:20:27 -0400 Subject: json serialization for rpc-relevant monero types Structured {de-,}serialization methods for (many new) types which are used for requests or responses in the RPC. New types include RPC requests and responses, and structs which compose types within those. # Conflicts: # src/cryptonote_core/blockchain.cpp --- src/daemon/CMakeLists.txt | 3 +++ src/daemon/command_line_args.h | 25 +++++++++++++++++++++++++ src/daemon/daemon.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- src/daemon/daemon.h | 2 ++ src/daemon/main.cpp | 4 ++++ 5 files changed, 74 insertions(+), 1 deletion(-) (limited to 'src/daemon') diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index 795442a2d..782667867 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -92,12 +92,15 @@ target_link_libraries(daemon p2p cryptonote_protocol daemonizer + serialization + daemon_rpc_server ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} + ${ZMQ_LIB} ${EXTRA_LIBRARIES}) add_dependencies(daemon version) set_property(TARGET daemon diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h index 8eb3db195..797285354 100644 --- a/src/daemon/command_line_args.h +++ b/src/daemon/command_line_args.h @@ -64,6 +64,31 @@ namespace daemon_args , "Max number of threads to use for a parallel job" , 0 }; + + const command_line::arg_descriptor arg_zmq_rpc_bind_ip = { + "zmq-rpc-bind-ip" + , "IP for ZMQ RPC server to listen on" + , "127.0.0.1" + }; + + const command_line::arg_descriptor arg_zmq_rpc_bind_port = { + "zmq-rpc-bind-port" + , "Port for ZMQ RPC server to listen on" + , std::to_string(config::ZMQ_RPC_DEFAULT_PORT) + }; + + const command_line::arg_descriptor arg_zmq_testnet_rpc_bind_port = { + "zmq-testnet-rpc-bind-port" + , "Port for testnet ZMQ RPC server to listen on" + , std::to_string(config::testnet::ZMQ_RPC_DEFAULT_PORT) + }; + + const command_line::arg_descriptor arg_zmq_restricted_rpc = { + "zmq-restricted-rpc" + , "Restrict ZMQ RPC to view only commands" + , false + }; + } // namespace daemon_args #endif // DAEMON_COMMAND_LINE_ARGS_H diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 683eaf4ff..faa620c54 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -32,6 +32,8 @@ #include #include "misc_log_ex.h" #include "daemon/daemon.h" +#include "rpc/daemon_handler.h" +#include "rpc/zmq_server.h" #include "common/password.h" #include "common/util.h" @@ -85,7 +87,18 @@ t_daemon::t_daemon( boost::program_options::variables_map const & vm ) : mp_internals{new t_internals{vm}} -{} +{ + bool testnet = command_line::get_arg(vm, command_line::arg_testnet_on); + if (testnet) + { + zmq_rpc_bind_port = command_line::get_arg(vm, daemon_args::arg_zmq_testnet_rpc_bind_port); + } + else + { + zmq_rpc_bind_port = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port); + } + zmq_rpc_bind_address = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip); +} t_daemon::~t_daemon() = default; @@ -133,6 +146,30 @@ bool t_daemon::run(bool interactive) rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this)); } + cryptonote::rpc::DaemonHandler rpc_daemon_handler(mp_internals->core.get(), mp_internals->p2p.get()); + cryptonote::rpc::ZmqServer zmq_server(rpc_daemon_handler); + + if (!zmq_server.addTCPSocket(zmq_rpc_bind_address, zmq_rpc_bind_port)) + { + LOG_ERROR(std::string("Failed to add TCP Socket (") + zmq_rpc_bind_address + + ":" + zmq_rpc_bind_port + ") to ZMQ RPC Server"); + + if (interactive) + { + rpc_commands->stop_handling(); + } + + mp_internals->rpc.stop(); + + return false; + } + + MINFO("Starting ZMQ server..."); + zmq_server.run(); + + MINFO(std::string("ZMQ server started at ") + zmq_rpc_bind_address + + ":" + zmq_rpc_bind_port + "."); + mp_internals->p2p.run(); // blocks until p2p goes down if (rpc_commands) @@ -140,6 +177,8 @@ bool t_daemon::run(bool interactive) rpc_commands->stop_handling(); } + zmq_server.stop(); + mp_internals->rpc.stop(); mp_internals->core.get().get_miner().stop(); MGINFO("Node stopped."); diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 2b9f18669..8c7547f62 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -43,6 +43,8 @@ private: void stop_p2p(); private: std::unique_ptr mp_internals; + std::string zmq_rpc_bind_address; + std::string zmq_rpc_bind_port; public: t_daemon( boost::program_options::variables_map const & vm diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 456eeee64..0d93a49ff 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -89,6 +89,10 @@ int main(int argc, char const * argv[]) command_line::add_arg(core_settings, daemon_args::arg_log_file, default_log.string()); command_line::add_arg(core_settings, daemon_args::arg_log_level); command_line::add_arg(core_settings, daemon_args::arg_max_concurrency); + command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_ip); + command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_port); + command_line::add_arg(core_settings, daemon_args::arg_zmq_testnet_rpc_bind_port); + command_line::add_arg(core_settings, daemon_args::arg_zmq_restricted_rpc); daemonizer::init_options(hidden_options, visible_options); daemonize::t_executor::init_options(core_settings); -- cgit v1.2.3 From 0299cb77ca18073daf3cf371f8da013fb596ae48 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Tue, 5 Sep 2017 12:20:40 -0400 Subject: Fix various oversights/bugs in ZMQ RPC server code - Add some RPC commands (and touch up a couple others) - some bounds checking - some better pointer management - const correctness and error handling -- Thanks @vtnerd for type help with serialization and CMake changes --- src/daemon/command_line_args.h | 6 ------ src/daemon/main.cpp | 1 - 2 files changed, 7 deletions(-) (limited to 'src/daemon') diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h index 797285354..f19d5cc63 100644 --- a/src/daemon/command_line_args.h +++ b/src/daemon/command_line_args.h @@ -83,12 +83,6 @@ namespace daemon_args , std::to_string(config::testnet::ZMQ_RPC_DEFAULT_PORT) }; - const command_line::arg_descriptor arg_zmq_restricted_rpc = { - "zmq-restricted-rpc" - , "Restrict ZMQ RPC to view only commands" - , false - }; - } // namespace daemon_args #endif // DAEMON_COMMAND_LINE_ARGS_H diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 0d93a49ff..b35a864e9 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -92,7 +92,6 @@ int main(int argc, char const * argv[]) command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_ip); command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_port); command_line::add_arg(core_settings, daemon_args::arg_zmq_testnet_rpc_bind_port); - command_line::add_arg(core_settings, daemon_args::arg_zmq_restricted_rpc); daemonizer::init_options(hidden_options, visible_options); daemonize::t_executor::init_options(core_settings); -- cgit v1.2.3