Vidalia 0.3.1
ExitPolicy.cpp
Go to the documentation of this file.
1/*
2** This file is part of Vidalia, and is subject to the license terms in the
3** LICENSE file, found in the top level directory of this distribution. If you
4** did not receive the LICENSE file with this file, you may obtain it from the
5** Vidalia source package distributed by the Vidalia Project at
6** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7** including this file, may be copied, modified, propagated, or distributed
8** except according to the terms described in the LICENSE file.
9*/
10
11/*
12** \file ExitPolicy.cpp
13** \brief Collection of Policy objects representing an exit policy
14*/
15
16#include "ExitPolicy.h"
17
18#include <QStringList>
19
20
21/** Default constructor. */
23{
24}
25
26/** Constructor. Creates an exit policy based on the given type. */
28{
29 if (exitPolicy == Middleman) {
31 } else if (exitPolicy == Default) {
32 _exitPolicy << Policy("reject *:25")
33 << Policy("reject *:119")
34 << Policy("reject *:135-139")
35 << Policy("reject *:445")
36 << Policy("reject *:465")
37 << Policy("reject *:587")
38 << Policy("reject *:1214")
39 << Policy("reject *:4661-4666")
40 << Policy("reject *:6346-6429")
41 << Policy("reject *:6699")
42 << Policy("reject *:6881-6999")
43 << Policy("accept *:*");
44 }
45}
46
47/** Parses the given string for a comma-delimited list of policies and
48 * adds them to this this policy. */
49ExitPolicy::ExitPolicy(QString exitPolicy)
50{
51 if (!exitPolicy.isEmpty()) {
52 QStringList policyList = exitPolicy.split(",");
53 foreach(QString policy, policyList) {
54 addPolicy(Policy(policy));
55 }
56 }
57}
58
59/** Adds a policy to this exit policy. */
60void
62{
63 if (!contains(policy)) {
64 _exitPolicy << policy;
65 }
66}
67
68/** Removes a policy from this exit policy. */
69void
71{
72 for (int i = 0; i < _exitPolicy.size(); i++) {
73 if (policy == _exitPolicy.at(i)) {
74 _exitPolicy.removeAt(i);
75 return;
76 }
77 }
78}
79
80/** Adds the ports specified in <b>portList</b> to a list of ports accepted
81 * by this exit policy. Ports may be given either individually or as ranges. */
82void
83ExitPolicy::addAcceptedPorts(QStringList portList)
84{
85 foreach (QString port, portList) {
86 addPolicy(Policy("accept *:" + port));
87 }
88}
89
90/** Returns true if this exit policy accepts all ports specified in
91 * <b>portList</b>. Ports in <b>portList</b> may be given either individually
92 * or in ranges (e.g., "6660-6669"). */
93bool
94ExitPolicy::acceptsPorts(QStringList portList)
95{
96 foreach (QString port, portList) {
97 if (!contains(Policy("accept *:" + port))) {
98 return false;
99 }
100 }
101 return true;
102}
103
104/** Adds the ports specified in <b>portList</b> to a list of ports rejected
105 * by this exit policy. Ports may be given either individually or as ranges. */
106void
107ExitPolicy::addRejectedPorts(QStringList portList)
108{
109 foreach (QString port, portList) {
110 addPolicy(Policy("reject *:" + port));
111 }
112}
113
114/** Returns true if this exit policy rejects all ports specified in
115 * <b>portList</b>. Ports in <b>portList</b> may be given either individually
116 * or in ranges (e.g., "6660-6669"). */
117bool
118ExitPolicy::rejectsPorts(QStringList portList)
119{
120 foreach (QString port, portList) {
121 if (!contains(Policy("reject *:" + port))) {
122 return false;
123 }
124 }
125 return true;
126}
127
128/** Returns true if this exit policy contains the given policy. */
129bool
131{
132 Policy acceptAll(Policy::AcceptAll);
133 Policy rejectAll(Policy::RejectAll);
134
135 /* Check for this policy item in the explicitly defined policies */
136 foreach (Policy p, _exitPolicy) {
137 if (p.matches(policy)) {
138 return true;
139 }
140 if ((p == acceptAll) || (p == rejectAll)) {
141 /* This exit policy replaces the default policy, so stop checking */
142 return false;
143 }
144 }
145 /* Now check the default exit policy */
146 foreach (Policy p, ExitPolicy(Default).policyList()) {
147 if (p.matches(policy)) {
148 return true;
149 }
150 }
151 return false;
152}
153
154/** Converts the exit policy to a format Tor understands. */
155QString
157{
158 QStringList policyList;
159 foreach (Policy policy, _exitPolicy) {
160 policyList << policy.toString();
161 }
162 return policyList.join(",");
163}
164
bool contains(Policy policy)
Definition: ExitPolicy.cpp:130
void addRejectedPorts(QStringList portList)
Definition: ExitPolicy.cpp:107
void addPolicy(Policy policy)
Definition: ExitPolicy.cpp:61
QList< Policy > policyList()
Definition: ExitPolicy.h:65
bool rejectsPorts(QStringList portList)
Definition: ExitPolicy.cpp:118
QList< Policy > _exitPolicy
Definition: ExitPolicy.h:72
bool acceptsPorts(QStringList portList)
Definition: ExitPolicy.cpp:94
QString toString()
Definition: ExitPolicy.cpp:156
void addAcceptedPorts(QStringList portList)
Definition: ExitPolicy.cpp:83
void removePolicy(Policy policy)
Definition: ExitPolicy.cpp:70
Definition: Policy.h:25
QString toString() const
Definition: Policy.cpp:143
@ AcceptAll
Definition: Policy.h:36
@ RejectAll
Definition: Policy.h:37
QString i(QString str)
Definition: html.cpp:32
QString p(QString str)
Definition: html.cpp:22