00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file geoipresolver.h 00024 * \version $Id: geoipresolver.h 1563 2006-12-26 06:06:04Z edmanm $ 00025 * \brief Requests GeoIP information and caches the result 00026 */ 00027 00028 #ifndef _GEOIPRESOLVER_H 00029 #define _GEOIPRESOLVER_H 00030 00031 #include <QObject> 00032 #include <QList> 00033 #include <QHash> 00034 #include <QString> 00035 #include <QHostAddress> 00036 #include <util/torsocket.h> 00037 00038 #include "geoip.h" 00039 #include "geoipcache.h" 00040 #include "geoiprequest.h" 00041 #include "geoipresponse.h" 00042 00043 00044 class GeoIpResolver : public QObject 00045 { 00046 Q_OBJECT 00047 00048 public: 00049 /** Default constructor. */ 00050 GeoIpResolver() 00051 { _socksAddr = QHostAddress::LocalHost; _socksPort = 9050; } 00052 00053 /** Sets the address and port of Tor, through which GeoIP requests will be 00054 * made. */ 00055 void setSocksHost(QHostAddress addr, quint16 port); 00056 /** Resolves a single IP to a geographic location. */ 00057 int resolve(QHostAddress ip); 00058 /** Resolves a list of IPs to a geographic location. */ 00059 int resolve(QList<QHostAddress> ips); 00060 /** Resolves <b>ip</b> to geographic information only if it is cached. */ 00061 bool resolveFromCache(QHostAddress ip); 00062 /** Resolves a list of IPs to a geographic location, but only those which 00063 * are cached. Returns a list of which IPs were not cached. */ 00064 QList<QHostAddress> resolveFromCache(QList<QHostAddress> ips); 00065 00066 signals: 00067 /** Emitted when a list of IPs have been resolved to lat/long. */ 00068 void resolved(int id, QList<GeoIp> geoips); 00069 /** Emitted when a resolve has failed. */ 00070 void resolveFailed(int id, QString errorString); 00071 00072 private slots: 00073 /** Called when the socket has connected to the Geo IP host. */ 00074 void connected(); 00075 /** Called when the socket has disconnected from the Geo IP host. */ 00076 void disconnected(); 00077 /** Called when an error has occurred getting the Geo IP information. */ 00078 void socketError(QString errorString); 00079 00080 private: 00081 /** Creates an HTTP request for Geo IP information. */ 00082 GeoIpRequest* createRequest(QList<QHostAddress> ips); 00083 /** Creates a socket used to request Geo IP information over Tor. */ 00084 TorSocket* createRequestSocket(); 00085 00086 /**< Cached GeoIp objects. */ 00087 GeoIpCache _cache; 00088 /**< List of sockets used for requests. */ 00089 QHash<TorSocket *,GeoIpRequest*> _requestList; 00090 /** Tor's SocksListenAddress. */ 00091 QHostAddress _socksAddr; 00092 /** Tor's SocksPort. */ 00093 quint16 _socksPort; 00094 }; 00095 00096 #endif 00097