Did some fixes to the opposite hosts table and added some nice features
[darkstat] / hosts_db.h
1 /* darkstat 3
2 * copyright (c) 2001-2011 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, *hosts_opp;
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 host_opp {
40 struct addr addr;
41 char *dns;
42 time_t last_seen;
43 };
44
45 struct bucket {
46 struct bucket *next;
47 uint64_t in, out, total;
48 union {
49 struct host host;
50 struct port_tcp port_tcp;
51 struct port_udp port_udp;
52 struct ip_proto ip_proto;
53 struct host_opp host_opp;
54 } u;
55 };
56
57 enum sort_dir { IN, OUT, TOTAL, LASTSEEN, IP, HOSTNAME, MAC,
58 LASTSEENO, IPO, HOSTNAMEO,
59 REVIN, REVOUT, REVTOTAL, REVLASTSEEN, REVIP, REVHOSTNAME, REVMAC,
60 REVLASTSEENO, REVIPO, REVHOSTNAMEO };
61
62 extern int hosts_db_show_macs;
63
64 void hosts_db_init(void);
65 void hosts_db_reduce(void);
66 void hosts_db_reset(void);
67 void hosts_db_free(void);
68 int hosts_db_import(const int fd);
69 int hosts_db_export(const int fd);
70
71 struct bucket *host_find(const struct addr *const a); /* can return NULL */
72 struct bucket *host_find_host_opp(struct bucket *host, const struct addr *const a); /* can return NULL */
73
74 struct bucket *host_get(const struct addr *const a);
75 struct bucket *host_get_host_opp(struct bucket *host, const struct addr *const a);
76 struct bucket *host_get_port_tcp(struct bucket *host, const uint16_t port);
77 struct bucket *host_get_port_udp(struct bucket *host, const uint16_t port);
78 struct bucket *host_get_ip_proto(struct bucket *host, const uint8_t proto);
79
80 /* Web pages. */
81 struct str *html_hosts(const char *uri, const char *query);
82
83 /* From hosts_sort */
84 void qsort_buckets(const struct bucket **a, size_t n,
85 size_t left, size_t right, const enum sort_dir d);
86
87 #endif /* __DARKSTAT_HOSTS_DB_H */
88 /* vim:set ts=3 sw=3 tw=78 expandtab: */