From 9a434a919f1973ffa8c226724f7d8bb1f70b7699 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 22 Apr 2008 00:05:23 +0000 Subject: [PATCH] some more ASNum additions --- docs/manual.rst | 9 +++++++-- examples/client_test.cpp | 18 ++++++++++++++++++ include/libtorrent/session.hpp | 1 + src/session.cpp | 7 +++++++ src/session_impl.cpp | 5 +++-- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 81494ecc0..0006e8445 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -134,6 +134,7 @@ The ``session`` class has the following synopsis:: bool load_asnum_db(char const* file); bool load_country_db(char const* file); + int as_for_ip(address const& adr); void load_state(entry const& ses_state); entry state() const; @@ -421,18 +422,22 @@ their turn to get connected. ``max_half_open_connections()`` returns the set limit. This limit defaults to 8 on windows. -load_asnum_db() load_country_db() ---------------------------------- +load_asnum_db() load_country_db() int as_for_ip() +------------------------------------------------- :: bool load_asnum_db(char const* file); bool load_country_db(char const* file); + int as_for_ip(address const& adr); These functions are not available if ``TORRENT_DISABLE_GEO_IP`` is defined. They expects a path to the `MaxMind ASN database`_ and `MaxMind GeoIP database`_ respectively. This will be used to look up which AS and country peers belong to. +``as_for_ip`` returns the AS number for the IP address specified. If the IP is not +in the database or the ASN database is not loaded, 0 is returned. + .. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum .. _`MaxMind GeoIP database`: http://www.maxmind.com/app/geolitecountry diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 2a246f154..c756c0d5a 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -980,6 +980,24 @@ int main(int ac, char* av[]) char c; if (sleep_and_input(&c)) { + if (c == 'm') + { + std::cout << "saving peers for torrents" << std::endl; + + std::vector peers; + for (handles_t::iterator i = handles.begin(); + i != handles.end(); ++i) + { + i->second.get_full_peer_list(peers); + std::ofstream f(("peers_" + i->second.name()).c_str()); + for (std::vector::iterator k = peers.begin() + , end(peers.end()); k != end; ++k) + { + f << k->ip.address() << "\t" << ses.as_for_ip(k->ip.address()) << std::endl; + } + } + } + if (c == 'q') { // keep track of the number of resume data diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 63dae16e7..45bb27688 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -203,6 +203,7 @@ namespace libtorrent #endif #ifndef TORRENT_DISABLE_GEO_IP + int as_for_ip(address const& addr); bool load_asnum_db(char const* file); bool load_country_db(char const* file); #endif diff --git a/src/session.cpp b/src/session.cpp index 538ae47d5..1d54bc40d 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -185,6 +185,13 @@ namespace libtorrent { return m_impl->load_country_db(file); } + + int session::as_for_ip(address const& addr) + { + aux::session_impl::mutex_t::scoped_lock l(m_impl->m_mutex); + return m_impl->as_for_ip(addr); + } + #endif void session::load_state(entry const& ses_state) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 419dccf85..89340964b 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -159,7 +159,7 @@ namespace aux { , m_num_unchoked(0) , m_unchoke_time_scaler(0) , m_optimistic_unchoke_time_scaler(0) - , m_disconnect_time_scaler(0) + , m_disconnect_time_scaler(90) , m_incoming_connection(false) , m_last_tick(time_now()) #ifndef TORRENT_DISABLE_DHT @@ -1355,7 +1355,7 @@ namespace aux { --m_disconnect_time_scaler; if (m_disconnect_time_scaler <= 0) { - m_disconnect_time_scaler = 60; + m_disconnect_time_scaler = 90; // every 60 seconds, disconnect the worst peer // if we have reached the connection limit @@ -1366,6 +1366,7 @@ namespace aux { < bind(&torrent::num_peers, bind(&torrent_map::value_type::second, _2))); TORRENT_ASSERT(i != m_torrents.end()); + // TODO: make the number of peers a percentage of the number of connected peers i->second->get_policy().disconnect_one_peer(); } }