Vidalia 0.2.10

GeoIpDatabase.cpp

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