Vidalia  0.3.1
Circuit.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
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 Circuit.cpp
13 ** \brief Object representing a Tor circuit
14 */
15 
16 #include "tcglobal.h"
17 #include "Circuit.h"
18 
19 #include <QStringList>
20 #include <QRegExp>
21 
22 
23 /** Default constructor. */
25 {
26  _status = Unknown;
27  _isValid = false;
28 }
29 
30 /** Parses the string given in Tor control protocol format for a circuit. The
31  * format is:
32  *
33  * CircuitID SP CircStatus [SP Path]
34  *
35  * If the status is "LAUNCHED", the Path is empty. Server names in the path
36  * must follow Tor's VERBOSE_NAMES format.
37  */
38 Circuit::Circuit(const QString &circuit)
39 {
40  QStringList parts = circuit.split(" ", QString::SkipEmptyParts);
41  if (parts.size() >= 2) {
42  /* Get the circuit ID */
43  _circId = parts.at(0);
45  goto err;
46 
47  /* Get the circuit status value */
48  _status = Circuit::toStatus(parts.at(1));
49 
50  /* Get the circuit path (list of routers) */
51  if (parts.size() > 2 && parts.at(2).startsWith("$")) {
52  foreach (QString hop, parts.at(2).split(",")) {
53  QStringList parts = hop.split(QRegExp("[=~]"));
54  if (parts.size() != 2)
55  goto err;
56 
57  _ids << parts.at(0).mid(1);
58  _names << parts.at(1);
59  }
60  }
61 
62  _isValid = true;
63  }
64  return;
65 
66 err:
67  tc::warn("Improperly formatted circuit: '%1'").arg(circuit);
68  _isValid = false;
69 }
70 
71 /** Returns true iff <b>circId</b> consists of only between 1 and 16
72  * (inclusive) ASCII-encoded letters and numbers. */
73 bool
75 {
76  int length = circId.length();
77  if (length < 1 || length > 16)
78  return false;
79 
80  for (int i = 0; i < length; i++) {
81  char c = circId[i].toAscii();
82  if (c < '0' && c > '9' && c < 'A' && c > 'Z' && c < 'a' && c > 'z')
83  return false;
84  }
85  return true;
86 }
87 
88 /** Converts the circuit status string to its proper enum value */
90 Circuit::toStatus(const QString &status)
91 {
92  if (!status.compare("LAUNCHED", Qt::CaseInsensitive))
93  return Launched;
94  if (!status.compare("BUILT", Qt::CaseInsensitive))
95  return Built;
96  if (!status.compare("EXTENDED", Qt::CaseInsensitive))
97  return Extended;
98  if (!status.compare("FAILED", Qt::CaseInsensitive))
99  return Failed;
100  if (!status.compare("CLOSED", Qt::CaseInsensitive))
101  return Closed;
102  return Unknown;
103 }
104 
105 /** Returns a string representation of the circuit's status. */
106 QString
108 {
109  QString status;
110  switch (_status) {
111  case Launched: status = tr("New"); break;
112  case Built: status = tr("Open"); break;
113  case Extended: status = tr("Building"); break;
114  case Failed: status = tr("Failed"); break;
115  case Closed: status = tr("Closed"); break;
116  default: status = tr("Unknown"); break;
117  }
118  return status;
119 }
120 
bool err(QString *str, const QString &errmsg)
Definition: stringutil.cpp:37
static bool isValidCircuitId(const CircuitId &circId)
Definition: Circuit.cpp:74
QStringList _names
Definition: Circuit.h:73
DebugMessage arg(const QString &a)
Definition: tcglobal.h:48
Status
Definition: Circuit.h:33
QString i(QString str)
Definition: html.cpp:32
static Status toStatus(const QString &strStatus)
Definition: Circuit.cpp:90
CircuitId _circId
Definition: Circuit.h:71
Status _status
Definition: Circuit.h:72
uint length() const
Definition: Circuit.h:57
QStringList _ids
Definition: Circuit.h:74
bool _isValid
Definition: Circuit.h:75
QString CircuitId
Definition: Circuit.h:24
Circuit()
Definition: Circuit.cpp:24
Status status() const
Definition: Circuit.h:53
DebugMessage warn(const QString &fmt)
Definition: tcglobal.cpp:32
QString statusString() const
Definition: Circuit.cpp:107