forked from jpr5/ngrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngrep.c
1611 lines (1272 loc) · 40.9 KB
/
ngrep.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) 2017 Jordan Ritter <jpr5@darkridge.com>
*
* Please refer to the LICENSE file for more information.
*
*/
#if defined(BSD) || defined(SOLARIS) || defined(MACOSX)
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/tty.h>
#include <pwd.h>
#endif
#if defined(OSF1)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <net/route.h>
#include <sys/mbuf.h>
#include <arpa/inet.h>
#include <unistd>
#include <pwd.h>
#endif
#if defined(LINUX) || defined(__GLIBC__) || defined(__GNU__)
#include <getopt.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#endif
#if defined(AIX)
#include <sys/machine.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include <pwd.h>
#endif
#if defined(_WIN32)
#include <io.h>
#include <getopt.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <types.h>
#include <config.h>
#define strcasecmp stricmp
#define strncasecmp strnicmp
#else
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <netinet/igmp.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <locale.h>
#if !defined(_WIN32)
#include <errno.h>
#include <sys/ioctl.h>
#endif
#include <pcap.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if USE_IPv6 && !defined(_WIN32)
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#endif
#if USE_PCRE
#include <pcre.h>
#else
#include <regex.h>
#endif
#include "ngrep.h"
/*
* Configuration Options
*/
uint32_t snaplen = 65535, limitlen = 65535, promisc = 1, to = 100;
uint32_t match_after = 0, keep_matching = 0, matches = 0, max_matches = 0;
uint32_t seen_frames = 0;
#if USE_TCPKILL
uint32_t tcpkill_active = 0;
#endif
uint8_t re_match_word = 0, re_ignore_case = 0, re_multiline_match = 1;
uint8_t show_empty = 0, show_hex = 0, show_proto = 0, quiet = 0;
uint8_t invert_match = 0, bin_match = 0;
uint8_t live_read = 1, want_delay = 0;
uint8_t dont_dropprivs = 0;
uint8_t enable_hilite = 0;
char *read_file = NULL, *dump_file = NULL;
char *usedev = NULL;
char nonprint_char = '.';
/*
* GNU Regex/PCRE
*/
#if USE_PCRE
int32_t err_offset;
char *re_err = NULL;
pcre *pattern = NULL;
pcre_extra *pattern_extra = NULL;
#else
const char *re_err = NULL;
struct re_pattern_buffer pattern;
#endif
/*
* Matching
*/
char *match_data = NULL, *bin_data = NULL;
uint16_t match_len = 0;
int8_t (*match_func)() = &blank_match_func;
int8_t dump_single = 0;
void (*dump_func)(unsigned char *, uint32_t, uint16_t, uint16_t) = &dump_formatted;
/*
* BPF/Network
*/
char *filter = NULL, *filter_file = NULL;
char pc_err[PCAP_ERRBUF_SIZE];
uint8_t link_offset;
uint8_t radiotap_present = 0;
uint8_t include_vlan = USE_VLAN_HACK;
pcap_t *pd = NULL, *pd_dumppcap = NULL;
pcap_dumper_t *pd_dump = NULL;
struct bpf_program pcapfilter;
struct in_addr net, mask;
/*
* Timestamp/delay functionality
*/
struct timeval prev_ts = {0, 0}, prev_delay_ts = {0,0};
#if defined(_WIN32)
struct timeval delay_tv;
FD_SET delay_fds;
SOCKET delay_socket = 0;
#endif
void (*print_time)() = NULL, (*dump_delay)() = dump_delay_proc_init;
/*
* Window-size functionality (adjust output based on width of console display)
*/
uint32_t ws_row, ws_col = 80, ws_col_forced = 0;
int main(int argc, char **argv) {
int32_t c;
signal(SIGINT, clean_exit);
signal(SIGABRT, clean_exit);
#if !defined(_WIN32)
signal(SIGQUIT, clean_exit);
signal(SIGPIPE, clean_exit);
signal(SIGWINCH, update_windowsize);
#endif
setlocale(LC_ALL, "");
#if !defined(_WIN32)
{
char const *locale = getenv("LANG");
if (locale == NULL)
locale = "en_US";
setlocale(LC_CTYPE, locale);
}
#endif
while ((c = getopt(argc, argv, "LNhXViwqpevxlDtTRMK:Cs:n:c:d:A:I:O:S:P:F:W:")) != EOF) {
switch (c) {
case 'W': {
if (!strcasecmp(optarg, "normal"))
dump_func = &dump_formatted;
else if (!strcasecmp(optarg, "byline"))
dump_func = &dump_byline;
else if (!strcasecmp(optarg, "none"))
dump_func = &dump_unwrapped;
else if (!strcasecmp(optarg, "single")) {
dump_func = &dump_unwrapped;
dump_single = 1;
} else {
printf("fatal: unknown wrap method '%s'\n", optarg);
usage();
}
} break;
case 'F':
filter_file = optarg;
break;
case 'P':
nonprint_char = *optarg;
break;
case 'S': {
limitlen = _atoui32(optarg);
break;
}
case 'O':
dump_file = optarg;
break;
case 'I':
read_file = optarg;
break;
case 'A':
match_after = _atoui32(optarg);
if (match_after < UINT32_MAX)
match_after++;
break;
#if defined(_WIN32)
case 'L':
win32_listdevices();
clean_exit(2);
case 'd':
usedev = win32_usedevice(optarg);
break;
#else
case 'L':
perror("-L is a Win32-only option");
clean_exit(2);
case 'd':
usedev = optarg;
/* Linux: any = DLT_LINUX_SLL, pcap says incompatible with VLAN */
if (!strncasecmp(usedev, "any", 3))
include_vlan = 0;
break;
#endif
case 'c':
ws_col_forced = atoi(optarg);
break;
case 'n':
max_matches = _atoui32(optarg);
break;
case 's': {
uint16_t value = _atoui32(optarg);
if (value > 0)
snaplen = value;
} break;
case 'C':
enable_hilite = 1;
break;
case 'M':
re_multiline_match = 0;
break;
case 'R':
dont_dropprivs = 1;
break;
case 'T':
if (print_time == &print_time_diff) {
print_time = print_time_offset;
memset(&prev_ts, 0, sizeof(prev_ts));
} else {
print_time = &print_time_diff;
#if defined(_WIN32)
prev_ts.tv_sec = (uint32_t)time(NULL);
prev_ts.tv_usec = 0;
#else
gettimeofday(&prev_ts, NULL);
#endif
}
break;
case 't':
print_time = &print_time_absolute;
break;
case 'D':
want_delay = 1;
break;
case 'l':
setvbuf(stdout, NULL, _IOLBF, 0);
break;
case 'x':
show_hex++;
break;
case 'v':
invert_match++;
break;
case 'e':
show_empty++;
break;
case 'p':
promisc = 0;
break;
case 'q':
quiet++;
break;
case 'w':
re_match_word++;
break;
case 'i':
re_ignore_case++;
break;
case 'V':
version();
case 'X':
bin_match++;
break;
case 'N':
show_proto++;
break;
#if USE_TCPKILL
case 'K':
tcpkill_active = _atoui32(optarg);
break;
#endif
case 'h':
usage();
default:
usage();
}
}
if (show_hex && dump_func != &dump_formatted) {
printf("fatal: -x (hex dump) is incompatible with -W (alternate format)\n");
usage();
}
if (argv[optind])
match_data = argv[optind++];
#if USE_TCPKILL
if (tcpkill_active)
tcpkill_init();
#endif
/* Setup PCAP input */
if (setup_pcap_source())
clean_exit(2);
/* Setup BPF filter */
if (setup_bpf_filter(argv)) {
include_vlan = 0;
if (filter) { free(filter); filter = NULL; }
if (setup_bpf_filter(argv)) {
pcap_perror(pd, "pcap");
clean_exit(2);
}
}
if (filter) {
if (quiet < 2)
printf("filter: %s\n", filter);
free(filter);
}
/* Setup matcher */
if (match_data) {
if (setup_matcher())
clean_exit(2);
if (quiet < 2 && strlen(match_data))
printf("%smatch: %s%s\n", invert_match?"don't ":"",
(bin_data && !strchr(match_data, 'x'))?"0x":"", match_data);
if (re_match_word) free(match_data);
}
/* Misc */
if (dump_file) {
pd_dump = pcap_dump_open(pd, dump_file);
if (!pd_dump) {
fprintf(stderr, "fatal: %s\n", pcap_geterr(pd));
clean_exit(2);
} else printf("output: %s\n", dump_file);
}
update_windowsize(0);
#if defined(_WIN32)
win32_initwinsock();
#endif
#if !defined(_WIN32) && USE_DROPPRIVS
drop_privs();
#endif
while (pcap_loop(pd, -1, (pcap_handler)process, 0));
clean_exit(0);
/* NOT REACHED */
return 0;
}
int setup_pcap_source(void) {
if (read_file) {
if (!(pd = pcap_open_offline(read_file, pc_err))) {
perror(pc_err);
return 1;
}
live_read = 0;
printf("input: %s\n", read_file);
} else {
char *dev = usedev ? usedev :
#if defined(_WIN32)
win32_choosedevice();
#else
pcap_lookupdev(pc_err);
#endif
if (!dev) {
perror(pc_err);
return 1;
}
if ((pd = pcap_open_live(dev, snaplen, promisc, to, pc_err)) == NULL) {
perror(pc_err);
return 1;
}
if (pcap_lookupnet(dev, &net.s_addr, &mask.s_addr, pc_err) == -1) {
perror(pc_err);
memset(&net, 0, sizeof(net));
memset(&mask, 0, sizeof(mask));
}
if (quiet < 2) {
printf("interface: %s", dev);
if (net.s_addr && mask.s_addr) {
printf(" (%s/", inet_ntoa(net));
printf("%s)", inet_ntoa(mask));
}
printf("\n");
}
}
switch(pcap_datalink(pd)) {
case DLT_EN10MB:
link_offset = ETHHDR_SIZE;
break;
case DLT_IEEE802:
link_offset = TOKENRING_SIZE;
break;
case DLT_FDDI:
link_offset = FDDIHDR_SIZE;
break;
case DLT_SLIP:
link_offset = SLIPHDR_SIZE;
break;
case DLT_PPP:
link_offset = PPPHDR_SIZE;
break;
#if HAVE_DLT_LOOP
case DLT_LOOP:
#endif
case DLT_NULL:
link_offset = LOOPHDR_SIZE;
break;
#if HAVE_DLT_RAW
case DLT_RAW:
link_offset = RAWHDR_SIZE;
break;
#endif
#if HAVE_DLT_LINUX_SLL
case DLT_LINUX_SLL:
link_offset = ISDNHDR_SIZE;
include_vlan = 0;
break;
#endif
#if HAVE_DLT_IEEE802_11_RADIO
case DLT_IEEE802_11_RADIO:
radiotap_present = 1;
#endif
#if HAVE_DLT_IEEE802_11
case DLT_IEEE802_11:
link_offset = IEEE80211HDR_SIZE;
break;
#endif
#if HAVE_DLT_PFLOG
case DLT_PFLOG:
link_offset = PFLOGHDR_SIZE;
break;
#endif
#if HAVE_DLT_IPNET
case DLT_IPNET:
link_offset = IPNETHDR_SIZE;
include_vlan = 0;
break;
#endif
default:
fprintf(stderr, "fatal: unsupported interface type %u\n", pcap_datalink(pd));
return 1;
}
return 0;
}
int setup_bpf_filter(char **argv) {
if (filter_file) {
char buf[1024] = {0};
FILE *f = fopen(filter_file, "r");
if (!f || !fgets(buf, sizeof(buf)-1, f)) {
fprintf(stderr, "fatal: unable to get filter from %s: %s\n", filter_file, strerror(errno));
usage();
}
fclose(f);
filter = get_filter_from_string(buf);
if (pcap_compile(pd, &pcapfilter, filter, 0, mask.s_addr))
return 1;
} else if (argv[optind]) {
filter = get_filter_from_argv(&argv[optind]);
if (pcap_compile(pd, &pcapfilter, filter, 0, mask.s_addr)) {
free(filter);
filter = get_filter_from_argv(&argv[optind-1]);
#if USE_PCAP_RESTART
PCAP_RESTART_FUNC();
#endif
if (pcap_compile(pd, &pcapfilter, filter, 0, mask.s_addr))
return 1;
match_data = NULL;
}
} else {
filter = include_vlan ? strdup(BPF_TEMPLATE_IP_VLAN) : strdup(BPF_TEMPLATE_IP);
if (pcap_compile(pd, &pcapfilter, filter, 0, mask.s_addr))
return 1;
}
if (pcap_setfilter(pd, &pcapfilter))
return 1;
return 0;
}
int setup_matcher(void) {
if (bin_match) {
uint32_t i = 0, n;
uint32_t len;
char *s, *d;
if (re_match_word || re_ignore_case) {
fprintf(stderr, "fatal: regex switches are incompatible with binary matching\n");
return 1;
}
len = (uint32_t)strlen(match_data);
if (len % 2 != 0 || !strishex(match_data)) {
fprintf(stderr, "fatal: invalid hex string specified\n");
return 1;
}
bin_data = (char*)malloc(len / 2);
memset(bin_data, 0, len / 2);
d = bin_data;
if ((s = strchr(match_data, 'x')))
len -= (uint32_t)(++s - match_data - 1);
else s = match_data;
while (i <= len) {
sscanf(s+i, "%2x", &n);
*d++ = n;
i += 2;
}
match_len = len / 2;
match_func = &bin_match_func;
} else {
#if USE_PCRE
uint32_t pcre_options = PCRE_UNGREEDY;
if (re_ignore_case)
pcre_options |= PCRE_CASELESS;
if (re_multiline_match)
pcre_options |= PCRE_DOTALL;
#else
re_syntax_options = RE_CHAR_CLASSES | RE_NO_BK_PARENS | RE_NO_BK_VBAR |
RE_CONTEXT_INDEP_ANCHORS | RE_CONTEXT_INDEP_OPS;
if (re_multiline_match)
re_syntax_options |= RE_DOT_NEWLINE;
if (re_ignore_case) {
uint32_t i;
char *s;
pattern.translate = (char*)malloc(256);
s = pattern.translate;
for (i = 0; i < 256; i++)
s[i] = i;
for (i = 'A'; i <= 'Z'; i++)
s[i] = i + 32;
s = match_data;
while (*s) {
*s = tolower(*s);
s++;
}
} else pattern.translate = NULL;
#endif
if (re_match_word) {
char *word_regex = (char*)malloc(strlen(match_data) * 3 + strlen(WORD_REGEX));
sprintf(word_regex, WORD_REGEX, match_data, match_data, match_data);
match_data = word_regex;
}
#if USE_PCRE
pattern = pcre_compile(match_data, pcre_options, (const char **)&re_err, &err_offset, 0);
if (!pattern) {
fprintf(stderr, "compile failed: %s\n", re_err);
return 1;
}
pattern_extra = pcre_study(pattern, 0, (const char **)&re_err);
#else
re_err = re_compile_pattern(match_data, strlen(match_data), &pattern);
if (re_err) {
fprintf(stderr, "regex compile: %s\n", re_err);
return 1;
}
pattern.fastmap = (char*)malloc(256);
if (re_compile_fastmap(&pattern)) {
perror("fastmap compile failed");
return 1;
}
#endif
match_func = &re_match_func;
}
return 0;
}
static inline uint8_t vlan_frame_count(u_char *p, uint16_t limit) {
uint8_t *et = (uint8_t*)(p + 12);
uint16_t ether_type = EXTRACT_16BITS(et);
uint8_t count = 0;
while ((void*)et < (void*)(p + limit) &&
ether_type != ETHERTYPE_IP &&
ether_type != ETHERTYPE_IPV6) {
count++;
et += VLANHDR_SIZE;
ether_type = EXTRACT_16BITS(et);
}
return count;
}
void process(u_char *d, struct pcap_pkthdr *h, u_char *p) {
uint8_t vlan_offset = include_vlan ? vlan_frame_count(p, h->caplen) * VLANHDR_SIZE : 0;
struct ip *ip4_pkt = (struct ip *) (p + link_offset + vlan_offset);
#if USE_IPv6
struct ip6_hdr *ip6_pkt = (struct ip6_hdr*)(p + link_offset + vlan_offset);
#endif
uint32_t ip_ver;
uint8_t ip_proto = 0;
uint32_t ip_hl = 0;
uint32_t ip_off = 0;
uint8_t fragmented = 0;
uint16_t frag_offset = 0;
uint32_t frag_id = 0;
char ip_src[INET6_ADDRSTRLEN + 1],
ip_dst[INET6_ADDRSTRLEN + 1];
unsigned char *data;
uint32_t len = h->caplen - vlan_offset;
seen_frames++;
#if HAVE_DLT_IEEE802_11_RADIO
if (radiotap_present) {
uint16_t radio_len = ((struct NGREP_rtaphdr_t *)(p))->it_len;
ip4_pkt = (struct ip *)(p + link_offset + radio_len);
len -= radio_len;
}
#endif
ip_ver = ip4_pkt->ip_v;
switch (ip_ver) {
case 4: {
#if defined(AIX)
#undef ip_hl
ip_hl = ip4_pkt->ip_ff.ip_fhl * 4;
#else
ip_hl = ip4_pkt->ip_hl * 4;
#endif
ip_proto = ip4_pkt->ip_p;
ip_off = ntohs(ip4_pkt->ip_off);
fragmented = ip_off & (IP_MF | IP_OFFMASK);
frag_offset = (fragmented) ? (ip_off & IP_OFFMASK) * 8 : 0;
frag_id = ntohs(ip4_pkt->ip_id);
inet_ntop(AF_INET, (const void *)&ip4_pkt->ip_src, ip_src, sizeof(ip_src));
inet_ntop(AF_INET, (const void *)&ip4_pkt->ip_dst, ip_dst, sizeof(ip_dst));
} break;
#if USE_IPv6
case 6: {
ip_hl = sizeof(struct ip6_hdr);
ip_proto = ip6_pkt->ip6_nxt;
if (ip_proto == IPPROTO_FRAGMENT) {
struct ip6_frag *ip6_fraghdr;
ip6_fraghdr = (struct ip6_frag *)((unsigned char *)(ip6_pkt) + ip_hl);
ip_hl += sizeof(struct ip6_frag);
ip_proto = ip6_fraghdr->ip6f_nxt;
fragmented = 1;
frag_offset = ntohs(ip6_fraghdr->ip6f_offlg & IP6F_OFF_MASK);
frag_id = ntohl(ip6_fraghdr->ip6f_ident);
}
inet_ntop(AF_INET6, (const void *)&ip6_pkt->ip6_src, ip_src, sizeof(ip_src));
inet_ntop(AF_INET6, (const void *)&ip6_pkt->ip6_dst, ip_dst, sizeof(ip_dst));
} break;
#endif
}
if (quiet < 1) {
printf("#");
fflush(stdout);
}
switch (ip_proto) {
case IPPROTO_TCP: {
struct tcphdr *tcp_pkt = (struct tcphdr *)((unsigned char *)(ip4_pkt) + ip_hl);
uint16_t tcphdr_offset = (frag_offset) ? 0 : (tcp_pkt->th_off * 4);
data = (unsigned char *)(tcp_pkt) + tcphdr_offset;
len -= link_offset + ip_hl + tcphdr_offset;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
ip_src, ip_dst, ntohs(tcp_pkt->th_sport), ntohs(tcp_pkt->th_dport), tcp_pkt->th_flags,
tcphdr_offset, fragmented, frag_offset, frag_id);
} break;
case IPPROTO_UDP: {
struct udphdr *udp_pkt = (struct udphdr *)((unsigned char *)(ip4_pkt) + ip_hl);
uint16_t udphdr_offset = (frag_offset) ? 0 : sizeof(*udp_pkt);
data = (unsigned char *)(udp_pkt) + udphdr_offset;
len -= link_offset + ip_hl + udphdr_offset;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
ntohs(udp_pkt->uh_sport), ntohs(udp_pkt->uh_dport), 0,
udphdr_offset, fragmented, frag_offset, frag_id);
} break;
case IPPROTO_ICMP: {
struct icmp *icmp4_pkt = (struct icmp *)((unsigned char *)(ip4_pkt) + ip_hl);
uint16_t icmp4hdr_offset = (frag_offset) ? 0 : 4;
data = (unsigned char *)(icmp4_pkt) + icmp4hdr_offset;
len -= link_offset + ip_hl + icmp4hdr_offset;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
ip_src, ip_dst, icmp4_pkt->icmp_type, icmp4_pkt->icmp_code, 0,
icmp4hdr_offset, fragmented, frag_offset, frag_id);
} break;
#if USE_IPv6
case IPPROTO_ICMPV6: {
struct icmp6_hdr *icmp6_pkt = (struct icmp6_hdr *)((unsigned char *)(ip6_pkt) + ip_hl);
uint16_t icmp6hdr_offset = (frag_offset) ? 0 : 4;
data = (unsigned char *)(icmp6_pkt) + icmp6hdr_offset;
len -= link_offset + ip_hl + icmp6hdr_offset;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
ip_src, ip_dst, icmp6_pkt->icmp6_type, icmp6_pkt->icmp6_code, 0,
icmp6hdr_offset, fragmented, frag_offset, frag_id);
} break;
#endif
case IPPROTO_IGMP: {
struct igmp *igmp_pkt = (struct igmp *)((unsigned char *)(ip4_pkt) + ip_hl);
uint16_t igmphdr_offset = (frag_offset) ? 0 : 4;
data = (unsigned char *)(igmp_pkt) + igmphdr_offset;
len -= link_offset + ip_hl + igmphdr_offset;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
ip_src, ip_dst, igmp_pkt->igmp_type, igmp_pkt->igmp_code, 0,
igmphdr_offset, fragmented, frag_offset, frag_id);
} break;
default: {
data = (unsigned char *)(ip4_pkt) + ip_hl;
len -= link_offset + ip_hl;
if ((int32_t)len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
ip_src, ip_dst, 0, 0, 0,
0, fragmented, frag_offset, frag_id);
} break;
}
if (max_matches && matches >= max_matches)
clean_exit(0);
if (match_after && keep_matching)
keep_matching--;
}
void dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, unsigned char *data, uint32_t len,
const char *ip_src, const char *ip_dst, uint16_t sport, uint16_t dport, uint8_t flags,
uint16_t hdr_offset, uint8_t frag, uint16_t frag_offset, uint32_t frag_id) {
uint16_t match_size, match_index;
if (!show_empty && len == 0)
return;
if (len > limitlen)
len = limitlen;
if ((len > 0 && match_func(data, len, &match_index, &match_size) == invert_match) && !keep_matching)
return;
if (!live_read && want_delay)
dump_delay(h);
{
char ident;
switch (proto) {
case IPPROTO_TCP: ident = TCP; break;
case IPPROTO_UDP: ident = UDP; break;
case IPPROTO_ICMP: ident = ICMP; break;
case IPPROTO_ICMPV6: ident = ICMPv6; break;
case IPPROTO_IGMP: ident = IGMP; break;
default: ident = UNKNOWN; break;
}
printf("\n%c", ident);
}
if (show_proto)
printf("(%u)", proto);
printf(" ");
if (print_time)
print_time(h);
if ((proto == IPPROTO_TCP || proto == IPPROTO_UDP) && (sport || dport) && (hdr_offset || frag_offset == 0))
printf("%s:%u -> %s:%u", ip_src, sport, ip_dst, dport);
else
printf("%s -> %s", ip_src, ip_dst);
if (proto == IPPROTO_TCP && flags)
printf(" [%s%s%s%s%s%s%s%s]",
(flags & TH_ACK) ? "A" : "",
(flags & TH_SYN) ? "S" : "",
(flags & TH_RST) ? "R" : "",
(flags & TH_FIN) ? "F" : "",
(flags & TH_URG) ? "U" : "",
(flags & TH_PUSH)? "P" : "",
(flags & TH_ECE) ? "E" : "",
(flags & TH_CWR) ? "C" : "");
switch (proto) {
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
case IPPROTO_IGMP:
printf(" %u:%u", sport, dport);
}
if (frag)
printf(" %s%u@%u:%u",
frag_offset?"+":"", frag_id, frag_offset, len);
if (dump_single)
printf(" ");
else
printf(" #%u\n", seen_frames);
if (quiet < 3)
dump_func(data, len, match_index, match_size);
if (pd_dump)
pcap_dump((u_char*)pd_dump, h, p);
#if USE_TCPKILL
if (tcpkill_active)
tcpkill_kill(h, p, link_offset, tcpkill_active);
#endif
}
int8_t re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, uint16_t *msize) {
#if USE_PCRE
static int sub[2];
switch(pcre_exec(pattern, 0, (char const *)data, (int32_t)len, 0, 0, 0, 0)) {
case PCRE_ERROR_NULL:
case PCRE_ERROR_BADOPTION:
case PCRE_ERROR_BADMAGIC:
case PCRE_ERROR_UNKNOWN_NODE: