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 1238 2006-09-25 17:50:57Z 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     addPolicy(Policy(Policy::RejectAll));
00043   } else if (exitPolicy == Default) {
00044     addPolicy(Policy("reject *:25"));
00045     addPolicy(Policy("reject *:119"));
00046     addPolicy(Policy("reject *:135-139"));
00047     addPolicy(Policy("reject *:445"));
00048     addPolicy(Policy("reject *:465"));
00049     addPolicy(Policy("reject *:587"));
00050     addPolicy(Policy("reject *:1214"));
00051     addPolicy(Policy("reject *:4661-4666"));
00052     addPolicy(Policy("reject *:6346-6429"));
00053     addPolicy(Policy("reject *:6699"));
00054     addPolicy(Policy("reject *:6881-6999"));
00055     addPolicy(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 /** Returns true if this exit policy contains the given policy. */ 
00093 bool
00094 ExitPolicy::contains(Policy policy)
00095 {
00096   foreach (Policy p, _exitPolicy) {
00097     if (p == policy) {
00098       return true;
00099     }
00100   }
00101   return false;
00102 }
00103 
00104 /** Converts the exit policy to a format Tor understands. */
00105 QString
00106 ExitPolicy::toString()
00107 {
00108   QStringList policyList;
00109   foreach (Policy policy, _exitPolicy) {
00110     policyList << policy.toString();
00111   }
00112   return policyList.join(",");
00113 }
00114 

Generated on Mon Oct 23 20:08:15 2006 for Vidalia by  doxygen 1.5.0