summaryrefslogtreecommitdiff
path: root/trie.c
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-03-15 10:41:48 +0100
committerDaniel Borkmann <dborkman@redhat.com>2013-03-15 10:41:48 +0100
commit1a9fbac03c684f29cff9ac44875bd9504a89f54e (patch)
tree1b2e40dbe5dc1899ef5b62c4325c9b94c9c450fc /trie.c
all: import netsniff-ng 0.5.8-rc0 source
We decided to get rid of the old Git history and start a new one for several reasons: *) Allow / enforce only high-quality commits (which was not the case for many commits in the history), have a policy that is more close to the one from the Linux kernel. With high quality commits, we mean code that is logically split into commits and commit messages that are signed-off and have a proper subject and message body. We do not allow automatic Github merges anymore, since they are total bullshit. However, we will either cherry-pick your patches or pull them manually. *) The old archive was about ~27MB for no particular good reason. This basically derived from the bad decision that also some PDF files where stored there. From this moment onwards, no binary objects are allowed to be stored in this repository anymore. The old archive is not wiped away from the Internet. You will still be able to find it, e.g. on git.cryptoism.org etc. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trie.c')
-rw-r--r--trie.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/trie.c b/trie.c
new file mode 100644
index 0000000..f0eecd5
--- /dev/null
+++ b/trie.c
@@ -0,0 +1,117 @@
+/*
+ * netsniff-ng - the packet sniffing beast
+ * Copyright 2011 Daniel Borkmann.
+ * Subject to the GPL, version 2.
+ */
+
+#include <stdint.h>
+#include <string.h>
+#include <netinet/in.h>
+
+#include "patricia.h"
+#include "locking.h"
+#include "trie.h"
+#include "ipv4.h"
+#include "ipv6.h"
+
+static struct patricia_node *tree = NULL;
+
+static struct rwlock tree_lock;
+
+void trie_addr_lookup(char *buff, size_t len, int ipv4, int *fd,
+ struct sockaddr_storage *addr, size_t *alen)
+{
+ void *data;
+ size_t dlen;
+ struct ipv4hdr *hdr4 = (void *) buff;
+ struct ipv6hdr *hdr6 = (void *) buff;
+
+ data = ipv4 ? (void *) &hdr4->h_daddr : (void *) &hdr6->daddr;
+ dlen = ipv4 ? sizeof(hdr4->h_daddr) : sizeof(hdr6->daddr);
+
+ if (unlikely((ipv4 && ((struct ipv4hdr *) buff)->h_version != 4) ||
+ (!ipv4 && ((struct ipv6hdr *) buff)->version != 6))) {
+ memset(addr, 0, sizeof(*addr));
+ (*alen) = 0;
+ (*fd) = -1;
+ return;
+ }
+
+ rwlock_rd_lock(&tree_lock);
+ (*fd) = ptree_search_data_exact(data, dlen, addr, alen, tree);
+ rwlock_unlock(&tree_lock);
+}
+
+int trie_addr_maybe_update(char *buff, size_t len, int ipv4, int fd,
+ struct sockaddr_storage *addr, size_t alen)
+{
+ int ret;
+ void *data;
+ size_t dlen;
+ struct ipv4hdr *hdr4 = (void *) buff;
+ struct ipv6hdr *hdr6 = (void *) buff;
+
+ data = ipv4 ? (void *) &hdr4->h_saddr : (void *) &hdr6->saddr;
+ dlen = ipv4 ? sizeof(hdr4->h_saddr) : sizeof(hdr6->saddr);
+
+ if (unlikely((ipv4 && ((struct ipv4hdr *) buff)->h_version != 4) ||
+ (!ipv4 && ((struct ipv6hdr *) buff)->version != 6)))
+ return -1;
+
+ rwlock_wr_lock(&tree_lock);
+ ret = ptree_add_entry(data, dlen, fd, addr, alen, &tree);
+ rwlock_unlock(&tree_lock);
+
+ return ret;
+}
+
+void trie_addr_remove(int fd)
+{
+ int found = 1;
+ struct patricia_node *n = NULL;
+
+ rwlock_wr_lock(&tree_lock);
+
+ while (found) {
+ ptree_get_key(fd, tree, &n);
+ if (n) {
+ ptree_del_entry(n->key, n->klen, &tree);
+ n = NULL;
+ } else
+ found = 0;
+ }
+
+ rwlock_unlock(&tree_lock);
+}
+
+void trie_addr_remove_addr(struct sockaddr_storage *addr, size_t alen)
+{
+ int found = 1;
+ struct patricia_node *n = NULL;
+
+ rwlock_wr_lock(&tree_lock);
+
+ while (found) {
+ ptree_get_key_addr(addr, alen, tree, &n);
+ if (n) {
+ ptree_del_entry(n->key, n->klen, &tree);
+ n = NULL;
+ } else
+ found = 0;
+ }
+
+ rwlock_unlock(&tree_lock);
+}
+
+void trie_init(void)
+{
+ rwlock_init(&tree_lock);
+}
+
+void trie_cleanup(void)
+{
+ rwlock_wr_lock(&tree_lock);
+ ptree_free(tree);
+ rwlock_unlock(&tree_lock);
+ rwlock_destroy(&tree_lock);
+}