/
main.c
1275 lines (1054 loc) · 33 KB
/
main.c
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------------------------
//
// Copyright (c) 2015, fmad engineering llc
//
// The MIT License (MIT) see LICENSE file for details
//
// pcap flow exporter
//
//---------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <locale.h>
#include <linux/sched.h>
#include <pthread.h>
#include "fTypes.h"
#include "tcpstream.h"
#include "udpstream.h"
//---------------------------------------------------------------------------------------------
typedef struct
{
char* Path; // path to the file
char Name[128]; // short name
FILE* F; // bufferd io file handle
int fd; // file handler of the mmap attached data
u64 Length; // exact file length
u64 MapLength; // 4KB aligned mmap length
u8* Map; // raw mmap ptr
u64 ReadPos; // current read pointer
u64 PktCnt; // number of packets processed
u8* PacketBuffer; // temp read buffer
bool Finished; // read completed
u64 TS; // last TS processed
} PCAPFile_t;
#define FLOW_TYPE_TCP 1
#define FLOW_TYPE_UDP 2
typedef struct
{
u64 Data[64/8]; // protocol specific unique hash
u32 Type; // what kind of flow is this
u64 PktCnt; // number of packets in this flow
u64 Bytes; // number of bytes in this flow
u64 TSFirst; // first packet of the flow
u64 TSLast; // last packet of the flow
// for duplex matching
u32 TCPSeqNo; // tcp syn/synack seq no
u32 Next; // next flow has index for this hash
} FlowHash_t;
// specific protocol hash info
typedef struct
{
u8 MACSrc[6];
u8 MACDst[6];
IP4_t IPSrc;
IP4_t IPDst;
u16 PortSrc;
u16 PortDst;
} TCPHash_t;
typedef struct
{
u8 MACSrc[6];
u8 MACDst[6];
IP4_t IPSrc;
IP4_t IPDst;
u16 PortSrc;
u16 PortDst;
} UDPHash_t;
double TSC2Nano = 0;
//---------------------------------------------------------------------------------------------
// tunables
static u64 s_MaxPackets = (1ULL<<63); // max number of packets to process
static s64 s_TimeZoneOffset = 0; // local timezone
u64 g_TotalMemory = 0; // total memory consumption
u64 g_TotalMemoryTCP = 0; // total memory of keeping out of order tcp packets
bool g_Verbose = false; // verbose print mode
//---------------------------------------------------------------------------------------------
// first level index
static u32* s_FlowIndex; // 24b index into first level list
static FlowHash_t* s_FlowList; // statically allocated max number of flows
static u32 s_FlowListPos = 1; // current allocated flow
static u32 s_FlowListMax; // max number of flows
static u64 s_FlowListPacketMin = 0; // minimum number of packets to show entry for
static u8* s_FlowExtract = NULL; // boolean to extract the specified flow id
static bool s_FlowExtractEnable = false; // indicaes flow extraction
static u32 s_FlowExtractMax = 1024*1024;
static u32 s_ExtractTCPEnable = false; // request extraction of tcp stream
static u8* s_ExtractTCPFlow = NULL; // boolean to extract the specified flow id
static bool s_ExtractTCPPortEnable = false; // extract all tcp flows with the specified port number
static u32 s_ExtractTCPPortMin = 0;
static u32 s_ExtractTCPPortMax = 0;
static struct TCPStream_t** s_ExtractTCP = NULL; // list of tcp stream extraction objects
static u32 s_DisableTCPPortCnt = 0; // extract all tcp flows with the specified port number
static u32 s_DisableTCPPortMin[32];
static u32 s_DisableTCPPortMax[32];
static bool s_ExtractUDPPortEnable = false; // extract all UDP flows within the specified port range
static u32 s_ExtractUDPPortMin = 0;
static u32 s_ExtractUDPPortMax = 0;
static struct UDPStream_t** s_ExtractUDP = NULL;
static bool s_ExtractIPEnable = false; // extract an IP range into a seperate pcap file
static u32 s_ExtractIPMask = 0; // /32 mask
static u32 s_ExtractIPMatch = 0; // match
static bool s_ExtractPortEnable = false; // extract TCP/UDP port range to a seperate pcap file
static u32 s_ExtractPortMin = 0; // min port range
static u32 s_ExtractPortMax = 0; // max port range
static bool s_EnableFlowDisplay = true; // print full flow information
bool g_EnableTCPHeader = false; // output packet header in tcp stream
static bool s_EnableFlowLog = true; // write flow log in realtime
static char s_FlowLogPath[128]; // where to store the flow log
static FILE* s_FlowLogFile = NULL; // file handle where to write flows
static u64 s_PCAPTimeScale = 1; // timescale all raw pcap time stamps
//---------------------------------------------------------------------------------------------
// mmaps a pcap file in full
static PCAPFile_t* OpenPCAP(char* Path, bool EnableStdin)
{
PCAPFile_t* F = (PCAPFile_t*)malloc( sizeof(PCAPFile_t) );
assert(F != NULL);
memset(F, 0, sizeof(PCAPFile_t));
F->Path = Path;
if (!EnableStdin)
{
F->F = fopen(Path, "r");
if (F->F == NULL)
{
fprintf(stderr, "failed to open buffered file [%s]\n", Path);
return NULL;
}
struct stat fstat;
if (stat(Path, &fstat) < 0)
{
fprintf(stderr, "failed to get file size [%s]\n", Path);
return NULL;
}
F->Length = fstat.st_size;
}
else
{
F->F = stdin;
F->Length = 1e12;
}
// note always map as read-only
PCAPHeader_t Header1;
PCAPHeader_t* Header = NULL;
{
int ret = fread(&Header1, 1, sizeof(Header1), F->F);
if (ret != sizeof(PCAPHeader_t))
{
fprintf(stderr, "failed to read header %i\n", ret);
return NULL;
}
Header = &Header1;
F->PacketBuffer = malloc(32*1024);
assert(F->PacketBuffer != NULL);
}
switch (Header->Magic)
{
case PCAPHEADER_MAGIC_USEC: fprintf(stderr, "USec PACP\n"); s_PCAPTimeScale = 1000; break;
case PCAPHEADER_MAGIC_NANO: fprintf(stderr, "Nano PACP\n"); s_PCAPTimeScale = 1; break;
default:
fprintf(stderr, "invalid pcap header %08x\n", Header->Magic);
return NULL;
}
F->ReadPos += sizeof(PCAPHeader_t);
return F;
}
//---------------------------------------------------------------------------------------------
// get the next packet
static PCAPPacket_t* ReadPCAP(PCAPFile_t* PCAP)
{
int ret;
PCAPPacket_t* Pkt = (PCAPPacket_t*)PCAP->PacketBuffer;
ret = fread(Pkt, 1, sizeof(PCAPPacket_t), PCAP->F);
if (ret != sizeof(PCAPPacket_t)) return NULL;
if (PCAP->ReadPos + sizeof(PCAPPacket_t) + Pkt->LengthCapture > PCAP->Length) return NULL;
ret = fread(Pkt+1, 1, Pkt->LengthCapture, PCAP->F);
if (ret != Pkt->LengthCapture) return NULL;
PCAP->ReadPos += Pkt->LengthCapture;
return Pkt;
}
//---------------------------------------------------------------------------------------------
// helpers for network formating
static u64 PCAPTimeStamp(PCAPPacket_t* Pkt)
{
return s_TimeZoneOffset + Pkt->Sec * k1E9 + Pkt->NSec * s_PCAPTimeScale;
}
static fEther_t * PCAPETHHeader(PCAPPacket_t* Pkt)
{
fEther_t* E = (fEther_t*)(Pkt+1);
return E;
}
static IP4Header_t* PCAPIP4Header(PCAPPacket_t* Pkt)
{
fEther_t* E = (fEther_t*)(Pkt+1);
IP4Header_t* IP4 = (IP4Header_t*)(E + 1);
u32 IPOffset = (IP4->Version & 0x0f)*4;
return IP4;
}
static TCPHeader_t* PCAPTCPHeader(PCAPPacket_t* Pkt)
{
fEther_t* E = (fEther_t*)(Pkt+1);
IP4Header_t* IP4 = (IP4Header_t*)(E + 1);
u32 IPOffset = (IP4->Version & 0x0f)*4;
TCPHeader_t* TCP = (TCPHeader_t*)( ((u8*)IP4) + IPOffset);
u32 TCPOffset = ((TCP->Flags&0xf0)>>4)*4;
return TCP;
}
static u8* PCAPTCPPayload(PCAPPacket_t* Pkt, u32* Length)
{
fEther_t* E = (fEther_t*)(Pkt+1);
IP4Header_t* IP4 = (IP4Header_t*)(E + 1);
u32 IPOffset = (IP4->Version & 0x0f)*4;
TCPHeader_t* TCP = (TCPHeader_t*)( ((u8*)IP4) + IPOffset);
u32 TCPOffset = ((TCP->Flags&0xf0)>>4)*4;
Length[0] = swap16(IP4->Len) - IPOffset - TCPOffset;
return (u8*)TCP + TCPOffset;
}
static UDPHeader_t* PCAPUDPHeader(PCAPPacket_t* Pkt)
{
fEther_t* E = (fEther_t*)(Pkt+1);
IP4Header_t* IP4 = (IP4Header_t*)(E + 1);
u32 IPOffset = (IP4->Version & 0x0f)*4;
UDPHeader_t* UDP = (UDPHeader_t*)( ((u8*)IP4) + IPOffset);
return UDP;
}
//---------------------------------------------------------------------------------------------
static void PrintMAC(FILE* Out, u8* MAC)
{
fprintf(Out, "%02x:%02x:%02x:%02x:%02x:%02x",
MAC[0],
MAC[1],
MAC[2],
MAC[3],
MAC[4],
MAC[5]
);
}
static void PrintIP4(FILE* Out, IP4_t IP)
{
fprintf(Out, "%3i.%3i.%3i.%3i", IP.IP[0], IP.IP[1], IP.IP[2], IP.IP[3]);
}
//---------------------------------------------------------------------------------------------
static void PrintFlowTCP(FILE* Out, FlowHash_t* F, u32 FlowID, u32 FlowCnt)
{
TCPHash_t* TCP = (TCPHash_t*)F->Data;
fprintf(Out, "%5i FlowID: %8i | TCP ", FlowCnt, FlowID);
PrintMAC(Out, TCP->MACSrc);
fprintf(Out, " -> ");
PrintMAC(Out, TCP->MACDst);
fprintf(Out, " | ");
PrintIP4(Out, TCP->IPSrc);
fprintf(Out, " -> ");
PrintIP4(Out, TCP->IPDst);
fprintf(Out, " | %6i -> %6i ", TCP->PortSrc, TCP->PortDst);
fprintf(Out, " | ");
fprintf(Out, " %'16lld Pkts ", F->PktCnt);
fprintf(Out, " %'16lli Bytes ", F->Bytes);
fprintf(Out, " | ");
fprintf(Out, " %s -> %s", FormatTS(F->TSFirst), FormatTS(F->TSLast) );
fprintf(Out, " : %s", FormatTS(F->TSLast - F->TSFirst));
fprintf(Out, " | ");
fprintf(Out, " Seq:%08x", F->TCPSeqNo);
fprintf(Out, "\n");
}
//---------------------------------------------------------------------------------------------
static void PrintFlowUDP(FILE* Out, FlowHash_t* F, u32 FlowID, u32 FlowCnt)
{
UDPHash_t* UDP = (UDPHash_t*)F->Data;
fprintf(Out, "%5i FlowID: %8i | UDP ", FlowCnt, FlowID);
PrintMAC(Out, UDP->MACSrc);
fprintf(Out, " -> ");
PrintMAC(Out, UDP->MACDst);
fprintf(Out, " | ");
PrintIP4(Out, UDP->IPSrc);
fprintf(Out, " -> ");
PrintIP4(Out, UDP->IPDst);
fprintf(Out, " | %6i -> %6i ", UDP->PortSrc, UDP->PortDst);
fprintf(Out, " | ");
fprintf(Out, " %'16lld Pkts ", F->PktCnt);
fprintf(Out, " %'16lli Bytes ", F->Bytes);
fprintf(Out, " | ");
fprintf(Out, " %s -> %s", FormatTS(F->TSFirst), FormatTS(F->TSLast) );
fprintf(Out, " : %s", FormatTS(F->TSLast - F->TSFirst));
fprintf(Out, "\n");
}
//---------------------------------------------------------------------------------------------
static u32 FlowHash(u32 Type, u8* Payload, u32 Length)
{
// DEK packets usually have enough entropy for this to be enough
u32 Hash = Type;
for (int i=0; i < Length; i++)
{
Hash = ((Hash << 5ULL) ^ (Hash >> (32-5))) ^ (u64)Payload[i];
}
return Hash;
}
//---------------------------------------------------------------------------------------------
static u32 FlowAdd(FlowHash_t* Flow, u32 PktLength, u64 TS)
{
if (s_FlowListPos >= s_FlowExtractMax) return 0;
FlowHash_t* F = NULL;
// first level has is 24b index, followed by list of leaf nodes
u32 Hash = FlowHash(Flow->Type, (u8*)Flow->Data, 64);
u32 Index = Hash & 0x00ffffff;
u32 FlowIndex = 0;
bool IsFlowNew = false;
if (s_FlowIndex[Index] != 0)
{
F = s_FlowList + s_FlowIndex[Index];
bool Found = false;
for (int t=0; t < 1e6; t++)
{
// flow matched
if (memcmp(F->Data, Flow->Data, 64) == 0)
{
Found = true;
break;
}
if (F->Next == 0) break;
F = s_FlowList + F->Next;
assert(t < 99e3);
}
// new flow
if (Found)
{
}
else
{
F = &s_FlowList[ s_FlowListPos++ ];
assert(s_FlowListPos < s_FlowListMax);
memcpy(F, Flow, sizeof(FlowHash_t));
F->Next = s_FlowIndex[Index];
s_FlowIndex[Index] = F - s_FlowList;
IsFlowNew = true;
}
}
else
{
F = &s_FlowList[ s_FlowListPos++ ];
assert(s_FlowListPos < s_FlowListMax);
memcpy(F, Flow, sizeof(FlowHash_t));
F->Next = 0;
s_FlowIndex[Index] = F - s_FlowList;
IsFlowNew = true;
}
// update stats
F->PktCnt++;
F->Bytes += PktLength;
F->TSFirst = IsFlowNew ? TS : F->TSFirst;
F->TSLast = TS;
// update flow log
if (IsFlowNew && s_EnableFlowLog)
{
u32 ID = F - s_FlowList;
switch (F->Type)
{
case FLOW_TYPE_TCP: PrintFlowTCP(s_FlowLogFile, F, ID, s_FlowListPos); break;
case FLOW_TYPE_UDP: PrintFlowUDP(s_FlowLogFile, F, ID, s_FlowListPos); break;
}
}
return F - s_FlowList;
}
//---------------------------------------------------------------------------------------------
static void PrintHumanFlows(void)
{
u64 PktMax = 0;
/*
for (int i=1; i < s_FlowListPos; i++)
{
FlowHash_t* F = &s_FlowList[i];
if (PktMax < F->PktCnt) PktMax = F->PktCnt;
}
*/
u32 FlowCnt = 0;
s32 Remain = s_FlowListPos-1;
while (Remain > 0)
{
u64 NextMax = 1e16;
for (int i=1; i < s_FlowListPos; i++)
{
FlowHash_t* F = &s_FlowList[i];
if (F->PktCnt == PktMax)
{
if (F->PktCnt >= s_FlowListPacketMin)
{
switch (F->Type)
{
case FLOW_TYPE_TCP: PrintFlowTCP(stdout, F, i, FlowCnt); break;
case FLOW_TYPE_UDP: PrintFlowUDP(stdout, F, i, FlowCnt); break;
}
}
Remain--;
FlowCnt++;
assert(Remain >= 0);
}
else if (F->PktCnt > PktMax)
{
if (NextMax > F->PktCnt) NextMax = F->PktCnt;
}
}
//printf("%i -> %i : %i\n", PktMax, NextMax, Remain);
PktMax = NextMax;
}
}
//---------------------------------------------------------------------------------------------
static void print_usage(void)
{
fprintf(stderr, "pcap_flows: <pcap>>\n");
fprintf(stderr, "\n");
fprintf(stderr, "Version: %s %s\n", __DATE__, __TIME__);
fprintf(stderr, "Contact: support at fmad.io\n");
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " --output-tcp <filename> | write TCP output to the specified file name\n");
fprintf(stderr, " --output-udp <filename> | write UDP output to the specified file name\n");
fprintf(stderr, "\n");
fprintf(stderr, " --packet-max <number> | only process the first <number> packets\n");
fprintf(stderr, " --flow-max <number> | sets max flow count to <number> packets\n");
fprintf(stderr, " --extract <number> | extract FlowID <number> into the output PCAP file\n");
fprintf(stderr, " --extract-tcp <number> | extract FlowID <number> as a TCP stream to the output file name\n");
fprintf(stderr, " --extract-tcp-port <min port> <max port> | extract all TCP flows with the specified port in src or dest\n");
fprintf(stderr, " --extract-tcp-all | extract all TCP flows\n");
fprintf(stderr, " --stdin | read pcap from stdin. e.g. zcat capture.pcap | pcap_flow --stdin\n");
fprintf(stderr, " --flow-packet-min <number> | minimum packet count to display flow info\n");
fprintf(stderr, " --disable-display | do not display flow information to stdout\n");
fprintf(stderr, " --cpu <number> | pin thread to a specific CPU\n");
fprintf(stderr, "\n");
}
//---------------------------------------------------------------------------------------------
static void FlowAlloc(u32 FlowMax)
{
s_FlowListMax = FlowMax;
if (s_FlowExtract) free(s_FlowExtract);
if (s_ExtractTCPFlow) free(s_ExtractTCPFlow);
if (s_ExtractTCP) free(s_ExtractTCP);
if (s_ExtractUDP) free(s_ExtractUDP);
s_FlowExtract = (u8*)malloc( s_FlowListMax * sizeof(u8) );
s_ExtractTCPFlow = (u8*)malloc( s_FlowListMax * sizeof(u8) );
s_ExtractTCP = (struct TCPStream_t**)malloc( s_FlowListMax * sizeof(void*) );
s_ExtractUDP = (struct UDPStream_t**)malloc( s_FlowListMax * sizeof(void*) );
memset(s_FlowExtract, 0, s_FlowListMax * sizeof(u8) );
memset(s_ExtractTCPFlow, 0, s_FlowListMax * sizeof(u8) );
memset(s_ExtractTCP, 0, s_FlowListMax * sizeof(void*) );
memset(s_ExtractUDP, 0, s_FlowListMax * sizeof(void*) );
}
//---------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
int FileNameListPos = 0;
char FileNameList[16][256];
int FileStdin = false;
char* UDPOutputFileName = NULL;
char* TCPOutputFileName = NULL;
// allocate flow lists
FlowAlloc(100e3); // default flow count
for (int i=1; i < argc; i++)
{
if (argv[i][0] != '-')
{
strcpy(FileNameList[FileNameListPos], argv[i]);
FileNameListPos++;
}
else
{
if (strcmp(argv[i], "--packet-max") == 0)
{
s_MaxPackets = atoll(argv[i+1]);
i+= 1;
fprintf(stderr, "setting maximum number of packets to %lli\n", s_MaxPackets);
}
// set the maximum number of flows
else if (strcmp(argv[i], "--flow-max") == 0)
{
u32 FlowMax = (u32)atof(argv[i+1]);
i++;
fprintf(stderr, "set max flow count to %i\n", FlowMax);
FlowAlloc(FlowMax); // default flow count
}
else if (strcmp(argv[i], "--extract") == 0)
{
u32 FlowID = atoi(argv[i+1]);
if (FlowID >= sizeof(s_FlowExtract))
{
fprintf(stderr, "flow overflow\n");
return 0;
}
s_FlowExtract[ FlowID ] = 1<<7;
s_FlowExtractEnable = true;
i++;
}
// extract the specified ip range into a seperate pcap
else if (strcmp(argv[i], "--extract-ip") == 0)
{
// in the form of 192.168.1.1/255.255.255.255
char* IPRange = argv[i+1];
i++;
u8 Segment[8][256];
u32 SegmentPos = 0;
u32 SegmentLen = 0;
for (int p=0; p < strlen(IPRange); p++)
{
u8 c = IPRange[p];
if ((c == '.') || (c == '/'))
{
Segment[SegmentPos][SegmentLen] = 0;
//printf("seg len: %i %i : %s\n", SegmentPos, SegmentLen, Segment[SegmentPos]);
SegmentPos++;
SegmentLen = 0;
}
else
{
Segment[SegmentPos][SegmentLen] = c;
SegmentLen++;
}
}
u32 IP[4];
u32 Mask[4];
IP[0] = atoi(Segment[0]);
IP[1] = atoi(Segment[1]);
IP[2] = atoi(Segment[2]);
IP[3] = atoi(Segment[3]);
Mask[0] = atoi(Segment[4]);
Mask[1] = atoi(Segment[5]);
Mask[2] = atoi(Segment[6]);
Mask[3] = atoi(Segment[7]);
fprintf(stderr, "extract ip range %i.%i.%i.%i/%i.%i.%i.%i\n",
IP[0],
IP[1],
IP[2],
IP[3],
Mask[0],
Mask[1],
Mask[2],
Mask[3]);
s_ExtractIPEnable = true;
s_ExtractIPMatch = (IP[0] << 0) | (IP[1] << 8) | (IP[2] << 16) | (IP[3] << 24);
s_ExtractIPMask = (Mask[0] << 0) | (Mask[1] << 8) | (Mask[2] << 16) | (Mask[3] << 24);
}
// extract all packets with the specified udp port
else if (strcmp(argv[i], "--extract-port") == 0)
{
s_ExtractPortEnable = true;
s_ExtractPortMin = atoi(argv[i+1]);
s_ExtractPortMax = atoi(argv[i+2]);
i+= 2;
fprintf(stderr, "extract port range: %i-%i\n", s_ExtractPortMin, s_ExtractPortMax);
}
// extract the specified flow as tcp stream
else if (strcmp(argv[i], "--extract-tcp") == 0)
{
u32 FlowID = atoi(argv[i+1]);
s_ExtractTCPEnable = true;
s_ExtractTCPFlow[ FlowID ] = 1;
i++;
fprintf(stderr, "extract tcp flow %i\n", FlowID);
}
// extract all tcp flows with the matching port
else if (strcmp(argv[i], "--extract-tcp-port") == 0)
{
u32 PortMin = atoi(argv[i+1]);
u32 PortMax = atoi(argv[i+2]);
s_ExtractTCPPortEnable = true;
s_ExtractTCPPortMin = PortMin;
s_ExtractTCPPortMax = PortMax;
i += 2;
fprintf(stderr, "extract all tcp flow with port %i-%i\n", PortMin, PortMax);
}
// extract all tcp flows
else if (strcmp(argv[i], "--extract-tcp-all") == 0)
{
s_ExtractTCPPortEnable = true;
s_ExtractTCPPortMin = 0;
s_ExtractTCPPortMax = 0x10000;
fprintf(stderr, "extract all tcp flow with port %i-%i\n", s_ExtractTCPPortMin, s_ExtractTCPPortMax);
}
// disable port range
else if (strcmp(argv[i], "--disable-tcp-port") == 0)
{
u32 PortMin = atoi(argv[i+1]);
u32 PortMax = atoi(argv[i+2]);
s_DisableTCPPortMin[s_DisableTCPPortCnt] = PortMin;
s_DisableTCPPortMax[s_DisableTCPPortCnt] = PortMax;
s_DisableTCPPortCnt++;
i += 2;
fprintf(stderr, "disable tcp extraction on ports [%i] %i-%i\n", s_DisableTCPPortCnt-1, PortMin, PortMax);
}
// extract udp flows within the specified range to individual files
else if (strcmp(argv[i], "--extract-udp-port") == 0)
{
u32 PortMin = atoi(argv[i+1]);
u32 PortMax = atoi(argv[i+2]);
s_ExtractUDPPortEnable = true;
s_ExtractUDPPortMin = PortMin;
s_ExtractUDPPortMax = PortMax;
i += 2;
fprintf(stderr, "extract all udp flow`s with port %i-%i\n", PortMin, PortMax);
}
// extract udp all ports
else if (strcmp(argv[i], "--extract-udp-all") == 0)
{
s_ExtractUDPPortEnable = true;
s_ExtractUDPPortMin = 0;
s_ExtractUDPPortMax = 65535;
fprintf(stderr, "extract all udp flows\n");
}
// input is from stdin
else if (strcmp(argv[i], "--stdin") == 0)
{
TCPOutputFileName = "stdin";
UDPOutputFileName = "stdin";
FileStdin = true;
fprintf(stderr, "reading PCAP from stdin\n");
}
// minimum number of packets
else if (strcmp(argv[i], "--flow-packet-min") == 0)
{
s_FlowListPacketMin = atoi(argv[i+1]);
fprintf(stderr, "minimum packet count %lli\n", s_FlowListPacketMin);
}
// display flow info
else if (strcmp(argv[i], "--disable-display") == 0)
{
s_EnableFlowDisplay = false;
}
// enable tcp header output
else if (strcmp(argv[i], "--tcpheader") == 0)
{
g_EnableTCPHeader =true;
fprintf(stderr, "enabling output tcp header\n");
}
// UDP output file
else if (strcmp(argv[i], "--output-udp") == 0)
{
UDPOutputFileName = argv[i+1];
i++;
fprintf(stderr, "writing UDP PCAP to [%s]\n", UDPOutputFileName);
}
// TCP output file
else if (strcmp(argv[i], "--output-tcp") == 0)
{
TCPOutputFileName = argv[i+1];
i++;
fprintf(stderr, "writing TCP PCAP to [%s]\n", TCPOutputFileName);
}
// pin to a specific CPU
else if (strcmp(argv[i], "--cpu") == 0)
{
u32 CPU = atoi(argv[i+1]);
i++;
// pin to a thread
cpu_set_t MainCPUS;
CPU_ZERO(&MainCPUS);
CPU_SET(CPU, &MainCPUS);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &MainCPUS);
}
else if (strcmp(argv[i], "--verbose") == 0)
{
fprintf(stderr, "enable verbose mode\n");
g_Verbose = true;
}
else
{
fprintf(stderr, "unknown option [%s]\n", argv[i]);
return 0;
}
}
}
// needs atleast 2 files
if (!(FileStdin) && (FileNameListPos <= 0))
{
print_usage();
return 0;
}
if (FileStdin) strcpy(s_FlowLogPath, "capture");
else strcpy(s_FlowLogPath, FileNameList[0]);
if (s_FlowExtractEnable && s_ExtractTCPEnable)
{
fprintf(stderr, "can not extract flow and tcp at the same time\n");
return 0;
}
if (s_ExtractTCPEnable && (!TCPOutputFileName))
{
fprintf(stderr, "specify tcp extract path --output-tcp <path>\n");
return 0;
}
// open output file
FILE* OutPCAP = NULL;
if (TCPOutputFileName && (!s_ExtractTCPEnable) )
{
OutPCAP = fopen(TCPOutputFileName, "w");
if (OutPCAP == NULL)
{
fprintf(stderr, "failed to open output file [%s]\n", TCPOutputFileName);
return 0;
}
PCAPHeader_t Header;
Header.Magic = PCAPHEADER_MAGIC_NANO;
Header.Major = PCAPHEADER_MAJOR;
Header.Minor = PCAPHEADER_MINOR;
Header.TimeZone = 0;
Header.SigFlag = 0;
Header.SnapLen = 8192;
Header.Link = PCAPHEADER_LINK_ETHERNET;
fwrite(&Header, sizeof(Header), 1, OutPCAP);
}
// open flow log file
if (s_EnableFlowLog)
{
char Path[1024];
sprintf(Path, "%s.flow", s_FlowLogPath);
s_FlowLogFile = fopen(Path, "w");
if (!s_FlowLogFile )
{
fprintf(stderr, "failed to create flow log\n");
return 0;
}
}
// calcuate tsc frequency
CycleCalibration();
setlocale(LC_NUMERIC, "");
// get timezone offset
time_t t = time(NULL);
struct tm lt = {0};
localtime_r(&t, <);
s_TimeZoneOffset = lt.tm_gmtoff * 1e9;
s_FlowIndex = (u32*)malloc( sizeof(u32)*(1ULL<<24));
assert(s_FlowIndex != NULL);
memset(s_FlowIndex, 0, sizeof(u32)*(1ULL<<24));
g_TotalMemory += sizeof(u32)*(1ULL<<24);
s_FlowList = (FlowHash_t*)malloc( sizeof(FlowHash_t) * s_FlowListMax );
memset(s_FlowList, 0, sizeof(FlowHash_t) * s_FlowListMax );
assert(s_FlowList != NULL);
g_TotalMemory += sizeof(FlowHash_t) * s_FlowListMax;
// open pcap diff files
PCAPFile_t* PCAPFile = OpenPCAP(FileNameList[0], FileStdin);
if (!PCAPFile) return 0;
// init tcp reassembly
/*
if (s_ExtractTCPEnable)
{
for (int i=0; i < s_FlowExtractMax; i++)
{
if (s_ExtractTCP[i]j
{
char Path[256];
u32 FlowID = i;
sprintf(Path, "%s.tcpflow.%i", TCPOutputFileName, FlowID);
s_ExtractTCP[i] = fTCPStream_Init(kMB(128), Path, FlowID, 0);
}
}
}
*/
// clear tcp output
memset(s_ExtractTCP, 0, sizeof(s_ExtractTCP));
printf("[%30s] FileSize: %lliGB\n", PCAPFile->Path, PCAPFile->Length / kGB(1));
u64 TotalByte = 0;
u64 TotalPkt = 0;
u64 NextPrintTSC = 0;
u64 StartTSC = rdtsc();
u64 OutputByte = 0;
while (true)
{
PCAPPacket_t* Pkt = ReadPCAP(PCAPFile);
if (!Pkt) break;
PCAPFile->TS = PCAPTimeStamp(Pkt);
u32 HashLength = 0;
FlowHash_t Flow;
memset(&Flow, 0, sizeof(Flow));
fEther_t * Ether = PCAPETHHeader(Pkt);
switch (swap16(Ether->Proto))
{
case ETHER_PROTO_IPV4:
{
IP4Header_t* IP4 = PCAPIP4Header(Pkt);
u32 IPOffset = (IP4->Version & 0x0f)*4;
switch (IP4->Proto)
{
case IPv4_PROTO_TCP:
{
TCPHeader_t* TCP = (TCPHeader_t*)( ((u8*)IP4) + IPOffset);
Flow.Type = FLOW_TYPE_TCP;
TCPHash_t* TCPHash = (TCPHash_t*)Flow.Data;
memset(TCPHash, 0, 64);
memcpy(TCPHash->MACDst, Ether->Dst, 6);
memcpy(TCPHash->MACSrc, Ether->Src, 6);
TCPHash->IPSrc = IP4->Src;
TCPHash->IPDst = IP4->Dst;
TCPHash->PortSrc = swap16(TCP->PortSrc);
TCPHash->PortDst = swap16(TCP->PortDst);
HashLength = 64;
// mark tcp SYN/SYNACK sequence numbers for duplex matching
if (TCP_FLAG_SYN(TCP->Flags) & (!TCP_FLAG_ACK(TCP->Flags)))
{
// syn seq no
Flow.TCPSeqNo = swap32(TCP->SeqNo);
}
if (TCP_FLAG_SYN(TCP->Flags) & (TCP_FLAG_ACK(TCP->Flags)))
{
// syn.ack ack no (syn.ack bumps it by 1)
Flow.TCPSeqNo = swap32(TCP->AckNo) -1;
}
}
break;
case IPv4_PROTO_UDP:
{
UDPHeader_t* UDP = (UDPHeader_t*)( ((u8*)IP4) + IPOffset);
Flow.Type = FLOW_TYPE_UDP;
UDPHash_t* UDPHash = (UDPHash_t*)Flow.Data;
memset(UDPHash, 0, 64);
memcpy(UDPHash->MACDst, Ether->Dst, 6);
memcpy(UDPHash->MACSrc, Ether->Src, 6);
UDPHash->IPSrc = IP4->Src;
UDPHash->IPDst = IP4->Dst;
UDPHash->PortSrc = swap16(UDP->PortSrc);
UDPHash->PortDst = swap16(UDP->PortDst);
HashLength = 64;
}
break;
}
}
break;