Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > by-pkgid > 9601c7beb4ff23e834bfa171795ed560 > files > 127

vidalia-0.2.9-1mdv2010.1.i586.rpm

<!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: GeoIpCache.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&nbsp;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&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_695ba8f8357372e02d38c40b3c6dbe81.html">vidalia</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_3776330d706700e272fa8df738fecea6.html">network</a>
  </div>
</div>
<div class="contents">
<h1>GeoIpCache.cpp</h1><a href="_geo_ip_cache_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 GeoIpCache.cpp</span>
<a name="l00013"></a>00013 <span class="comment">** \version $Id: GeoIpCache.cpp 3768 2009-05-13 19:07:26Z edmanm $</span>
<a name="l00014"></a>00014 <span class="comment">** \brief Caches the results of previous GeoIP requests</span>
<a name="l00015"></a>00015 <span class="comment">*/</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;<a class="code" href="_geo_ip_cache_8h.html">GeoIpCache.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="_geo_ip_cache_item_8h.html">GeoIpCacheItem.h</a>&quot;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &quot;<a class="code" href="_geo_ip_8h.html">GeoIp.h</a>&quot;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &quot;<a class="code" href="_vidalia_8h.html">Vidalia.h</a>&quot;</span>
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 <span class="preprocessor">#include &quot;<a class="code" href="file_8h.html">file.h</a>&quot;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &quot;<a class="code" href="stringutil_8h.html">stringutil.h</a>&quot;</span>
<a name="l00024"></a>00024 
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;QFile&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include &lt;QDir&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &lt;QString&gt;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include &lt;QDateTime&gt;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include &lt;QTextStream&gt;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &lt;QHostAddress&gt;</span>
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 
<a name="l00033"></a><a class="code" href="class_geo_ip_cache.html#a7aac6aeb90bb684d166a86a2577d8fb7">00033</a> <a class="code" href="class_geo_ip_cache.html#a7aac6aeb90bb684d166a86a2577d8fb7">GeoIpCache::GeoIpCache</a>(QObject *parent)
<a name="l00034"></a>00034   : QObject(parent)
<a name="l00035"></a>00035 {
<a name="l00036"></a>00036   <a class="code" href="class_geo_ip_cache.html#ac8e7f835d8c48ec6740ebcfd89085b23">loadFromDisk</a>();
<a name="l00037"></a>00037 }
<a name="l00038"></a>00038 
<a name="l00039"></a>00039 QString
<a name="l00040"></a><a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">00040</a> <a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">GeoIpCache::cacheFileName</a>()<span class="keyword"> const</span>
<a name="l00041"></a>00041 <span class="keyword"></span>{
<a name="l00042"></a>00042   <span class="keywordflow">return</span> (<a class="code" href="class_vidalia.html#a5c923ad28bb9f02299f5c4d3c6c2bde0">Vidalia::dataDirectory</a>() + <span class="stringliteral">&quot;/geoip-cache&quot;</span>);
<a name="l00043"></a>00043 }
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="keywordtype">bool</span>
<a name="l00046"></a><a class="code" href="class_geo_ip_cache.html#abb01140493b35004398ce8ca6f468e10">00046</a> <a class="code" href="class_geo_ip_cache.html#abb01140493b35004398ce8ca6f468e10">GeoIpCache::saveToDisk</a>(QString *errmsg)
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048   <span class="comment">/* Make sure we have a data directory. */</span>
<a name="l00049"></a>00049   <span class="keywordflow">if</span> (!<a class="code" href="file_8cpp.html#ae15354f79a89888da691222de003851f">create_path</a>(<a class="code" href="class_vidalia.html#a5c923ad28bb9f02299f5c4d3c6c2bde0">Vidalia::dataDirectory</a>())) {
<a name="l00050"></a>00050     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00051"></a>00051   }
<a name="l00052"></a>00052   
<a name="l00053"></a>00053   <span class="comment">/* Try to open a temporary cache file for writing */</span>
<a name="l00054"></a>00054   QFile tmpCacheFile(<a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">cacheFileName</a>() + <span class="stringliteral">&quot;.tmp&quot;</span>);
<a name="l00055"></a>00055   <span class="keywordflow">if</span> (!tmpCacheFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
<a name="l00056"></a>00056     <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tmpCacheFile.errorString());
<a name="l00057"></a>00057   }
<a name="l00058"></a>00058 
<a name="l00059"></a>00059   <span class="comment">/* Write the cache entries to the file. */</span>
<a name="l00060"></a>00060   QTextStream cache(&amp;tmpCacheFile);
<a name="l00061"></a>00061   <span class="keywordflow">foreach</span> (<a class="code" href="class_geo_ip_cache_item.html">GeoIpCacheItem</a> cacheItem, <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>) {
<a name="l00062"></a>00062     <span class="comment">/* Save the cache item if it&#39;s not too old. */</span>
<a name="l00063"></a>00063     <span class="keywordflow">if</span> (!cacheItem.<a class="code" href="class_geo_ip_cache_item.html#aa044a2be3a9f99738beb25c87313a237">isExpired</a>()) {
<a name="l00064"></a>00064       cache &lt;&lt; cacheItem.<a class="code" href="class_geo_ip_cache_item.html#a4097e1c1065b4875dc41e0b861b6a482">toCacheString</a>() &lt;&lt; endl;
<a name="l00065"></a>00065     }
<a name="l00066"></a>00066   }
<a name="l00067"></a>00067 
<a name="l00068"></a>00068   QFile cacheFile(<a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">cacheFileName</a>());
<a name="l00069"></a>00069   <span class="comment">/* Check if an previous cache file exists. */</span>
<a name="l00070"></a>00070   <span class="keywordflow">if</span> (cacheFile.exists()) {
<a name="l00071"></a>00071     <span class="comment">/* A previous cache file exists, so try to remove it */</span>
<a name="l00072"></a>00072     <span class="keywordflow">if</span> (!cacheFile.remove()) {
<a name="l00073"></a>00073       <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, cacheFile.errorString());
<a name="l00074"></a>00074     }
<a name="l00075"></a>00075   }
<a name="l00076"></a>00076   <span class="comment">/* Rename the temporary file into place. */</span>
<a name="l00077"></a>00077   <span class="keywordflow">if</span> (!tmpCacheFile.rename(cacheFile.fileName())) {
<a name="l00078"></a>00078     <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tmpCacheFile.errorString());
<a name="l00079"></a>00079   }
<a name="l00080"></a>00080   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00081"></a>00081 }
<a name="l00082"></a>00082 
<a name="l00083"></a>00083 <span class="keywordtype">bool</span>
<a name="l00084"></a><a class="code" href="class_geo_ip_cache.html#ac8e7f835d8c48ec6740ebcfd89085b23">00084</a> <a class="code" href="class_geo_ip_cache.html#ac8e7f835d8c48ec6740ebcfd89085b23">GeoIpCache::loadFromDisk</a>(QString *errmsg)
<a name="l00085"></a>00085 {
<a name="l00086"></a>00086   QFile cacheFile(<a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">cacheFileName</a>());
<a name="l00087"></a>00087 
<a name="l00088"></a>00088   <span class="keywordflow">if</span> (cacheFile.exists()) {
<a name="l00089"></a>00089     <span class="comment">/* Try to open the cache file */</span>
<a name="l00090"></a>00090     <span class="keywordflow">if</span> (!cacheFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
<a name="l00091"></a>00091       <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, cacheFile.errorString());
<a name="l00092"></a>00092     }
<a name="l00093"></a>00093     
<a name="l00094"></a>00094     <span class="comment">/* Read the cached items from the cache file */</span>
<a name="l00095"></a>00095     QTextStream cache(&amp;cacheFile);
<a name="l00096"></a>00096     QString line = cache.readLine();
<a name="l00097"></a>00097     <span class="keywordflow">while</span> (! line.isNull()) {
<a name="l00098"></a>00098       <span class="comment">/* Create a GeoIpCacheItem from the line and save it */</span>
<a name="l00099"></a>00099       <a class="code" href="class_geo_ip_cache_item.html">GeoIpCacheItem</a> item = <a class="code" href="class_geo_ip_cache_item.html#a46b9407efbe1ecf073889327b343db43">GeoIpCacheItem::fromCacheString</a>(line);
<a name="l00100"></a>00100       <span class="keywordflow">if</span> (item.<a class="code" href="class_geo_ip_cache_item.html#a7875b8609f1f72b75770ea4ac909218b">isValid</a>() &amp;&amp; ! item.<a class="code" href="class_geo_ip_cache_item.html#aa044a2be3a9f99738beb25c87313a237">isExpired</a>())
<a name="l00101"></a>00101         <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">addToCache</a>(item);
<a name="l00102"></a>00102 
<a name="l00103"></a>00103       line = cache.readLine();
<a name="l00104"></a>00104     }
<a name="l00105"></a>00105     <a class="code" href="_vidalia_8h.html#a7743cb328f470aa8f382716fbcf61daa">vInfo</a>(<span class="stringliteral">&quot;Parsed %1 GeoIP entries from &#39;%2&#39;&quot;</span>).arg(<a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.size())
<a name="l00106"></a>00106                                               .arg(<a class="code" href="class_geo_ip_cache.html#ad02269cb69f6d820dd60e01b2e6160c2">cacheFileName</a>());
<a name="l00107"></a>00107   }
<a name="l00108"></a>00108   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00109"></a>00109 }
<a name="l00110"></a>00110 
<a name="l00111"></a>00111 <span class="keywordtype">void</span>
<a name="l00112"></a><a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">00112</a> <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">GeoIpCache::addToCache</a>(<span class="keyword">const</span> <a class="code" href="class_geo_ip.html">GeoIp</a> &amp;geoip)
<a name="l00113"></a>00113 {
<a name="l00114"></a>00114   <span class="comment">/* Create a &quot;range&quot; consisting of only a single IP address. */</span>
<a name="l00115"></a>00115   <span class="keywordflow">if</span> (! <a class="code" href="class_geo_ip_cache.html#a3f5f4ed2bd9edc9da640c1d47eec3d1a">contains</a>(geoip.<a class="code" href="class_geo_ip.html#a054c1b4097bc3cb0bbdd30d4bd8ed6d2">ip</a>()))
<a name="l00116"></a>00116     <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">addToCache</a>(geoip.<a class="code" href="class_geo_ip.html#a054c1b4097bc3cb0bbdd30d4bd8ed6d2">ip</a>(), geoip.<a class="code" href="class_geo_ip.html#a054c1b4097bc3cb0bbdd30d4bd8ed6d2">ip</a>(), geoip);
<a name="l00117"></a>00117 }
<a name="l00118"></a>00118 
<a name="l00119"></a>00119 <span class="keywordtype">void</span>
<a name="l00120"></a><a class="code" href="class_geo_ip_cache.html#a3cfad7a842e712102a8d0d6edd59cd38">00120</a> <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">GeoIpCache::addToCache</a>(<span class="keyword">const</span> QHostAddress &amp;from, <span class="keyword">const</span> QHostAddress &amp;to,
<a name="l00121"></a>00121                        <span class="keyword">const</span> <a class="code" href="class_geo_ip.html">GeoIp</a> &amp;geoip)
<a name="l00122"></a>00122 {
<a name="l00123"></a>00123   <span class="comment">/* New cache entries expire 30 days from the time they are first cached. */</span>
<a name="l00124"></a>00124   QDateTime expires = QDateTime::currentDateTime().toUTC().addDays(30);
<a name="l00125"></a>00125   
<a name="l00126"></a>00126   <span class="comment">/* Create a new GeoIpCacheItem and add it to the cache */</span>
<a name="l00127"></a>00127   <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">addToCache</a>(<a class="code" href="class_geo_ip_cache_item.html">GeoIpCacheItem</a>(from, to, geoip, expires));
<a name="l00128"></a>00128 }
<a name="l00129"></a>00129 
<a name="l00130"></a>00130 <span class="keywordtype">void</span>
<a name="l00131"></a><a class="code" href="class_geo_ip_cache.html#ac964304d7cd627bc5f0423d358c3bec6">00131</a> <a class="code" href="class_geo_ip_cache.html#a6fa5f291254511cadb420c04db8d0aba">GeoIpCache::addToCache</a>(<span class="keyword">const</span> <a class="code" href="class_geo_ip_cache_item.html">GeoIpCacheItem</a> &amp;ci)
<a name="l00132"></a>00132 {
<a name="l00133"></a>00133   <span class="keywordflow">if</span> (! ci.<a class="code" href="class_geo_ip_cache_item.html#a7875b8609f1f72b75770ea4ac909218b">isValid</a>() || ci.<a class="code" href="class_geo_ip_cache_item.html#aa044a2be3a9f99738beb25c87313a237">isExpired</a>())
<a name="l00134"></a>00134     <span class="keywordflow">return</span>;
<a name="l00135"></a>00135 
<a name="l00136"></a>00136   <span class="comment">/* The key for the cache is the last IP address in the range of IP addresses</span>
<a name="l00137"></a>00137 <span class="comment">   * covered by the GeoIpCacheItem. We do this because QMap::upperBound() and </span>
<a name="l00138"></a>00138 <span class="comment">   * QMap::lowerBound() return an iterator to the next item higher than the</span>
<a name="l00139"></a>00139 <span class="comment">   * search value (an IP address) if an exact match is not found.</span>
<a name="l00140"></a>00140 <span class="comment">   */</span>
<a name="l00141"></a>00141   <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.insert(ci.<a class="code" href="class_geo_ip_cache_item.html#a38ed4f56779ba54c3a4b56216d9a1cb4">ipRangeEnd</a>().toIPv4Address(), ci);
<a name="l00142"></a>00142 }
<a name="l00143"></a>00143 
<a name="l00144"></a>00144 <a class="code" href="class_geo_ip.html">GeoIp</a>
<a name="l00145"></a><a class="code" href="class_geo_ip_cache.html#aca04d2de2e69721f241efc5ad58f0136">00145</a> <a class="code" href="class_geo_ip_cache.html#aca04d2de2e69721f241efc5ad58f0136">GeoIpCache::geoIpForAddress</a>(<span class="keyword">const</span> QHostAddress &amp;ip)
<a name="l00146"></a>00146 {
<a name="l00147"></a>00147   GeoIpCacheMap::iterator <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.upperBound(ip.toIPv4Address());
<a name="l00148"></a>00148   <span class="keywordflow">if</span> (i != <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.end() &amp;&amp; (*i).contains(ip)) 
<a name="l00149"></a>00149     <span class="keywordflow">return</span> (*i).toGeoIp(ip);
<a name="l00150"></a>00150   <span class="keywordflow">return</span> <a class="code" href="class_geo_ip.html">GeoIp</a>();
<a name="l00151"></a>00151 }
<a name="l00152"></a>00152 
<a name="l00153"></a>00153 <span class="keywordtype">bool</span>
<a name="l00154"></a><a class="code" href="class_geo_ip_cache.html#a3f5f4ed2bd9edc9da640c1d47eec3d1a">00154</a> <a class="code" href="class_geo_ip_cache.html#a3f5f4ed2bd9edc9da640c1d47eec3d1a">GeoIpCache::contains</a>(<span class="keyword">const</span> QHostAddress &amp;ip)
<a name="l00155"></a>00155 {
<a name="l00156"></a>00156   GeoIpCacheMap::iterator <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.upperBound(ip.toIPv4Address());
<a name="l00157"></a>00157   <span class="keywordflow">return</span> (i != <a class="code" href="class_geo_ip_cache.html#a2b911a5de7501b6dfcd54569873839a7">_cache</a>.end() &amp;&amp; (*i).contains(ip));
<a name="l00158"></a>00158 }
<a name="l00159"></a>00159 
<a name="l00160"></a>00160 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 2010 for Vidalia by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>