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