-
Notifications
You must be signed in to change notification settings - Fork 108
/
hep.cpp
650 lines (615 loc) · 20.2 KB
/
hep.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "voipmonitor.h"
#include "hep.h"
#include "tools.h"
#include "header_packet.h"
#include "sniff_inline.h"
#include "sniff_proc_class.h"
#include <pcap.h>
extern bool opt_hep_kamailio_protocol_id_fix;
static cHEP_Server *HEP_Server;
cHEP_ProcessData::cHEP_ProcessData() {
}
void cHEP_ProcessData::processData(u_char *data, size_t dataLen) {
/*
cout << " *** " << (isBeginHep(data, dataLen) ? "BEGIN" : "not begin") << endl;
cout << " *** " << dataLen << endl;
if(isBeginHep(data, dataLen)) {
cout << " *** " << hepLength(data, dataLen) << endl;
cout << " *** " << (isCompleteHep(data, dataLen) ? "COMPLETE" : "not complete") << endl;
}
*/
if(isBeginHep(data, dataLen)) {
hep_buffer.clear();
if(isCompleteHep(data, dataLen)) {
unsigned processed = processHeps(data, dataLen);
if(processed < dataLen) {
hep_buffer.add(data + processed, dataLen - processed);
}
} else {
hep_buffer.add(data, dataLen);
}
} else if(!hep_buffer.empty()) {
hep_buffer.add(data, dataLen);
if(isCompleteHep(hep_buffer.data(), hep_buffer.data_len())) {
unsigned processed = processHeps(hep_buffer.data(), hep_buffer.data_len());
if(processed == hep_buffer.data_len()) {
hep_buffer.clear();
} else if(processed < dataLen) {
hep_buffer.removeDataFromLeft(processed);
}
}
if(hep_buffer.size() > 1024 * 1024) {
hep_buffer.clear();
syslog(LOG_NOTICE, "HEP data exceeded the limit of 1MB. Check the HEP data sent.");
}
}
}
bool cHEP_ProcessData::isCompleteHep(u_char *data, size_t dataLen) {
return(isBeginHep(data, dataLen) &&
hepLength(data, dataLen) <= dataLen);
}
bool cHEP_ProcessData::isBeginHep(u_char *data, size_t dataLen) {
return(dataLen >= 4 &&
data[0] == 0x48 && data[1] == 0x45 && data[2] == 0x50 && data[3] == 0x33);
}
u_int16_t cHEP_ProcessData::hepLength(u_char *data, size_t dataLen) {
return(dataLen >= 6 ? ntohs(*(u_int32_t*)(data + 4)) : 0);
}
unsigned cHEP_ProcessData::processHeps(u_char *data, size_t dataLen) {
unsigned processed = 0;
while(processed < dataLen - 6) {
if(!isBeginHep(data + processed, dataLen - processed)) {
break;
}
unsigned hep_length = hepLength(data + processed, dataLen - processed);
if(hep_length > dataLen - processed) {
break;
}
processHep(data + processed, hep_length);
processed += hep_length;
}
return(processed);
}
void cHEP_ProcessData::processHep(u_char *data, size_t dataLen) {
sHEP_Data hepData;
processChunks(data + 6, dataLen - 6, &hepData);
if(sverb.hep3) {
cout << " * HEP3 * " << endl
<< hepData.dump() << endl;
}
if((hepData.ip_protocol_family == PF_INET || hepData.ip_protocol_family == PF_INET6) &&
(hepData.ip_protocol_id == IPPROTO_UDP || hepData.ip_protocol_id == IPPROTO_TCP || hepData.ip_protocol_id == IPPROTO_ESP)) {
extern int opt_id_sensor;
extern PreProcessPacket *preProcessPacket[PreProcessPacket::ppt_end_base];
int dlink = PcapDumper::get_global_pcap_dlink_en10();
int pcap_handle_index = PcapDumper::get_global_handle_index_en10();
ether_header header_eth;
memset(&header_eth, 0, sizeof(header_eth));
header_eth.ether_type = htons(hepData.ip_protocol_family == PF_INET6 ? ETHERTYPE_IPV6 : ETHERTYPE_IP);
string payload_str;
SimpleBuffer payload_buf;
u_char *payload_data = hepData.captured_packet_payload.data();
unsigned payload_len = hepData.captured_packet_payload.data_len();
if(hepData.protocol_type == _hep_prot_SIP &&
payload_len > 0 && payload_data[0] == '{' && payload_data[payload_len - 1] == '}') {
JsonItem jsonData;
jsonData.parse((char*)payload_data);
string payload_value = jsonData.getValue("payload");
if(!payload_value.empty()) {
payload_str = payload_value;
payload_data = (u_char*)payload_str.c_str();
payload_len = payload_str.length();
}
} else if(hepData.protocol_type == _hep_prot_RTCP_JSON &&
payload_len > 0 && payload_data[0] == '{' && payload_data[payload_len - 1] == '}') {
extern bool createRtcpPayloadFromJson(const char *json, SimpleBuffer *buffer);
if(createRtcpPayloadFromJson((const char*)payload_data, &payload_buf)) {
payload_data = payload_buf.data();
payload_len = payload_buf.size();
}
}
if(hepData.ip_protocol_id == IPPROTO_TCP || hepData.ip_protocol_id == IPPROTO_ESP) {
pcap_pkthdr *tcpHeader;
u_char *tcpPacket;
createSimpleTcpDataPacket(sizeof(header_eth), &tcpHeader, &tcpPacket,
(u_char*)&header_eth, payload_data, payload_len, 0,
hepData.ip_source_address, hepData.ip_destination_address, hepData.protocol_source_port, hepData.protocol_destination_port,
0, 0, (hepData.set_flags & (1ull << _hep_chunk_tcp_flag)) ? hepData.tcp_flag : 0,
hepData.timestamp_seconds, hepData.timestamp_microseconds, dlink);
unsigned iphdrSize = ((iphdr2*)(tcpPacket + sizeof(header_eth)))->get_hdr_size();
unsigned dataOffset = sizeof(header_eth) +
iphdrSize +
((tcphdr2*)(tcpPacket + sizeof(header_eth) + iphdrSize))->doff * 4;
packet_flags pflags;
pflags.init();
pflags.tcp = 2;
sPacketInfoData pid;
pid.clear();
if(opt_t2_boost_direct_rtp) {
sHeaderPacketPQout hp(tcpHeader, tcpPacket,
dlink, opt_id_sensor, vmIP());
preProcessPacket[PreProcessPacket::ppt_detach_x]->push_packet(
sizeof(header_eth), 0xFFFF,
dataOffset, payload_len,
hepData.protocol_source_port, hepData.protocol_destination_port,
pflags,
&hp,
pcap_handle_index);
} else {
preProcessPacket[PreProcessPacket::ppt_detach]->push_packet(
#if USE_PACKET_NUMBER
0,
#endif
hepData.ip_source_address, hepData.protocol_source_port, hepData.ip_destination_address, hepData.protocol_destination_port,
payload_len, dataOffset,
pcap_handle_index, tcpHeader, tcpPacket, _t_packet_alloc_header_std,
pflags, (iphdr2*)(tcpPacket + sizeof(header_eth)), (iphdr2*)(tcpPacket + sizeof(header_eth)),
NULL, 0, dlink, opt_id_sensor, vmIP(), pid,
false);
}
} else if(hepData.ip_protocol_id == IPPROTO_UDP) {
pcap_pkthdr *udpHeader;
u_char *udpPacket;
createSimpleUdpDataPacket(sizeof(header_eth), &udpHeader, &udpPacket,
(u_char*)&header_eth, payload_data, payload_len, 0,
hepData.ip_source_address, hepData.ip_destination_address, hepData.protocol_source_port, hepData.protocol_destination_port,
hepData.timestamp_seconds, hepData.timestamp_microseconds);
unsigned iphdrSize = ((iphdr2*)(udpPacket + sizeof(header_eth)))->get_hdr_size();
unsigned dataOffset = sizeof(header_eth) +
iphdrSize +
sizeof(udphdr2);
packet_flags pflags;
pflags.init();
sPacketInfoData pid;
pid.clear();
if(opt_t2_boost_direct_rtp) {
sHeaderPacketPQout hp(udpHeader, udpPacket,
dlink, opt_id_sensor, vmIP());
preProcessPacket[PreProcessPacket::ppt_detach_x]->push_packet(
sizeof(header_eth), 0xFFFF,
dataOffset, payload_len,
hepData.protocol_source_port, hepData.protocol_destination_port,
pflags,
&hp,
pcap_handle_index);
} else {
preProcessPacket[PreProcessPacket::ppt_detach]->push_packet(
#if USE_PACKET_NUMBER
0,
#endif
hepData.ip_source_address, hepData.protocol_source_port, hepData.ip_destination_address, hepData.protocol_destination_port,
payload_len, dataOffset,
pcap_handle_index, udpHeader, udpPacket, _t_packet_alloc_header_std,
pflags, (iphdr2*)(udpPacket + sizeof(header_eth)), (iphdr2*)(udpPacket + sizeof(header_eth)),
NULL, 0, dlink, opt_id_sensor, vmIP(), pid,
false);
}
}
}
}
u_int16_t cHEP_ProcessData::chunkVendor(u_char *data, size_t dataLen) {
return(dataLen >= 2 ? ntohs(*(u_int32_t*)(data + 0)) : 0);
}
u_int16_t cHEP_ProcessData::chunkType(u_char *data, size_t dataLen) {
return(dataLen >= 4 ? ntohs(*(u_int32_t*)(data + 2)) : 0);
}
u_int16_t cHEP_ProcessData::chunkLength(u_char *data, size_t dataLen) {
return(dataLen >= 6 ? ntohs(*(u_int32_t*)(data + 4)) : 0);
}
void cHEP_ProcessData::processChunks(u_char *data, size_t dataLen, sHEP_Data *hepData) {
unsigned offset = 0;
while(offset < dataLen - 6) {
unsigned chunk_length = chunkLength(data + offset, dataLen - offset);
if(!chunk_length || chunk_length > dataLen - offset) {
break;
}
processChunk(data + offset, chunk_length, hepData);
offset += chunk_length;
}
}
void cHEP_ProcessData::processChunk(u_char *data, size_t dataLen, sHEP_Data *hepData) {
unsigned chunk_type = chunkType(data, dataLen);
unsigned payloadLen = dataLen - 6;
u_char *payload = data + 6;
bool ok = false;
SimpleBuffer *bin = NULL;
switch(chunk_type) {
case _hep_chunk_ip_protocol_family:
if(payloadLen == 1) {
hepData->ip_protocol_family = *(u_int8_t*)payload;
ok = true;
}
break;
case _hep_chunk_ip_protocol_id:
if(payloadLen == 1) {
hepData->ip_protocol_id = *(u_int8_t*)payload;
if(hepData->ip_protocol_id == IPPROTO_IDP && opt_hep_kamailio_protocol_id_fix) {
hepData->ip_protocol_id = IPPROTO_UDP;
}
ok = true;
}
break;
case _hep_chunk_ip_source_address_v4:
if(payloadLen == 4) {
hepData->ip_source_address.setIPv4(*(u_int32_t*)payload, true);
ok = true;
}
break;
case _hep_chunk_ip_source_address_v6:
if(payloadLen == 16) {
hepData->ip_source_address.setIPv6(*(in6_addr*)payload, true);
ok = true;
}
break;
case _hep_chunk_ip_destination_address_v4:
if(payloadLen == 4) {
hepData->ip_destination_address.setIPv4(*(u_int32_t*)payload, true);
ok = true;
}
break;
case _hep_chunk_ip_destination_address_v6:
if(payloadLen == 16) {
hepData->ip_destination_address.setIPv6(*(in6_addr*)payload, true);
ok = true;
}
break;
case _hep_chunk_protocol_source_port:
if(payloadLen == 2) {
hepData->protocol_source_port.setPort(*(u_int16_t*)payload, true);
ok = true;
}
break;
case _hep_chunk_protocol_destination_port:
if(payloadLen == 2) {
hepData->protocol_destination_port.setPort(*(u_int16_t*)payload, true);
ok = true;
}
break;
case _hep_chunk_timestamp_seconds:
if(payloadLen == 4) {
hepData->timestamp_seconds = ntohl(*(u_int32_t*)payload);
ok = true;
}
break;
case _hep_chunk_timestamp_microseconds:
if(payloadLen == 4) {
hepData->timestamp_microseconds = ntohl(*(u_int32_t*)payload);
ok = true;
}
break;
case _hep_chunk_protocol_type:
if(payloadLen == 1) {
hepData->protocol_type = *(u_int8_t*)payload;
ok = true;
}
break;
case _hep_chunk_capture_agent_id:
if(payloadLen == 4) {
hepData->capture_agent_id = ntohl(*(u_int32_t*)payload);
ok = true;
}
break;
case _hep_chunk_keep_alive_timer:
if(payloadLen == 2) {
hepData->keep_alive_timer = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
case _hep_chunk_authenticate_key:
bin = &hepData->authenticate_key;
break;
case _hep_chunk_captured_packet_payload:
hepData->captured_packet_payload.add(payload, payloadLen);
ok = true;
break;
case _hep_chunk_captured_packet_payload_compressed:
{
cGzip gzipDecompress;
if(gzipDecompress.isCompress(payload, payloadLen)) {
u_char *dbuffer;
size_t dbufferLength;
if(gzipDecompress.decompress(payload, payloadLen, &dbuffer, &dbufferLength)) {
hepData->captured_packet_payload.add(dbuffer, dbufferLength);
delete [] dbuffer;
}
}
}
break;
case _hep_chunk_internal_correlation_id:
bin = &hepData->internal_correlation_id;
break;
case _hep_chunk_vlan_id:
if(payloadLen == 2) {
hepData->vlan_id = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
case _hep_chunk_group_id:
bin = &hepData->group_id;
break;
case _hep_chunk_source_mac:
if(payloadLen == 8) {
hepData->source_mac = be64toh(*(u_int64_t*)payload);
ok = true;
}
break;
case _hep_chunk_destination_mac:
if(payloadLen == 8) {
hepData->destination_mac = be64toh(*(u_int64_t*)payload);
ok = true;
}
break;
case _hep_chunk_ethernet_type:
if(payloadLen == 2) {
hepData->ethernet_type = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
case _hep_chunk_tcp_flag:
if(payloadLen == 1) {
hepData->tcp_flag = *(u_int8_t*)payload;
ok = true;
}
break;
case _hep_chunk_ip_tos:
if(payloadLen == 1) {
hepData->ip_tos = *(u_int8_t*)payload;
ok = true;
}
break;
case _hep_chunk_mos_value:
if(payloadLen == 2) {
hepData->mos_value = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
case _hep_chunk_r_factor:
if(payloadLen == 2) {
hepData->r_factor = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
case _hep_chunk_geo_location:
bin = &hepData->geo_location;
break;
case _hep_chunk_jitter:
if(payloadLen == 4) {
hepData->jitter = ntohl(*(u_int32_t*)payload);
ok = true;
}
break;
case _hep_chunk_transaction_type:
bin = &hepData->transaction_type;
break;
case _hep_chunk_payload_json_keys:
bin = &hepData->payload_json_keys;
break;
case _hep_chunk_tags_values:
bin = &hepData->tags_values;
break;
case _hep_chunk_tag_type:
if(payloadLen == 2) {
hepData->tag_type = ntohs(*(u_int16_t*)payload);
ok = true;
}
break;
}
if(bin) {
bin->add(payload, payloadLen);
ok = true;
}
if(ok) {
hepData->set_flags |= (1ull << chunk_type);
}
}
string sHEP_Data::dump() {
ostringstream out;
if(set_flags & (1ull << _hep_chunk_ip_protocol_family)) {
out << "ip_protocol_family: " << (int)ip_protocol_family << endl;
}
if(set_flags & (1ull << _hep_chunk_ip_protocol_id)) {
out << "ip_protocol_id: " << (int)ip_protocol_id << endl;
}
if(set_flags & (1ull << _hep_chunk_ip_source_address_v4) ||
set_flags & (1ull << _hep_chunk_ip_source_address_v6)) {
out << "ip_source_address: " << ip_source_address.getString() << endl;
}
if(set_flags & (1ull << _hep_chunk_ip_destination_address_v4) ||
set_flags & (1ull << _hep_chunk_ip_destination_address_v6)) {
out << "ip_destination_address: " << ip_destination_address.getString() << endl;
}
if(set_flags & (1ull << _hep_chunk_protocol_source_port)) {
out << "protocol_source_port: " << protocol_source_port.getString() << endl;
}
if(set_flags & (1ull << _hep_chunk_protocol_destination_port)) {
out << "protocol_destination_port: " << protocol_destination_port.getString() << endl;
}
if(set_flags & (1ull << _hep_chunk_timestamp_seconds)) {
out << "timestamp_seconds: " << timestamp_seconds << endl;
}
if(set_flags & (1ull << _hep_chunk_timestamp_microseconds)) {
out << "timestamp_microseconds: " << timestamp_microseconds << endl;
}
if(set_flags & (1ull << _hep_chunk_protocol_type)) {
out << "protocol_type: " << (int)protocol_type << endl;
}
if(set_flags & (1ull << _hep_chunk_capture_agent_id)) {
out << "capture_agent_id: " << capture_agent_id << endl;
}
if(set_flags & (1ull << _hep_chunk_keep_alive_timer)) {
out << "keep_alive_timer: " << keep_alive_timer << endl;
}
if(set_flags & (1ull << _hep_chunk_authenticate_key)) {
out << "authenticate_key: " << hexdump_to_string(&authenticate_key) << endl;
}
if(set_flags & (1ull << _hep_chunk_captured_packet_payload) ||
set_flags & (1ull << _hep_chunk_captured_packet_payload_compressed)) {
string dump_payload((char*)captured_packet_payload);
find_and_replace(dump_payload, "\r", "\\r");
find_and_replace(dump_payload, "\n", "\\n");
out << "captured_packet_payload: " << dump_payload << endl;
}
if(set_flags & (1ull << _hep_chunk_internal_correlation_id)) {
out << "internal_correlation_id: " << hexdump_to_string(&internal_correlation_id) << endl;
}
if(set_flags & (1ull << _hep_chunk_vlan_id)) {
out << "vlan_id: " << vlan_id << endl;
}
if(set_flags & (1ull << _hep_chunk_group_id)) {
out << "group_id: " << hexdump_to_string(&group_id) << endl;
}
if(set_flags & (1ull << _hep_chunk_source_mac)) {
out << "source_mac: " << hex << source_mac << dec << endl;
}
if(set_flags & (1ull << _hep_chunk_destination_mac)) {
out << "destination_mac: " << hex << destination_mac << dec << endl;
}
if(set_flags & (1ull << _hep_chunk_ethernet_type)) {
out << "ethernet_type: " << ethernet_type << endl;
}
if(set_flags & (1ull << _hep_chunk_tcp_flag)) {
out << "tcp_flag: " << hex << (int)tcp_flag << dec << endl;
}
if(set_flags & (1ull << _hep_chunk_ip_tos)) {
out << "ip_tos: " << (int)ip_tos << endl;
}
if(set_flags & (1ull << _hep_chunk_mos_value)) {
out << "mos_value: " << mos_value << endl;
}
if(set_flags & (1ull << _hep_chunk_r_factor)) {
out << "r_factor: " << r_factor << endl;
}
if(set_flags & (1ull << _hep_chunk_geo_location)) {
out << "geo_location: " << hexdump_to_string(&geo_location) << endl;
}
if(set_flags & (1ull << _hep_chunk_jitter)) {
out << "jitter: " << jitter << endl;
}
if(set_flags & (1ull << _hep_chunk_transaction_type)) {
out << "transaction_type: " << hexdump_to_string(&transaction_type) << endl;
}
if(set_flags & (1ull << _hep_chunk_payload_json_keys)) {
out << "payload_json_keys: " << hexdump_to_string(&payload_json_keys) << endl;
}
if(set_flags & (1ull << _hep_chunk_tags_values)) {
out << "tags_values: " << hexdump_to_string(&tags_values) << endl;
}
if(set_flags & (1ull << _hep_chunk_tag_type)) {
out << "tag_type: " << tag_type << endl;
}
return(out.str());
}
cHEP_Server::cHEP_Server(bool udp)
: cServer(udp, true) {
}
cHEP_Server::~cHEP_Server() {
}
void cHEP_Server::createConnection(cSocket *socket) {
if(is_terminating()) {
return;
}
cHEP_Connection *connection = new FILE_LINE(0) cHEP_Connection(socket);
connection->connection_start();
}
void cHEP_Server::evData(u_char *data, size_t dataLen) {
processData(data, dataLen);
}
cHEP_Connection::cHEP_Connection(cSocket *socket)
: cServerConnection(socket, true) {
}
cHEP_Connection::~cHEP_Connection() {
}
void cHEP_Connection::evData(u_char *data, size_t dataLen) {
processData(data, dataLen);
}
void cHEP_Connection::connection_process() {
cServerConnection::connection_process();
delete this;
}
void HEP_ServerStart(const char *host, int port, bool udp) {
if(HEP_Server) {
delete HEP_Server;
}
HEP_Server = new FILE_LINE(0) cHEP_Server(udp);
HEP_Server->setStartVerbString("START HEP LISTEN");
HEP_Server->listen_start("hep_server", host, port);
}
void HEP_ServerStop() {
if(HEP_Server) {
delete HEP_Server;
HEP_Server = NULL;
}
}
void HEP_client_emulation(const char *pcap, vmIP client_ip, vmIP server_ip, vmIP destination_ip, vmPort destination_port, bool udp) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
if(!(handle = pcap_open_offline_zip(pcap, errbuf))) {
fprintf(stderr, "Couldn't open pcap file '%s': %s\n", pcap, errbuf);
return;
}
set_all_ports_for_tcp();
int dlink = pcap_datalink(handle);
pcap_pkthdr *pcap_next_ex_header;
const u_char *pcap_next_ex_packet;
sHeaderPacket *header_packet = NULL;
pcapProcessData ppd;
int res;
cSocket socket("x");
if(udp) {
socket.setUdp(true);
}
socket.setHostPort(destination_ip.getString(), destination_port);
if(!socket.connect()) {
return;
} else {
cout << "ok connect" << endl;
}
while((res = pcap_next_ex(handle, &pcap_next_ex_header, &pcap_next_ex_packet)) > 0) {
if(header_packet && header_packet->packet_alloc_size != 0xFFFF) {
DESTROY_HP(&header_packet);
}
if(header_packet) {
header_packet->clearPcapProcessData();
} else {
header_packet = CREATE_HP(0xFFFF);
}
memcpy_heapsafe(HPH(header_packet), header_packet,
pcap_next_ex_header, NULL,
sizeof(pcap_pkthdr));
memcpy_heapsafe(HPP(header_packet), header_packet,
pcap_next_ex_packet, NULL,
pcap_next_ex_header->caplen);
ppd.header_udp = NULL;
ppd.header_tcp = NULL;
ppd.datalen = 0;
if(!pcapProcess(&header_packet, -1,
NULL, 0,
ppf_all,
&ppd, dlink, NULL, NULL)) {
continue;
}
u_int32_t caplen = HPH(header_packet)->caplen;
u_char *packet = HPP(header_packet);
if(ppd.header_ip) {
if(ppd.header_ip->get_protocol() == IPPROTO_UDP) {
ppd.header_udp = (udphdr2*)((char*)ppd.header_ip + ppd.header_ip->get_hdr_size());
ppd.datalen = get_udp_data_len(ppd.header_ip, ppd.header_udp, &ppd.data, packet, caplen);
} else if(ppd.header_ip->get_protocol() == IPPROTO_TCP) {
ppd.header_tcp = (tcphdr2*) ((char*) ppd.header_ip + ppd.header_ip->get_hdr_size());
ppd.datalen = get_tcp_data_len(ppd.header_ip, ppd.header_tcp, &ppd.data, packet, caplen);
}
if(ppd.datalen) {
if(ppd.header_ip->get_saddr() == client_ip && ppd.header_ip->get_daddr() == server_ip) {
cout << " -> " << flush;
if(socket.write((u_char*)ppd.data, ppd.datalen)) {
cout << "ok write " << ppd.datalen << endl;
}
}
}
}
}
if(header_packet) {
DESTROY_HP(&header_packet);
}
pcap_close(handle);
}