Vidalia 0.2.10
|
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 GeoIpResolver.cpp 00013 ** \version $Id: GeoIpResolver.cpp 4378 2010-08-05 20:28:54Z edmanm $ 00014 ** \brief Retrieves GeoIP information either from Tor or from a local 00015 ** database and returns the result. 00016 */ 00017 00018 #include "GeoIpResolver.h" 00019 #include "GeoIpRecord.h" 00020 #include "Vidalia.h" 00021 00022 00023 /** Default constructor. */ 00024 GeoIpResolver::GeoIpResolver(QObject *parent) 00025 : QObject(parent), _useLocalDatabase(false) 00026 { 00027 } 00028 00029 bool 00030 GeoIpResolver::setLocalDatabase(const QString &databaseFile) 00031 { 00032 #if defined(USE_GEOIP) 00033 return _database.open(databaseFile); 00034 #else 00035 return false; 00036 #endif 00037 } 00038 00039 void 00040 GeoIpResolver::setUseLocalDatabase(bool useLocalDatabase) 00041 { 00042 _useLocalDatabase = useLocalDatabase; 00043 } 00044 00045 GeoIpRecord 00046 GeoIpResolver::resolveUsingTor(const QHostAddress &ip) 00047 { 00048 QString countryCode = Vidalia::torControl()->ipToCountry(ip); 00049 if (! countryCode.isEmpty()) { 00050 QPair<float,float> coords = CountryInfo::countryLocation(countryCode); 00051 return GeoIpRecord(ip, coords.first, coords.second, 00052 CountryInfo::countryName(countryCode), 00053 countryCode); 00054 } 00055 return GeoIpRecord(); 00056 } 00057 00058 GeoIpRecord 00059 GeoIpResolver::resolveUsingLocalDatabase(const QHostAddress &ip) 00060 { 00061 #if defined(USE_GEOIP) 00062 if (_database.type() == GeoIpDatabase::CityDatabase) { 00063 return _database.recordByAddr(ip); 00064 } else { 00065 QString countryCode = _database.countryCodeByAddr(ip); 00066 if (! countryCode.isEmpty()) { 00067 QPair<float,float> coords = CountryInfo::countryLocation(countryCode); 00068 return GeoIpRecord(ip, coords.first, coords.second, 00069 CountryInfo::countryName(countryCode), 00070 countryCode); 00071 } 00072 } 00073 #endif 00074 return GeoIpRecord(); 00075 } 00076 00077 /** Resolves a single IP to a geographic location. */ 00078 GeoIpRecord 00079 GeoIpResolver::resolve(const QHostAddress &ip) 00080 { 00081 #if defined(USE_GEOIP) 00082 if (_useLocalDatabase) 00083 return resolveUsingLocalDatabase(ip); 00084 #endif 00085 return resolveUsingTor(ip); 00086 } 00087