Vidalia 0.2.12

AddressMap.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.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 AddressMap.h
00013 ** \brief Stores a list of address mappings and their expiration times
00014 */
00015 
00016 #ifndef _ADDRESSMAP_H
00017 #define _ADDRESSMAP_H
00018 
00019 #include <QHash>
00020 #include <QDateTime>
00021 #include <QPair>
00022 
00023 /** Defines a type that pairs a mapping's target address with an expiration
00024  * time for that mapping. */
00025 typedef QPair<QString, QDateTime> AddressMapEntry;
00026 
00027 
00028 class AddressMap : public QHash<QString, AddressMapEntry>
00029 {
00030 public:
00031   /** Types of address mappings. */
00032   enum AddressMapType {
00033     AddressMapAll,    /**< All address mapping types. */
00034     AddressMapConfig, /**< Address mappings set in the torrc. */
00035     AddressMapCache,  /**< Address mappings cached by Tor. */
00036     AddressMapControl /**< Address mappings set by a controller. */
00037   };
00038 
00039   /** Constructor. Creates an empty table for storing address mappinsgs. */
00040   AddressMap()
00041    : QHash<QString, AddressMapEntry>() {}
00042 
00043   /** Adds a new address mapping or updates an existing one for the address
00044    * specified by <b>from</b>. The mapping will remain valid until the date in
00045    * <b>expires</b>. */
00046   void add(const QString &from, const QString &to, const QDateTime &expires);
00047   /** Adds a new address mapping or updates an existing one based on fields
00048    * parsed from <b>mapping</b>. */
00049   void add(const QString &mapping);
00050 
00051   /** Returns true if the address map table contains a mapping for <b>addr</b>
00052    * that is not expired. */
00053   bool isMapped(const QString &addr) const;
00054   
00055   /** Returns the address to which <b>addr</b> is currently mapped. If there
00056    * is no mapping for <b>addr</b> (or the mapping is expired), then an
00057    * empty string is returned. */
00058   QString mappedTo(const QString &addr) const;
00059 
00060   /** Returns the reverse of this address map. */
00061   AddressMap reverse() const;
00062 
00063 private:
00064   /** Returns true if <b>entry</b> is expired; false otherwise. */
00065   bool isExpired(const AddressMapEntry &entry) const;
00066 };
00067 
00068 #endif
00069