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