[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2508: Add a RouterStatus class for parsing and storing router stat (in vidalia/trunk: . src/torcontrol)



Author: edmanm
Date: 2008-04-12 18:02:22 -0400 (Sat, 12 Apr 2008)
New Revision: 2508

Added:
   vidalia/trunk/src/torcontrol/routerstatus.cpp
   vidalia/trunk/src/torcontrol/routerstatus.h
Modified:
   vidalia/trunk/
Log:
 r345@lysithea:  edmanm | 2008-04-12 15:54:01 -0400
 Add a RouterStatus class for parsing and storing router statuses from network
 status information.



Property changes on: vidalia/trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r345] on 90112fd6-a33b-4cea-8d39-48ff1d78625c

Added: vidalia/trunk/src/torcontrol/routerstatus.cpp
===================================================================
--- vidalia/trunk/src/torcontrol/routerstatus.cpp	                        (rev 0)
+++ vidalia/trunk/src/torcontrol/routerstatus.cpp	2008-04-12 22:02:22 UTC (rev 2508)
@@ -0,0 +1,123 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If 
+**  you did not receive the LICENSE file with this file, you may obtain it
+**  from the Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to
+**  the terms described in the LICENSE file.
+*/
+
+/* 
+** \file routerstatus.cpp
+** \version $Id$
+** \brief Parses a blob of router status text from Tor
+*/
+
+#include <stringutil.h>
+#include "routerstatus.h"
+
+/** Defines the time format used when parsing the published date and time from
+ * a router's status. */
+#define TIME_FORMAT  "yyyy-MM-dd HH:mm:ss"
+
+
+/** Constructor. Parses <b>status</b> for router status information. The given
+ * string should match the router status entry format from Tor's dir-spec.txt.
+ * The currently recognized lines are:
+ *
+ *  "r" SP nickname SP identity SP digest SP publication SP IP SP ORPort
+ *      SP DirPort NL
+ *  "s" SP Flags NL
+ *
+ * Unrecognized lines are currently ignored.
+ *
+ * */
+RouterStatus::RouterStatus(const QStringList &status)
+{
+  bool ok;
+
+  _valid = false;
+  _flags = 0;
+
+  foreach (QString line, status) {
+    if (line.startsWith("r ")) {
+      QStringList parts = line.split(" ", QString::SkipEmptyParts);
+      if (parts.size() < 9)
+        return;
+
+      /* Nickname */
+      _name = parts.at(1);
+      /* Identity key digest */
+      _id = base16_encode(QByteArray::fromBase64(parts.at(2).toAscii()));
+      if (_id.isEmpty())
+        return;
+      /* Most recent descriptor digest */
+      _digest = base16_encode(QByteArray::fromBase64(parts.at(3).toAscii()));
+      if (_digest.isEmpty())
+        return;
+      /* Most recent publication date */
+      _published = QDateTime::fromString(parts.at(4) + " " + parts.at(5),
+                                         TIME_FORMAT);
+      if (!_published.isValid())
+        return;
+      /* IP address */
+      _ipAddress = QHostAddress(parts.at(6));
+      if (_ipAddress.isNull())
+        return;
+      /* ORPort */
+      _orPort = parts.at(7).toUInt(&ok);
+      if (!ok)
+        return;
+      /* DirPort */
+      _dirPort = parts.at(8).toUInt(&ok);
+      if (!ok)
+        return;
+
+      _valid = true;
+    } else if (line.startsWith("s ")) {
+      /* Status flags */
+      QStringList flags = line.split(" ", QString::SkipEmptyParts);
+      flags.removeFirst(); /* Remove the "s" */
+
+      foreach (QString flag, flags) {
+        _flags |= flagValue(flag);
+      }
+    }
+  }
+}
+
+/** Returns a Flags enum value for the given router status <b>flag</b>. If
+ * <b>flag</b> is not recognized, then <i>Unknown</i> is returned. */
+RouterStatus::Flag
+RouterStatus::flagValue(const QString &flag)
+{
+  if (!flag.compare("Authority", Qt::CaseInsensitive))
+    return Authority;
+  if (!flag.compare("BadExit", Qt::CaseInsensitive))
+    return BadExit;
+  if (!flag.compare("BadDirectory", Qt::CaseInsensitive))
+    return BadDirectory;
+  if (!flag.compare("Exit", Qt::CaseInsensitive))
+    return Exit;
+  if (!flag.compare("Fast", Qt::CaseInsensitive))
+    return Fast;
+  if (!flag.compare("Guard", Qt::CaseInsensitive))
+    return Guard;
+  if (!flag.compare("HSDir", Qt::CaseInsensitive))
+    return HSDir;
+  if (!flag.compare("Named", Qt::CaseInsensitive))
+    return Named;
+  if (!flag.compare("Running", Qt::CaseInsensitive))
+    return Running;
+  if (!flag.compare("Stable", Qt::CaseInsensitive))
+    return Stable;
+  if (!flag.compare("Valid", Qt::CaseInsensitive))
+    return Valid;
+  if (!flag.compare("V2Dir", Qt::CaseInsensitive))
+    return V2Dir;
+  if (!flag.compare("V3Dir", Qt::CaseInsensitive))
+    return V3Dir;
+  return Unknown; /* Unknown status flag */
+}
+


Property changes on: vidalia/trunk/src/torcontrol/routerstatus.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: vidalia/trunk/src/torcontrol/routerstatus.h
===================================================================
--- vidalia/trunk/src/torcontrol/routerstatus.h	                        (rev 0)
+++ vidalia/trunk/src/torcontrol/routerstatus.h	2008-04-12 22:02:22 UTC (rev 2508)
@@ -0,0 +1,101 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If 
+**  you did not receive the LICENSE file with this file, you may obtain it
+**  from the Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to
+**  the terms described in the LICENSE file.
+*/
+
+/* 
+** \file routerstatus.h
+** \version $Id$
+** \brief Parses a blob of router status text from Tor
+*/
+
+#ifndef _ROUTERSTATUS_H
+#define _ROUTERSTATUS_H
+
+#include <QFlags>
+#include <QStringList>
+#include <QHostAddress>
+#include <QDateTime>
+
+
+class RouterStatus
+{
+public:
+  /** Possible router status flags. */
+  enum Flag {
+    Unknown      = 0x0000,
+    Authority    = 0x0001,
+    BadExit      = 0x0002,
+    BadDirectory = 0x0004,
+    Exit         = 0x0008,
+    Fast         = 0x0010,
+    Guard        = 0x0020,
+    HSDir        = 0x0040,
+    Named        = 0x0080,
+    Stable       = 0x0100,
+    Running      = 0x0200,
+    Valid        = 0x0400,
+    V2Dir        = 0x0800,
+    V3Dir        = 0x1000
+  };
+  Q_DECLARE_FLAGS(Flags, Flag)
+
+  /** Constructor. */
+  RouterStatus(const QStringList &status);
+  
+  /** Returns the router's hexadecimal-encoded router identity key digest. */
+  QString id() const { return _id; }
+  /** Returns the router's nickname. */
+  QString name() const { return _name; }
+  /** Returns the hexadecimal-encoded digest of the router's most recent
+   * descriptor. */
+  QString descriptorDigest() const { return _digest; }
+  /** Returns the router's most recent IP address. */
+  QHostAddress ipAddress() const { return _ipAddress; }
+  /** Returns the publication time of the router's most recent descriptor. */
+  QDateTime published() const { return _published; }
+  /** Returns the router's OR port number. */
+  quint16 orPort() const { return _orPort; }
+  /** Returns the router's directory port number. */
+  quint16 dirPort() const { return _dirPort; }
+
+  /** Returns an OR-ed field of the router's current status flags. */
+  Flags flags() const { return _flags; }
+  /** Returns true if this router is currently listed as Running. */
+  bool isRunning() const { return (flags() & Running); }
+
+  /** Returns true if this router status object is valid. This method should
+   * be called to verify that the QStringList given in this object's
+   * constructor contained properly formatted router status lines. */
+  bool isValid() const { return _valid; }
+
+private:
+  /** Returns a Flags enum value for the given router status <b>flag</b>. If
+   * <b>flag</b> is not recognized, then <i>Unknown</i> is returned. */
+  Flag flagValue(const QString &flag);
+
+  bool _valid;   /**< True if this object is a valid RouterStatus. */
+  QString _name; /**< Router nickname. */
+  QString _id;   /**< Hexadecimal-encoded router identity digest. */
+  QString _digest; /**< Hexadecimal-encoded hash of the router's most recent
+                        descriptor. */
+  QDateTime _published; /**< The publication time of the router's most recent
+                             descriptor. */
+  QHostAddress _ipAddress;  /**< Current IP address. */
+  quint16 _orPort;  /**< Current OR port. */
+  quint16 _dirPort; /**< Current directory port. */
+  Flags _flags;     /**< OR-ed field of the router's current status flags. */
+};
+
+Q_DECLARE_OPERATORS_FOR_FLAGS(RouterStatus::Flags)
+
+/** A collection of RouterStatus objects. */
+typedef QList<RouterStatus> NetworkStatus;
+
+#endif
+


Property changes on: vidalia/trunk/src/torcontrol/routerstatus.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native