]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Eui64.cc
Merged from trunk
[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 Splay<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 delete data;
47 }
48 }
49
50 char const *
51 ACLEui64::typeString() const
52 {
53 return class_;
54 }
55
56 bool
57 ACLEui64::empty () const
58 {
59 return data->empty();
60 }
61
62 Eui::Eui64 *
63 aclParseEuiData(const char *t)
64 {
65 char buf[256];
66 Eui::Eui64 *q = new Eui::Eui64;
67 debugs(28, 5, "aclParseEuiData: " << t);
68
69 if (sscanf(t, "%[0-9a-fA-F:]", buf) != 1) {
70 debugs(28, DBG_CRITICAL, "aclParseEuiData: Bad EUI-64 address: '" << t << "'");
71 safe_free(q);
72 return NULL;
73 }
74
75 if (!q->decode(buf)) {
76 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
77 debugs(28, DBG_CRITICAL, "aclParseEuiData: Ignoring invalid EUI-64 acl entry: can't parse '" << buf << "'");
78 safe_free(q);
79 return NULL;
80 }
81
82 return q;
83 }
84
85 /*******************/
86 /* aclParseEuiList */
87 /*******************/
88 void
89 ACLEui64::parse()
90 {
91 aclParseEuiList(&data);
92 }
93
94 void
95 aclParseEuiList(Splay<Eui::Eui64 *> **curlist)
96 {
97 char *t = NULL;
98 Eui::Eui64 *q = NULL;
99
100 while ((t = strtokFile())) {
101 if ((q = aclParseEuiData(t)) == NULL)
102 continue;
103
104 (*curlist)->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(Splay<Eui::Eui64 *> **dataptr, Ip::Address &c)
127 {
128 Eui::Eui64 lookingFor;
129
130 if (lookingFor.lookup(c)) {
131 Eui::Eui64 * const * lookupResult = (*dataptr)->find(&lookingFor, aclEui64Compare);
132 debugs(28, 3, "aclMatchEui: '" << c << "' " << (lookupResult ? "found" : "NOT found"));
133 return (lookupResult != NULL);
134 }
135
136 /*
137 * Address was not found on any interface
138 */
139 debugs(28, 3, "aclMatchEui: " << c << " NOT found");
140 return 0;
141 }
142
143 static int
144 aclEui64Compare(Eui::Eui64 * const &a, Eui::Eui64 * const &b)
145 {
146 return memcmp(a, b, sizeof(Eui::Eui64));
147 }
148
149 struct AclEui64DumpVisitor {
150 SBufList contents;
151 void operator() ( const Eui::Eui64 * v) {
152 static char buf[48];
153 v->encode(buf, 48);
154 contents.push_back(SBuf(buf));
155 }
156 };
157
158 SBufList
159 ACLEui64::dump() const
160 {
161 AclEui64DumpVisitor visitor;
162 data->visit(visitor);
163 return visitor.contents;
164 }
165
166 #endif /* USE_SQUID_EUI */
167