2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
10 #include <netinet/in.h>
18 static struct patricia_node
*tree
= NULL
;
20 static struct rwlock tree_lock
;
22 void trie_addr_lookup(char *buff
, size_t len
, int ipv4
, int *fd
,
23 struct sockaddr_storage
*addr
, size_t *alen
)
27 struct ipv4hdr
*hdr4
= (void *) buff
;
28 struct ipv6hdr
*hdr6
= (void *) buff
;
30 data
= ipv4
? (void *) &hdr4
->h_daddr
: (void *) &hdr6
->daddr
;
31 dlen
= ipv4
? sizeof(hdr4
->h_daddr
) : sizeof(hdr6
->daddr
);
33 if (unlikely((ipv4
&& ((struct ipv4hdr
*) buff
)->h_version
!= 4) ||
34 (!ipv4
&& ((struct ipv6hdr
*) buff
)->version
!= 6))) {
35 memset(addr
, 0, sizeof(*addr
));
41 /* Always happens on the dst address */
42 rwlock_rd_lock(&tree_lock
);
43 (*fd
) = ptree_search_data_exact(data
, dlen
, addr
, alen
, tree
);
44 rwlock_unlock(&tree_lock
);
47 int trie_addr_maybe_update(char *buff
, size_t len
, int ipv4
, int fd
,
48 struct sockaddr_storage
*addr
, size_t alen
)
53 struct ipv4hdr
*hdr4
= (void *) buff
;
54 struct ipv6hdr
*hdr6
= (void *) buff
;
56 data
= ipv4
? (void *) &hdr4
->h_saddr
: (void *) &hdr6
->saddr
;
57 dlen
= ipv4
? sizeof(hdr4
->h_saddr
) : sizeof(hdr6
->saddr
);
59 if (unlikely((ipv4
&& ((struct ipv4hdr
*) buff
)->h_version
!= 4) ||
60 (!ipv4
&& ((struct ipv6hdr
*) buff
)->version
!= 6)))
63 /* Always happens on the src address */
64 rwlock_wr_lock(&tree_lock
);
65 ret
= ptree_maybe_add_entry(data
, dlen
, fd
, addr
, alen
, &tree
);
66 rwlock_unlock(&tree_lock
);
71 void trie_addr_remove(int fd
)
74 struct patricia_node
*n
= NULL
;
76 rwlock_wr_lock(&tree_lock
);
79 ptree_get_key(fd
, tree
, &n
);
81 ptree_del_entry(n
->key
, n
->klen
, &tree
);
87 rwlock_unlock(&tree_lock
);
90 void trie_addr_remove_addr(struct sockaddr_storage
*addr
, size_t alen
)
93 struct patricia_node
*n
= NULL
;
95 rwlock_wr_lock(&tree_lock
);
98 ptree_get_key_addr(addr
, alen
, tree
, &n
);
100 ptree_del_entry(n
->key
, n
->klen
, &tree
);
106 rwlock_unlock(&tree_lock
);
111 rwlock_init(&tree_lock
);
114 void trie_cleanup(void)
116 rwlock_wr_lock(&tree_lock
);
118 rwlock_unlock(&tree_lock
);
120 rwlock_destroy(&tree_lock
);