Vidalia  0.2.17
RouterStatus.h
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.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file RouterStatus.h
00013 ** \brief Parses a blob of router status text from Tor
00014 */
00015 
00016 #ifndef _ROUTERSTATUS_H
00017 #define _ROUTERSTATUS_H
00018 
00019 #include <QFlags>
00020 #include <QStringList>
00021 #include <QHostAddress>
00022 #include <QDateTime>
00023 
00024 
00025 class RouterStatus
00026 {
00027 public:
00028   /** Possible router status flags. */
00029   enum Flag {
00030     Unknown      = 0x0000,
00031     Authority    = 0x0001,
00032     BadExit      = 0x0002,
00033     BadDirectory = 0x0004,
00034     Exit         = 0x0008,
00035     Fast         = 0x0010,
00036     Guard        = 0x0020,
00037     HSDir        = 0x0040,
00038     Named        = 0x0080,
00039     Stable       = 0x0100,
00040     Running      = 0x0200,
00041     Valid        = 0x0400,
00042     V2Dir        = 0x0800,
00043     V3Dir        = 0x1000
00044   };
00045   Q_DECLARE_FLAGS(Flags, Flag)
00046 
00047   /** Constructor. */
00048   RouterStatus(const QStringList &status);
00049   
00050   /** Returns the router's hexadecimal-encoded router identity key digest. */
00051   QString id() const { return _id; }
00052   /** Returns the router's nickname. */
00053   QString name() const { return _name; }
00054   /** Returns the hexadecimal-encoded digest of the router's most recent
00055    * descriptor. */
00056   QString descriptorDigest() const { return _digest; }
00057   /** Returns the router's most recent IP address. */
00058   QHostAddress ipAddress() const { return _ipAddress; }
00059   /** Returns the publication time of the router's most recent descriptor. */
00060   QDateTime published() const { return _published; }
00061   /** Returns the router's OR port number. */
00062   quint16 orPort() const { return _orPort; }
00063   /** Returns the router's directory port number. */
00064   quint16 dirPort() const { return _dirPort; }
00065 
00066   /** Returns an OR-ed field of the router's current status flags. */
00067   Flags flags() const { return _flags; }
00068   /** Returns true if this router is currently listed as Running. */
00069   bool isRunning() const { return (flags() & Running); }
00070 
00071   /** Returns true if this router status object is valid. This method should
00072    * be called to verify that the QStringList given in this object's
00073    * constructor contained properly formatted router status lines. */
00074   bool isValid() const { return _valid; }
00075 
00076 private:
00077   /** Returns a Flags enum value for the given router status <b>flag</b>. If
00078    * <b>flag</b> is not recognized, then <i>Unknown</i> is returned. */
00079   Flag flagValue(const QString &flag);
00080 
00081   bool _valid;   /**< True if this object is a valid RouterStatus. */
00082   QString _name; /**< Router nickname. */
00083   QString _id;   /**< Hexadecimal-encoded router identity digest. */
00084   QString _digest; /**< Hexadecimal-encoded hash of the router's most recent
00085                         descriptor. */
00086   QDateTime _published; /**< The publication time of the router's most recent
00087                              descriptor. */
00088   QHostAddress _ipAddress;  /**< Current IP address. */
00089   quint16 _orPort;  /**< Current OR port. */
00090   quint16 _dirPort; /**< Current directory port. */
00091   Flags _flags;     /**< OR-ed field of the router's current status flags. */
00092 };
00093 
00094 Q_DECLARE_OPERATORS_FOR_FLAGS(RouterStatus::Flags)
00095 
00096 /** A collection of RouterStatus objects. */
00097 typedef QList<RouterStatus> NetworkStatus;
00098 
00099 #endif
00100