torcontrol.h

Go to the documentation of this file.
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 torcontrol.h
00024  * \version $Id: torcontrol.h 1261 2006-10-03 06:29:35Z edmanm $
00025  * \brief Object for interacting with the Tor process and control interface
00026  */
00027 
00028 #ifndef _TORCONTROL_H
00029 #define _TORCONTROL_H
00030 
00031 #include <QObject>
00032 #include <QHash>
00033 #include <QList>
00034 
00035 #include "controlconnection.h"
00036 #include "torprocess.h"
00037 #include "torservice.h"
00038 #include "torevents.h"
00039 #include "torsignal.h"
00040 #include "routerdescriptor.h"
00041 
00042 class TorControl : public QObject
00043 {
00044   Q_OBJECT
00045   
00046 public:
00047   /** Default constructor */
00048   TorControl();
00049   /** Default destructor */
00050   ~TorControl();
00051 
00052   /** Start the Tor process */
00053   void start();
00054   /** Stop the Tor process */
00055   bool stop(QString *errmsg = 0);
00056   /** Detect if the Tor process is running */
00057   bool isRunning();
00058   /** Detects if the Tor process is running under Vidalia. */
00059   bool isVidaliaRunningTor();
00060   
00061   /** Connect to Tor's control socket */
00062   void connect();
00063   /** Disconnect from Tor's control socket */
00064   void disconnect();
00065   /** Check if we're connected to Tor's control socket */
00066   bool isConnected();
00067   /** Sends an authentication token to Tor */
00068   bool authenticate(QString *errmsg = 0);
00069 
00070   /** Sends a GETINFO message to Tor based on the given keys */
00071   bool getInfo(QHash<QString,QString> &map, QString *errmsg = 0);
00072   /** Sends a GETINFO message for a single info value to Tor */
00073   bool getInfo(QString key, QString &val, QString *errmsg = 0);
00074 
00075   /** Sends a signal to Tor */
00076   bool signal(TorSignal::Signal sig, QString *errmsg = 0);
00077   
00078   /** Returns Tor's version as a string. */
00079   QString getTorVersionString();
00080   /** Returns Tor's version as a numeric value. */
00081   quint32 getTorVersion();
00082 
00083   /** Sets an event and its handler. If add is true, then the event is added,
00084    * otherwise it is removed. If set is true, then the given event will be
00085    * registered with Tor. */
00086   bool setEvent(TorEvents::TorEvent e, QObject *obj, 
00087                 bool add, bool set = true, QString *errmsg = 0);
00088   /** Registers for a set of logging events according to the given filter. */
00089   bool setLogEvents(uint filter, QObject *obj, QString *errmsg = 0);
00090   /** Register events of interest with Tor */
00091   bool setEvents(QString *errmsg = 0);
00092   
00093 
00094   /** Sets each configuration key in <b>map</b> to the value associated with its key. */
00095   bool setConf(QHash<QString,QString> map, QString *errmsg = 0);
00096   /** Sets a single configuration key to the given value. */
00097   bool setConf(QString key, QString value, QString *errmsg = 0);
00098   /** Gets a set of configuration keyvalues and stores them in <b>map</b>. */
00099   bool getConf(QHash<QString,QString> &map, QString *errmsg = 0);
00100   /** Gets a single configuration keyvalue. */
00101   bool getConf(QString key, QString &value, QString *errmsg = 0);
00102   /** Asks Tor to save the current configuration to its torrc */
00103   bool saveConf(QString *errmsg = 0);
00104   /** Tells Tor to reset the given configuration keys back to defaults. */
00105   bool resetConf(QStringList keys, QString *errmsg = 0);
00106   /** Tells Tor to reset a configuration key back to its default value. */
00107   bool resetConf(QString key, QString *errmsg = 0);
00108 
00109   /** Gets a descriptor for the given router name. */
00110   RouterDescriptor getDescriptorByName(QString name, QString *errmsg = 0);
00111   /** Gets a descriptor for the given router ID. */
00112   RouterDescriptor getDescriptorById(QString id, QString *errmsg = 0);
00113   /** Gets descriptors for the given list of router names. */
00114   QList<RouterDescriptor> getDescriptorListByName(QStringList names, QString *errmsg = 0);
00115   /** Gets descriptors for the given list of router IDs. */
00116   QList<RouterDescriptor> getDescriptorListById(QStringList ids, QString *errmsg = 0);
00117   /** Gets a list of descriptors for all routers Tor knows about. */
00118   QList<RouterDescriptor> getRouterList(QString *errmsg = 0);
00119   /** Gets a list of router IDs for all routers Tor knows about. */
00120   QStringList getRouterIDList(QString *errmsg = 0);
00121 
00122   /** Gets a list of current circuits. */
00123   QList<Circuit> getCircuits(QString *errmsg = 0);
00124   /** Gets a list of current streams. */
00125   QList<Stream> getStreams(QString *errmsg = 0);
00126 
00127 signals:
00128   /** Emitted when the Tor process has started */
00129   void started();
00130   /** Emitted when the Tor process fails to start. */
00131   void startFailed(QString errmsg);
00132   /** Emitted when the Tor process has stopped */
00133   void stopped(int exitCode, QProcess::ExitStatus exitStatus);
00134   /** Emitted when the controller has connected to Tor */
00135   void connected();
00136   /** Emitted when the controller failed to connect to Tor. */
00137   void connectFailed(QString errmsg);
00138   /** Emitted when the controller has disconnected from Tor */
00139   void disconnected();
00140   /** Emitted when the connection status changes. */
00141   void connected(bool connected);
00142 
00143 private:
00144   /** Instantiates a connection used to talk to Tor's control port */
00145   ControlConnection* _controlConn;
00146   /** Manages and monitors the Tor process */
00147   TorProcess* _torProcess;
00148   /** Manages the Tor service, if supported and enabled */
00149   TorService* _torService;
00150   /** Keep track of which events we're interested in */
00151   TorEvents _torEvents;
00152   /** The version of Tor we're currently talking to. */
00153   QString _torVersion;
00154   
00155   /** Send a message to Tor and read the response */
00156   bool send(ControlCommand cmd, ControlReply &reply, QString *errmsg = 0);
00157   /** Send a message to Tor and discard the response */
00158   bool send(ControlCommand cmd, QString *errmsg = 0);
00159   /** Disconnects signals from the TorProcess and frees its memory. */
00160   void closeTorProcess();
00161   
00162 /* The slots below simply relay signals from the appropriate member objects */
00163 private slots:
00164   void onStarted();
00165   void onStartFailed(QString errmsg);
00166   void onStopped(int exitCode, QProcess::ExitStatus exitStatus);
00167   void onConnected();
00168   void onConnectFailed(QString errmsg);
00169   void onDisconnected();
00170   void onLogStdout(QString severity, QString message);
00171 };
00172 
00173 #endif
00174 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0