support for wide character paths for the GeoIP databases. Fixes #431

This commit is contained in:
Arvid Norberg 2008-12-21 08:48:06 +00:00
parent 8a0fa577b1
commit 7b8663fa8b
6 changed files with 54 additions and 0 deletions

View File

@ -134,7 +134,9 @@ The ``session`` class has the following synopsis::
int num_connections() const; int num_connections() const;
bool load_asnum_db(char const* file); bool load_asnum_db(char const* file);
bool load_asnum_db(wchar_t const* file);
bool load_country_db(char const* file); bool load_country_db(char const* file);
bool load_country_db(wchar_t const* file);
int as_for_ip(address const& adr); int as_for_ip(address const& adr);
void load_state(entry const& ses_state); void load_state(entry const& ses_state);
@ -470,7 +472,9 @@ load_asnum_db() load_country_db() int as_for_ip()
:: ::
bool load_asnum_db(char const* file); bool load_asnum_db(char const* file);
bool load_asnum_db(wchar_t const* file);
bool load_country_db(char const* file); bool load_country_db(char const* file);
bool load_country_db(wchar_t const* file);
int as_for_ip(address const& adr); int as_for_ip(address const& adr);
These functions are not available if ``TORRENT_DISABLE_GEO_IP`` is defined. They These functions are not available if ``TORRENT_DISABLE_GEO_IP`` is defined. They
@ -480,6 +484,8 @@ 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 ``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. in the database or the ASN database is not loaded, 0 is returned.
The ``wchar_t`` overloads are for wide character paths.
.. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum .. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum
.. _`MaxMind GeoIP database`: http://www.maxmind.com/app/geolitecountry .. _`MaxMind GeoIP database`: http://www.maxmind.com/app/geolitecountry

View File

@ -285,9 +285,11 @@ namespace libtorrent
int as_for_ip(address const& a); int as_for_ip(address const& a);
std::pair<const int, int>* lookup_as(int as); std::pair<const int, int>* lookup_as(int as);
bool load_asnum_db(char const* file); bool load_asnum_db(char const* file);
bool load_asnum_db(wchar_t const* file);
bool has_asnum_db() const { return m_asnum_db; } bool has_asnum_db() const { return m_asnum_db; }
bool load_country_db(char const* file); bool load_country_db(char const* file);
bool load_country_db(wchar_t const* file);
bool has_country_db() const { return m_country_db; } bool has_country_db() const { return m_country_db; }
char const* country_for_ip(address const& a); char const* country_for_ip(address const& a);
#endif #endif

View File

@ -289,7 +289,9 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_GEO_IP #ifndef TORRENT_DISABLE_GEO_IP
int as_for_ip(address const& addr); int as_for_ip(address const& addr);
bool load_asnum_db(char const* file); bool load_asnum_db(char const* file);
bool load_asnum_db(wchar_t const* file);
bool load_country_db(char const* file); bool load_country_db(char const* file);
bool load_country_db(wchar_t const* file);
#endif #endif
void load_state(entry const& ses_state); void load_state(entry const& ses_state);

View File

@ -344,7 +344,11 @@ int _check_mtime(GeoIP *gi) {
} }
/* refresh filehandle */ /* refresh filehandle */
fclose(gi->GeoIPDatabase); fclose(gi->GeoIPDatabase);
#ifdef WIN32
gi->GeoIPDatabase = _wfopen(libtorrent::safe_convert(gi->file_path).c_str(),L"rb");
#else
gi->GeoIPDatabase = fopen(gi->file_path,"rb"); gi->GeoIPDatabase = fopen(gi->file_path,"rb");
#endif
if (gi->GeoIPDatabase == NULL) { if (gi->GeoIPDatabase == NULL) {
fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path); fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path);
return -1; return -1;
@ -524,6 +528,12 @@ GeoIP* GeoIP_new (int flags) {
return gi; return gi;
} }
*/ */
namespace libtorrent
{
std::wstring safe_convert(std::string const& s);
}
GeoIP* GeoIP_open (const char * filename, int flags) { GeoIP* GeoIP_open (const char * filename, int flags) {
struct stat buf; struct stat buf;
GeoIP * gi; GeoIP * gi;
@ -539,7 +549,11 @@ GeoIP* GeoIP_open (const char * filename, int flags) {
return NULL; return NULL;
} }
strncpy(gi->file_path, filename, len); strncpy(gi->file_path, filename, len);
#ifdef WIN32
gi->GeoIPDatabase = _wfopen(libtorrent::safe_convert(filename).c_str(),L"rb");
#else
gi->GeoIPDatabase = fopen(filename,"rb"); gi->GeoIPDatabase = fopen(filename,"rb");
#endif
if (gi->GeoIPDatabase == NULL) { if (gi->GeoIPDatabase == NULL) {
fprintf(stderr,"Error Opening file %s\n",filename); fprintf(stderr,"Error Opening file %s\n",filename);
free(gi->file_path); free(gi->file_path);

View File

@ -231,11 +231,21 @@ namespace libtorrent
return m_impl->load_asnum_db(file); return m_impl->load_asnum_db(file);
} }
bool session::load_asnum_db(wchar_t const* file)
{
return m_impl->load_asnum_db(file);
}
bool session::load_country_db(char const* file) bool session::load_country_db(char const* file)
{ {
return m_impl->load_country_db(file); return m_impl->load_country_db(file);
} }
bool session::load_country_db(wchar_t const* file)
{
return m_impl->load_country_db(file);
}
int session::as_for_ip(address const& addr) int session::as_for_ip(address const& addr)
{ {
aux::session_impl::mutex_t::scoped_lock l(m_impl->m_mutex); aux::session_impl::mutex_t::scoped_lock l(m_impl->m_mutex);

View File

@ -341,6 +341,16 @@ namespace aux {
return m_asnum_db; return m_asnum_db;
} }
bool session_impl::load_asnum_db(wchar_t const* file)
{
mutex_t::scoped_lock l(m_mutex);
if (m_asnum_db) GeoIP_delete(m_asnum_db);
std::string utf8;
wchar_utf8(file, utf8);
m_asnum_db = GeoIP_open(utf8, GEOIP_STANDARD);
return m_asnum_db;
}
bool session_impl::load_country_db(char const* file) bool session_impl::load_country_db(char const* file)
{ {
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
@ -349,6 +359,16 @@ namespace aux {
return m_country_db; return m_country_db;
} }
bool session_impl::load_country_db(wchar_t const* file)
{
mutex_t::scoped_lock l(m_mutex);
if (m_country_db) GeoIP_delete(m_country_db);
std::string utf8;
wchar_utf8(file, utf8);
m_country_db = GeoIP_open(utf8, GEOIP_STANDARD);
return m_country_db;
}
#endif #endif
void session_impl::load_state(entry const& ses_state) void session_impl::load_state(entry const& ses_state)