From 71e0da6705b895c90ac42efbc6494e62b148a4f5 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Thu, 1 Aug 2013 17:05:37 -0300 Subject: [PATCH] initialize libtorrent session with external ip set --- .../include/libtorrent/aux_/session_impl.hpp | 3 +- libtorrent/include/libtorrent/session.hpp | 8 +++-- libtorrent/src/session.cpp | 4 +-- libtorrent/src/session_impl.cpp | 5 +++ src/init.cpp | 32 +++++++++++++++++-- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/libtorrent/include/libtorrent/aux_/session_impl.hpp b/libtorrent/include/libtorrent/aux_/session_impl.hpp index f93255fb..b8591c32 100644 --- a/libtorrent/include/libtorrent/aux_/session_impl.hpp +++ b/libtorrent/include/libtorrent/aux_/session_impl.hpp @@ -215,7 +215,8 @@ namespace libtorrent std::pair listen_port_range , fingerprint const& cl_fprint , char const* listen_interface - , boost::uint32_t alert_mask); + , boost::uint32_t alert_mask + , char const* ext_ip); virtual ~session_impl(); void update_dht_announce_interval(); void init(); diff --git a/libtorrent/include/libtorrent/session.hpp b/libtorrent/include/libtorrent/session.hpp index 215f4734..3eb43556 100644 --- a/libtorrent/include/libtorrent/session.hpp +++ b/libtorrent/include/libtorrent/session.hpp @@ -139,10 +139,11 @@ namespace libtorrent , LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) , int flags = start_default_features | add_default_plugins , boost::uint32_t alert_mask = alert::error_notification + , char const* ext_ip = NULL TORRENT_LOGPATH_ARG_DEFAULT) { TORRENT_CFG(); - init(std::make_pair(0, 0), "0.0.0.0", print, alert_mask); + init(std::make_pair(0, 0), "0.0.0.0", print, alert_mask, ext_ip); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING set_log_path(logpath); #endif @@ -153,12 +154,13 @@ namespace libtorrent , char const* listen_interface = "0.0.0.0" , int flags = start_default_features | add_default_plugins , int alert_mask = alert::error_notification + , char const* ext_ip = NULL TORRENT_LOGPATH_ARG_DEFAULT) { TORRENT_CFG(); TORRENT_ASSERT(listen_port_range.first > 0); TORRENT_ASSERT(listen_port_range.first < listen_port_range.second); - init(listen_port_range, listen_interface, print, alert_mask); + init(listen_port_range, listen_interface, print, alert_mask, ext_ip); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING set_log_path(logpath); #endif @@ -851,7 +853,7 @@ namespace libtorrent private: void init(std::pair listen_range, char const* listen_interface - , fingerprint const& id, boost::uint32_t alert_mask); + , fingerprint const& id, boost::uint32_t alert_mask, char const* ext_ip); void set_log_path(std::string const& p); void start(int flags); diff --git a/libtorrent/src/session.cpp b/libtorrent/src/session.cpp index b59b1615..e7546f1a 100644 --- a/libtorrent/src/session.cpp +++ b/libtorrent/src/session.cpp @@ -398,7 +398,7 @@ namespace libtorrent #endif void session::init(std::pair listen_range, char const* listen_interface - , fingerprint const& id, boost::uint32_t alert_mask) + , fingerprint const& id, boost::uint32_t alert_mask, char const* ext_ip) { #if defined _MSC_VER && defined TORRENT_DEBUG // workaround for microsofts @@ -407,7 +407,7 @@ namespace libtorrent ::_set_se_translator(straight_to_debugger); #endif - m_impl.reset(new session_impl(listen_range, id, listen_interface, alert_mask)); + m_impl.reset(new session_impl(listen_range, id, listen_interface, alert_mask, ext_ip)); #ifdef TORRENT_MEMDEBUG start_malloc_debug(); diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 59204037..2d563fea 100644 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -597,6 +597,7 @@ namespace aux { , fingerprint const& cl_fprint , char const* listen_interface , boost::uint32_t alert_mask + , char const* ext_ip ) : m_ipv4_peer_pool(500) #if TORRENT_USE_IPV6 @@ -711,6 +712,10 @@ namespace aux { m_listen_interface = tcp::endpoint(address::from_string(listen_interface, ec), listen_port_range.first); TORRENT_ASSERT_VAL(!ec, ec); + if (ext_ip) { + m_external_ip.cast_vote(address::from_string(ext_ip), source_router, address()); + } + // ---- generate a peer id ---- static seed_random_generator seeder; diff --git a/src/init.cpp b/src/init.cpp index 23fb5e9b..924df580 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -961,16 +961,34 @@ bool AppInit2(boost::thread_group& threadGroup) #include "libtorrent/bencode.hpp" #include "libtorrent/session.hpp" +#define TORRENT_DISABLE_GEO_IP +#include "libtorrent/aux_/session_impl.hpp" + using namespace libtorrent; static session *ses; -void startSessionTorrent(boost::thread_group& threadGroup) +void ThreadWaitExtIP() { + RenameThread("wait-extip"); + + std::string ipStr; + + // wait up to 5 seconds for bitcoin to get the external IP + for( int i = 0; i < 10; i++ ) { + const CNetAddr paddrPeer("8.8.8.8"); + CAddress addr( GetLocalAddress(&paddrPeer) ); + if( addr != CAddress() ) { + ipStr = addr.ToStringIP(); + break; + } + MilliSleep(500); + } + error_code ec; int listen_port = GetListenPort() + 1000; std::string bind_to_interface = ""; - printf("startSessionTorrent port=%d\n", listen_port); + printf("Creating new libtorrent session ext_ip=%s port=%d\n", ipStr.c_str(), listen_port); ses = new session(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) , session::add_default_plugins @@ -978,7 +996,8 @@ void startSessionTorrent(boost::thread_group& threadGroup) & ~(alert::dht_notification + alert::progress_notification + alert::debug_notification - + alert::stats_notification)); + + alert::stats_notification) + , ipStr.size() ? ipStr.c_str() : NULL ); /* std::vector in; @@ -1003,3 +1022,10 @@ void startSessionTorrent(boost::thread_group& threadGroup) printf("libtorrent + dht started\n"); } +void startSessionTorrent(boost::thread_group& threadGroup) +{ + printf("startSessionTorrent (waiting for external IP)\n"); + + threadGroup.create_thread(boost::bind(&ThreadWaitExtIP)); +} +