forked from jow-/nlbwmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.h
118 lines (88 loc) · 2.88 KB
/
database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
ISC License
Copyright (c) 2016-2017, Jo-Philipp Wich <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <stdint.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <libubox/avl.h>
#include "timing.h"
#include "nlbwmon.h"
#define MAGIC 0x6e6c626d /* 'nlbm' */
#define db_size(db, n) \
(sizeof(*(db)) + (n) * sizeof(struct record))
#define db_recsize \
offsetof(struct record, node)
#define db_entries(db) \
be32toh((db)->entries)
#define db_timestamp(db) \
be32toh((db)->timestamp)
#define db_disksize(db) \
(sizeof(*(db)) + db_entries(db) * db_recsize)
#define db_diskrecord(db, n) \
(struct record *)((void *)(db)->records + (n) * db_recsize)
#define db_record(db, n) \
(struct record *)&(db)->records[(n)];
struct record {
uint8_t family;
uint8_t proto;
uint16_t dst_port;
union {
struct ether_addr ea;
uint64_t u64;
} src_mac;
union {
struct in6_addr in6;
struct in_addr in;
} src_addr;
uint64_t count;
uint64_t out_pkts;
uint64_t out_bytes;
uint64_t in_pkts;
uint64_t in_bytes;
struct avl_node node;
};
struct database {
uint32_t magic;
uint32_t entries;
uint32_t timestamp;
struct interval interval;
struct record records[];
};
struct dbhandle {
bool prealloc;
bool pristine;
uint32_t limit;
uint32_t size;
uint32_t off;
struct avl_tree index;
struct database *db;
};
extern struct dbhandle *gdbh;
struct dbhandle * database_mem(avl_tree_comp key_fn, void *key_ptr);
struct dbhandle * database_init(const struct interval *intv, bool prealloc,
uint32_t limit);
int database_insert(struct dbhandle *h, struct record *rec);
int database_update(struct dbhandle *h, struct record *rec);
void database_reorder(struct dbhandle *h, avl_tree_comp sort_fn,
void *sort_ptr);
struct record * database_next(struct dbhandle *h, struct record *prev);
int database_save(struct dbhandle *h, const char *path, uint32_t timestamp,
bool compress);
int database_load(struct dbhandle *h, const char *path, uint32_t timestamp);
int database_archive(struct dbhandle *h);
int database_cleanup(void);
void database_free(struct dbhandle *h);
#endif /* __DATABASE_H__ */