forked from lthn/blockchain
* stratum_server: resolve CID 210144 (UNINIT_CTOR) * stratum_server: resolve CID 210042/210085/210104 (UNCAUGHT_EXCEPT) The potential to throw exists within the logger, remove_blockchain_update_listener, and any paths involving the logger (including CATCH_ENTRY_*). * epee: misc_log_ex: create CATCH_ENTRY_NO_RETURN macro A temporary substition for what I hope will eventually be a full-fledged exception-dispatcher (class-based, not macro). * stratum_server: resolve CID 210080/210084/210089 (UNCAUGHT_EXCEPT) The potential to throw exists within the logger, remove_protocol_handler, and any paths involving the logger (including CATCH_ENTRY_*). * epee: levin_protocol_handler_async: resolve CID 210140/210182/210165 (UNCAUGHT_EXCEPT) The potential to throw exists within guarded_critical_region_t, and any paths involving the logger (including CATCH_ENTRY_*). * epee: levin_protocol_handler_async: resolve CID 210110/210119/210155 (UNCAUGHT_EXCEPT) The potential to throw exists within the logger, del_connection, and any paths involving the logger (including CATCH_ENTRY_*). * epee: misc_log_ex: move macros to *top* of file so they can be used *within* this file. * daemon: resolve CID 210069/210092/210166 (UNCAUGHT_EXCEPT) The potential to throw exists within log_space, and any paths involving the logger (including CATCH_ENTRY_*). * daemon: return cstdlib proper types in main * simplewallet: resolve 6 different CIDs (UNCAUGHT_EXCEPT) CID: 210082 CID: 210086 CID: 210096 CID: 210147 CID: 210149 CID: 210150 The potential to throw exists throughout various paths in main. * simplewallet: return cstdlib proper types in main * simplewallet: resolve CID 210128/210160 (UNCAUGHT_EXCEPT) The potential to throw exists within various paths, and any paths involving the logger (including CATCH_ENTRY_*). * conn_tool: resolve 5 different CIDs (UNCAUGHT_EXCEPT) CID: 210038 CID: 210047 CID: 210108 CID: 210122 CID: 210157 The potential to throw exists throughout various paths in main. * conn_tool: return cstdlib proper types in main * miniupnp_helper: resolve CID 210050 (UNCAUGHT_EXCEPT) The potential to throw exists within deinit, including any paths involving the logger (including CATCH_ENTRY_*). * epee: profile_tools: resolve CID 210055 (UNCAUGHT_EXCEPT) The potential to throw exists within boost microsec_clock::localtime(), and any paths involving the logger (including CATCH_ENTRY_*). * db_backend_lmdb: resolve CID 210056/210133 (UNCAUGHT_EXCEPT) The potential to throw exists within close(), including any paths involving the logger (including CATCH_ENTRY_*). * epee: misc_log_ex: resolve CID 210060/210124 (UNCAUGHT_EXCEPT) The potential to throw exists within several paths, including any paths involving the logger (including CATCH_ENTRY_*). * epee: misc_language: resolve 4 CIDs (UNCAUGHT_EXCEPT) CID: 210064 CID: 210093 CID: 210136 CID: 210139 The potential to throw exists within m_func(), including any paths involving the logger (including CATCH_ENTRY_*). * db_abstract_accessor: resolve 4 CIDs (UNCAUGHT_EXCEPT) CID: 210072 CID: 210094 CID: 210116 CID: 210141 The potential to throw exists within m_cache.clear(), including any paths involving the logger (including CATCH_ENTRY_*). * epee: net_helper: resolve CID 210100 (UNCAUGHT_EXCEPT) The potential to throw exists within shutdown(), including any paths involving the logger (including CATCH_ENTRY_*). * epee: syncobj: resolve CID 210123 (UNCAUGHT_EXCEPT) The potential to throw exists within unlock(), including any paths involving the logger (including CATCH_ENTRY_*). * epee: profile_tools: resolve CID 210145/210154 (UNCAUGHT_EXCEPT) The potential to throw exists within various paths, including any paths involving the logger (including CATCH_ENTRY_*). * epee: http_base: resolve CID 210176 (UNINIT_CTOR) * p2p: net_node: resolve CID 210173 (UNINIT_CTOR) * epee: net_helper: resolve CID 210138 (UNINIT_CTOR) * p2p: net_peerlist: resolve CID 210137 (UNINIT_CTOR) * currency_basic: resolve CID 210117 (UNINIT_CTOR) * epee: abstract_tcp_server2: resolve 3 CIDs (UNINIT_CTOR) CID: 210040 CID: 210090 CID: 210105 * simplewallet: resolve CID 210103 (UNINIT_CTOR) * epee: levin_protocol_handler_async: resolve CID 210091 (UNINIT_CTOR) * json_archive: resolve CID 210087 (UNINIT_CTOR) * epee: levin_protocol_handler_async: resolve CID 210073 (UNINIT_CTOR) * miniupnp_helper: resolve CID 210037 (UNINIT_CTOR) * crypto: ge_frombytes_vartime: resolve CID 210142 (CHECKED_RETURN) * wallet2: resolve CID 210041 (CHECKED_RETURN) * epee: misc_log_ex: resolve CID 210127 (DEADCODE) * epee: levin_protocol_handler_sync: resolve 3 CIDs (PASS_BY_VALUE) CID: 210167 CID: 210170 CID: 210180 * p2p: net_node: resolve CID 210065 (PASS_BY_VALUE) * epee: levin_abstract_invoke2: resolve CID 210049 (PASS_BY_VALUE) * epee: abstract_tcp_server2: resolve CID 210045 (PASS_BY_VALUE) * epee: misc_log_ex: add NESTED_*_ENTRY macros * simplewallet: use NESTED_*_ENTRY in message_writer dtor * stratum_protocol_handler_config: use NESTED_*_ENTRY in dtor * stratum_protocol_handler: use NESTED_*_ENTRY in dtor * lmdb_db_backend: use NESTED_*_ENTRY in dtor * epee: abstract_tcp_server2: resolve 4 CIDs (UNCAUGHT_EXCEPT) CID: 210088 CID: 210106 CID: 210164 CID: 210179 The potential to throw exists within various paths, including any paths involving the logger (including CATCH_ENTRY_*). * db_abstract_accessor: use NESTED_*_ENTRY in dtor * miniupnp_helper: use NESTED_*_ENTRY in dtor * epee: misc_log_ex: use NESTED_*_ENTRY in log_frame dtor * epee: levin_protocol_handler_async: use NESTED_*_ENTRY in dtors * epee: net_helper: use NESTED_*_ENTRY in dtor * epee: profile_tools: use NESTED_*_ENTRY in dtors * epee: misc_language: use NESTED_*_ENTRY in dtor * epee: syncobj: use NESTED_*_ENTRY in dtor * zano: license contact w/ zano.org email instead of sekreta.org email
164 lines
4.3 KiB
C++
164 lines
4.3 KiB
C++
// Copyright (c) 2014-2019 Zano Project
|
|
// Copyright (c) 2014-2018 The Louisdor Project
|
|
// Copyright (c) 2012-2013 The Boolberry developers
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#pragma once
|
|
|
|
#include <thread>
|
|
#include <string>
|
|
#include <boost/thread.hpp>
|
|
#include "include_base_utils.h"
|
|
extern "C" {
|
|
#include "miniupnp/miniupnpc/miniupnpc.h"
|
|
#include "miniupnp/miniupnpc/upnpcommands.h"
|
|
#include "miniupnp/miniupnpc/upnperrors.h"
|
|
}
|
|
|
|
#include "misc_language.h"
|
|
#include "currency_core/currency_config.h"
|
|
#include "version.h"
|
|
|
|
namespace tools
|
|
{
|
|
class miniupnp_helper
|
|
{
|
|
UPNPDev *m_devlist;
|
|
UPNPUrls m_urls;
|
|
IGDdatas m_data;
|
|
char m_lanaddr[64];
|
|
int m_IGD;
|
|
boost::thread m_mapper_thread;
|
|
uint32_t m_external_port;
|
|
public:
|
|
miniupnp_helper():m_devlist(nullptr),
|
|
m_urls(AUTO_VAL_INIT(m_urls)),
|
|
m_data(AUTO_VAL_INIT(m_data)),
|
|
m_IGD(0),
|
|
m_external_port{}
|
|
{
|
|
m_lanaddr[0] = 0;
|
|
}
|
|
~miniupnp_helper()
|
|
{
|
|
NESTED_TRY_ENTRY();
|
|
|
|
deinit();
|
|
|
|
NESTED_CATCH_ENTRY(__func__);
|
|
}
|
|
|
|
bool start_regular_mapping(uint32_t internal_port, uint32_t external_port, uint32_t period_ms)
|
|
{
|
|
m_external_port = external_port;
|
|
if(!init())
|
|
return false;
|
|
m_mapper_thread = boost::thread([=](){run_port_mapping_loop(internal_port, external_port, period_ms);});
|
|
return true;
|
|
}
|
|
|
|
bool stop_mapping()
|
|
{
|
|
if(m_mapper_thread.joinable())
|
|
{
|
|
m_mapper_thread.interrupt();
|
|
m_mapper_thread.join();
|
|
}
|
|
|
|
if(m_IGD == 1)
|
|
{
|
|
do_port_unmapping();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
|
|
bool init()
|
|
{
|
|
deinit();
|
|
|
|
int error = 0;
|
|
m_devlist = upnpDiscover(2000, nullptr, nullptr, 0, 0, 2, &error);
|
|
if(error)
|
|
{
|
|
LOG_PRINT_L0("Failed to call upnpDiscover");
|
|
return false;
|
|
}
|
|
|
|
m_IGD = UPNP_GetValidIGD(m_devlist, &m_urls, &m_data, m_lanaddr, sizeof(m_lanaddr));
|
|
if(m_IGD != 1)
|
|
{
|
|
LOG_PRINT_L2("IGD not found");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool deinit()
|
|
{
|
|
stop_mapping();
|
|
|
|
if(m_devlist)
|
|
{
|
|
freeUPNPDevlist(m_devlist);
|
|
m_devlist = nullptr;
|
|
}
|
|
|
|
if(m_IGD > 0)
|
|
{
|
|
FreeUPNPUrls(&m_urls);
|
|
m_IGD = 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool run_port_mapping_loop(uint32_t internal_port, uint32_t external_port, uint32_t period_ms)
|
|
{
|
|
while(true)
|
|
{
|
|
do_port_mapping(external_port, internal_port);
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds( period_ms ));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool do_port_mapping(uint32_t external_port, uint32_t internal_port)
|
|
{
|
|
std::string internal_port_str = std::to_string(internal_port);
|
|
std::string external_port_str = std::to_string(external_port);
|
|
std::string str_desc = CURRENCY_NAME " v" PROJECT_VERSION_LONG;
|
|
|
|
int r = UPNP_AddPortMapping(m_urls.controlURL, m_data.first.servicetype,
|
|
external_port_str.c_str(), internal_port_str.c_str(), m_lanaddr, str_desc.c_str(), "TCP", nullptr, "0");
|
|
|
|
if(r!=UPNPCOMMAND_SUCCESS)
|
|
{
|
|
LOG_PRINT_L1("AddPortMapping with external_port_str= " << external_port_str <<
|
|
", internal_port_str=" << internal_port_str <<
|
|
", failed with code=" << r << "(" << strupnperror(r) << ")");
|
|
}else
|
|
{
|
|
LOG_PRINT_L0("[upnp] port mapped successful (ext: " << external_port_str << ", int:" << internal_port_str << ")");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void do_port_unmapping()
|
|
{
|
|
std::string external_port_str = std::to_string(m_external_port);
|
|
|
|
int r = UPNP_DeletePortMapping(m_urls.controlURL, m_data.first.servicetype, external_port_str.c_str(), "TCP", nullptr);
|
|
if(r!=UPNPCOMMAND_SUCCESS)
|
|
{
|
|
LOG_PRINT_L1("DeletePortMapping with external_port_str= " << external_port_str <<
|
|
", failed with code=" << r << "(" << strupnperror(r) << ")");
|
|
}else
|
|
{
|
|
LOG_PRINT_L0("[upnp] port unmapped successful (ext: " << external_port_str << ")");
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|