Vidalia 0.2.12

ExitPolicy.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 ExitPolicy.cpp
00013 ** \brief Collection of Policy objects representing an exit policy 
00014 */
00015 
00016 #include "ExitPolicy.h"
00017 
00018 #include <QStringList>
00019 
00020 
00021 /** Default constructor. */
00022 ExitPolicy::ExitPolicy()
00023 {
00024 }
00025 
00026 /** Constructor. Creates an exit policy based on the given type. */
00027 ExitPolicy::ExitPolicy(SpecialExitPolicy exitPolicy)
00028 {
00029   if (exitPolicy == Middleman) {
00030     _exitPolicy << Policy(Policy::RejectAll);
00031   } else if (exitPolicy == Default) {
00032     _exitPolicy << Policy("reject *:25")
00033                 << Policy("reject *:119")
00034                 << Policy("reject *:135-139")
00035                 << Policy("reject *:445")
00036                 << Policy("reject *:465")
00037                 << Policy("reject *:587")
00038                 << Policy("reject *:1214")
00039                 << Policy("reject *:4661-4666")
00040                 << Policy("reject *:6346-6429")
00041                 << Policy("reject *:6699")
00042                 << Policy("reject *:6881-6999")
00043                 << Policy("accept *:*");
00044   }
00045 }
00046 
00047 /** Parses the given string for a comma-delimited list of policies and 
00048  * adds them to this this policy. */
00049 ExitPolicy::ExitPolicy(QString exitPolicy)
00050 {
00051   if (!exitPolicy.isEmpty()) {
00052     QStringList policyList = exitPolicy.split(",");
00053     foreach(QString policy, policyList) {
00054       addPolicy(Policy(policy));
00055     }
00056   }
00057 }
00058 
00059 /** Adds a policy to this exit policy. */
00060 void
00061 ExitPolicy::addPolicy(Policy policy)
00062 {
00063   if (!contains(policy)) {
00064     _exitPolicy << policy;
00065   }
00066 }
00067 
00068 /** Removes a policy from this exit policy. */
00069 void
00070 ExitPolicy::removePolicy(Policy policy)
00071 {
00072   for (int i = 0; i < _exitPolicy.size(); i++) {
00073     if (policy == _exitPolicy.at(i)) {
00074       _exitPolicy.removeAt(i);
00075       return;
00076     }
00077   }
00078 }
00079 
00080 /** Adds the ports specified in <b>portList</b> to a list of ports accepted
00081  * by this exit policy. Ports may be given either individually or as ranges. */
00082 void
00083 ExitPolicy::addAcceptedPorts(QStringList portList)
00084 {
00085   foreach (QString port, portList) {
00086     addPolicy(Policy("accept *:" + port));
00087   }
00088 }
00089 
00090 /** Returns true if this exit policy accepts all ports specified in
00091  * <b>portList</b>. Ports in <b>portList</b> may be given either individually
00092  * or in ranges (e.g., "6660-6669"). */
00093 bool
00094 ExitPolicy::acceptsPorts(QStringList portList)
00095 {
00096   foreach (QString port, portList) {
00097     if (!contains(Policy("accept *:" + port))) {
00098       return false;
00099     }
00100   }
00101   return true;
00102 }
00103 
00104 /** Adds the ports specified in <b>portList</b> to a list of ports rejected
00105  * by this exit policy. Ports may be given either individually or as ranges. */
00106 void
00107 ExitPolicy::addRejectedPorts(QStringList portList)
00108 {
00109   foreach (QString port, portList) {
00110     addPolicy(Policy("reject *:" + port));
00111   }
00112 }
00113 
00114 /** Returns true if this exit policy rejects all ports specified in
00115  * <b>portList</b>. Ports in <b>portList</b> may be given either individually
00116  * or in ranges (e.g., "6660-6669"). */
00117 bool
00118 ExitPolicy::rejectsPorts(QStringList portList)
00119 {
00120   foreach (QString port, portList) {
00121     if (!contains(Policy("reject *:" + port))) {
00122       return false;
00123     }
00124   }
00125   return true;
00126 }
00127 
00128 /** Returns true if this exit policy contains the given policy. */ 
00129 bool
00130 ExitPolicy::contains(Policy policy)
00131 {
00132   Policy acceptAll(Policy::AcceptAll);
00133   Policy rejectAll(Policy::RejectAll);
00134   
00135   /* Check for this policy item in the explicitly defined policies */
00136   foreach (Policy p, _exitPolicy) {
00137     if (p.matches(policy)) {
00138       return true;
00139     }
00140     if ((p == acceptAll) || (p == rejectAll)) {
00141       /* This exit policy replaces the default policy, so stop checking */
00142       return false;
00143     }
00144   }
00145   /* Now check the default exit policy */
00146   foreach (Policy p, ExitPolicy(Default).policyList()) {
00147     if (p.matches(policy)) {
00148       return true;
00149     }
00150   }
00151   return false;
00152 }
00153 
00154 /** Converts the exit policy to a format Tor understands. */
00155 QString
00156 ExitPolicy::toString()
00157 {
00158   QStringList policyList;
00159   foreach (Policy policy, _exitPolicy) {
00160     policyList << policy.toString();
00161   }
00162   return policyList.join(",");
00163 }
00164