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