Fix "clock error" due to machine reboot.
[darkstat] / hosts_db.h
1 /* darkstat 3
2 * copyright (c) 2001-2014 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 uint64_t */
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 /* last_seen_mono is converted to/from time_t in export/import.
23 * It can be negative (due to machine reboots).
24 */
25 int64_t last_seen_mono;
26 struct hashtable *ports_tcp, *ports_udp, *ip_protos;
27 };
28
29 struct port_tcp {
30 uint16_t port;
31 uint64_t syn;
32 };
33
34 struct port_udp {
35 uint16_t port;
36 };
37
38 struct ip_proto {
39 uint8_t proto;
40 };
41
42 struct bucket {
43 struct bucket *next;
44 uint64_t in, out, total;
45 union {
46 struct host host;
47 struct port_tcp port_tcp;
48 struct port_udp port_udp;
49 struct ip_proto ip_proto;
50 } u;
51 };
52
53 enum sort_dir { IN, OUT, TOTAL, LASTSEEN };
54
55 extern int hosts_db_show_macs;
56
57 void hosts_db_init(void);
58 void hosts_db_reduce(void);
59 void hosts_db_reset(void);
60 void hosts_db_free(void);
61 int hosts_db_import(const int fd);
62 int hosts_db_export(const int fd);
63
64 struct bucket *host_find(const struct addr *const a); /* can return NULL */
65 struct bucket *host_get(const struct addr *const a);
66 struct bucket *host_get_port_tcp(struct bucket *host, const uint16_t port);
67 struct bucket *host_get_port_udp(struct bucket *host, const uint16_t port);
68 struct bucket *host_get_ip_proto(struct bucket *host, const uint8_t proto);
69
70 /* Web pages. */
71 struct str *html_hosts(const char *uri, const char *query);
72
73 /* From hosts_sort */
74 void qsort_buckets(const struct bucket **a, size_t n,
75 size_t left, size_t right, const enum sort_dir d);
76
77 #endif /* __DARKSTAT_HOSTS_DB_H */
78 /* vim:set ts=3 sw=3 tw=78 expandtab: */