[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r4377: Add a GeoIpDatabase wrapper class that provides an interface (vidalia/trunk/src/vidalia/network)
Author: edmanm
Date: 2010-08-05 16:26:44 -0400 (Thu, 05 Aug 2010)
New Revision: 4377
Added:
vidalia/trunk/src/vidalia/network/GeoIpDatabase.cpp
vidalia/trunk/src/vidalia/network/GeoIpDatabase.h
Log:
Add a GeoIpDatabase wrapper class that provides an interface to a local
GeoIP database using the MaxMind C library.
Added: vidalia/trunk/src/vidalia/network/GeoIpDatabase.cpp
===================================================================
--- vidalia/trunk/src/vidalia/network/GeoIpDatabase.cpp (rev 0)
+++ vidalia/trunk/src/vidalia/network/GeoIpDatabase.cpp 2010-08-05 20:26:44 UTC (rev 4377)
@@ -0,0 +1,149 @@
+/*
+** This file is part of Vidalia, and is subject to the license terms in the
+** LICENSE file, found in the top level directory of this distribution. If you
+** did not receive the LICENSE file with this file, you may obtain it from the
+** Vidalia source package distributed by the Vidalia Project at
+** http://www.vidalia-project.net/. No part of Vidalia, including this file,
+** may be copied, modified, propagated, or distributed except according to the
+** terms described in the LICENSE file.
+*/
+
+/*
+** \file GeoIpDatabase.cpp
+** \version $Id$
+** \brief Interface to a local MaxMind GeoIP database
+*/
+
+#include "GeoIpDatabase.h"
+#include "GeoIpRecord.h"
+#include "Vidalia.h"
+
+#include <QString>
+#include <QHostAddress>
+
+
+/** Default constructor. */
+GeoIpDatabase::GeoIpDatabase(QObject *parent)
+ : QObject(parent), _db(0)
+{
+}
+
+GeoIpDatabase::~GeoIpDatabase()
+{
+ close();
+}
+
+bool
+GeoIpDatabase::open(const QString &fname)
+{
+ if (isOpen())
+ close();
+
+ _db = GeoIP_open(fname.toLocal8Bit().constData(), GEOIP_STANDARD);
+ if (_db) {
+ GeoIP_set_charset(_db, GEOIP_CHARSET_UTF8);
+ return true;
+ }
+ vError("Unable to open local GeoIP database: %1").arg(fname);
+ return false;
+}
+
+void
+GeoIpDatabase::close()
+{
+ if (isOpen()) {
+ GeoIP_delete(_db);
+ _db = 0;
+ }
+}
+
+bool
+GeoIpDatabase::isOpen() const
+{
+ return (_db != 0);
+}
+
+GeoIpDatabase::DatabaseType
+GeoIpDatabase::type() const
+{
+ if (! isOpen())
+ return UnknownDatabase;
+
+ switch (_db->databaseType) {
+ case GEOIP_COUNTRY_EDITION:
+ case GEOIP_COUNTRY_EDITION_V6:
+ return CountryDatabase;
+
+ case GEOIP_CITY_EDITION_REV0:
+ case GEOIP_CITY_EDITION_REV1:
+ return CityDatabase;
+
+ case GEOIP_REGION_EDITION_REV0:
+ case GEOIP_REGION_EDITION_REV1:
+ return RegionDatabase;
+
+ case GEOIP_ORG_EDITION:
+ return OrganizationDatabase;
+
+ case GEOIP_ISP_EDITION:
+ return IspDatabase;
+
+ case GEOIP_PROXY_EDITION:
+ return ProxyDatabase;
+
+ case GEOIP_ASNUM_EDITION:
+ return AsnDatabase;
+
+ case GEOIP_NETSPEED_EDITION:
+ return NetSpeedDatabase;
+
+ case GEOIP_DOMAIN_EDITION:
+ return DomainDatabase;
+
+ default:
+ return UnknownDatabase;
+ }
+}
+
+QString
+GeoIpDatabase::countryCodeByAddr(const QHostAddress &ip)
+{
+ if (isOpen() && ! ip.isNull()) {
+ const char *addr = ip.toString().toAscii().constData();
+ const char *countryCode = GeoIP_country_code_by_addr(_db, addr);
+ if (countryCode)
+ return QString::fromUtf8(countryCode);
+ }
+ return QString();
+}
+
+GeoIpRecord
+GeoIpDatabase::recordByAddr(const QHostAddress &ip)
+{
+ if (isOpen() && ! ip.isNull()) {
+ const char *addr = ip.toString().toAscii().constData();
+
+ GeoIPRecord *r;
+ if (ip.protocol() == QAbstractSocket::IPv6Protocol)
+ r = GeoIP_record_by_addr_v6(_db, addr);
+ else
+ r = GeoIP_record_by_addr(_db, addr);
+
+ if (r) {
+ QString countryCode = QString::fromUtf8(r->country_code);
+ QString countryName = QString::fromUtf8(r->country_name);
+ QString city = QString::fromUtf8(r->city);
+
+ QString region;
+ const char *regionName = GeoIP_region_name_by_code(r->country_code,
+ r->region);
+ if (regionName)
+ region = QString::fromUtf8(regionName);
+
+ return GeoIpRecord(ip, r->latitude, r->longitude, city, region,
+ countryName, countryCode);
+ }
+ }
+ return GeoIpRecord();
+}
+
Property changes on: vidalia/trunk/src/vidalia/network/GeoIpDatabase.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: vidalia/trunk/src/vidalia/network/GeoIpDatabase.h
===================================================================
--- vidalia/trunk/src/vidalia/network/GeoIpDatabase.h (rev 0)
+++ vidalia/trunk/src/vidalia/network/GeoIpDatabase.h 2010-08-05 20:26:44 UTC (rev 4377)
@@ -0,0 +1,100 @@
+/*
+** This file is part of Vidalia, and is subject to the license terms in the
+** LICENSE file, found in the top level directory of this distribution. If you
+** did not receive the LICENSE file with this file, you may obtain it from the
+** Vidalia source package distributed by the Vidalia Project at
+** http://www.vidalia-project.net/. No part of Vidalia, including this file,
+** may be copied, modified, propagated, or distributed except according to the
+** terms described in the LICENSE file.
+*/
+
+/*
+** \file GeoIpDatabase.h
+** \version $Id$
+** \brief Interface to a local MaxMind GeoIP database.
+*/
+
+#ifndef _GEOIPDATABASE_H
+#define _GEOIPDATABASE_H
+
+#include <GeoIP.h>
+#include <GeoIPCity.h>
+
+#include <QObject>
+
+class QString;
+class QHostAddress;
+class GeoIpRecord;
+
+
+class GeoIpDatabase : public QObject
+{
+ Q_OBJECT
+
+public:
+ enum DatabaseType {
+ UnknownDatabase = 0,
+ CountryDatabase,
+ CityDatabase,
+ RegionDatabase,
+ OrganizationDatabase,
+ IspDatabase,
+ ProxyDatabase,
+ AsnDatabase,
+ NetSpeedDatabase,
+ DomainDatabase
+ };
+
+ /** Default constructor.
+ */
+ GeoIpDatabase(QObject *parent = 0);
+
+ /** Virtual destructor. Closes the database if it is currently open.
+ */
+ virtual ~GeoIpDatabase();
+
+ /** Open the GeoIP database file <b>fname</b> and return true if
+ * successful. Otherwise, return false. If a different database file is
+ * already open, the open database will be closed before the new one is
+ * opened.
+ * \sa close()
+ * \sa isOpen()
+ */
+ bool open(const QString &fname);
+
+ /** Closes an open dataase, or does nothing if no database file is
+ * currently open.
+ * \sa open()
+ * \sa isOpen()
+ */
+ void close();
+
+ /** Return true if this object has a currently open GeoIP database.
+ * \sa open()
+ */
+ bool isOpen() const;
+
+ /** Returns the DatabaseType enum value corresponding to the current
+ * database type. If no database is open, this will simply return
+ * UnknownDatabase.
+ */
+ GeoIpDatabase::DatabaseType type() const;
+
+ /** Resolves the IP address <b>ip</b> to its two-letter ISO-3166 country
+ * code and returns the result on success. On failure, this returns a
+ * default-constructed QString.
+ */
+ QString countryCodeByAddr(const QHostAddress &ip);
+
+ /** Resolves the IP address <b>ip</b> to an approximate geographic
+ * location and returns the result on success. On failure, this returns
+ * a default-constructed QString.
+ */
+ GeoIpRecord recordByAddr(const QHostAddress &ip);
+
+private:
+ GeoIP *_db; /**< Pointer to the local GeoIP database object. */
+};
+
+#endif
+
Property changes on: vidalia/trunk/src/vidalia/network/GeoIpDatabase.h
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native