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 
Circuit::Status
Status
Definition: Circuit.h:33
Circuit::_names
QStringList _names
Definition: Circuit.h:73
Circuit::statusString
QString statusString() const
Definition: Circuit.cpp:107
Circuit::status
Status status() const
Definition: Circuit.h:53
err
bool err(QString *str, const QString &errmsg)
Definition: stringutil.cpp:37
Circuit::Failed
@ Failed
Definition: Circuit.h:38
Circuit::Circuit
Circuit()
Definition: Circuit.cpp:24
Circuit::Closed
@ Closed
Definition: Circuit.h:39
i
QString i(QString str)
Definition: html.cpp:32
Circuit::Extended
@ Extended
Definition: Circuit.h:37
tcglobal.h
Circuit::length
uint length() const
Definition: Circuit.h:57
Circuit::Built
@ Built
Definition: Circuit.h:36
tc::warn
DebugMessage warn(const QString &fmt)
Definition: tcglobal.cpp:32
tc::DebugMessage::arg
DebugMessage arg(const QString &a)
Definition: tcglobal.h:48
Circuit::Launched
@ Launched
Definition: Circuit.h:35
Circuit::_ids
QStringList _ids
Definition: Circuit.h:74
Circuit::_status
Status _status
Definition: Circuit.h:72
Circuit::Unknown
@ Unknown
Definition: Circuit.h:34
Circuit::toStatus
static Status toStatus(const QString &strStatus)
Definition: Circuit.cpp:90
Circuit.h
Circuit::isValidCircuitId
static bool isValidCircuitId(const CircuitId &circId)
Definition: Circuit.cpp:74
Circuit::_isValid
bool _isValid
Definition: Circuit.h:75
Circuit::_circId
CircuitId _circId
Definition: Circuit.h:71
CircuitId
QString CircuitId
Definition: Circuit.h:24