00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stringutil.h>
00018 #include "routerstatus.h"
00019
00020
00021
00022 #define TIME_FORMAT "yyyy-MM-dd HH:mm:ss"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 RouterStatus::RouterStatus(const QStringList &status)
00037 {
00038 bool ok;
00039
00040 _valid = false;
00041 _flags = 0;
00042
00043 foreach (QString line, status) {
00044 if (line.startsWith("r ")) {
00045 QStringList parts = line.split(" ", QString::SkipEmptyParts);
00046 if (parts.size() < 9)
00047 return;
00048
00049
00050 _name = parts.at(1);
00051
00052 _id = base16_encode(QByteArray::fromBase64(parts.at(2).toAscii()));
00053 if (_id.isEmpty())
00054 return;
00055
00056 _digest = base16_encode(QByteArray::fromBase64(parts.at(3).toAscii()));
00057 if (_digest.isEmpty())
00058 return;
00059
00060 _published = QDateTime::fromString(parts.at(4) + " " + parts.at(5),
00061 TIME_FORMAT);
00062 if (!_published.isValid())
00063 return;
00064
00065 _ipAddress = QHostAddress(parts.at(6));
00066 if (_ipAddress.isNull())
00067 return;
00068
00069 _orPort = parts.at(7).toUInt(&ok);
00070 if (!ok)
00071 return;
00072
00073 _dirPort = parts.at(8).toUInt(&ok);
00074 if (!ok)
00075 return;
00076
00077 _valid = true;
00078 } else if (line.startsWith("s ")) {
00079
00080 QStringList flags = line.split(" ", QString::SkipEmptyParts);
00081 flags.removeFirst();
00082
00083 foreach (QString flag, flags) {
00084 _flags |= flagValue(flag);
00085 }
00086 }
00087 }
00088 }
00089
00090
00091
00092 RouterStatus::Flag
00093 RouterStatus::flagValue(const QString &flag)
00094 {
00095 if (!flag.compare("Authority", Qt::CaseInsensitive))
00096 return Authority;
00097 if (!flag.compare("BadExit", Qt::CaseInsensitive))
00098 return BadExit;
00099 if (!flag.compare("BadDirectory", Qt::CaseInsensitive))
00100 return BadDirectory;
00101 if (!flag.compare("Exit", Qt::CaseInsensitive))
00102 return Exit;
00103 if (!flag.compare("Fast", Qt::CaseInsensitive))
00104 return Fast;
00105 if (!flag.compare("Guard", Qt::CaseInsensitive))
00106 return Guard;
00107 if (!flag.compare("HSDir", Qt::CaseInsensitive))
00108 return HSDir;
00109 if (!flag.compare("Named", Qt::CaseInsensitive))
00110 return Named;
00111 if (!flag.compare("Running", Qt::CaseInsensitive))
00112 return Running;
00113 if (!flag.compare("Stable", Qt::CaseInsensitive))
00114 return Stable;
00115 if (!flag.compare("Valid", Qt::CaseInsensitive))
00116 return Valid;
00117 if (!flag.compare("V2Dir", Qt::CaseInsensitive))
00118 return V2Dir;
00119 if (!flag.compare("V3Dir", Qt::CaseInsensitive))
00120 return V3Dir;
00121 return Unknown;
00122 }
00123