routerstatus.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to
00008 **  the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file routerstatus.cpp
00013 ** \version $Id: routerstatus.cpp 2508 2008-04-12 22:02:22Z edmanm $
00014 ** \brief Parses a blob of router status text from Tor
00015 */
00016 
00017 #include <stringutil.h>
00018 #include "routerstatus.h"
00019 
00020 /** Defines the time format used when parsing the published date and time from
00021  * a router's status. */
00022 #define TIME_FORMAT  "yyyy-MM-dd HH:mm:ss"
00023 
00024 
00025 /** Constructor. Parses <b>status</b> for router status information. The given
00026  * string should match the router status entry format from Tor's dir-spec.txt.
00027  * The currently recognized lines are:
00028  *
00029  *  "r" SP nickname SP identity SP digest SP publication SP IP SP ORPort
00030  *      SP DirPort NL
00031  *  "s" SP Flags NL
00032  *
00033  * Unrecognized lines are currently ignored.
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       /* Nickname */
00050       _name = parts.at(1);
00051       /* Identity key digest */
00052       _id = base16_encode(QByteArray::fromBase64(parts.at(2).toAscii()));
00053       if (_id.isEmpty())
00054         return;
00055       /* Most recent descriptor digest */
00056       _digest = base16_encode(QByteArray::fromBase64(parts.at(3).toAscii()));
00057       if (_digest.isEmpty())
00058         return;
00059       /* Most recent publication date */
00060       _published = QDateTime::fromString(parts.at(4) + " " + parts.at(5),
00061                                          TIME_FORMAT);
00062       if (!_published.isValid())
00063         return;
00064       /* IP address */
00065       _ipAddress = QHostAddress(parts.at(6));
00066       if (_ipAddress.isNull())
00067         return;
00068       /* ORPort */
00069       _orPort = parts.at(7).toUInt(&ok);
00070       if (!ok)
00071         return;
00072       /* DirPort */
00073       _dirPort = parts.at(8).toUInt(&ok);
00074       if (!ok)
00075         return;
00076 
00077       _valid = true;
00078     } else if (line.startsWith("s ")) {
00079       /* Status flags */
00080       QStringList flags = line.split(" ", QString::SkipEmptyParts);
00081       flags.removeFirst(); /* Remove the "s" */
00082 
00083       foreach (QString flag, flags) {
00084         _flags |= flagValue(flag);
00085       }
00086     }
00087   }
00088 }
00089 
00090 /** Returns a Flags enum value for the given router status <b>flag</b>. If
00091  * <b>flag</b> is not recognized, then <i>Unknown</i> is returned. */
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; /* Unknown status flag */
00122 }
00123 

Generated on Wed Dec 23 21:11:09 2009 for Vidalia by  doxygen 1.6.1