Vidalia 0.2.15
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file GeoIpDatabase.cpp 00013 ** \brief Interface to a local MaxMind GeoIP database 00014 */ 00015 00016 #include "GeoIpDatabase.h" 00017 #include "GeoIpRecord.h" 00018 #include "Vidalia.h" 00019 00020 #include <QString> 00021 #include <QHostAddress> 00022 00023 00024 /** Default constructor. */ 00025 GeoIpDatabase::GeoIpDatabase(QObject *parent) 00026 : QObject(parent), _db(0) 00027 { 00028 } 00029 00030 GeoIpDatabase::~GeoIpDatabase() 00031 { 00032 close(); 00033 } 00034 00035 bool 00036 GeoIpDatabase::open(const QString &fname) 00037 { 00038 if (isOpen()) 00039 close(); 00040 00041 _db = GeoIP_open(fname.toLocal8Bit().constData(), GEOIP_STANDARD); 00042 if (_db) { 00043 GeoIP_set_charset(_db, GEOIP_CHARSET_UTF8); 00044 return true; 00045 } 00046 vError("Unable to open local GeoIP database: %1").arg(fname); 00047 return false; 00048 } 00049 00050 void 00051 GeoIpDatabase::close() 00052 { 00053 if (isOpen()) { 00054 GeoIP_delete(_db); 00055 _db = 0; 00056 } 00057 } 00058 00059 bool 00060 GeoIpDatabase::isOpen() const 00061 { 00062 return (_db != 0); 00063 } 00064 00065 GeoIpDatabase::DatabaseType 00066 GeoIpDatabase::type() const 00067 { 00068 if (! isOpen()) 00069 return UnknownDatabase; 00070 00071 switch (_db->databaseType) { 00072 case GEOIP_COUNTRY_EDITION: 00073 case GEOIP_COUNTRY_EDITION_V6: 00074 return CountryDatabase; 00075 00076 case GEOIP_CITY_EDITION_REV0: 00077 case GEOIP_CITY_EDITION_REV1: 00078 return CityDatabase; 00079 00080 case GEOIP_REGION_EDITION_REV0: 00081 case GEOIP_REGION_EDITION_REV1: 00082 return RegionDatabase; 00083 00084 case GEOIP_ORG_EDITION: 00085 return OrganizationDatabase; 00086 00087 case GEOIP_ISP_EDITION: 00088 return IspDatabase; 00089 00090 case GEOIP_PROXY_EDITION: 00091 return ProxyDatabase; 00092 00093 case GEOIP_ASNUM_EDITION: 00094 return AsnDatabase; 00095 00096 case GEOIP_NETSPEED_EDITION: 00097 return NetSpeedDatabase; 00098 00099 case GEOIP_DOMAIN_EDITION: 00100 return DomainDatabase; 00101 00102 default: 00103 return UnknownDatabase; 00104 } 00105 } 00106 00107 QString 00108 GeoIpDatabase::countryCodeByAddr(const QHostAddress &ip) 00109 { 00110 if (isOpen() && ! ip.isNull()) { 00111 const char *addr = ip.toString().toAscii().constData(); 00112 const char *countryCode = GeoIP_country_code_by_addr(_db, addr); 00113 if (countryCode) 00114 return QString::fromUtf8(countryCode); 00115 } 00116 return QString(); 00117 } 00118 00119 GeoIpRecord 00120 GeoIpDatabase::recordByAddr(const QHostAddress &ip) 00121 { 00122 if (isOpen() && ! ip.isNull()) { 00123 const char *addr = ip.toString().toAscii().constData(); 00124 00125 GeoIPRecord *r; 00126 if (ip.protocol() == QAbstractSocket::IPv6Protocol) 00127 r = GeoIP_record_by_addr_v6(_db, addr); 00128 else 00129 r = GeoIP_record_by_addr(_db, addr); 00130 00131 if (r) { 00132 QString countryCode = QString::fromUtf8(r->country_code); 00133 QString countryName = QString::fromUtf8(r->country_name); 00134 QString city = QString::fromUtf8(r->city); 00135 00136 QString region; 00137 const char *regionName = GeoIP_region_name_by_code(r->country_code, 00138 r->region); 00139 if (regionName) 00140 region = QString::fromUtf8(regionName); 00141 00142 return GeoIpRecord(ip, r->latitude, r->longitude, city, region, 00143 countryName, countryCode); 00144 } 00145 } 00146 return GeoIpRecord(); 00147 } 00148