-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (266 loc) · 8.05 KB
/
main.cpp
File metadata and controls
313 lines (266 loc) · 8.05 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#define __STDC_FORMAT_MACROS
#include <caputils/caputils.h>
#include <caputils/log.h>
#include <cstring>
#include <ctime>
#include <cstdio>
#include <inttypes.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <openssl/md5.h>
#include <netinet/ip.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include "printpkt.hpp"
static size_t num_locations = 0;
typedef struct packet_data {
picotime timestamp;
// std::string mampid;
std::string location;
} packet_data;
typedef struct packet_id {
packet_id()
: seq(0)
, num(0)
, data(num_locations){}
unsigned int seq;
unsigned int num;
std::vector<packet_data> data;
} packet_id;
static bool running = true;
static bool verbose = false;
static bool printpkt = false;
static const char* program_name;
static const char* iface = NULL;
static uint32_t seek = 0;
static uint32_t count = 1500;
static uint32_t batchSize = 0;
static uint32_t silent = 1;
static unsigned int timeout = 60;
static size_t matched = 0;
static std::map<std::string, packet_id> table; //Out;
//static std::map<std::string, packet_id> tableIn;
static const struct stream_stat* stream_stat = NULL;
static const char* shortopt = "i:s:c:t:b:hp:dv";
static struct option longopt[] = {
{"iface", required_argument, 0, 'i'},
{"seek", required_argument, 0, 's'},
{"count", required_argument, 0, 'c'},
{"timeout", required_argument, 0, 't'},
{"batchsize", required_argument, 0, 'b'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"displaypkt", no_argument, 0, 'd'},
{0,0,0,0}, /* sentinel */
};
static void show_usage(){
printf("%s\n"
"usage: %s -i IFACE -s BEGIN -c END STREAM..\n"
"\n"
" -i, --iface=IFACE Interface to listen on.\n"
" -s, --seek=BYTES Byte offset to begin from.\n"
" -c, --count=BYTES Byte offset to end.\n"
" Will look between Seek -- Count bytes. \n"
" -t, --timeout=SEC Discards packets after SEC.\n"
" -b, --batchsize=N Read batchsize packets, then exit.\n"
" -p N Number of locations packets are expected to arrive at.\n"
" -d, --displaypkt Show the packets.\n"
" -v, --verbose Verbose.\n"
" -h, --help This text.\n"
"\n", program_name, program_name);
filter_from_argv_usage();
}
template <class T>
T min(T a, T b){ return a<b?a:b; }
static void handle_alarm(int signum){
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
timepico cur = timespec_to_timepico(ts);
size_t pruned = 0;
for ( auto it = table.begin(); it != table.end(); ) {
const packet_id& id = it->second;
const bool old = timepico_sub(cur, id.data[0].timestamp).tv_sec > timeout + 5; /* some slack */
if ( old ) {
pruned++;
table.erase(it++);
} else {
++it;
}
}
static char timestr[64];
time_t t = time(NULL);
struct tm tm = *localtime(&t);
strftime(timestr, sizeof(timestr), "%a, %d %b %Y %H:%M:%S %z", &tm);
if (!silent){
fprintf(stderr, "%s: [%s] progress report: %'" PRIu64 " packets read (%zd matched, %zd pruned, %zd in progress).\n",
program_name, timestr, (long int)0, matched, pruned, table.size());
}
matched = 0;
}
static void handle_sigint(int signum){
if ( running ){
fprintf(stderr, "\rGot SIGINT, terminating graceful.\n");
running = false;
} else {
fprintf(stderr, "\rGot SIGINT again, aborting.\n");
abort();
}
}
static std::string point_id(const struct cap_header* cp){
char buf[18];
sprintf(buf, "%.8s_%.8s", cp->mampid, cp->nic);
return std::string(buf);
}
static bool packet_sort(const packet_data& a, const packet_data& b) {
return timecmp(&a.timestamp, &b.timestamp) == -1;
}
static void format(packet_id& pkt, const struct cap_header* cp, bool compact){
fprintf(stdout, "%d ", pkt.seq);
std::sort(pkt.data.begin(), pkt.data.end(), packet_sort);
for ( unsigned int i = 1; i < num_locations; i++ ){
const timepico &a = pkt.data[i].timestamp;
const timepico &b = pkt.data[i-1].timestamp;
const timepico dt = timepico_sub(a, b);
fprintf(stdout, " %s %d.%012" PRIu64 " %s %d.%012" PRIu64, pkt.data[i-1].location.c_str(), b.tv_sec, b.tv_psec, pkt.data[i].location.c_str(), a.tv_sec, a.tv_psec);
fprintf(stdout, " %d.%012" PRIu64, dt.tv_sec, dt.tv_psec);
}
if(printpkt){
if(compact){
fprintf(stdout, " LINK(%4d) CAPLEN(%4d):", cp->len, cp->caplen);
}
fprintf(stdout," ");
print_eth(stdout, cp->ethhdr,compact);
}
fprintf(stdout, "\n");
}
int main(int argc, char* argv[]){
/* extract program name from path. e.g. /path/to/MArCd -> MArCd */
const char* separator = strrchr(argv[0], '/');
if ( separator ){
program_name = separator + 1;
} else {
program_name = argv[0];
}
struct filter filter;
if ( filter_from_argv(&argc, argv, &filter) != 0 ){
exit(1); /* error already shown */
}
int op, option_index;
while ( (op=getopt_long(argc, argv, shortopt, longopt, &option_index)) != -1 ){
switch (op){
case 'i': /* --iface */
iface = optarg;
break;
case 's': /* --seek */
seek = (size_t)atoi(optarg);
break;
case 'c': /* --count */
count = (size_t)atoi(optarg);
break;
case 't': /* --timeout */
timeout = atoi(optarg);
break;
case 'p':
num_locations = atoi(optarg);
break;
case 'b':
batchSize = (uint32_t)atoi(optarg);
break;
case 'd':
printpkt=true;
break;
case 'v':
verbose=true;
break;
case 'h': /* --help */
show_usage();
exit(0);
}
}
if ( num_locations < 2 ){
fprintf(stderr, "%s: need at least 2 expected points, use -p to specify.\n", program_name);
exit(1);
}
struct itimerval tv = {
{timeout, 0},
{timeout, 0},
};
setitimer(ITIMER_REAL, &tv, NULL);
signal(SIGALRM, handle_alarm);
signal(SIGINT, handle_sigint);
stream_t st;
if ( stream_from_getopt(&st, argv, optind, argc, iface, NULL, program_name, 0) != 0 ){
exit(1); /* error already shown */
}
stream_stat = stream_get_stat(st);
unsigned int gseq = 0;
if (verbose) {
fprintf(stdout, "VERBOSE = %d\nSeek=%d -- %d \nbatchSize = %d \n", verbose, seek,count,batchSize );
}
do {
struct cap_header* cp;
int ret = stream_read(st, &cp, &filter, NULL);
if ( ret == EAGAIN ) continue;
if ( ret == -1 ) break;
const size_t offset = min(cp->len, seek);
const size_t bytes = min(cp->len, min(cp->caplen, count)) - offset;
if ( offset - bytes < (count<seek) ) continue;
char hex[33];
unsigned char* _ = MD5((const unsigned char*)&cp->payload[offset], bytes, NULL);
sprintf(hex, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
_[0], _[1], _[2], _[3], _[4], _[5], _[6], _[7], _[8], _[9], _[10], _[11], _[12], _[13], _[14], _[15]);
const std::string hash(hex);
const std::string point = point_id(cp);
if ( verbose ) {
fprintf(stdout, ":%.4s:%.8s:", cp->nic, cp->mampid);
fprintf(stdout, "Pkt->LINK(%4d):CAPLEN(%4d):", cp->len, cp->caplen);
print_eth(stdout, cp->ethhdr,verbose);
fprintf(stdout,"hash=%s start=%zd, end=%zd\n",hex,offset,bytes);
}
auto it = table.find(hash);
if ( it != table.end() ){ /* match found */
if(verbose) {
fprintf(stdout,"Match \n");
}
packet_id& id = it->second;
/* find duplicates (e.g. arp) */
unsigned int i;
for ( i = 0; i < id.num; i++ ){
if ( point == id.data[i].location ){
break;
}
}
if ( i < id.num ) continue;
id.data[id.num] = {cp->ts, point};
if ( ++id.num == num_locations ){ /* passed all points */
matched++;
if(verbose) {
fprintf(stdout,"matched = %d id = %d batchSize = %d \n",(int)matched, id.seq, batchSize);
}
if (batchSize>0) {
if ( id.seq > batchSize ){
running=false;
}
}
format(id, cp,verbose);
table.erase(it);
}
} else { /* no match */
if(verbose) {
fprintf(stdout,"No match \n");
}
packet_id id;
id.seq = ++gseq;
id.num = 1;
id.data[0] = {cp->ts, point};
table[hash] = id;
}
if(verbose) {
fprintf(stdout,"Next \n");
}
} while ( running );
stream_close(st);
}