exitpolicy.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file exitpolicy.cpp
00024  * \version $Id: exitpolicy.cpp 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief Collection of Policy objects representing an exit policy 
00026  */
00027 
00028 #include <QStringList>
00029 
00030 #include "exitpolicy.h"
00031 
00032 
00033 /** Default constructor. */
00034 ExitPolicy::ExitPolicy()
00035 {
00036 }
00037 
00038 /** Constructor. Creates an exit policy based on the given type. */
00039 ExitPolicy::ExitPolicy(SpecialExitPolicy exitPolicy)
00040 {
00041   if (exitPolicy == Middleman) {
00042     _exitPolicy << Policy(Policy::RejectAll);
00043   } else if (exitPolicy == Default) {
00044     _exitPolicy << Policy("reject *:25")
00045                 << Policy("reject *:119")
00046                 << Policy("reject *:135-139")
00047                 << Policy("reject *:445")
00048                 << Policy("reject *:465")
00049                 << Policy("reject *:587")
00050                 << Policy("reject *:1214")
00051                 << Policy("reject *:4661-4666")
00052                 << Policy("reject *:6346-6429")
00053                 << Policy("reject *:6699")
00054                 << Policy("reject *:6881-6999")
00055                 << Policy("accept *:*");
00056   }
00057 }
00058 
00059 /** Parses the given string for a comma-delimited list of policies and 
00060  * adds them to this this policy. */
00061 ExitPolicy::ExitPolicy(QString exitPolicy)
00062 {
00063   if (!exitPolicy.isEmpty()) {
00064     QStringList policyList = exitPolicy.split(",");
00065     foreach(QString policy, policyList) {
00066       addPolicy(Policy(policy));
00067     }
00068   }
00069 }
00070 
00071 /** Adds a policy to this exit policy. */
00072 void
00073 ExitPolicy::addPolicy(Policy policy)
00074 {
00075   if (!contains(policy)) {
00076     _exitPolicy << policy;
00077   }
00078 }
00079 
00080 /** Removes a policy from this exit policy. */
00081 void
00082 ExitPolicy::removePolicy(Policy policy)
00083 {
00084   for (int i = 0; i < _exitPolicy.size(); i++) {
00085     if (policy == _exitPolicy.at(i)) {
00086       _exitPolicy.removeAt(i);
00087       return;
00088     }
00089   }
00090 }
00091 
00092 /** Adds the ports specified in <b>portList</b> to a list of ports accepted
00093  * by this exit policy. Ports may be given either individually or as ranges. */
00094 void
00095 ExitPolicy::addAcceptedPorts(QStringList portList)
00096 {
00097   foreach (QString port, portList) {
00098     addPolicy(Policy("accept *:" + port));
00099   }
00100 }
00101 
00102 /** Returns true if this exit policy accepts all ports specified in
00103  * <b>portList</b>. Ports in <b>portList</b> may be given either individually
00104  * or in ranges (e.g., "6660-6669"). */
00105 bool
00106 ExitPolicy::acceptsPorts(QStringList portList)
00107 {
00108   foreach (QString port, portList) {
00109     if (!contains(Policy("accept *:" + port))) {
00110       return false;
00111     }
00112   }
00113   return true;
00114 }
00115 
00116 /** Adds the ports specified in <b>portList</b> to a list of ports rejected
00117  * by this exit policy. Ports may be given either individually or as ranges. */
00118 void
00119 ExitPolicy::addRejectedPorts(QStringList portList)
00120 {
00121   foreach (QString port, portList) {
00122     addPolicy(Policy("reject *:" + port));
00123   }
00124 }
00125 
00126 /** Returns true if this exit policy rejects all ports specified in
00127  * <b>portList</b>. Ports in <b>portList</b> may be given either individually
00128  * or in ranges (e.g., "6660-6669"). */
00129 bool
00130 ExitPolicy::rejectsPorts(QStringList portList)
00131 {
00132   foreach (QString port, portList) {
00133     if (!contains(Policy("reject *:" + port))) {
00134       return false;
00135     }
00136   }
00137   return true;
00138 }
00139 
00140 /** Returns true if this exit policy contains the given policy. */ 
00141 bool
00142 ExitPolicy::contains(Policy policy)
00143 {
00144   Policy acceptAll(Policy::AcceptAll);
00145   Policy rejectAll(Policy::RejectAll);
00146   
00147   /* Check for this policy item in the explicitly defined policies */
00148   foreach (Policy p, _exitPolicy) {
00149     if (p.matches(policy)) {
00150       return true;
00151     }
00152     if ((p == acceptAll) || (p == rejectAll)) {
00153       /* This exit policy replaces the default policy, so stop checking */
00154       return false;
00155     }
00156   }
00157   /* Now check the default exit policy */
00158   foreach (Policy p, ExitPolicy(Default).policyList()) {
00159     if (p.matches(policy)) {
00160       return true;
00161     }
00162   }
00163   return false;
00164 }
00165 
00166 /** Converts the exit policy to a format Tor understands. */
00167 QString
00168 ExitPolicy::toString()
00169 {
00170   QStringList policyList;
00171   foreach (Policy policy, _exitPolicy) {
00172     policyList << policy.toString();
00173   }
00174   return policyList.join(",");
00175 }
00176 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3