[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1798: Add support for 'getinfo addr-mappings/[all|config|cache|con (trunk/src/control)



Author: edmanm
Date: 2007-07-02 21:31:17 -0400 (Mon, 02 Jul 2007)
New Revision: 1798

Added:
   trunk/src/control/addressmap.cpp
   trunk/src/control/addressmap.h
Modified:
   trunk/src/control/control.pri
   trunk/src/control/torcontrol.cpp
   trunk/src/control/torcontrol.h
Log:
Add support for 'getinfo addr-mappings/[all|config|cache|control]'.


Added: trunk/src/control/addressmap.cpp
===================================================================
--- trunk/src/control/addressmap.cpp	                        (rev 0)
+++ trunk/src/control/addressmap.cpp	2007-07-03 01:31:17 UTC (rev 1798)
@@ -0,0 +1,107 @@
+/****************************************************************
+ *  Vidalia is distributed under the following license:
+ *
+ *  Copyright (C) 2007,  Matt Edman, Justin Hipple
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+ *  Boston, MA  02110-1301, USA.
+  ****************************************************************/
+
+/** 
+ * \file addressmap.cpp
+ * \version $Id: bandwidthevent.h 1563 2006-12-26 06:06:04Z edmanm $
+ * \brief Stores a list of address mappings and their expiration times
+ */
+
+#include <QStringList>
+#include <vidalia.h>
+
+#include "addressmap.h"
+
+/** Format of expiry times in address map events. */
+#define DATE_FMT "\"yyyy-MM-dd HH:mm:ss\""
+
+
+/** Adds a new address mapping from the address <b>from</b> to the address
+ * <b>to</b>, that expires at <b>expires</b>. */
+void
+AddressMap::add(QString from, QString to, QDateTime expires)
+{
+  vInfo("New address mapping: %1 -> %2 (expires %3)")
+    .arg(from).arg(to)
+    .arg(expires.isValid() ? expires.toString(DATE_FMT) : "never");
+  insert(from, addr_map_entry_t(to, expires));
+}
+
+/** Adds a new address mapping by parsing the fields in <b>mapping</b>, which
+ * should be formatted as follows: 
+ *
+ *   Address SP Address SP Expiry
+ *   Expiry = DQUOTE ISOTime DQUOTE / "NEVER"
+ */
+void
+AddressMap::add(QString mapping)
+{
+  QStringList parts = mapping.split(" ");
+  if (parts.size() >= 2) {
+    QDateTime expires;
+    if (parts.size() >= 4 && parts.at(2) != "NEVER")
+      expires = QDateTime::fromString(parts.at(2) + " " + parts.at(3),
+                                      DATE_FMT);
+    add(parts.at(0), parts.at(1), expires);
+  }
+}
+
+/** Returns true if <b>entry</b> is expired; false otherwise. */
+bool
+AddressMap::isExpired(addr_map_entry_t entry) const
+{
+  if (entry.second.isValid())
+    return (entry.second < QDateTime::currentDateTime());
+  return false; /* No expiry time == valid forever */
+}
+
+/** Returns true if there exists a mapping for <b>addr</b> and that mapping is
+ * not expired. */
+bool
+AddressMap::isMapped(QString addr) const
+{
+  return (contains(addr) && !isExpired(value(addr)));
+}
+
+/** Returns the address to which <b>addr</b> is currently mapped. If there is
+ * no mapping for <b>addr</b> (or the mapping is expired), then an empty
+ * string is returned. */
+QString
+AddressMap::mappedTo(QString addr) const
+{
+  addr_map_entry_t entry = value(addr);
+  return (isExpired(entry) ? QString() : entry.first);
+}
+
+/** Returns the reverse of this address map by swapping each address in the
+ * address map with its mapped address. The expiration times are unaltered. */
+AddressMap
+AddressMap::reverse() const
+{
+  AddressMap reverseMap;
+  foreach (QString from, keys()) {
+    /* Flip the "from" and the "to" addresses and retain the expiry time. */
+    addr_map_entry_t entry = value(from);
+    reverseMap.add(entry.first, from, entry.second);
+  }
+  return reverseMap;
+}
+

Added: trunk/src/control/addressmap.h
===================================================================
--- trunk/src/control/addressmap.h	                        (rev 0)
+++ trunk/src/control/addressmap.h	2007-07-03 01:31:17 UTC (rev 1798)
@@ -0,0 +1,81 @@
+/****************************************************************
+ *  Vidalia is distributed under the following license:
+ *
+ *  Copyright (C) 2007,  Matt Edman, Justin Hipple
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+ *  Boston, MA  02110-1301, USA.
+  ****************************************************************/
+
+/** 
+ * \file addressmap.h
+ * \version $Id: bandwidthevent.h 1563 2006-12-26 06:06:04Z edmanm $
+ * \brief Stores a list of address mappings and their expiration times
+ */
+
+#ifndef _ADDRESSMAP_H
+#define _ADDRESSMAP_H
+
+#include <QHash>
+#include <QDateTime>
+#include <QPair>
+
+/** Defines a type that pairs a mapping's target address with an expiration
+ * time for that mapping. */
+typedef QPair<QString, QDateTime> addr_map_entry_t;
+
+
+class AddressMap : public QHash<QString, addr_map_entry_t>
+{
+public:
+  /** Types of address mappings. */
+  enum AddressMapType {
+    AddressMapAll,    /**< All address mapping types. */
+    AddressMapConfig, /**< Address mappings set in the torrc. */
+    AddressMapCache,  /**< Address mappings cached by Tor. */
+    AddressMapControl /**< Address mappings set by a controller. */
+  };
+
+  /** Constructor. Creates an empty table for storing address mappinsgs. */
+  AddressMap()
+   : QHash<QString, addr_map_entry_t>() {}
+
+  /** Adds a new address mapping or updates an existing one for the address
+   * specified by <b>from</b>. The mapping will remain valid until the date in
+   * <b>expires</b>. */
+  void add(QString from, QString to, QDateTime expires);
+  /** Adds a new address mapping or updates an existing one based on fields
+   * parsed from <b>mapping</b>. */
+  void add(QString mapping);
+
+  /** Returns true if the address map table contains a mapping for <b>addr</b>
+   * that is not expired. */
+  bool isMapped(QString addr) const;
+  
+  /** Returns the address to which <b>addr</b> is currently mapped. If there
+   * is no mapping for <b>addr</b> (or the mapping is expired), then an
+   * empty string is returned. */
+  QString mappedTo(QString addr) const;
+
+  /** Returns the reverse of this address map. */
+  AddressMap reverse() const;
+
+private:
+  /** Returns true if <b>entry</b> is expired; false otherwise. */
+  bool isExpired(addr_map_entry_t entry) const;
+};
+
+#endif
+

Modified: trunk/src/control/control.pri
===================================================================
--- trunk/src/control/control.pri	2007-07-03 01:30:35 UTC (rev 1797)
+++ trunk/src/control/control.pri	2007-07-03 01:31:17 UTC (rev 1798)
@@ -30,6 +30,7 @@
            $$PWD/replyline.h \
            $$PWD/torevents.h \
            $$PWD/eventtype.h \
+           $$PWD/addressmapevent.h \
            $$PWD/bandwidthevent.h \
            $$PWD/circuitevent.h \
            $$PWD/streamevent.h \
@@ -39,7 +40,8 @@
            $$PWD/newdescriptorevent.h \
            $$PWD/routerdescriptor.h \
            $$PWD/circuit.h \
-           $$PWD/stream.h
+           $$PWD/stream.h \
+           $$PWD/addressmap.h
 
 SOURCES += $$PWD/torcontrol.cpp \
            $$PWD/torprocess.cpp \
@@ -54,7 +56,8 @@
            $$PWD/torsignal.cpp \
            $$PWD/routerdescriptor.cpp \
            $$PWD/circuit.cpp \
-           $$PWD/stream.cpp
+           $$PWD/stream.cpp \
+           $$PWD/addressmap.cpp
 
 win32 {
   HEADERS += $$PWD/torservice.h

Modified: trunk/src/control/torcontrol.cpp
===================================================================
--- trunk/src/control/torcontrol.cpp	2007-07-03 01:30:35 UTC (rev 1797)
+++ trunk/src/control/torcontrol.cpp	2007-07-03 01:31:17 UTC (rev 1798)
@@ -928,3 +928,34 @@
   return send(cmd, errmsg);
 }
 
+ /** Gets a list of address mappings of the type specified by <b>type</b>
+  * (defaults to <i>AddressMapAll</i>. */
+AddressMap
+TorControl::getAddressMap(AddressMap::AddressMapType type, QString *errmsg)
+{
+  ControlCommand cmd("GETINFO");
+  ControlReply reply;
+  AddressMap addressMap;
+
+  switch (type) {
+    case AddressMap::AddressMapConfig:
+      cmd.addArgument("addr-mappings/config");
+      break;
+    case AddressMap::AddressMapCache:
+      cmd.addArgument("addr-mappings/cache");
+      break;
+    case AddressMap::AddressMapControl:
+      cmd.addArgument("addr-mappings/control");
+      break;
+    default:
+      cmd.addArgument("addr-mappings/all");
+  }
+
+  if (send(cmd, reply, errmsg)) {
+    foreach (QString mapping, reply.getData()) {
+      addressMap.add(mapping);
+    }
+  }
+  return addressMap;
+}
+

Modified: trunk/src/control/torcontrol.h
===================================================================
--- trunk/src/control/torcontrol.h	2007-07-03 01:30:35 UTC (rev 1797)
+++ trunk/src/control/torcontrol.h	2007-07-03 01:31:17 UTC (rev 1798)
@@ -38,6 +38,7 @@
 #include "torevents.h"
 #include "torsignal.h"
 #include "routerdescriptor.h"
+#include "addressmap.h"
 
 #if defined(Q_OS_WIN32)
 #include "torservice.h"
@@ -147,6 +148,12 @@
   /** Gets a list of current streams. */
   QList<Stream> getStreams(QString *errmsg = 0);
   
+  /** Gets a list of address mappings of the type specified by <b>type</b>
+   * (defaults to <i>AddressMapAll</i>. */
+  AddressMap getAddressMap(
+    AddressMap::AddressMapType type = AddressMap::AddressMapAll,
+    QString *errmsg = 0);
+
 public slots:
   /** Closes the circuit specified by <b>circid</b>. If <b>ifUnused</b> is
    * true, then the circuit will not be closed unless it is unused. */