The Pedigree Project  0.1
Filter.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Filter.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/utilities/Iterator.h"
23 #include "pedigree/kernel/utilities/utility.h"
24 
25 NetworkFilter NetworkFilter::m_Instance;
26 
28 {
29 }
30 
32 {
33 }
34 
35 bool NetworkFilter::filter(size_t level, uintptr_t packet, size_t sz)
36 {
37  // Check for a valid level
38  if (level <= 4 && level > 0)
39  {
40  // Grab the callback list
41  typedef List<void *>::Iterator callbackIterator;
42  List<void *> *list = m_Callbacks.lookup(level);
43  if (list)
44  {
45  // Iterate, call each callback until one returns false
46  for (callbackIterator it = list->begin(); it != list->end(); ++it)
47  {
48  bool (*callback)(uintptr_t, size_t) =
49  reinterpret_cast<bool (*)(uintptr_t, size_t)>(*it);
50  bool result = callback(packet, sz);
51  if (!result)
52  return result; // Short-circuit. This way we avoid
53  // executing extra filters if one says to
54  // drop.
55  }
56  }
57  }
58 
59  // Default response: allow packet
60  return true;
61 }
62 
64  size_t level, bool (*callback)(uintptr_t, size_t))
65 {
67 
68  // Check for a valid level
69  if (level <= 4 && level > 0)
70  {
71  // Grab the callback list
72  List<void *> *list = m_Callbacks.lookup(level);
73 
74  // If it already exists, add the callback
75  if (list)
76  {
77  // We return the index into the list of this callback
78  size_t index = list->count();
79  list->pushBack(reinterpret_cast<void *>(callback));
80  return index;
81  }
82  // Otherwise, allocate
83  else
84  {
85  list = new List<void *>;
86  if (!list)
87  {
88  ERROR(
89  "Ran out of memory creating list for level "
90  << Dec << level << Hex << " callbacks!");
91  return static_cast<size_t>(-1);
92  }
93 
94  list->pushBack(reinterpret_cast<void *>(callback));
95  m_Callbacks.insert(level, list);
96 
97  // First item
98  return 0;
99  }
100  }
101 
102  // Invalid input
103  return static_cast<size_t>(-1);
104 }
105 
106 void NetworkFilter::removeCallback(size_t level, size_t id)
107 {
109 }
void pushBack(const T &value)
Definition: List.h:232
bool filter(size_t level, uintptr_t packet, size_t sz)
Definition: Filter.cc:35
void removeCallback(size_t level, size_t id)
Definition: Filter.cc:106
NetworkFilter()
Default constructor, boring.
Definition: Filter.cc:27
Definition: List.h:64
void insert(const K &key, const E &value)
Definition: Tree.h:173
Definition: Log.h:136
Iterator begin()
Definition: List.h:123
size_t installCallback(size_t level, bool(*callback)(uintptr_t, size_t))
Definition: Filter.cc:63
Tree< size_t, List< void * > * > m_Callbacks
Level -> Callback list mapping.
Definition: Filter.h:75
#define ERROR(text)
Definition: Log.h:82
Definition: Log.h:138
virtual ~NetworkFilter()
Destructor, also boring.
Definition: Filter.cc:31
Iterator end()
Definition: List.h:135
E lookup(const K &key) const
Definition: Tree.h:192
size_t count() const
Definition: List.h:227