Vidalia  0.2.21
RouterDescriptor.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 RouterDescriptor.cpp
13 ** \brief Parses a blob of router descriptor text from Tor
14 */
15 
16 #include "RouterDescriptor.h"
17 
18 #include <QtGlobal>
19 
20 
21 /** Constructor. Just assigns the ID and determines whether the router is
22  * responsive or not based on the presence of a "!" at the start of the ID.
23  * See tor-spec.txt for details. */
24 RouterDescriptor::RouterDescriptor(QStringList descriptor, bool microdesc)
25  : _microdesc(microdesc), _observedBandwidth(0), _uptime(0) {
26  _status = Online;
27  parseDescriptor(descriptor);
28 }
29 
30 /** Parses this router's descriptor for relevant information. */
31 void
32 RouterDescriptor::parseDescriptor(QStringList descriptor)
33 {
34  if(_microdesc) {
35  bool key = false;
36  foreach(QString line, descriptor) {
37  if(line.startsWith("onion-key")) {
38  key = true;
39  } else if(line.startsWith("p ")) {
40  _exitPolicy = line.remove(0,qstrlen("p "));
41  } else if(line.startsWith("family ")) {
42  _family = line.remove(0,qstrlen("family "));
43  }
44 
45  if(key)
46  _onionKey += line;
47  if(line.startsWith("-----END RSA PUBLIC KEY-----"))
48  key = false;
49  }
50  } else {
51  foreach (QString line, descriptor) {
52  if (line.startsWith("router ")) {
53  QStringList parts = line.remove(0,qstrlen("router ")).split(" ");
54  _name = parts.at(0);
55  _ip = QHostAddress(parts.at(1));
56  _orPort = (quint16)parts.at(2).toUInt();
57  _dirPort = (quint16)parts.at(4).toUInt();
58  } else if (line.startsWith("platform ")) {
59  _platform = line.remove(0,qstrlen("platform "));
60  } else if (line.startsWith("published ")) {
61  _published = QDateTime::fromString(
62  line.remove(0,qstrlen("published ")),
63  "yyyy-MM-dd HH:mm:ss");
64  _published.setTimeSpec(Qt::UTC);
65  } else if (line.startsWith("opt fingerprint ")) {
66  _fingerprint = line.remove(0,qstrlen("opt fingerprint "));
67  _id = _fingerprint.remove(" ");
68  } else if (line.startsWith("fingerprint ")) {
69  _fingerprint = line.remove(0,qstrlen("fingerprint "));
70  _id = _fingerprint.remove(" ");
71  } else if (line.startsWith("uptime ")) {
72  _uptime = (quint64)line.remove(0,qstrlen("uptime ")).toULongLong();
73  } else if (line.startsWith("bandwidth ")) {
74  QStringList bw = line.remove(0,qstrlen("bandwidth ")).split(" ");
75  _avgBandwidth = (quint64)bw.at(0).toULongLong();
76  _burstBandwidth = (quint64)bw.at(1).toULongLong();
77  _observedBandwidth = (quint64)bw.at(2).toULongLong();
78  } else if (line.startsWith("contact ")) {
79  _contact = line.remove(0,qstrlen("contact "));
80  } else if (line.startsWith("hibernating ")) {
81  if (line.remove(0,qstrlen("hibernating ")).trimmed() == "1") {
83  }
84  }
85  }
86  }
87 }
88 
89 /** Returns a string representation of the status of this router. */
90 QString
92 {
93  if (_status == Online) {
94  return tr("Online");
95  } else if (_status == Hibernating) {
96  return tr("Hibernating");
97  }
98  return tr("Offline");
99 }
100 
101 void
103 {
104  _id = rs.id();
105  _name = rs.name();
106  _ip = rs.ipAddress();
107  _orPort = rs.orPort();
108  _dirPort = rs.dirPort();
109  _avgBandwidth = rs.bandwidth() * 1024;
110  _burstBandwidth = rs.bandwidth() * 1024;
111  _observedBandwidth = rs.bandwidth() * 1024;
112  _published = rs.published();
113 }