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