support for country lookup through GeoIP

This commit is contained in:
Arvid Norberg 2008-04-11 08:46:43 +00:00
parent b300c7f835
commit 42f55adcce
8 changed files with 77 additions and 22 deletions

View File

@ -37,7 +37,7 @@
<li><a class="reference internal" href="#set-max-uploads-set-max-connections" id="id28">set_max_uploads() set_max_connections()</a></li>
<li><a class="reference internal" href="#num-uploads-num-connections" id="id29">num_uploads() num_connections()</a></li>
<li><a class="reference internal" href="#set-max-half-open-connections-max-half-open-connections" id="id30">set_max_half_open_connections() max_half_open_connections()</a></li>
<li><a class="reference internal" href="#load-asnum-db" id="id31">load_asnum_db()</a></li>
<li><a class="reference internal" href="#load-asnum-db-load-country-db" id="id31">load_asnum_db() load_country_db()</a></li>
<li><a class="reference internal" href="#load-state-state" id="id32">load_state() state()</a></li>
<li><a class="reference internal" href="#set-ip-filter" id="id33">set_ip_filter()</a></li>
<li><a class="reference internal" href="#status" id="id34">status()</a></li>
@ -333,6 +333,7 @@ class session: public boost::noncopyable
int num_connections() const;
bool load_asnum_db(char const* file);
bool load_country_db(char const* file);
void load_state(entry const&amp; ses_state);
entry state() const;
@ -597,16 +598,17 @@ their turn to get connected.</p>
<p><tt class="docutils literal"><span class="pre">max_half_open_connections()</span></tt> returns the set limit. This limit defaults
to 8 on windows.</p>
</div>
<div class="section" id="load-asnum-db">
<h2>load_asnum_db()</h2>
<div class="section" id="load-asnum-db-load-country-db">
<h2>load_asnum_db() load_country_db()</h2>
<blockquote>
<pre class="literal-block">
bool load_asnum_db(char const* file);
bool load_country_db(char const* file);
</pre>
</blockquote>
<p>This function is not available if <tt class="docutils literal"><span class="pre">TORRENT_DISABLE_GEO_IP</span></tt> is defined. This
expects a path to the <a class="reference external" href="http://www.maxmind.com/app/asnum">MaxMind ASN database</a>. This will be used to look up
which AS peers belong to.</p>
<p>These functions are not available if <tt class="docutils literal"><span class="pre">TORRENT_DISABLE_GEO_IP</span></tt> is defined. They
expects a path to the <a class="reference external" href="http://www.maxmind.com/app/asnum">MaxMind ASN database</a> and <a class="reference external" href="http://www.maxmind.com/app/geolitecountry">MaxMind GeoIP database</a>
respectively. This will be used to look up which AS and country peers belong to.</p>
</div>
<div class="section" id="load-state-state">
<h2>load_state() state()</h2>

View File

@ -133,6 +133,7 @@ The ``session`` class has the following synopsis::
int num_connections() const;
bool load_asnum_db(char const* file);
bool load_country_db(char const* file);
void load_state(entry const& ses_state);
entry state() const;
@ -420,18 +421,20 @@ their turn to get connected.
``max_half_open_connections()`` returns the set limit. This limit defaults
to 8 on windows.
load_asnum_db()
---------------
load_asnum_db() load_country_db()
---------------------------------
::
bool load_asnum_db(char const* file);
bool load_country_db(char const* file);
This function is not available if ``TORRENT_DISABLE_GEO_IP`` is defined. This
expects a path to the `MaxMind ASN database`_. This will be used to look up
which AS peers belong to.
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.
.. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum
.. _`MaxMind GeoIP database`: http://www.maxmind.com/app/geolitecountry
load_state() state()
--------------------

View File

@ -810,6 +810,7 @@ int main(int ac, char* av[])
session ses;
#ifndef TORRENT_DISABLE_GEO_IP
ses.load_asnum_db("GeoIPASNum.dat");
ses.load_country_db("GeoIP.dat");
#endif
// UPnP port mapping
ses.start_upnp();

View File

@ -295,7 +295,11 @@ namespace libtorrent
int as_for_ip(address const& a);
std::pair<const int, int>* lookup_as(int as);
bool load_asnum_db(char const* file);
bool has_asnum_db() const { return m_geoip_db; }
bool has_asnum_db() const { return m_asnum_db; }
bool load_country_db(char const* file);
bool has_country_db() const { return m_country_db; }
char const* country_for_ip(address const& a);
#endif
void load_state(entry const& ses_state);
@ -591,7 +595,8 @@ namespace libtorrent
#endif
#ifndef TORRENT_DISABLE_GEO_IP
GeoIP* m_geoip_db;
GeoIP* m_asnum_db;
GeoIP* m_country_db;
// maps AS number to the peak download rate
// we've seen from it. Entries are never removed

View File

@ -204,6 +204,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_GEO_IP
bool load_asnum_db(char const* file);
bool load_country_db(char const* file);
#endif
void load_state(entry const& ses_state);

View File

@ -133,6 +133,17 @@ namespace libtorrent
TORRENT_ASSERT(peerinfo == 0 || peerinfo->banned == false);
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
std::fill(m_country, m_country + 2, 0);
#ifndef TORRENT_DISABLE_GEO_IP
if (m_ses.has_country_db())
{
char const *country = m_ses.country_for_ip(m_remote.address());
if (country != 0)
{
m_country[0] = country[0];
m_country[1] = country[1];
}
}
#endif
#endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
m_logger = m_ses.create_log(m_remote.address().to_string() + "_"
@ -218,6 +229,17 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
std::fill(m_country, m_country + 2, 0);
#ifndef TORRENT_DISABLE_GEO_IP
if (m_ses.has_country_db())
{
char const *country = m_ses.country_for_ip(m_remote.address());
if (country != 0)
{
m_country[0] = country[0];
m_country[1] = country[1];
}
}
#endif
#endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING

View File

@ -180,6 +180,11 @@ namespace libtorrent
{
return m_impl->load_asnum_db(file);
}
bool session::load_country_db(char const* file)
{
return m_impl->load_country_db(file);
}
#endif
void session::load_state(entry const& ses_state)

View File

@ -174,7 +174,8 @@ namespace aux {
, m_logpath(logpath)
#endif
#ifndef TORRENT_DISABLE_GEO_IP
, m_geoip_db(0)
, m_asnum_db(0)
, m_country_db(0)
#endif
{
m_tcp_mapping[0] = -1;
@ -265,10 +266,16 @@ namespace aux {
};
}
char const* session_impl::country_for_ip(address const& a)
{
if (!a.is_v4() || m_country_db == 0) return 0;
return GeoIP_country_code_by_ipnum(m_country_db, a.to_v4().to_ulong());
}
int session_impl::as_for_ip(address const& a)
{
if (!a.is_v4() || m_geoip_db == 0) return 0;
char* name = GeoIP_name_by_ipnum(m_geoip_db, a.to_v4().to_ulong());
if (!a.is_v4() || m_asnum_db == 0) return 0;
char* name = GeoIP_name_by_ipnum(m_asnum_db, a.to_v4().to_ulong());
if (name == 0) return 0;
free_ptr p(name);
// GeoIP returns the name as AS??? where ? is the AS-number
@ -277,8 +284,8 @@ namespace aux {
std::string session_impl::as_name_for_ip(address const& a)
{
if (!a.is_v4() || m_geoip_db == 0) return std::string();
char* name = GeoIP_name_by_ipnum(m_geoip_db, a.to_v4().to_ulong());
if (!a.is_v4() || m_asnum_db == 0) return std::string();
char* name = GeoIP_name_by_ipnum(m_asnum_db, a.to_v4().to_ulong());
if (name == 0) return std::string();
free_ptr p(name);
char* tmp = std::strchr(name, ' ');
@ -301,9 +308,17 @@ namespace aux {
bool session_impl::load_asnum_db(char const* file)
{
mutex_t::scoped_lock l(m_mutex);
if (m_geoip_db) GeoIP_delete(m_geoip_db);
m_geoip_db = GeoIP_open(file, GEOIP_STANDARD);
return m_geoip_db;
if (m_asnum_db) GeoIP_delete(m_asnum_db);
m_asnum_db = GeoIP_open(file, GEOIP_STANDARD);
return m_asnum_db;
}
bool session_impl::load_country_db(char const* file)
{
mutex_t::scoped_lock l(m_mutex);
if (m_country_db) GeoIP_delete(m_country_db);
m_country_db = GeoIP_open(file, GEOIP_STANDARD);
return m_country_db;
}
#endif
@ -1985,7 +2000,8 @@ namespace aux {
abort();
#ifndef TORRENT_DISABLE_GEO_IP
if (m_geoip_db) GeoIP_delete(m_geoip_db);
if (m_asnum_db) GeoIP_delete(m_asnum_db);
if (m_country_db) GeoIP_delete(m_country_db);
#endif
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
(*m_logger) << time_now_string() << " waiting for main thread\n";