]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/Tree.h
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / acl / Tree.h
CommitLineData
bbc27441 1/*
bf95c10a 2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
bbc27441
AJ
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
6f58d7d7
AR
9#ifndef SQUID_ACL_TREE_H
10#define SQUID_ACL_TREE_H
11
12#include "acl/BoolOps.h"
58d8264f 13#include "sbuf/List.h"
6f58d7d7 14
e936c41c
AR
15namespace Acl
16{
6f58d7d7
AR
17
18/// An ORed set of rules at the top of the ACL expression tree, providing two
19/// unique properties: cbdata protection and optional rule actions.
20class Tree: public OrNode
21{
5c2f68b7
AJ
22 // XXX: We should use refcounting instead, but it requires making ACLs
23 // refcounted as well. Otherwise, async lookups will reach deleted ACLs.
24 CBDATA_CLASS(Tree);
25
6f58d7d7
AR
26public:
27 /// dumps <name, action, rule, new line> tuples
ba6fffba
EB
28 /// the supplied converter maps action.kind to a string
29 template <class ActionToStringConverter>
30 SBufList treeDump(const char *name, ActionToStringConverter converter) const;
6f58d7d7
AR
31
32 /// Returns the corresponding action after a successful tree match.
329c128c 33 Answer winningAction() const;
6f58d7d7
AR
34
35 /// what action to use if no nodes matched
329c128c 36 Answer lastAction() const;
6f58d7d7
AR
37
38 /// appends and takes control over the rule with a given action
329c128c 39 void add(ACL *rule, const Answer &action);
6f58d7d7
AR
40 void add(ACL *rule); ///< same as InnerNode::add()
41
42protected:
640fe8fb 43 /// Acl::OrNode API
fd055a19 44 virtual bool bannedAction(ACLChecklist *, Nodes::const_iterator) const override;
329c128c 45 Answer actionAt(const Nodes::size_type pos) const;
6f58d7d7
AR
46
47 /// if not empty, contains actions corresponding to InnerNode::nodes
329c128c 48 typedef std::vector<Answer> Actions;
6f58d7d7 49 Actions actions;
6f58d7d7
AR
50};
51
ba6fffba 52inline const char *
329c128c 53AllowOrDeny(const Answer &action)
ba6fffba 54{
06bf5384 55 return action.allowed() ? "allow" : "deny";
ba6fffba
EB
56}
57
58template <class ActionToStringConverter>
59inline SBufList
60Tree::treeDump(const char *prefix, ActionToStringConverter converter) const
61{
62 SBufList text;
63 Actions::const_iterator action = actions.begin();
64 typedef Nodes::const_iterator NCI;
65 for (NCI node = nodes.begin(); node != nodes.end(); ++node) {
66
67 text.push_back(SBuf(prefix));
68
69 if (action != actions.end()) {
70 static const SBuf DefaultActString("???");
71 const char *act = converter(*action);
72 text.push_back(act ? SBuf(act) : DefaultActString);
73 ++action;
74 }
75
ba6fffba 76 text.splice(text.end(), (*node)->dump());
ba6fffba
EB
77 text.push_back(SBuf("\n"));
78 }
79 return text;
80}
81
6f58d7d7
AR
82} // namespace Acl
83
84#endif /* SQUID_ACL_TREE_H */
f53969cc 85