Vidalia  0.3.1
ControlConnection.h
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
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the 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 ControlConnection.h
13 ** \brief A connection to Tor's control interface, responsible for sending and
14 ** receiving commands and events
15 **/
16 
17 #ifndef _CONTROLCONNECTION_H
18 #define _CONTROLCONNECTION_H
19 
20 #include "ControlSocket.h"
21 #include "TorEvents.h"
22 #include "SendCommandEvent.h"
23 
24 #include <QThread>
25 #include <QMutex>
26 #include <QQueue>
27 #include <QWaitCondition>
28 #include <QTimer>
29 #include <QHostAddress>
30 
31 
32 class ControlConnection : public QThread
33 {
34  Q_OBJECT
35 
36 public:
37  /** Control connection status */
38  enum Status {
39  Unset, /**< Control connection status is not yet set. */
40  Disconnected, /**< Control connection disconnected. */
41  Disconnecting, /**< Control connection is disconnecting. */
42  Connecting, /**< Control connection attempt pending. */
43  Connected /**< Control connection established. */
44  };
45 
46  /** Default constructor. */
48  /** Destructor. */
50 
51  /** Connect to the specified Tor control interface. */
52  void connect(const QHostAddress &addr, quint16 port);
53  /** Connect to the specified Tor control socket interface. */
54  void connect(const QString &addr);
55  /** Cancels a pending control connection to Tor. */
56  void cancelConnect();
57  /** Disconnect from Tor's control interface. */
58  void disconnect();
59  /** Returns true if the control socket is connected to Tor. */
60  bool isConnected();
61  /** Returns the status of the control connection. */
62  Status status();
63  /** Sends a control command to Tor and waits for the reply. */
64  bool send(const ControlCommand &cmd, ControlReply &reply, QString *errmsg = 0);
65  /** Sends a control command to Tor and does not wait for a reply. */
66  bool send(const ControlCommand &cmd, QString *errmsg = 0);
67 
68 signals:
69  /** Emitted when a control connection has been established. */
70  void connected();
71  /** Emitted when a control connection has been closed. */
72  void disconnected();
73  /** Emitted when a control connection fails. */
74  void connectFailed(QString errmsg);
75 
76 private slots:
77  /** Connects to Tor's control interface. */
78  void connect();
79  /** Called when there is data on the control socket. */
80  void onReadyRead();
81  /** Called when the control socket is connected. */
82  void onConnected();
83  /** Called when the control socket is disconnected. */
84  void onDisconnected();
85  /** Called when the control socket encounters an error. */
86  void onError(QAbstractSocket::SocketError error);
87 
88 private:
89  /** Sets the control connection status. */
90  void setStatus(Status status);
91  /** Returns the string description of <b>status</b>. */
92  QString statusString(Status status);
93  /** Main thread implementation. */
94  void run();
95 
96  ControlSocket* _sock; /**< Socket used to communicate with Tor. */
97  ControlMethod::Method _method; /** Method used to communicate with Tor. */
98  QString _path; /**< Path to the socket */
99  TorEvents* _events; /**< Dispatches asynchronous events from Tor. */
100  Status _status; /**< Status of the control connection. */
101  QHostAddress _addr; /**< Address of Tor's control interface. */
102  quint16 _port; /**< Port of Tor's control interface. */
103  QMutex _connMutex; /**< Mutex around the control socket. */
104  QMutex _recvMutex; /**< Mutex around the queue of ReceiveWaiters. */
105  QMutex _statusMutex; /**< Mutex around the connection status value. */
106  int _connectAttempt; /**< How many times we've tried to connect to Tor while
107  waiting for Tor to start. */
108  QTimer* _connectTimer; /**< Timer used to delay connect attempts. */
109 
110  /** Private class used to wait for a response to a control command. */
112  public:
113  /** Default constructor. */
115  /** Waits for and gets the reply from a control command. */
116  bool getResult(ControlReply *reply, QString *errmsg = 0);
117  /** Sets the result and reply from a control command. */
118  void setResult(bool success, const ControlReply &reply,
119  const QString &errmsg = QString());
120  private:
121  /** Status of the receive waiter. */
123  ControlReply _reply; /**< Reply to a previous command. */
124  QMutex _mutex; /**< Mutex around the wait condition. */
125  QWaitCondition _waitCond; /**< Waits for a control rpely. */
126  QString _errmsg; /**< Error message if the reply fails. */
127  };
128  QQueue<ReceiveWaiter *> _recvQueue; /**< Objects waiting for a reply. */
130 };
131 
132 #endif
133