69d654480674ed0c153cef25e7772c42809d1623
[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 #ifndef __DARKSTAT_HOSTS_DB_H
10 #define __DARKSTAT_HOSTS_DB_H
11
12 #include <sys/types.h> /* for time_t and uint64_t (esp on FreeBSD) */
13
14 #include "addr.h"
15
16 struct hashtable;
17
18 struct host {
19 struct addr addr;
20 char *dns;
21 uint8_t mac_addr[6];
22 time_t last_seen;
23 struct hashtable *ports_tcp, *ports_udp, *ip_protos;
24 };
25
26 struct port_tcp {
27 uint16_t port;
28 uint64_t syn;
29 };
30
31 struct port_udp {
32 uint16_t port;
33 };
34
35 struct ip_proto {
36 uint8_t proto;
37 };
38
39 struct bucket {
40 struct bucket *next;
41 uint64_t in, out, total;
42 union {
43 struct host host;
44 struct port_tcp port_tcp;
45 struct port_udp port_udp;
46 struct ip_proto ip_proto;
47 } u;
48 };
49
50 enum sort_dir { IN, OUT, TOTAL, LASTSEEN };
51
52 extern int show_mac_addrs;
53
54 /*
55 * Table reduction - when the number of entries is about to exceed <max>, we
56 * reduce the table to the top <keep> entries.
57 */
58 extern unsigned int hosts_max, hosts_keep, ports_max, ports_keep;
59
60 void hosts_db_init(void);
61 void hosts_db_reduce(void);
62 void hosts_db_reset(void);
63 void hosts_db_free(void);
64 int hosts_db_import(const int fd);
65 int hosts_db_export(const int fd);
66
67 struct bucket *host_find(const struct addr *const a); /* can return NULL */
68 struct bucket *host_get(const struct addr *const a);
69 struct bucket *host_get_port_tcp(struct bucket *host, const uint16_t port);
70 struct bucket *host_get_port_udp(struct bucket *host, const uint16_t port);
71 struct bucket *host_get_ip_proto(struct bucket *host, const uint8_t proto);
72
73 /* Web pages. */
74 struct str *html_hosts(const char *uri, const char *query);
75
76 /* From hosts_sort */
77 void qsort_buckets(const struct bucket **a, size_t n,
78 size_t left, size_t right, const enum sort_dir d);
79
80 #endif /* __DARKSTAT_HOSTS_DB_H */
81 /* vim:set ts=3 sw=3 tw=78 expandtab: */