-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnetmon.cpp
More file actions
217 lines (187 loc) · 6.29 KB
/
netmon.cpp
File metadata and controls
217 lines (187 loc) · 6.29 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include "net/mon/workers.h"
#include "net/mon/configuration.h"
#include "net/capture/method.h"
#include "pcap/reader.h"
static bool process_pcap_file(const net::mon::configuration& config);
static bool process_interface(const net::mon::configuration& config);
static bool ethernet(const void* buf,
size_t len,
const pcap::timeval& ts,
void* user);
static bool ipv4(const void* buf,
size_t len,
const pcap::timeval& ts,
void* user);
static bool ipv6(const void* buf,
size_t len,
const pcap::timeval& ts,
void* user);
struct pcap_argument {
net::mon::worker* worker;
const pcap::reader* reader;
};
int main(int argc, const char** argv)
{
// Initialize configuration.
net::mon::configuration config;
if (config.init()) {
// Parse program options.
if (config.parse(argc, argv)) {
// Print configuration.
config.print();
if (config.cap.m == net::mon::configuration::capture::method::pcap) {
if (process_pcap_file(config)) {
return 0;
}
} else {
if (process_interface(config)) {
return 0;
}
}
} else {
config.help(argv[0]);
}
} else {
fprintf(stderr, "Error initializing configuration.\n");
}
return -1;
}
bool process_pcap_file(const net::mon::configuration& config)
{
// Open PCAP file.
pcap::reader reader;
if (reader.open(config.cap.device)) {
// Initialize netmon worker.
net::mon::worker worker(0,
net::mon::worker::no_processor,
config.evdir,
config.file_allocation_size,
config.buffer_size);
if (worker.init("pcap",
config.tcp4.size,
config.tcp4.maxconns,
config.tcp6.size,
config.tcp6.maxconns,
config.tcp4.timeout,
config.tcp4.time_wait)) {
pcap::callbacks callbacks;
callbacks.ethernet = ethernet;
callbacks.ipv4 = ipv4;
callbacks.ipv6 = ipv6;
pcap_argument arg;
arg.worker = &worker;
arg.reader = &reader;
// Read PCAP file.
if (reader.read_all(callbacks, &arg)) {
// Remove expired connections.
const pcap::timeval& timestamp = reader.timestamp();
worker.remove_expired((timestamp.tv_sec * 1000000ull) +
timestamp.tv_usec);
return true;
}
fprintf(stderr, "Error adding packet.\n");
} else {
fprintf(stderr, "Error initializing netmon worker.\n");
}
} else {
fprintf(stderr,
"Error opening file '%s' for reading.\n",
config.cap.device);
}
return false;
}
bool process_interface(const net::mon::configuration& config)
{
net::capture::method capture_method;
if (config.cap.m == net::mon::configuration::capture::method::ring_buffer) {
capture_method = net::capture::method::ring_buffer;
} else {
capture_method = net::capture::method::socket;
}
// Create netmon workers.
net::mon::workers workers;
if (workers.create(config.nworkers,
config.processors,
config.evdir,
config.file_allocation_size,
config.buffer_size,
capture_method,
config.cap.device,
config.cap.ifindex,
config.cap.rcvbuf_size,
config.cap.promiscuous_mode,
config.cap.rb.block_size,
config.cap.rb.frame_size,
config.cap.rb.frame_count,
config.tcp4.size,
config.tcp4.maxconns,
config.tcp6.size,
config.tcp6.maxconns,
config.tcp4.timeout,
config.tcp4.time_wait)) {
// Block signals SIGINT and SIGTERM.
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGTERM);
if (pthread_sigmask(SIG_BLOCK, &set, NULL) == 0) {
// Start workers.
if (workers.start()) {
printf("Waiting for signal to arrive.\n");
// Wait for signal to arrive.
int sig;
while (sigwait(&set, &sig) != 0);
printf("Signal received.\n");
workers.stop();
workers.show_statistics();
return true;
} else {
fprintf(stderr, "Error starting workers.\n");
}
} else {
fprintf(stderr, "Error blocking signals SIGINT and SIGTERM.\n");
}
} else {
fprintf(stderr, "Error creating workers.\n");
}
return false;
}
bool ethernet(const void* buf, size_t len, const pcap::timeval& ts, void* user)
{
uint32_t usec = (static_cast<pcap_argument*>(user)->reader->resolution() ==
pcap::resolution::microseconds) ? ts.tv_usec :
ts.tv_usec / 1000;
// Process ethernet frame.
return static_cast<pcap_argument*>(user)->worker->process_ethernet(
buf,
len,
{static_cast<time_t>(ts.tv_sec), static_cast<suseconds_t>(usec)}
);
}
bool ipv4(const void* buf, size_t len, const pcap::timeval& ts, void* user)
{
uint32_t usec = (static_cast<pcap_argument*>(user)->reader->resolution() ==
pcap::resolution::microseconds) ? ts.tv_usec :
ts.tv_usec / 1000;
// Process IPv4 packet.
return static_cast<pcap_argument*>(user)->worker->process_ipv4(
buf,
len,
{static_cast<time_t>(ts.tv_sec), static_cast<suseconds_t>(usec)}
);
}
bool ipv6(const void* buf, size_t len, const pcap::timeval& ts, void* user)
{
uint32_t usec = (static_cast<pcap_argument*>(user)->reader->resolution() ==
pcap::resolution::microseconds) ? ts.tv_usec :
ts.tv_usec / 1000;
// Process IPv6 packet.
return static_cast<pcap_argument*>(user)->worker->process_ipv6(
buf,
len,
{static_cast<time_t>(ts.tv_sec), static_cast<suseconds_t>(usec)}
);
}