-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract.cpp
282 lines (227 loc) · 7.04 KB
/
extract.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "extract.hpp"
#include <caputils/packet.h>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
/* the caputils/marker.h header wasn't c++ compatible so I include the declaration here instead */
extern "C" int is_marker(const struct cap_header* cp, struct marker* ptr, int port);
bool keep_running = true;
extern const char* program_name;
void output_format_list(){
printf("Supported output formats:\n");
const struct formatter_entry* cur = formatter_lut;
while ( cur->name ){
printf(" * %-10s (%s)\n", cur->name, cur->desc);
cur++;
}
printf("\n");
}
static int prefix_to_multiplier(char prefix){
prefix = tolower(prefix);
switch ( prefix ){
case 0: return 1;
case 'k': return 1e3;
case 'm': return 1e6;
case 'g': return 1e9;
default: return -1;
}
}
/**
* Get prefix from number represented by a string and removes the prefix by
* setting it to NULL.
* If no prefix was found it returns 0.
* E.g. "100k" -> 'k'.
*/
static char pop_prefix(char* string){
if ( *string == 0 ) return 0;
const size_t offset = strlen(string) - 1;
if ( ! isalpha(string[offset]) ){
return 0;
}
const char prefix = string[offset];
string[offset] = 0;
return prefix;
}
Extractor::Extractor()
: ignore_marker(false)
, first_packet(true)
, relative_time(false)
, max_packets(0)
, level(LEVEL_LINK) {
set_sampling_frequency(1.0); /* default to 1Hz */
set_link_capacity("100m"); /* default to 100mbps */
}
Extractor::~Extractor(){
}
void Extractor::set_ignore_marker(bool state){
ignore_marker = state;
}
void Extractor::set_sampling_frequency(double hz){
sampleFrequency = hz;
tSample = 1.0 / sampleFrequency;
}
void Extractor::set_sampling_frequency(const char* str){
char* tmp = strdup(str);
const char prefix = pop_prefix(tmp);
int multiplier = prefix_to_multiplier(prefix);
if ( multiplier == -1 ){
fprintf(stderr, "unknown prefix '%c' for --sampleFrequency, ignored.\n", prefix);
multiplier = 1;
}
set_sampling_frequency(atof(tmp) * multiplier);
free(tmp);
}
void Extractor::set_max_packets(size_t n){
max_packets = n;
}
void Extractor::set_link_capacity(unsigned long bps){
link_capacity = bps;
}
void Extractor::set_link_capacity(const char* str){
char* tmp = strdup(str);
const char prefix = pop_prefix(tmp);
int multiplier = prefix_to_multiplier(prefix);
if ( multiplier == -1 ){
fprintf(stderr, "unknown prefix '%c' for --linkCapacity, ignored.\n", prefix);
multiplier = 1;
}
set_link_capacity(atof(tmp) * multiplier);
free(tmp);
}
void Extractor::set_extraction_level(const char* str){
level = level_from_string(str);
if ( level == LEVEL_INVALID ){
fprintf(stderr, "%s: unrecognised level \"%s\", defaulting to \"link\".\n", program_name, str);
level = LEVEL_LINK;
}
}
void Extractor::set_relative_time(bool state){
relative_time = state;
}
void Extractor::set_formatter(const char* str){
const struct formatter_entry* cur = formatter_lut;
while ( cur->name ){
if ( strcasecmp(cur->name, str) == 0 ){
return set_formatter(cur->fmt);
}
cur++;
}
fprintf(stderr, "%s: unrecognised formatter \"%s\", ignored.\n", program_name, str);
}
void Extractor::set_mpid(const mampid_t mpid){
}
void Extractor::reset(){
first_packet = true;
counter = 1;
}
void Extractor::process_stream(const stream_t st, struct filter* filter){
static int index = 0;
const stream_stat_t* stat = stream_get_stat(st);
int ret = 0;
write_header(index);
while ( keep_running && ( max_packets == 0 || stat->matched < max_packets ) ) {
/* A short timeout is used to allow the application to "breathe", i.e
* terminate if SIGINT was received. */
struct timeval tv = {1,0};
/* Read the next packet */
cap_head* cp;
ret = stream_read(st, &cp, filter, &tv);
if ( ret == EAGAIN ){
if ( !first_packet ){
do_sample();
}
continue; /* timeout */
} else if ( ret != 0 ){
break; /* shutdown or error */
}
set_mpid(cp->mampid);
calculate_samples(cp);
}
/* push the final sample */
do_sample();
/* only write trailer if app isn't terminating */
if ( keep_running ){
write_trailer(index++);
}
/* if ret == -1 the stream was closed properly (e.g EOF or TCP shutdown)
* In addition EINTR should not give any errors because it is implied when the
* user presses C-c */
if ( ret > 0 && ret != EINTR ){
fprintf(stderr, "stream_read() returned 0x%08X: %s\n", ret, caputils_error_string(ret));
}
}
qd_real Extractor::estimate_transfertime(unsigned long bits){
return qd_real((double)bits) / link_capacity;
}
bool Extractor::valid_first_packet(const cap_head* cp){
if ( !ignore_marker ) return true;
/* ignore marker packets */
if ( is_marker(cp, nullptr, 0) ){
return false;
}
/* ignore initial ICMP packets which is a response to the marker packet being
undeliverable */
const struct ethhdr* ethhdr = cp->ethhdr;
const struct ip* ip = find_ipv4_header(ethhdr, nullptr);
if ( ip && ip->ip_p == IPPROTO_ICMP ){
const struct icmphdr* icmp = (const struct icmphdr*)((char*)ip + 4*ip->ip_hl);
if ( icmp->type == ICMP_DEST_UNREACH && icmp->code == ICMP_PORT_UNREACH ){
return false;
}
}
return true;
}
void Extractor::calculate_samples(const cap_head* cp){
const unsigned long packet_bits = layer_size(level, cp) * 8;
const qd_real current_time = qd_real((double)cp->ts.tv_sec) + qd_real((double)cp->ts.tv_psec/PICODIVIDER);
const qd_real transfertime_packet = estimate_transfertime(packet_bits);
if ( first_packet ) {
if ( !valid_first_packet(cp) ){
return;
}
ref_time = current_time;
start_time = ref_time;
end_time = ref_time + tSample;
first_packet = false;
}
while ( keep_running && current_time >= end_time ){
do_sample();
}
/* split large packets into multiple samples */
int packet_samples = 1;
qd_real remaining_transfertime = transfertime_packet;
remaining_samplinginterval = end_time - current_time;
while ( keep_running && remaining_transfertime >= remaining_samplinginterval ){
const qd_real fraction = remaining_samplinginterval / transfertime_packet;
accumulate(fraction, packet_bits, cp, packet_samples++);
remaining_transfertime -= remaining_samplinginterval;
do_sample();
}
/* If the previous loop was broken by keep_running we should not sample the remaining data */
if ( !keep_running ) return;
// handle small packets or the remaining fractional packets which are in next interval
const qd_real fraction = remaining_transfertime / transfertime_packet;
accumulate(fraction, packet_bits, cp, packet_samples++);
remaining_samplinginterval = end_time - current_time - transfertime_packet;
}
void Extractor::do_sample(){
const double t = to_double(relative_time ? (start_time - ref_time) : start_time);
write_sample(t);
// reset start_time ; end_time; remaining_sampling interval
start_time = ref_time + counter++ * tSample;
end_time = start_time + tSample;
remaining_samplinginterval = tSample;
}
void Extractor::write_header(int index){
/* do nothing */
}
void Extractor::write_trailer(int index){
/* do nothing */
}