Vidalia  0.3.1
AddressMap.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file AddressMap.cpp
13 ** \brief Stores a list of address mappings and their expiration times
14 */
15 
16 #include "tcglobal.h"
17 #include "AddressMap.h"
18 
19 #include <QStringList>
20 
21 
22 /** Format of expiry times in address map events. */
23 #define DATE_FMT "\"yyyy-MM-dd HH:mm:ss\""
24 
25 
26 /** Adds a new address mapping from the address <b>from</b> to the address
27  * <b>to</b>, that expires at <b>expires</b>. */
28 void
29 AddressMap::add(const QString &from, const QString &to,
30  const QDateTime &expires)
31 {
32  tc::debug("New address mapping: %1 -> %2 (expires %3)").arg(from)
33  .arg(to)
34  .arg(expires.isValid() ? expires.toString(DATE_FMT)
35  : "never");
36  insert(from, AddressMapEntry(to, expires));
37 }
38 
39 /** Adds a new address mapping by parsing the fields in <b>mapping</b>, which
40  * should be formatted as follows:
41  *
42  * Address SP Address SP Expiry
43  * Expiry = DQUOTE ISOTime DQUOTE / "NEVER"
44  */
45 void
46 AddressMap::add(const QString &mapping)
47 {
48  QStringList parts = mapping.split(" ");
49  if (parts.size() >= 2) {
50  QDateTime expires;
51  if (parts.size() >= 4 && parts.at(2) != "NEVER") {
52  expires = QDateTime::fromString(parts.at(2) + " " + parts.at(3),
53  DATE_FMT);
54 
55  /* Tor gives us the expiry time in UTC, but we will do subsequent
56  * comparisons based on local time. So do the proper conversion now. */
57  expires.setTimeSpec(Qt::UTC);
58  expires = expires.toLocalTime();
59  }
60  add(parts.at(0), parts.at(1), expires);
61  }
62 }
63 
64 /** Returns true if <b>entry</b> is expired; false otherwise. */
65 bool
67 {
68  if (entry.second.isValid())
69  return (entry.second < QDateTime::currentDateTime());
70  return false; /* No expiry time == valid forever */
71 }
72 
73 /** Returns true if there exists a mapping for <b>addr</b> and that mapping is
74  * not expired. */
75 bool
76 AddressMap::isMapped(const QString &addr) const
77 {
78  return (contains(addr) && !isExpired(value(addr)));
79 }
80 
81 /** Returns the address to which <b>addr</b> is currently mapped. If there is
82  * no mapping for <b>addr</b> (or the mapping is expired), then an empty
83  * string is returned. */
84 QString
85 AddressMap::mappedTo(const QString &addr) const
86 {
87  AddressMapEntry entry = value(addr);
88  return (isExpired(entry) ? QString() : entry.first);
89 }
90 
91 /** Returns the reverse of this address map by swapping each address in the
92  * address map with its mapped address. The expiration times are unaltered. */
95 {
96  AddressMap reverseMap;
97  foreach (QString from, keys()) {
98  /* Flip the "from" and the "to" addresses and retain the expiry time. */
99  AddressMapEntry entry = value(from);
100  reverseMap.add(entry.first, from, entry.second);
101  }
102  return reverseMap;
103 }
104 
AddressMap.h
tcglobal.h
tc::DebugMessage::arg
DebugMessage arg(const QString &a)
Definition: tcglobal.h:48
AddressMap::mappedTo
QString mappedTo(const QString &addr) const
Definition: AddressMap.cpp:85
tc::debug
DebugMessage debug(const QString &fmt)
Definition: tcglobal.cpp:24
AddressMap::add
void add(const QString &from, const QString &to, const QDateTime &expires)
Definition: AddressMap.cpp:29
AddressMap::isExpired
bool isExpired(const AddressMapEntry &entry) const
Definition: AddressMap.cpp:66
AddressMap::reverse
AddressMap reverse() const
Definition: AddressMap.cpp:94
AddressMapEntry
QPair< QString, QDateTime > AddressMapEntry
Definition: AddressMap.h:25
DATE_FMT
#define DATE_FMT
Definition: AddressMap.cpp:23
AddressMap::isMapped
bool isMapped(const QString &addr) const
Definition: AddressMap.cpp:76
AddressMap
Definition: AddressMap.h:28