]> 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-2017 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 "ConfigParser.h"
15 #include "Debug.h"
16 #include "fatal.h"
17 #include "Parsing.h"
18
19 void
20 ACLIntRange::parse()
21 {
22 while (char *a = ConfigParser::strtokFile()) {
23 char *b = strchr(a, '-');
24 unsigned short port1, port2;
25
26 if (b) {
27 *b = '\0';
28 ++b;
29 }
30
31 port1 = xatos(a);
32
33 if (b)
34 port2 = xatos(b);
35 else
36 port2 = port1;
37
38 if (port2 >= port1) {
39 RangeType temp(port1, port2+1);
40 ranges.push_back(temp);
41 } else {
42 debugs(28, DBG_CRITICAL, "ACLIntRange::parse: Invalid port value");
43 self_destruct();
44 }
45 }
46 }
47
48 bool
49 ACLIntRange::empty() const
50 {
51 return ranges.empty();
52 }
53
54 bool
55 ACLIntRange::match(int i)
56 {
57 RangeType const toFind(i, i+1);
58 for (std::list<RangeType>::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter) {
59 const RangeType & element = *iter;
60 RangeType result = element.intersection(toFind);
61
62 if (result.size())
63 return true;
64 }
65
66 return false;
67 }
68
69 ACLData<int> *
70 ACLIntRange::clone() const
71 {
72 if (!ranges.empty())
73 fatal("ACLIntRange::clone: attempt to clone used ACL");
74
75 return new ACLIntRange(*this);
76 }
77
78 ACLIntRange::~ACLIntRange()
79 {}
80
81 SBufList
82 ACLIntRange::dump() const
83 {
84 SBufList sl;
85 for (std::list<RangeType>::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter) {
86 SBuf sb;
87 const RangeType & element = *iter;
88
89 if (element.size() == 1)
90 sb.Printf("%d", element.start);
91 else
92 sb.Printf("%d-%d", element.start, element.end-1);
93
94 sl.push_back(sb);
95 }
96
97 return sl;
98 }
99