Vidalia  0.3.1
GeoIpDatabase.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 GeoIpDatabase.cpp
13 ** \brief Interface to a local MaxMind GeoIP database
14 */
15 
16 #include "GeoIpDatabase.h"
17 #include "GeoIpRecord.h"
18 #include "Vidalia.h"
19 
20 #include <QString>
21 #include <QHostAddress>
22 
23 
24 /** Default constructor. */
26  : QObject(parent), _db(0)
27 {
28 }
29 
31 {
32  close();
33 }
34 
35 bool
36 GeoIpDatabase::open(const QString &fname)
37 {
38  if (isOpen())
39  close();
40 
41  _db = GeoIP_open(fname.toLocal8Bit().constData(), GEOIP_STANDARD);
42  if (_db) {
43  GeoIP_set_charset(_db, GEOIP_CHARSET_UTF8);
44  return true;
45  }
46  vError("Unable to open local GeoIP database: %1").arg(fname);
47  return false;
48 }
49 
50 void
52 {
53  if (isOpen()) {
54  GeoIP_delete(_db);
55  _db = 0;
56  }
57 }
58 
59 bool
61 {
62  return (_db != 0);
63 }
64 
67 {
68  if (! isOpen())
69  return UnknownDatabase;
70 
71  switch (_db->databaseType) {
72  case GEOIP_COUNTRY_EDITION:
73  case GEOIP_COUNTRY_EDITION_V6:
74  return CountryDatabase;
75 
76  case GEOIP_CITY_EDITION_REV0:
77  case GEOIP_CITY_EDITION_REV1:
78  return CityDatabase;
79 
80  case GEOIP_REGION_EDITION_REV0:
81  case GEOIP_REGION_EDITION_REV1:
82  return RegionDatabase;
83 
84  case GEOIP_ORG_EDITION:
85  return OrganizationDatabase;
86 
87  case GEOIP_ISP_EDITION:
88  return IspDatabase;
89 
90  case GEOIP_PROXY_EDITION:
91  return ProxyDatabase;
92 
93  case GEOIP_ASNUM_EDITION:
94  return AsnDatabase;
95 
96  case GEOIP_NETSPEED_EDITION:
97  return NetSpeedDatabase;
98 
99  case GEOIP_DOMAIN_EDITION:
100  return DomainDatabase;
101 
102  default:
103  return UnknownDatabase;
104  }
105 }
106 
107 QString
108 GeoIpDatabase::countryCodeByAddr(const QHostAddress &ip)
109 {
110  if (isOpen() && ! ip.isNull()) {
111  const char *addr = ip.toString().toAscii().constData();
112  const char *countryCode = GeoIP_country_code_by_addr(_db, addr);
113  if (countryCode)
114  return QString::fromUtf8(countryCode);
115  }
116  return QString();
117 }
118 
120 GeoIpDatabase::recordByAddr(const QHostAddress &ip)
121 {
122  if (isOpen() && ! ip.isNull()) {
123  const char *addr = ip.toString().toAscii().constData();
124 
125  GeoIPRecord *r;
126  if (ip.protocol() == QAbstractSocket::IPv6Protocol)
127  r = GeoIP_record_by_addr_v6(_db, addr);
128  else
129  r = GeoIP_record_by_addr(_db, addr);
130 
131  if (r) {
132  QString countryCode = QString::fromUtf8(r->country_code);
133  QString countryName = QString::fromUtf8(r->country_name);
134  QString city = QString::fromUtf8(r->city);
135 
136  QString region;
137  const char *regionName = GeoIP_region_name_by_code(r->country_code,
138  r->region);
139  if (regionName)
140  region = QString::fromUtf8(regionName);
141 
142  return GeoIpRecord(ip, r->latitude, r->longitude, city, region,
143  countryName, countryCode);
144  }
145  }
146  return GeoIpRecord();
147 }
148 
GeoIpDatabase::DatabaseType type() const
virtual ~GeoIpDatabase()
GeoIpDatabase(QObject *parent=0)
QString countryCodeByAddr(const QHostAddress &ip)
GeoIpRecord recordByAddr(const QHostAddress &ip)
bool open(const QString &fname)
bool isOpen() const
QString toString() const
Definition: GeoIpRecord.cpp:64
#define vError(fmt)
Definition: Vidalia.h:43