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