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