]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Eui64.cc
Migrated acl/Eui64 to Splay
[thirdparty/squid.git] / src / acl / Eui64.cc
1 /*
2 * Copyright (C) 1996-2014 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
13 #if USE_SQUID_EUI
14
15 #include "acl/Eui64.h"
16 #include "acl/FilledChecklist.h"
17 #include "cache_cf.h"
18 #include "Debug.h"
19 #include "eui/Eui64.h"
20 #include "globals.h"
21 #include "ip/Address.h"
22
23 static void aclParseEuiList(Splay<Eui::Eui64 *> **curlist);
24 static int aclMatchEui(Splay<Eui::Eui64 *> **dataptr, Ip::Address &c);
25 static SplayNode<Eui::Eui64 *>::SPLAYCMP aclEui64Compare;
26
27 ACL *
28 ACLEui64::clone() const
29 {
30 return new ACLEui64(*this);
31 }
32
33 ACLEui64::ACLEui64 (char const *theClass) : data (NULL), class_ (theClass)
34 {}
35
36 ACLEui64::ACLEui64 (ACLEui64 const & old) : data (NULL), class_ (old.class_)
37 {
38 /* we don't have copy constructors for the data yet */
39 assert (!old.data);
40 }
41
42 ACLEui64::~ACLEui64()
43 {
44 if (data)
45 data->destroy();
46 }
47
48 char const *
49 ACLEui64::typeString() const
50 {
51 return class_;
52 }
53
54 bool
55 ACLEui64::empty () const
56 {
57 return data->empty();
58 }
59
60 Eui::Eui64 *
61 aclParseEuiData(const char *t)
62 {
63 char buf[256];
64 Eui::Eui64 *q = new Eui::Eui64;
65 debugs(28, 5, "aclParseEuiData: " << t);
66
67 if (sscanf(t, "%[0-9a-fA-F:]", buf) != 1) {
68 debugs(28, DBG_CRITICAL, "aclParseEuiData: Bad EUI-64 address: '" << t << "'");
69 safe_free(q);
70 return NULL;
71 }
72
73 if (!q->decode(buf)) {
74 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
75 debugs(28, DBG_CRITICAL, "aclParseEuiData: Ignoring invalid EUI-64 acl entry: can't parse '" << buf << "'");
76 safe_free(q);
77 return NULL;
78 }
79
80 return q;
81 }
82
83 /*******************/
84 /* aclParseEuiList */
85 /*******************/
86 void
87 ACLEui64::parse()
88 {
89 aclParseEuiList(&data);
90 }
91
92 void
93 aclParseEuiList(Splay<Eui::Eui64 *> **curlist)
94 {
95 char *t = NULL;
96 Eui::Eui64 *q = NULL;
97
98 while ((t = strtokFile())) {
99 if ((q = aclParseEuiData(t)) == NULL)
100 continue;
101
102 (*curlist)->insert(q, aclEui64Compare);
103 }
104 }
105
106 int
107 ACLEui64::match(ACLChecklist *cl)
108 {
109 ACLFilledChecklist *checklist = Filled(cl);
110
111 /* IPv4 does not do EUI-64 (yet) */
112 if (!checklist->src_addr.isIPv6()) {
113 debugs(14, 3, "ACLEui64::match: IPv6 Required for EUI-64 Lookups. Skipping " << checklist->src_addr );
114 return 0;
115 }
116
117 return aclMatchEui(&data, checklist->src_addr);
118 }
119
120 /***************/
121 /* aclMatchEui */
122 /***************/
123 int
124 aclMatchEui(Splay<Eui::Eui64 *> **dataptr, Ip::Address &c)
125 {
126 Eui::Eui64 lookingFor;
127
128 if (lookingFor.lookup(c)) {
129 Eui::Eui64 * const * lookupResult = (*dataptr)->find(&lookingFor, aclEui64Compare);
130 debugs(28, 3, "aclMatchEui: '" << c << "' " << (lookupResult ? "found" : "NOT found"));
131 return (lookupResult != NULL);
132 }
133
134 /*
135 * Address was not found on any interface
136 */
137 debugs(28, 3, "aclMatchEui: " << c << " NOT found");
138 return 0;
139 }
140
141 static int
142 aclEui64Compare(Eui::Eui64 * const &a, Eui::Eui64 * const &b)
143 {
144 return memcmp(a, b, sizeof(Eui::Eui64));
145 }
146
147 struct AclEui64DumpVisitor {
148 SBufList contents;
149 void operator() ( const Eui::Eui64 * v) {
150 static char buf[48];
151 v->encode(buf, 48);
152 contents.push_back(SBuf(buf));
153 }
154 };
155
156 SBufList
157 ACLEui64::dump() const
158 {
159 AclEui64DumpVisitor visitor;
160 data->visit(visitor);
161 return visitor.contents;
162 }
163
164 #endif /* USE_SQUID_EUI */
165