support for wide character paths for the GeoIP databases. Fixes #431
This commit is contained in:
parent
8a0fa577b1
commit
7b8663fa8b
|
@ -134,7 +134,9 @@ The ``session`` class has the following synopsis::
|
|||
int num_connections() const;
|
||||
|
||||
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(wchar_t const* file);
|
||||
int as_for_ip(address const& adr);
|
||||
|
||||
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(wchar_t const* file);
|
||||
bool load_country_db(char const* file);
|
||||
bool load_country_db(wchar_t const* file);
|
||||
int as_for_ip(address const& adr);
|
||||
|
||||
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
|
||||
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 GeoIP database`: http://www.maxmind.com/app/geolitecountry
|
||||
|
||||
|
|
|
@ -285,9 +285,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 load_asnum_db(wchar_t const* file);
|
||||
bool has_asnum_db() const { return m_asnum_db; }
|
||||
|
||||
bool load_country_db(char const* file);
|
||||
bool load_country_db(wchar_t const* file);
|
||||
bool has_country_db() const { return m_country_db; }
|
||||
char const* country_for_ip(address const& a);
|
||||
#endif
|
||||
|
|
|
@ -289,7 +289,9 @@ namespace libtorrent
|
|||
#ifndef TORRENT_DISABLE_GEO_IP
|
||||
int as_for_ip(address const& addr);
|
||||
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(wchar_t const* file);
|
||||
#endif
|
||||
|
||||
void load_state(entry const& ses_state);
|
||||
|
|
14
src/GeoIP.c
14
src/GeoIP.c
|
@ -344,7 +344,11 @@ int _check_mtime(GeoIP *gi) {
|
|||
}
|
||||
/* refresh filehandle */
|
||||
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");
|
||||
#endif
|
||||
if (gi->GeoIPDatabase == NULL) {
|
||||
fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path);
|
||||
return -1;
|
||||
|
@ -524,6 +528,12 @@ GeoIP* GeoIP_new (int flags) {
|
|||
return gi;
|
||||
}
|
||||
*/
|
||||
|
||||
namespace libtorrent
|
||||
{
|
||||
std::wstring safe_convert(std::string const& s);
|
||||
}
|
||||
|
||||
GeoIP* GeoIP_open (const char * filename, int flags) {
|
||||
struct stat buf;
|
||||
GeoIP * gi;
|
||||
|
@ -539,7 +549,11 @@ GeoIP* GeoIP_open (const char * filename, int flags) {
|
|||
return NULL;
|
||||
}
|
||||
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");
|
||||
#endif
|
||||
if (gi->GeoIPDatabase == NULL) {
|
||||
fprintf(stderr,"Error Opening file %s\n",filename);
|
||||
free(gi->file_path);
|
||||
|
|
|
@ -231,11 +231,21 @@ namespace libtorrent
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
aux::session_impl::mutex_t::scoped_lock l(m_impl->m_mutex);
|
||||
|
|
|
@ -341,6 +341,16 @@ namespace aux {
|
|||
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)
|
||||
{
|
||||
mutex_t::scoped_lock l(m_mutex);
|
||||
|
@ -349,6 +359,16 @@ namespace aux {
|
|||
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
|
||||
|
||||
void session_impl::load_state(entry const& ses_state)
|
||||
|
|
Loading…
Reference in New Issue