|
@@ -19,6 +19,7 @@
|
|
#include <vector>
|
|
#include <vector>
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
+#include <boost/noncopyable.hpp>
|
|
|
|
|
|
namespace isc {
|
|
namespace isc {
|
|
namespace acl {
|
|
namespace acl {
|
|
@@ -28,9 +29,10 @@ namespace acl {
|
|
*
|
|
*
|
|
* This is the default for the ACL class. It is possible to specify any other
|
|
* This is the default for the ACL class. It is possible to specify any other
|
|
* data type, as the ACL class does nothing about them, but these look
|
|
* data type, as the ACL class does nothing about them, but these look
|
|
- * reasonable, so they are provided for convenience.
|
|
|
|
|
|
+ * reasonable, so they are provided for convenience. It is not specified what
|
|
|
|
+ * exactly these mean and it's up to whoever uses them.
|
|
*/
|
|
*/
|
|
-enum Action {
|
|
|
|
|
|
+enum BasicAction {
|
|
ACCEPT,
|
|
ACCEPT,
|
|
REJECT,
|
|
REJECT,
|
|
DROP
|
|
DROP
|
|
@@ -44,42 +46,31 @@ enum Action {
|
|
* whenever the action matches. They are tested in the order and first
|
|
* whenever the action matches. They are tested in the order and first
|
|
* match counts.
|
|
* match counts.
|
|
*
|
|
*
|
|
- * \note There are protected members. In fact, you should consider them
|
|
|
|
|
|
+ * This is non-copyable. It seems that there's no need to copy them (even
|
|
|
|
+ * when it would be technically possible), so we forbid it just to prevent
|
|
|
|
+ * copying it by accident. If there really is legitimate use, this restriction
|
|
|
|
+ * can be removed.
|
|
|
|
+ *
|
|
|
|
+ * The class is template. It is possible to specify on which context the checks
|
|
|
|
+ * match and which actions it returns. The actions must be copyable
|
|
|
|
+ * for this to work and it is expected to be something small, usually an enum
|
|
|
|
+ * (but other objects are also possible).
|
|
|
|
+ *
|
|
|
|
+ * \note There are protected functions. In fact, you should consider them
|
|
* private, they are protected so tests can get inside. This class
|
|
* private, they are protected so tests can get inside. This class
|
|
* is not expected to be subclassed in real applications.
|
|
* is not expected to be subclassed in real applications.
|
|
*/
|
|
*/
|
|
-template<typename Context, typename Action = isc::acl::Action> class Acl {
|
|
|
|
-private:
|
|
|
|
- /**
|
|
|
|
- * \brief Copy constructor.
|
|
|
|
- *
|
|
|
|
- * It is private on purpose, this class is not supposed to be copied.
|
|
|
|
- * It is technically possible though, so if the need arises, it can be
|
|
|
|
- * added (or, more correctly, this privade one removed so default one
|
|
|
|
- * is created).
|
|
|
|
- */
|
|
|
|
- Acl(const Acl<Context, Action>& other);
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * \brief Assignment operator.
|
|
|
|
- *
|
|
|
|
- * It is private on purpose, this class is not supposed to be copied.
|
|
|
|
- * It is technically possible though, so if the need arises, it can be
|
|
|
|
- * added (or, more correctly, this privade one removed so default one
|
|
|
|
- * is created).
|
|
|
|
- */
|
|
|
|
- Acl& operator=(const Acl<Context, Action>& other);
|
|
|
|
-
|
|
|
|
|
|
+template<typename Context, typename Action = BasicAction> class ACL :
|
|
|
|
+ public boost::noncopyable {
|
|
public:
|
|
public:
|
|
/**
|
|
/**
|
|
* \brief Constructor.
|
|
* \brief Constructor.
|
|
*
|
|
*
|
|
- * \param policy It is the action that is returned when the checked things
|
|
|
|
- * "falls off" the end of the list (when no rule matched).
|
|
|
|
|
|
+ * \param default_action It is the action that is returned when the checked
|
|
|
|
+ * things "falls off" the end of the list (when no rule matched).
|
|
*/
|
|
*/
|
|
- Acl(Action policy) : policy_(policy)
|
|
|
|
|
|
+ ACL(const Action& default_action) : default_action_(default_action)
|
|
{}
|
|
{}
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* \brief Pointer to the check.
|
|
* \brief Pointer to the check.
|
|
*
|
|
*
|
|
@@ -87,26 +78,25 @@ public:
|
|
* However, we might need to copy the entries (when we concatenate ACLs
|
|
* However, we might need to copy the entries (when we concatenate ACLs
|
|
* together in future).
|
|
* together in future).
|
|
*/
|
|
*/
|
|
- typedef boost::shared_ptr<Check<Context> > CheckPtr;
|
|
|
|
-
|
|
|
|
|
|
+ typedef boost::shared_ptr<const Check<Context> > ConstCheckPtr;
|
|
/**
|
|
/**
|
|
* \brief The actual main function that decides.
|
|
* \brief The actual main function that decides.
|
|
*
|
|
*
|
|
* This is the function that takes the entries one by one, checks
|
|
* This is the function that takes the entries one by one, checks
|
|
* the context against conditions and if it matches, returns the
|
|
* the context against conditions and if it matches, returns the
|
|
- * action that belongs to the first matched entry or policy action
|
|
|
|
|
|
+ * action that belongs to the first matched entry or default action
|
|
* if nothing matches.
|
|
* if nothing matches.
|
|
* \param context The thing that should be checked. It is directly
|
|
* \param context The thing that should be checked. It is directly
|
|
* passed to the checks.
|
|
* passed to the checks.
|
|
*/
|
|
*/
|
|
- Action execute(const Context& context) const {
|
|
|
|
|
|
+ const Action& execute(const Context& context) const {
|
|
for (typename Entries::const_iterator i(entries_.begin());
|
|
for (typename Entries::const_iterator i(entries_.begin());
|
|
i != entries_.end(); ++i) {
|
|
i != entries_.end(); ++i) {
|
|
if (i->first->matches(context)) {
|
|
if (i->first->matches(context)) {
|
|
return (i->second);
|
|
return (i->second);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return (policy_);
|
|
|
|
|
|
+ return (default_action_);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -119,18 +109,26 @@ public:
|
|
* \param check The check to test if the thing matches.
|
|
* \param check The check to test if the thing matches.
|
|
* \param action The action to return when the thing matches this check.
|
|
* \param action The action to return when the thing matches this check.
|
|
*/
|
|
*/
|
|
- void append(CheckPtr check, const Action& action) {
|
|
|
|
|
|
+ void append(ConstCheckPtr check, const Action& action) {
|
|
entries_.push_back(Entry(check, action));
|
|
entries_.push_back(Entry(check, action));
|
|
}
|
|
}
|
|
private:
|
|
private:
|
|
// Just type abbreviations.
|
|
// Just type abbreviations.
|
|
- typedef std::pair<CheckPtr, Action> Entry;
|
|
|
|
|
|
+ typedef std::pair<ConstCheckPtr, Action> Entry;
|
|
typedef std::vector<Entry> Entries;
|
|
typedef std::vector<Entry> Entries;
|
|
-protected:
|
|
|
|
- /// \brief The policy.
|
|
|
|
- Action policy_;
|
|
|
|
|
|
+ /// \brief The default action, when nothing mathes.
|
|
|
|
+ const Action default_action_;
|
|
/// \brief The entries we have.
|
|
/// \brief The entries we have.
|
|
Entries entries_;
|
|
Entries entries_;
|
|
|
|
+protected:
|
|
|
|
+ /**
|
|
|
|
+ * \brief Get the default action.
|
|
|
|
+ *
|
|
|
|
+ * This is for testing purposes only.
|
|
|
|
+ */
|
|
|
|
+ const Action& getDefaultAction() const {
|
|
|
|
+ return (default_action_);
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
}
|
|
}
|