Vidalia  0.3.1
GeoIpResolver.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file GeoIpResolver.cpp
13 ** \brief Retrieves GeoIP information either from Tor or from a local
14 ** database and returns the result.
15 */
16 
17 #include "GeoIpResolver.h"
18 #include "GeoIpRecord.h"
19 #include "Vidalia.h"
20 
21 
22 /** Default constructor. */
24  : QObject(parent), _useLocalDatabase(false)
25 {
26 }
27 
28 bool
29 GeoIpResolver::setLocalDatabase(const QString &databaseFile)
30 {
31 #if defined(USE_GEOIP)
32  return _database.open(databaseFile);
33 #else
34  return false;
35 #endif
36 }
37 
38 void
39 GeoIpResolver::setUseLocalDatabase(bool useLocalDatabase)
40 {
41  _useLocalDatabase = useLocalDatabase;
42 }
43 
45 GeoIpResolver::resolveUsingTor(const QHostAddress &ip)
46 {
47  QString countryCode = Vidalia::torControl()->ipToCountry(ip);
48  if (! countryCode.isEmpty()) {
49  QPair<float,float> coords = CountryInfo::countryLocation(countryCode);
50  return GeoIpRecord(ip, coords.first, coords.second,
51  CountryInfo::countryName(countryCode),
52  countryCode);
53  }
54  return GeoIpRecord();
55 }
56 
59 {
60 #if defined(USE_GEOIP)
61  if (_database.type() == GeoIpDatabase::CityDatabase) {
62  return _database.recordByAddr(ip);
63  } else {
64  QString countryCode = _database.countryCodeByAddr(ip);
65  if (! countryCode.isEmpty()) {
66  QPair<float,float> coords = CountryInfo::countryLocation(countryCode);
67  return GeoIpRecord(ip, coords.first, coords.second,
68  CountryInfo::countryName(countryCode),
69  countryCode);
70  }
71  }
72 #endif
73  return GeoIpRecord();
74 }
75 
76 /** Resolves a single IP to a geographic location. */
78 GeoIpResolver::resolve(const QHostAddress &ip)
79 {
80 #if defined(USE_GEOIP)
82  return resolveUsingLocalDatabase(ip);
83 #endif
84  return resolveUsingTor(ip);
85 }
86