<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: AddressMap.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_67a2ce25601334632bdc5ef4a72c0f17.html">torcontrol</a> </div> </div> <div class="contents"> <h1>AddressMap.cpp</h1><a href="_address_map_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If you</span> <a name="l00004"></a>00004 <span class="comment">** did not receive the LICENSE file with this file, you may obtain it from the</span> <a name="l00005"></a>00005 <span class="comment">** Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to the</span> <a name="l00008"></a>00008 <span class="comment">** terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/* </span> <a name="l00012"></a>00012 <span class="comment">** \file AddressMap.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id: bandwidthevent.h 1563 2006-12-26 06:06:04Z edmanm $</span> <a name="l00014"></a>00014 <span class="comment">** \brief Stores a list of address mappings and their expiration times</span> <a name="l00015"></a>00015 <span class="comment">*/</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="tcglobal_8h.html">tcglobal.h</a>"</span> <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="_address_map_8h.html">AddressMap.h</a>"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#include <QStringList></span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="comment"></span> <a name="l00023"></a>00023 <span class="comment">/** Format of expiry times in address map events. */</span> <a name="l00024"></a><a class="code" href="_address_map_8cpp.html#ab88071d2dae07659090a23e67a1e4d7f">00024</a> <span class="preprocessor">#define DATE_FMT "\"yyyy-MM-dd HH:mm:ss\""</span> <a name="l00025"></a>00025 <span class="preprocessor"></span> <a name="l00026"></a>00026 <span class="comment"></span> <a name="l00027"></a>00027 <span class="comment">/** Adds a new address mapping from the address <b>from</b> to the address</span> <a name="l00028"></a>00028 <span class="comment"> * <b>to</b>, that expires at <b>expires</b>. */</span> <a name="l00029"></a>00029 <span class="keywordtype">void</span> <a name="l00030"></a><a class="code" href="class_address_map.html#a1bb12511981bfe02622253b514748698">00030</a> <a class="code" href="class_address_map.html#a1bb12511981bfe02622253b514748698">AddressMap::add</a>(<span class="keyword">const</span> QString &from, <span class="keyword">const</span> QString &to, <a name="l00031"></a>00031 <span class="keyword">const</span> QDateTime &expires) <a name="l00032"></a>00032 { <a name="l00033"></a>00033 <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">"New address mapping: %1 -> %2 (expires %3)"</span>).arg(from) <a name="l00034"></a>00034 .arg(to) <a name="l00035"></a>00035 .arg(expires.isValid() ? expires.toString(<a class="code" href="_address_map_8cpp.html#ab88071d2dae07659090a23e67a1e4d7f">DATE_FMT</a>) <a name="l00036"></a>00036 : <span class="stringliteral">"never"</span>); <a name="l00037"></a>00037 insert(from, <a class="code" href="_address_map_8h.html#aea1301cb291459a2b3a61054d3bf3041">AddressMapEntry</a>(to, expires)); <a name="l00038"></a>00038 } <a name="l00039"></a>00039 <span class="comment"></span> <a name="l00040"></a>00040 <span class="comment">/** Adds a new address mapping by parsing the fields in <b>mapping</b>, which</span> <a name="l00041"></a>00041 <span class="comment"> * should be formatted as follows: </span> <a name="l00042"></a>00042 <span class="comment"> *</span> <a name="l00043"></a>00043 <span class="comment"> * Address SP Address SP Expiry</span> <a name="l00044"></a>00044 <span class="comment"> * Expiry = DQUOTE ISOTime DQUOTE / "NEVER"</span> <a name="l00045"></a>00045 <span class="comment"> */</span> <a name="l00046"></a>00046 <span class="keywordtype">void</span> <a name="l00047"></a><a class="code" href="class_address_map.html#aef093602f0597f8288e3e532250155ff">00047</a> <a class="code" href="class_address_map.html#a1bb12511981bfe02622253b514748698">AddressMap::add</a>(<span class="keyword">const</span> QString &mapping) <a name="l00048"></a>00048 { <a name="l00049"></a>00049 QStringList parts = mapping.split(<span class="stringliteral">" "</span>); <a name="l00050"></a>00050 <span class="keywordflow">if</span> (parts.size() >= 2) { <a name="l00051"></a>00051 QDateTime expires; <a name="l00052"></a>00052 <span class="keywordflow">if</span> (parts.size() >= 4 && parts.at(2) != <span class="stringliteral">"NEVER"</span>) { <a name="l00053"></a>00053 expires = QDateTime::fromString(parts.at(2) + <span class="stringliteral">" "</span> + parts.at(3), <a name="l00054"></a>00054 <a class="code" href="_address_map_8cpp.html#ab88071d2dae07659090a23e67a1e4d7f">DATE_FMT</a>); <a name="l00055"></a>00055 <a name="l00056"></a>00056 <span class="comment">/* Tor gives us the expiry time in UTC, but we will do subsequent</span> <a name="l00057"></a>00057 <span class="comment"> * comparisons based on local time. So do the proper conversion now. */</span> <a name="l00058"></a>00058 expires.setTimeSpec(Qt::UTC); <a name="l00059"></a>00059 expires = expires.toLocalTime(); <a name="l00060"></a>00060 } <a name="l00061"></a>00061 <a class="code" href="class_address_map.html#a1bb12511981bfe02622253b514748698">add</a>(parts.at(0), parts.at(1), expires); <a name="l00062"></a>00062 } <a name="l00063"></a>00063 } <a name="l00064"></a>00064 <span class="comment"></span> <a name="l00065"></a>00065 <span class="comment">/** Returns true if <b>entry</b> is expired; false otherwise. */</span> <a name="l00066"></a>00066 <span class="keywordtype">bool</span> <a name="l00067"></a><a class="code" href="class_address_map.html#a3459e8cad420ef5594dafeed50e4b315">00067</a> <a class="code" href="class_address_map.html#a3459e8cad420ef5594dafeed50e4b315">AddressMap::isExpired</a>(<span class="keyword">const</span> <a class="code" href="_address_map_8h.html#aea1301cb291459a2b3a61054d3bf3041">AddressMapEntry</a> &entry)<span class="keyword"> const</span> <a name="l00068"></a>00068 <span class="keyword"></span>{ <a name="l00069"></a>00069 <span class="keywordflow">if</span> (entry.second.isValid()) <a name="l00070"></a>00070 <span class="keywordflow">return</span> (entry.second < QDateTime::currentDateTime()); <a name="l00071"></a>00071 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">/* No expiry time == valid forever */</span> <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <span class="comment"></span> <a name="l00074"></a>00074 <span class="comment">/** Returns true if there exists a mapping for <b>addr</b> and that mapping is</span> <a name="l00075"></a>00075 <span class="comment"> * not expired. */</span> <a name="l00076"></a>00076 <span class="keywordtype">bool</span> <a name="l00077"></a><a class="code" href="class_address_map.html#aeab78837799234d133513a819a877979">00077</a> <a class="code" href="class_address_map.html#aeab78837799234d133513a819a877979">AddressMap::isMapped</a>(<span class="keyword">const</span> QString &addr)<span class="keyword"> const</span> <a name="l00078"></a>00078 <span class="keyword"></span>{ <a name="l00079"></a>00079 <span class="keywordflow">return</span> (contains(addr) && !<a class="code" href="class_address_map.html#a3459e8cad420ef5594dafeed50e4b315">isExpired</a>(value(addr))); <a name="l00080"></a>00080 } <a name="l00081"></a>00081 <span class="comment"></span> <a name="l00082"></a>00082 <span class="comment">/** Returns the address to which <b>addr</b> is currently mapped. If there is</span> <a name="l00083"></a>00083 <span class="comment"> * no mapping for <b>addr</b> (or the mapping is expired), then an empty</span> <a name="l00084"></a>00084 <span class="comment"> * string is returned. */</span> <a name="l00085"></a>00085 QString <a name="l00086"></a><a class="code" href="class_address_map.html#a86c87e42aeb13954088468390d15dd9e">00086</a> <a class="code" href="class_address_map.html#a86c87e42aeb13954088468390d15dd9e">AddressMap::mappedTo</a>(<span class="keyword">const</span> QString &addr)<span class="keyword"> const</span> <a name="l00087"></a>00087 <span class="keyword"></span>{ <a name="l00088"></a>00088 <a class="code" href="_address_map_8h.html#aea1301cb291459a2b3a61054d3bf3041">AddressMapEntry</a> entry = value(addr); <a name="l00089"></a>00089 <span class="keywordflow">return</span> (<a class="code" href="class_address_map.html#a3459e8cad420ef5594dafeed50e4b315">isExpired</a>(entry) ? QString() : entry.first); <a name="l00090"></a>00090 } <a name="l00091"></a>00091 <span class="comment"></span> <a name="l00092"></a>00092 <span class="comment">/** Returns the reverse of this address map by swapping each address in the</span> <a name="l00093"></a>00093 <span class="comment"> * address map with its mapped address. The expiration times are unaltered. */</span> <a name="l00094"></a>00094 <a class="code" href="class_address_map.html">AddressMap</a> <a name="l00095"></a><a class="code" href="class_address_map.html#a9f0b073b8bbb6b5392602bd256f7ac14">00095</a> <a class="code" href="class_address_map.html#a9f0b073b8bbb6b5392602bd256f7ac14">AddressMap::reverse</a>()<span class="keyword"> const</span> <a name="l00096"></a>00096 <span class="keyword"></span>{ <a name="l00097"></a>00097 <a class="code" href="class_address_map.html">AddressMap</a> reverseMap; <a name="l00098"></a>00098 <span class="keywordflow">foreach</span> (QString from, keys()) { <a name="l00099"></a>00099 <span class="comment">/* Flip the "from" and the "to" addresses and retain the expiry time. */</span> <a name="l00100"></a>00100 <a class="code" href="_address_map_8h.html#aea1301cb291459a2b3a61054d3bf3041">AddressMapEntry</a> entry = value(from); <a name="l00101"></a>00101 reverseMap.<a class="code" href="class_address_map.html#a1bb12511981bfe02622253b514748698">add</a>(entry.first, from, entry.second); <a name="l00102"></a>00102 } <a name="l00103"></a>00103 <span class="keywordflow">return</span> reverseMap; <a name="l00104"></a>00104 } <a name="l00105"></a>00105 </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>