2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
9 #include <netinet/in.h>
17 static struct patricia_node
*tree
= NULL
;
19 static struct rwlock tree_lock
;
21 void trie_addr_lookup(char *buff
, size_t len
, int ipv4
, int *fd
,
22 struct sockaddr_storage
*addr
, size_t *alen
)
26 struct ipv4hdr
*hdr4
= (void *) buff
;
27 struct ipv6hdr
*hdr6
= (void *) buff
;
29 data
= ipv4
? (void *) &hdr4
->h_daddr
: (void *) &hdr6
->daddr
;
30 dlen
= ipv4
? sizeof(hdr4
->h_daddr
) : sizeof(hdr6
->daddr
);
32 if (unlikely((ipv4
&& ((struct ipv4hdr
*) buff
)->h_version
!= 4) ||
33 (!ipv4
&& ((struct ipv6hdr
*) buff
)->version
!= 6))) {
34 memset(addr
, 0, sizeof(*addr
));
40 rwlock_rd_lock(&tree_lock
);
41 (*fd
) = ptree_search_data_exact(data
, dlen
, addr
, alen
, tree
);
42 rwlock_unlock(&tree_lock
);
45 int trie_addr_maybe_update(char *buff
, size_t len
, int ipv4
, int fd
,
46 struct sockaddr_storage
*addr
, size_t alen
)
51 struct ipv4hdr
*hdr4
= (void *) buff
;
52 struct ipv6hdr
*hdr6
= (void *) buff
;
54 data
= ipv4
? (void *) &hdr4
->h_saddr
: (void *) &hdr6
->saddr
;
55 dlen
= ipv4
? sizeof(hdr4
->h_saddr
) : sizeof(hdr6
->saddr
);
57 if (unlikely((ipv4
&& ((struct ipv4hdr
*) buff
)->h_version
!= 4) ||
58 (!ipv4
&& ((struct ipv6hdr
*) buff
)->version
!= 6)))
61 rwlock_wr_lock(&tree_lock
);
62 ret
= ptree_add_entry(data
, dlen
, fd
, addr
, alen
, &tree
);
63 rwlock_unlock(&tree_lock
);
68 void trie_addr_remove(int fd
)
71 struct patricia_node
*n
= NULL
;
73 rwlock_wr_lock(&tree_lock
);
76 ptree_get_key(fd
, tree
, &n
);
78 ptree_del_entry(n
->key
, n
->klen
, &tree
);
84 rwlock_unlock(&tree_lock
);
87 void trie_addr_remove_addr(struct sockaddr_storage
*addr
, size_t alen
)
90 struct patricia_node
*n
= NULL
;
92 rwlock_wr_lock(&tree_lock
);
95 ptree_get_key_addr(addr
, alen
, tree
, &n
);
97 ptree_del_entry(n
->key
, n
->klen
, &tree
);
103 rwlock_unlock(&tree_lock
);
108 rwlock_init(&tree_lock
);
111 void trie_cleanup(void)
113 rwlock_wr_lock(&tree_lock
);
115 rwlock_unlock(&tree_lock
);
116 rwlock_destroy(&tree_lock
);