]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/IntRange.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / IntRange.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/IntRange.h"
13 #include "cache_cf.h"
14 #include "Debug.h"
15 #include "fatal.h"
16 #include "Parsing.h"
17
18 void
19 ACLIntRange::parse()
20 {
21 char *a;
22
23 while ((a = strtokFile())) {
24 char *b = strchr(a, '-');
25 unsigned short port1, port2;
26
27 if (b) {
28 *b = '\0';
29 ++b;
30 }
31
32 port1 = xatos(a);
33
34 if (b)
35 port2 = xatos(b);
36 else
37 port2 = port1;
38
39 if (port2 >= port1) {
40 RangeType temp(port1, port2+1);
41 ranges.push_back(temp);
42 } else {
43 debugs(28, DBG_CRITICAL, "ACLIntRange::parse: Invalid port value");
44 self_destruct();
45 }
46 }
47 }
48
49 bool
50 ACLIntRange::empty() const
51 {
52 return ranges.empty();
53 }
54
55 bool
56 ACLIntRange::match(int i)
57 {
58 RangeType const toFind(i, i+1);
59 for (std::list<RangeType>::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter) {
60 const RangeType & element = *iter;
61 RangeType result = element.intersection(toFind);
62
63 if (result.size())
64 return true;
65 }
66
67 return false;
68 }
69
70 ACLData<int> *
71 ACLIntRange::clone() const
72 {
73 if (!ranges.empty())
74 fatal("ACLIntRange::clone: attempt to clone used ACL");
75
76 return new ACLIntRange(*this);
77 }
78
79 ACLIntRange::~ACLIntRange()
80 {}
81
82 SBufList
83 ACLIntRange::dump() const
84 {
85 SBufList sl;
86 for (std::list<RangeType>::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter) {
87 SBuf sb;
88 const RangeType & element = *iter;
89
90 if (element.size() == 1)
91 sb.Printf("%d", element.start);
92 else
93 sb.Printf("%d-%d", element.start, element.end-1);
94
95 sl.push_back(sb);
96 }
97
98 return sl;
99 }
100