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

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1