AddressMap.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 you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  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 the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file AddressMap.cpp
00013 ** \version $Id: bandwidthevent.h 1563 2006-12-26 06:06:04Z edmanm $
00014 ** \brief Stores a list of address mappings and their expiration times
00015 */
00016 
00017 #include "tcglobal.h"
00018 #include "AddressMap.h"
00019 
00020 #include <QStringList>
00021 
00022 
00023 /** Format of expiry times in address map events. */
00024 #define DATE_FMT "\"yyyy-MM-dd HH:mm:ss\""
00025 
00026 
00027 /** Adds a new address mapping from the address <b>from</b> to the address
00028  * <b>to</b>, that expires at <b>expires</b>. */
00029 void
00030 AddressMap::add(const QString &from, const QString &to,
00031                 const QDateTime &expires)
00032 {
00033   tc::debug("New address mapping: %1 -> %2 (expires %3)").arg(from)
00034                                                          .arg(to)
00035                           .arg(expires.isValid() ? expires.toString(DATE_FMT)
00036                                                  : "never");
00037   insert(from, AddressMapEntry(to, expires));
00038 }
00039 
00040 /** Adds a new address mapping by parsing the fields in <b>mapping</b>, which
00041  * should be formatted as follows: 
00042  *
00043  *   Address SP Address SP Expiry
00044  *   Expiry = DQUOTE ISOTime DQUOTE / "NEVER"
00045  */
00046 void
00047 AddressMap::add(const QString &mapping)
00048 {
00049   QStringList parts = mapping.split(" ");
00050   if (parts.size() >= 2) {
00051     QDateTime expires;
00052     if (parts.size() >= 4 && parts.at(2) != "NEVER") {
00053       expires = QDateTime::fromString(parts.at(2) + " " + parts.at(3),
00054                                       DATE_FMT);
00055 
00056       /* Tor gives us the expiry time in UTC, but we will do subsequent
00057        * comparisons based on local time. So do the proper conversion now. */
00058       expires.setTimeSpec(Qt::UTC);
00059       expires = expires.toLocalTime();
00060     }
00061     add(parts.at(0), parts.at(1), expires);
00062   }
00063 }
00064 
00065 /** Returns true if <b>entry</b> is expired; false otherwise. */
00066 bool
00067 AddressMap::isExpired(const AddressMapEntry &entry) const
00068 {
00069   if (entry.second.isValid())
00070     return (entry.second < QDateTime::currentDateTime());
00071   return false; /* No expiry time == valid forever */
00072 }
00073 
00074 /** Returns true if there exists a mapping for <b>addr</b> and that mapping is
00075  * not expired. */
00076 bool
00077 AddressMap::isMapped(const QString &addr) const
00078 {
00079   return (contains(addr) && !isExpired(value(addr)));
00080 }
00081 
00082 /** Returns the address to which <b>addr</b> is currently mapped. If there is
00083  * no mapping for <b>addr</b> (or the mapping is expired), then an empty
00084  * string is returned. */
00085 QString
00086 AddressMap::mappedTo(const QString &addr) const
00087 {
00088   AddressMapEntry entry = value(addr);
00089   return (isExpired(entry) ? QString() : entry.first);
00090 }
00091 
00092 /** Returns the reverse of this address map by swapping each address in the
00093  * address map with its mapped address. The expiration times are unaltered. */
00094 AddressMap
00095 AddressMap::reverse() const
00096 {
00097   AddressMap reverseMap;
00098   foreach (QString from, keys()) {
00099     /* Flip the "from" and the "to" addresses and retain the expiry time. */
00100     AddressMapEntry entry = value(from);
00101     reverseMap.add(entry.first, from, entry.second);
00102   }
00103   return reverseMap;
00104 }
00105 

Generated on Mon Aug 30 19:14:02 2010 for Vidalia by  doxygen 1.5.9