Small IPv6 corrections and an IPv6 hash function.
[darkstat] / hosts_db.h
1 /* darkstat 3
2 * copyright (c) 2001-2008 Emil Mikulic.
3 *
4 * hosts_db.h: database of hosts, ports, protocols.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include <sys/types.h>
11 #include <netinet/in.h>
12 #include "str.h"
13
14 struct hashtable;
15
16 struct host {
17 union {
18 in_addr_t ip;
19 struct in6_addr ip6;
20 };
21 sa_family_t af;
22 char *dns;
23 uint8_t mac_addr[6];
24 time_t last_seen;
25 struct hashtable *ports_tcp, *ports_udp, *ip_protos;
26 };
27
28 struct port_tcp {
29 uint16_t port;
30 uint64_t syn;
31 };
32
33 struct port_udp {
34 uint16_t port;
35 };
36
37 struct ip_proto {
38 uint8_t proto;
39 };
40
41 struct bucket {
42 struct bucket *next;
43 uint64_t in, out, total;
44 union {
45 struct host host;
46 struct port_tcp port_tcp;
47 struct port_udp port_udp;
48 struct ip_proto ip_proto;
49 } u;
50 };
51
52 enum sort_dir { IN, OUT, TOTAL };
53
54 extern int show_mac_addrs;
55
56 /*
57 * Table reduction - when the number of entries is about to exceed <max>, we
58 * reduce the table to the top <keep> entries.
59 */
60 extern unsigned int hosts_max, hosts_keep, ports_max, ports_keep;
61
62 void hosts_db_init(void);
63 void hosts_db_reduce(void);
64 void hosts_db_reset(void);
65 void hosts_db_free(void);
66 int hosts_db_import(const int fd);
67 int hosts_db_export(const int fd);
68
69 struct bucket *host_find(const in_addr_t ip); /* can return NULL */
70 struct bucket *host_get(const in_addr_t ip);
71 struct bucket *host_get_port_tcp(struct bucket *host, const uint16_t port);
72 struct bucket *host_get_port_udp(struct bucket *host, const uint16_t port);
73 struct bucket *host_get_ip_proto(struct bucket *host, const uint8_t proto);
74
75 /* Web pages. */
76 struct str *html_hosts(const char *uri, const char *query);
77
78 /* From hosts_sort */
79 void qsort_buckets(const struct bucket **a, size_t n,
80 size_t left, size_t right, const enum sort_dir d);
81
82 /* vim:set ts=3 sw=3 tw=78 expandtab: */