From 4a53898764d961374f0874ab9029e9b7c2897b75 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Mon, 15 Dec 2014 17:23:42 -0500 Subject: [PATCH 1/8] DNS seed timeout and fallback --- src/cryptonote_config.h | 2 ++ src/p2p/net_node.inl | 70 ++++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index 7864b974e..1d4efe8b4 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -33,6 +33,8 @@ #include #include +#define CRYPTONOTE_DNS_TIMEOUT_MS 20000 + #define CRYPTONOTE_MAX_BLOCK_NUMBER 500000000 #define CRYPTONOTE_MAX_BLOCK_SIZE 500000000 // block header blob limit, never used! #define CRYPTONOTE_GETBLOCKTEMPLATE_MAX_BLOCK_SIZE 196608 //size of block (bytes) that is the maximum that miners will produce diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 01fbaa497..c0aaad8ef 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -31,6 +31,9 @@ #pragma once #include +#include +#include +#include #include "version.h" #include "string_tools.h" @@ -46,13 +49,13 @@ // We have to look for miniupnpc headers in different places, dependent on if its compiled or external #ifdef UPNP_STATIC - #include - #include - #include + #include + #include + #include #else - #include "miniupnpc.h" - #include "upnpcommands.h" - #include "upnperrors.h" + #include "miniupnpc.h" + #include "upnpcommands.h" + #include "upnperrors.h" #endif #define NET_MAKE_IP(b1,b2,b3,b4) ((LPARAM)(((DWORD)(b1)<<24)+((DWORD)(b2)<<16)+((DWORD)(b3)<<8)+((DWORD)(b4)))) @@ -252,19 +255,64 @@ namespace nodetool // add the result addresses as seed nodes // TODO: at some point add IPv6 support, but that won't be relevant // for some time yet. + + std::vector> dns_results; + + std::vector dns_finished; + + dns_results.resize(m_seed_nodes_list.size()); + dns_finished.resize(m_seed_nodes_list.size()); + + // set each flag, thread will release when finished + for (auto& u : dns_finished) + u.test_and_set(); + + uint64_t result_index = 0; for (const std::string& addr_str : m_seed_nodes_list) { - // TODO: care about dnssec avail/valid - bool avail, valid; - std::vector addr_list = tools::DNSResolver::instance().get_ipv4(addr_str, avail, valid); - for (const std::string& a : addr_list) + + uint64_t result_index_capture = result_index++; + boost::thread t([&] { - append_net_address(m_seed_nodes, a + ":18080"); + // TODO: care about dnssec avail/valid + bool avail, valid; + std::vector addr_list = tools::DNSResolver().get_ipv4(addr_str, avail, valid); + + dns_results[result_index_capture] = addr_list; + dns_finished[result_index_capture].clear(); + }); + + } + + uint64_t sleep_count = 0; + uint64_t sleep_interval_ms = 100; + while (sleep_count++ * sleep_interval_ms < CRYPTONOTE_DNS_TIMEOUT_MS) + { + boost::this_thread::sleep(boost::posix_time::milliseconds()); + bool all_done = false; + for (auto& done : dns_finished) + { + if (done.test_and_set()) + break; + else + done.clear(); + all_done = true; + } + if (all_done) + break; + } + + for (const auto& result : dns_results) + { + for (const auto& addr_string : result) + { + append_net_address(m_seed_nodes, addr_string + ":18080"); } } if (!m_seed_nodes.size()) { + LOG_PRINT_L0("DNS seed node lookup either timed out or failed, falling back to defaults"); append_net_address(m_seed_nodes, "62.210.78.186:18080"); append_net_address(m_seed_nodes, "195.12.60.154:18080"); append_net_address(m_seed_nodes, "54.241.246.125:18080"); From df53c0a595cb472f78954deeb293d983bdf02002 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Mon, 15 Dec 2014 17:28:11 -0500 Subject: [PATCH 2/8] small typo in previous commit --- src/p2p/net_node.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index c0aaad8ef..ee2d3974f 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -288,7 +288,7 @@ namespace nodetool uint64_t sleep_interval_ms = 100; while (sleep_count++ * sleep_interval_ms < CRYPTONOTE_DNS_TIMEOUT_MS) { - boost::this_thread::sleep(boost::posix_time::milliseconds()); + boost::this_thread::sleep(boost::posix_time::milliseconds(sleep_interval_ms)); bool all_done = false; for (auto& done : dns_finished) { From 1b462261b88da00f20c743b8638969b8f1ff2a19 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Mon, 15 Dec 2014 17:43:12 -0500 Subject: [PATCH 3/8] std::atomic_flag has no copy/move constructor, can't have a vector --- src/p2p/net_node.inl | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index ee2d3974f..17a81db79 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -257,15 +257,13 @@ namespace nodetool // for some time yet. std::vector> dns_results; - - std::vector dns_finished; - dns_results.resize(m_seed_nodes_list.size()); - dns_finished.resize(m_seed_nodes_list.size()); + + std::unique_ptr dns_finished(new std::atomic_flag[m_seed_nodes_list.size()]); // set each flag, thread will release when finished - for (auto& u : dns_finished) - u.test_and_set(); + for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) + dns_finished[i].test_and_set(); uint64_t result_index = 0; for (const std::string& addr_str : m_seed_nodes_list) @@ -290,12 +288,12 @@ namespace nodetool { boost::this_thread::sleep(boost::posix_time::milliseconds(sleep_interval_ms)); bool all_done = false; - for (auto& done : dns_finished) + for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) { - if (done.test_and_set()) + if (dns_finished[i].test_and_set()) break; else - done.clear(); + dns_finished[i].clear(); all_done = true; } if (all_done) From 7de1a2d57ae1e242b7949dffcd902266500b47ca Mon Sep 17 00:00:00 2001 From: Riccardo Spagni Date: Tue, 6 Jan 2015 18:37:10 +0200 Subject: [PATCH 4/8] previous hash added to GBT --- src/rpc/core_rpc_server.cpp | 1 + src/rpc/core_rpc_server_commands_defs.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index be45a5145..7a41e9b9f 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -475,6 +475,7 @@ namespace cryptonote LOG_ERROR("Failed to calculate offset for "); return false; } + res.prev_hash = string_tools::pod_to_hex(b.prev_id); res.blocktemplate_blob = string_tools::buff_to_hex_nodelimer(block_blob); res.status = CORE_RPC_STATUS_OK; return true; diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 21dbb9f73..9fea933cb 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -383,6 +383,7 @@ namespace cryptonote uint64_t difficulty; uint64_t height; uint64_t reserved_offset; + std::string prev_hash; blobdata blocktemplate_blob; std::string status; @@ -390,6 +391,7 @@ namespace cryptonote KV_SERIALIZE(difficulty) KV_SERIALIZE(height) KV_SERIALIZE(reserved_offset) + KV_SERIALIZE(prev_hash) KV_SERIALIZE(blocktemplate_blob) KV_SERIALIZE(status) END_KV_SERIALIZE_MAP() From 87839cd4843efb231f0406881437d724dbf0161c Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 10 Jan 2015 19:32:08 +0000 Subject: [PATCH 5/8] Allow get_bulk_payments to return all payments regardless of payment ID by giving an empty list of payment IDs. --- src/wallet/wallet2.cpp | 36 +++++++++++++++++++++----------- src/wallet/wallet2.h | 1 + src/wallet/wallet_rpc_server.cpp | 20 ++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index da8898132..6698e7296 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -227,24 +227,25 @@ void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_ } tx_extra_nonce extra_nonce; + crypto::hash payment_id = null_hash; if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) { - crypto::hash payment_id; if(get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) { - uint64_t received = (tx_money_spent_in_ins < tx_money_got_in_outs) ? tx_money_got_in_outs - tx_money_spent_in_ins : 0; - if (0 < received && null_hash != payment_id) - { - payment_details payment; - payment.m_tx_hash = cryptonote::get_transaction_hash(tx); - payment.m_amount = received; - payment.m_block_height = height; - payment.m_unlock_time = tx.unlock_time; - m_payments.emplace(payment_id, payment); - LOG_PRINT_L2("Payment found: " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); - } + // We got a payment ID to go with this tx } } + uint64_t received = (tx_money_spent_in_ins < tx_money_got_in_outs) ? tx_money_got_in_outs - tx_money_spent_in_ins : 0; + if (0 < received) + { + payment_details payment; + payment.m_tx_hash = cryptonote::get_transaction_hash(tx); + payment.m_amount = received; + payment.m_block_height = height; + payment.m_unlock_time = tx.unlock_time; + m_payments.emplace(payment_id, payment); + LOG_PRINT_L2("Payment found: " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); + } } //---------------------------------------------------------------------------------------------------- void wallet2::process_unconfirmed(const cryptonote::transaction& tx) @@ -816,6 +817,17 @@ void wallet2::get_payments(const crypto::hash& payment_id, std::list>& payments, uint64_t min_height) const +{ + auto range = std::make_pair(m_payments.begin(), m_payments.end()); + std::for_each(range.first, range.second, [&payments, &min_height](const payment_container::value_type& x) { + if (min_height < x.second.m_block_height) + { + payments.push_back(x); + } + }); +} +//---------------------------------------------------------------------------------------------------- bool wallet2::is_transfer_unlocked(const transfer_details& td) const { if(!is_tx_spendtime_unlocked(td.m_tx.unlock_time)) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 073fff58b..9b60d6ac5 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -211,6 +211,7 @@ namespace tools bool check_connection(); void get_transfers(wallet2::transfer_container& incoming_transfers) const; void get_payments(const crypto::hash& payment_id, std::list& payments, uint64_t min_height = 0) const; + void get_payments(std::list>& payments, uint64_t min_height) const; uint64_t get_blockchain_current_height() const { return m_local_bc_height; } template inline void serialize(t_archive &a, const unsigned int ver) diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index d7b3f8434..6b6bb4fe2 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -315,6 +315,26 @@ namespace tools { res.payments.clear(); + /* If the payment ID list is empty, we get payments to any payment ID (or lack thereof) */ + if (req.payment_ids.empty()) + { + std::list> payment_list; + m_wallet.get_payments(payment_list, req.min_block_height); + + for (auto & payment : payment_list) + { + wallet_rpc::payment_details rpc_payment; + rpc_payment.payment_id = epee::string_tools::pod_to_hex(payment.first); + rpc_payment.tx_hash = epee::string_tools::pod_to_hex(payment.second.m_tx_hash); + rpc_payment.amount = payment.second.m_amount; + rpc_payment.block_height = payment.second.m_block_height; + rpc_payment.unlock_time = payment.second.m_unlock_time; + res.payments.push_back(std::move(rpc_payment)); + } + + return true; + } + for (auto & payment_id_str : req.payment_ids) { crypto::hash payment_id; From 24d500ce8e27eade6e591f83e1381be62134e151 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 11 Jan 2015 11:06:35 +0000 Subject: [PATCH 6/8] Add a --restricted-rpc flag to simplewallet It restricts RPC to a subset of "view only" commands. Kind of like a poor man's view key replacement. --- src/simplewallet/simplewallet.cpp | 5 +++- src/wallet/wallet2.h | 4 +++- src/wallet/wallet_rpc_server.cpp | 28 ++++++++++++++++++++++ src/wallet/wallet_rpc_server_error_codes.h | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 6f3874981..ae61871dc 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -81,6 +81,7 @@ namespace const command_line::arg_descriptor arg_daemon_port = {"daemon-port", "Use daemon instance at port instead of 8081", 0}; const command_line::arg_descriptor arg_log_level = {"set_log", "", 0, true}; const command_line::arg_descriptor arg_testnet = {"testnet", "Used to deploy test nets. The daemon must be launched with --testnet flag", false}; + const command_line::arg_descriptor arg_restricted = {"restricted-rpc", "Restricts RPC to view only commands", false}; const command_line::arg_descriptor< std::vector > arg_command = {"command", ""}; @@ -1336,6 +1337,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_non_deterministic ); command_line::add_arg(desc_params, arg_electrum_seed ); command_line::add_arg(desc_params, arg_testnet); + command_line::add_arg(desc_params, arg_restricted); tools::wallet_rpc_server::init_options(desc_params); po::positional_options_description positional_options; @@ -1406,6 +1408,7 @@ int main(int argc, char* argv[]) } bool testnet = command_line::get_arg(vm, arg_testnet); + bool restricted = command_line::get_arg(vm, arg_restricted); std::string wallet_file = command_line::get_arg(vm, arg_wallet_file); std::string wallet_password = command_line::get_arg(vm, arg_password); std::string daemon_address = command_line::get_arg(vm, arg_daemon_address); @@ -1418,7 +1421,7 @@ int main(int argc, char* argv[]) if (daemon_address.empty()) daemon_address = std::string("http://") + daemon_host + ":" + std::to_string(daemon_port); - tools::wallet2 wal(testnet); + tools::wallet2 wal(testnet,restricted); try { LOG_PRINT_L0("Loading wallet..."); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 073fff58b..314d8f4af 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -82,7 +82,7 @@ namespace tools { wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false) {}; public: - wallet2(bool testnet = false) : m_run(true), m_callback(0), m_testnet(testnet), is_old_file_format(false) {}; + wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_restricted(restricted), is_old_file_format(false) {}; struct transfer_details { uint64_t m_block_height; @@ -196,6 +196,7 @@ namespace tools bool refresh(size_t & blocks_fetched, bool& received_money, bool& ok); bool testnet() { return m_testnet; } + bool restricted() const { return m_restricted; } uint64_t balance(); uint64_t unlocked_balance(); @@ -296,6 +297,7 @@ namespace tools i_wallet2_callback* m_callback; bool m_testnet; + bool m_restricted; std::string seed_language; /*!< Language of the mnemonics (seed). */ bool is_old_file_format; /*!< Whether the wallet file is of an old file format */ }; diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index d7b3f8434..be20f05de 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -167,6 +167,13 @@ namespace tools std::vector dsts; std::vector extra; + if (m_wallet.restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + // validate the transfer requested and populate dsts & extra if (!validate_transfer(req.destinations, req.payment_id, dsts, extra, er)) { @@ -218,6 +225,13 @@ namespace tools std::vector dsts; std::vector extra; + if (m_wallet.restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + // validate the transfer requested and populate dsts & extra; RPC_TRANSFER::request and RPC_TRANSFER_SPLIT::request are identical types. if (!validate_transfer(req.destinations, req.payment_id, dsts, extra, er)) { @@ -261,6 +275,13 @@ namespace tools //------------------------------------------------------------------------------------------------------------------------------ bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx) { + if (m_wallet.restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + try { m_wallet.store(); @@ -409,6 +430,13 @@ namespace tools //------------------------------------------------------------------------------------------------------------------------------ bool wallet_rpc_server::on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er, connection_context& cntx) { + if (m_wallet.restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + if (req.key_type.compare("mnemonic") == 0) { if (!m_wallet.get_seed(res.key)) diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h index 28df1acf2..28642c19d 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -37,3 +37,4 @@ #define WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR -4 #define WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID -5 #define WALLET_RPC_ERROR_CODE_TRANSFER_TYPE -6 +#define WALLET_RPC_ERROR_CODE_DENIED -7 From 23c3d382c8a0c1bbbff8eacebf96152fca8cf476 Mon Sep 17 00:00:00 2001 From: Riccardo Spagni Date: Sun, 11 Jan 2015 15:49:07 +0200 Subject: [PATCH 7/8] added monoermooo's gpg key --- utils/gpg_keys/moneromooo.asc | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 utils/gpg_keys/moneromooo.asc diff --git a/utils/gpg_keys/moneromooo.asc b/utils/gpg_keys/moneromooo.asc new file mode 100644 index 000000000..80cb7ae0b --- /dev/null +++ b/utils/gpg_keys/moneromooo.asc @@ -0,0 +1,52 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- +Version: GnuPG v2 + +mQINBFQym34BEADDKCspvziDW0f+T9i6iOewFO9m2XTWKXlQutCPgTkIlZZUrcTR +K+ApsfPxk+PBWgucQDPv/nJVs0CNaSzqewxk7Swjsf8+YjvRmxSSg/NQEgsiBx/s +M0JYlJfCwwFdk9+5b/eAnS3nhcZs/6lFSKk65YCCkj0NSQ6C07gEQyIFagLFKVqW +kAlJoMTURyFKPbcwLj/helG9kUT4I/1DdMx5GhKyL6ogFdLYH5iySAkblLk7I43T +jFE/1OcObKUtkAUgJSirrTJcQ+gtyRUUd/xyU3dE0s9T4h4mG+mm0zRzir+dtjQu +i1OWCjMEe1FItz7We8dWwFlM9G+eWDU7/0mBH8Z3qviPN2ccjJatoFq2t+/tc708 +GcG8uuPs47lvCleXxcH8FidGiITz2gFYAjh5LMSGipNM+1N+Lya8VJQ6B2MdF8hI +XCGtzeiNJSRoklNSTpUo+qQ+bLjETjfogUvq8HpYU+oGi4qaHf+Hkmp4c5Mv3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TYc/Fj +pfGV5kuLJEVxpGsGPJTirh+1nbOss0vr7pngy1MeH5Wqk1+LBZPRgJ2KNaOcCiNT +8beMCZxOHkLVxJRj318zT5sgT9DWq+OB+m+XGz1wNx0jbMOPrhnwf5tUru7Aqi4/ +3dibY8q68rewT77R7whAood7Zmi98K5SzRnk5F3jp+Ui47TpqQp/SWwpn7gMpDcN +POY/RnsFrhJjUV9DSF0rm2o2RBtV+TVrIrtEOAjof5jcjDJeU7hV7AK6zOYzksJ/ +l5ajwVyde4plP3/R0PJTYFe2tCDXhhCYc79kgIZO5df7mTygeMU20sTMgs3RMWlg +42Yz4PoruQRT3YeUC1Bsgr0ONqQShlM+68NbCe+3uhZm9WbNg6+oqcNt +=itPR +-----END PGP PUBLIC KEY BLOCK----- From f74792b77881318087312afb27a12dd26d2edb6e Mon Sep 17 00:00:00 2001 From: warptangent Date: Wed, 14 Jan 2015 13:14:01 -0800 Subject: [PATCH 8/8] Fix seed node threaded DNS lookup Use copied value of seed node index during thread creation, not reference. - fixes segfault Use boost::thread::try_join_until() instead of an atomic flag result variable for each thread. Add and handle interrupt for thread timeout. - fixes segfault where a thread exceeds requested timeout and tries to assign results to a referenced, but now out-of-scope, variable in the main thread. --- src/p2p/net_node.inl | 67 ++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 3454e112c..ee4a10789 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -259,53 +259,66 @@ namespace nodetool std::vector> dns_results; dns_results.resize(m_seed_nodes_list.size()); - std::unique_ptr dns_finished(new std::atomic_flag[m_seed_nodes_list.size()]); - - // set each flag, thread will release when finished - for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) - dns_finished[i].test_and_set(); - + std::list dns_threads; uint64_t result_index = 0; for (const std::string& addr_str : m_seed_nodes_list) { - - uint64_t result_index_capture = result_index++; - boost::thread t([&] + boost::thread* th = new boost::thread([=, &dns_results, &addr_str] { + LOG_PRINT_L4("dns_threads[" << result_index << "] created for: " << addr_str) // TODO: care about dnssec avail/valid bool avail, valid; - std::vector addr_list = tools::DNSResolver().get_ipv4(addr_str, avail, valid); + std::vector addr_list; - dns_results[result_index_capture] = addr_list; - dns_finished[result_index_capture].clear(); + try + { + addr_list = tools::DNSResolver().get_ipv4(addr_str, avail, valid); + LOG_PRINT_L4("dns_threads[" << result_index << "] DNS resolve done"); + boost::this_thread::interruption_point(); + } + catch(const boost::thread_interrupted&) + { + // thread interruption request + // even if we now have results, finish thread without setting + // result variables, which are now out of scope in main thread + LOG_PRINT_L4("dns_threads[" << result_index << "] interrupted"); + return; + } + + LOG_PRINT_L4("dns_threads[" << result_index << "] addr_str: " << addr_str << " number of results: " << addr_list.size()); + dns_results[result_index] = addr_list; }); + dns_threads.push_back(th); + ++result_index; } - uint64_t sleep_count = 0; - uint64_t sleep_interval_ms = 100; - while (sleep_count++ * sleep_interval_ms < CRYPTONOTE_DNS_TIMEOUT_MS) + LOG_PRINT_L4("dns_threads created, now waiting for completion or timeout of " << CRYPTONOTE_DNS_TIMEOUT_MS << "ms"); + boost::chrono::system_clock::time_point deadline = boost::chrono::system_clock::now() + boost::chrono::milliseconds(CRYPTONOTE_DNS_TIMEOUT_MS); + uint64_t i = 0; + for (boost::thread* th : dns_threads) { - boost::this_thread::sleep(boost::posix_time::milliseconds(sleep_interval_ms)); - bool all_done = false; - for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) + if (! th->try_join_until(deadline)) { - if (dns_finished[i].test_and_set()) - break; - else - dns_finished[i].clear(); - all_done = true; + LOG_PRINT_L4("dns_threads[" << i << "] timed out, sending interrupt"); + th->interrupt(); } - if (all_done) - break; + ++i; } + i = 0; for (const auto& result : dns_results) { - for (const auto& addr_string : result) + LOG_PRINT_L4("DNS lookup for " << m_seed_nodes_list[i] << ": " << result.size() << " results"); + // if no results for node, thread's lookup likely timed out + if (result.size()) { - append_net_address(m_seed_nodes, addr_string + ":18080"); + for (const auto& addr_string : result) + { + append_net_address(m_seed_nodes, addr_string + ":18080"); + } } + ++i; } if (!m_seed_nodes.size())