forked from measurement-factory/dnstop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnstop.c
2113 lines (1943 loc) · 44.5 KB
/
dnstop.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
/*
* $Id$
*
* http://dnstop.measurement-factory.com/
*
* Copyright (c) 2002, The Measurement Factory, Inc. All rights reserved. See
* the LICENSE file for details.
*/
static const char *Version = "@VERSION@";
#include "config.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <pcap.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <curses.h>
#include <assert.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
#include <arpa/nameser_compat.h>
#endif
#include <sys/socket.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netdb.h>
#ifdef HAVE_NET_IF_PPP_H
#include <net/if_ppp.h>
#define PPP_ADDRESS_VAL 0xff /* The address byte value */
#define PPP_CONTROL_VAL 0x03 /* The control byte value */
#endif
#include "hashtbl.h"
#include "inX_addr.h"
#define PCAP_SNAPLEN 65535
#define MAX_QNAME_SZ 512
#define DNS_MSG_HDR_SZ 12
#ifndef ETHER_HDR_LEN
#define ETHER_ADDR_LEN 6
#define ETHER_TYPE_LEN 2
#define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
#endif
#ifndef ETHERTYPE_8021Q
#define ETHERTYPE_8021Q 0x8100
#endif
#ifndef ETHERTYPE_IPV6
#define ETHERTYPE_IPV6 0x86DD
#endif
#if defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)
#define uh_dport dest
#define uh_sport source
#endif
typedef struct {
inX_addr src;
int count;
} AgentAddr;
typedef struct {
char *s;
int count;
} StringCounter;
typedef struct {
inX_addr addr;
char *str;
} StringAddr;
/* This struct cobbles together Source and Nld */
typedef struct {
StringAddr straddr;
int count;
} StringAddrCounter;
typedef struct {
int cnt;
void *ptr;
} SortItem;
typedef struct _rfc1035_header rfc1035_header;
struct _rfc1035_header {
unsigned short id;
unsigned int qr:1;
unsigned int opcode:4;
unsigned int aa:1;
unsigned int tc:1;
unsigned int rd:1;
unsigned int ra:1;
unsigned int rcode:4;
unsigned short qdcount;
unsigned short ancount;
unsigned short nscount;
unsigned short arcount;
};
struct ip_list_s {
inX_addr addr;
void *data;
struct ip_list_s *next;
};
typedef struct ip_list_s ip_list_t;
char *device = NULL;
pcap_t *pcap = NULL;
/*
* bpf_program_str used to default to:
*
* udp dst port 53 and udp[10:2] & 0x8000 = 0
*
* but that didn't work so well with IPv6. Now we have the command line options
* -Q and -R to choose counting queries, responses, or both.
*/
char *bpf_program_str = "udp port 53";
WINDOW *w;
static unsigned short check_port = 0;
void (*SubReport) (void)= NULL;
int (*handle_datalink) (const u_char * pkt, int len)= NULL;
int Quit = 0;
int Got_EOF = 0;
unsigned int hash_buckets = 100057;
char *progname = NULL;
int anon_flag = 0;
int max_level = 2;
int cur_level = 1;
int promisc_flag = 1;
int progress_flag = 0;
ip_list_t *IgnoreList = NULL;
int do_redraw = 1;
int opt_count_queries = 0;
int opt_count_replies = 0;
int opt_count_ipv4 = 0;
int opt_count_ipv6 = 0;
int opt_count_domsrc = 1;
const char *opt_filter_by_name = 0;
/*
* flags/features for non-interactive mode
*/
int interactive = 1;
typedef int (printer) (const char *,...);
printer *print_func = (printer *) printw;
typedef const char *(col_fmt) (const SortItem *);
typedef char *(strify) (unsigned int);
#define T_MAX 65536
#define C_MAX 65536
#define OP_MAX 16
#define RC_MAX 16
unsigned int query_count_intvl = 0;
unsigned int query_count_total = 0;
unsigned int reply_count_intvl = 0;
unsigned int reply_count_total = 0;
int qtype_counts[T_MAX];
int opcode_counts[OP_MAX];
int rcode_counts[RC_MAX];
int qclass_counts[C_MAX];
hashtbl *Sources = NULL;
hashtbl *Destinations = NULL;
hashtbl *Domains[10];
hashtbl *DomSrcs[10];
hashtbl *KnownTLDs = NULL;
hashtbl *NewGTLDs = NULL;
#ifdef HAVE_STRUCT_BPF_TIMEVAL
struct bpf_timeval last_ts;
#else
struct timeval last_ts;
#endif
time_t report_interval = 1;
/*
* These hold "%d" printfed strings for non-standard types/codes
*/
static char *qtypes_buf[T_MAX];
static char *rcodes_buf[RC_MAX];
static char *opcodes_buf[OP_MAX];
/* Prototypes */
void Sources_report(void);
void Destinatioreport(void);
void Qtypes_report(void);
void Opcodes_report(void);
void Rcodes_report(void);
void Domain_report();
void DomSrc_report();
void Help_report(void);
void ResetCounters(void);
void report(void);
typedef struct {
unsigned short qtype;
unsigned short qclass;
const char *qname;
const inX_addr *src_addr;
const inX_addr *dst_addr;
unsigned char rcode;
} FilterData;
typedef int Filter_t(FilterData *);
Filter_t UnknownTldFilter;
Filter_t AforAFilter;
Filter_t RFC1918PtrFilter;
Filter_t RcodeRefusedFilter;
Filter_t RcodeServfailFilter;
Filter_t QnameFilter;
Filter_t BitsquatFilter;
Filter_t QtypeAnyFilter;
Filter_t *Filter = NULL;
unsigned int
my_inXaddr_hash(const void *key)
{
return inXaddr_hash(key, 24);
}
int
my_inXaddr_cmp(const void *a, const void *b)
{
return inXaddr_cmp(a, b);
}
int
ignore_list_match(const inX_addr * addr)
{
ip_list_t *ptr;
for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
if (0 == inXaddr_cmp(addr, &ptr->addr))
return (1);
return (0);
}
void
ignore_list_add(const inX_addr * addr)
{
ip_list_t *new;
if (ignore_list_match(addr) != 0)
return;
new = malloc(sizeof(ip_list_t));
if (new == NULL) {
perror("malloc");
return;
}
new->addr = *addr;
new->next = IgnoreList;
IgnoreList = new;
}
void
ignore_list_add_name(const char *name)
{
struct addrinfo *ai_list;
struct addrinfo *ai_ptr;
inX_addr addr;
int status;
status = getaddrinfo(name, NULL, NULL, &ai_list);
if (status != 0)
return;
for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
if (ai_ptr->ai_family == AF_INET) {
inXaddr_assign_v4(&addr, &((struct sockaddr_in *)ai_ptr->ai_addr)->
sin_addr);
ignore_list_add(&addr);
}
#if USE_IPV6
else if (ai_ptr->ai_family == AF_INET6) {
inXaddr_assign_v6(&addr, &((struct sockaddr_in6 *)ai_ptr->ai_addr)->sin6_addr);
ignore_list_add(&addr);
}
#endif
}
freeaddrinfo(ai_list);
}
void
allocate_anonymous_address(inX_addr * anon_addr, const inX_addr * orig_addr)
{
static ip_list_t *list = NULL;
static int entropy_fd = -1;
ip_list_t *ptr;
for (ptr = list; ptr != NULL; ptr = ptr->next) {
if (0 == inXaddr_cmp(orig_addr, &ptr->addr))
break;
}
if (-1 == entropy_fd) {
entropy_fd = open("/dev/urandom", 0);
if (-1 == entropy_fd) {
entropy_fd = open("/dev/random", 0);
if (-1 == entropy_fd) {
fprintf(stderr, "failed to open /dev/urandom or /dev/random");
exit(1);
}
}
}
if (ptr == NULL) {
char buf[16];
ptr = (ip_list_t *) malloc(sizeof(ip_list_t) + sizeof(inX_addr));
if (ptr == NULL)
return;
ptr->addr = *orig_addr;
ptr->data = (void *)(ptr + 1);
if (4 == inXaddr_version(orig_addr)) {
ssize_t rd = read(entropy_fd, buf, 4);
if (rd < 4)
err(errno, "read entropy");
inXaddr_assign_v4(ptr->data, (struct in_addr *)buf);
}
#if USE_IPV6
else {
ssize_t rd = read(entropy_fd, buf, 16);
if (rd < 16)
err(errno, "read entropy");
inXaddr_assign_v6(ptr->data, (struct in6_addr *)buf);
}
#endif
ptr->next = list;
list = ptr;
}
*anon_addr = *(inX_addr *) ptr->data;
}
const char *
anon_inet_ntoa(const inX_addr * addr)
{
static char buffer[INET6_ADDRSTRLEN];
inX_addr anon_addr;
if (anon_flag) {
allocate_anonymous_address(&anon_addr, addr);
addr = &anon_addr;
}
return inXaddr_ntop(addr, buffer, sizeof(buffer));
}
AgentAddr *
AgentAddr_lookup_or_add(hashtbl * tbl, const inX_addr * addr)
{
AgentAddr *x = hash_find(addr, tbl);
if (NULL == x) {
x = calloc(1, sizeof(*x));
x->src = *addr;
hash_add(&x->src, x, tbl);
}
return x;
}
static unsigned int
string_hash(const void *s)
{
return hashendian(s, strlen(s), 0);
}
static int
string_cmp(const void *a, const void *b)
{
return strcmp(a, b);
}
StringCounter *
StringCounter_lookup_or_add(hashtbl * tbl, const char *s)
{
StringCounter *x = hash_find(s, tbl);
if (NULL == x) {
x = calloc(1, sizeof(*x));
x->s = strdup(s);
hash_add(x->s, x, tbl);
}
return x;
}
static unsigned int
stringaddr_hash(const void *p)
{
const StringAddr *sa = p;
unsigned int h0 = inXaddr_hash(&sa->addr, 32);
unsigned int h1 = hashendian(sa->str, strlen(sa->str), h0);
return h1;
}
static int
stringaddr_cmp(const void *a, const void *b)
{
const StringAddr *A = a;
const StringAddr *B = b;
int x = inXaddr_cmp(&A->addr, &B->addr);
if (x)
return x;
return strcmp(A->str, B->str);
}
StringAddrCounter *
StringAddrCounter_lookup_or_add(hashtbl * tbl, const inX_addr * addr, const char *str)
{
StringAddr sa;
StringAddrCounter *x;
sa.addr = *addr;
sa.str = (char *)str;
x = hash_find(&sa, tbl);
if (NULL == x) {
x = calloc(1, sizeof(*x));
x->straddr.str = strdup(str);
x->straddr.addr = *addr;
hash_add(&x->straddr, x, tbl);
}
return x;
}
int
SortItem_cmp(const void *A, const void *B)
{
const SortItem *a = A;
const SortItem *b = B;
if (a->cnt < b->cnt)
return 1;
if (a->cnt > b->cnt)
return -1;
if (a->ptr < b->ptr)
return 1;
if (a->ptr > b->ptr)
return -1;
return 0;
}
#define RFC1035_MAXLABELSZ 63
static int
rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
)
{
off_t no = 0;
unsigned char c;
size_t len;
static int loop_detect = 0;
if (loop_detect > 2)
return 4; /* compression loop */
if (ns <= 0)
return 4; /* probably compression loop */
do {
if ((*off) >= sz)
break;
c = *(buf + (*off));
if (c > 191) {
/* blasted compression */
int rc;
unsigned short s;
off_t ptr;
memcpy(&s, buf + (*off), sizeof(s));
s = ntohs(s);
(*off) += sizeof(s);
/* Sanity check */
if ((*off) >= sz)
return 1; /* message too short */
ptr = s & 0x3FFF;
/* Make sure the pointer is inside this message */
if (ptr >= sz)
return 2; /* bad compression ptr */
if (ptr < DNS_MSG_HDR_SZ)
return 2; /* bad compression ptr */
loop_detect++;
rc = rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
loop_detect--;
return rc;
} else if (c > RFC1035_MAXLABELSZ) {
/*
* "(The 10 and 01 combinations are reserved for future use.)"
*/
return 3; /* reserved label/compression flags */
break;
} else {
(*off)++;
len = (size_t) c;
if (len == 0)
break;
if (len > (ns - 1))
len = ns - 1;
if ((*off) + len > sz)
return 4; /* message is too short */
if (no + len + 1 > ns)
return 5; /* qname would overflow name buffer */
memcpy(name + no, buf + (*off), len);
(*off) += len;
no += len;
*(name + (no++)) = '.';
}
} while (c > 0);
if (no > 0)
*(name + no - 1) = '\0';
/* make sure we didn't allow someone to overflow the name buffer */
assert(no <= ns);
return 0;
}
const char *
QnameToNld(const char *qname, int nld)
{
const char *t = strrchr(qname, '.');
int dotcount = 1;
if (NULL == t)
t = qname;
if (0 == strcmp(t, ".arpa"))
dotcount--;
while (t > qname && dotcount < nld) {
t--;
if ('.' == *t)
dotcount++;
}
if (t > qname)
t++;
return t;
}
char *
str_tolower(char *s)
{
char *t;
for (t = s; *t; t++)
*t = tolower(*t);
return s;
}
int
handle_dns(const char *buf, int len,
const inX_addr * src_addr,
const inX_addr * dst_addr)
{
rfc1035_header qh;
unsigned short us;
char qname[MAX_QNAME_SZ];
unsigned short qtype;
unsigned short qclass;
off_t offset;
char *t;
const char *s;
int x;
StringCounter *sc;
AgentAddr *agent;
int lvl;
if (len < sizeof(qh))
return 0;
memcpy(&us, buf + 00, 2);
qh.id = ntohs(us);
memcpy(&us, buf + 2, 2);
us = ntohs(us);
qh.qr = (us >> 15) & 0x01;
if (0 == qh.qr && 0 == opt_count_queries)
return 0;
if (1 == qh.qr && 0 == opt_count_replies)
return 0;
qh.opcode = (us >> 11) & 0x0F;
qh.aa = (us >> 10) & 0x01;
qh.tc = (us >> 9) & 0x01;
qh.rd = (us >> 8) & 0x01;
qh.ra = (us >> 7) & 0x01;
qh.rcode = us & 0x0F;
memcpy(&us, buf + 4, 2);
qh.qdcount = ntohs(us);
memcpy(&us, buf + 6, 2);
qh.ancount = ntohs(us);
memcpy(&us, buf + 8, 2);
qh.nscount = ntohs(us);
memcpy(&us, buf + 10, 2);
qh.arcount = ntohs(us);
offset = sizeof(qh);
memset(qname, '\0', MAX_QNAME_SZ);
x = rfc1035NameUnpack(buf, len, &offset, qname, MAX_QNAME_SZ);
if (0 != x)
return 0;
if ('\0' == qname[0])
strcpy(qname, ".");
while ((t = strchr(qname, '\n')))
*t = ' ';
while ((t = strchr(qname, '\r')))
*t = ' ';
str_tolower(qname);
memcpy(&us, buf + offset, 2);
qtype = ntohs(us);
memcpy(&us, buf + offset + 2, 2);
qclass = ntohs(us);
if (Filter) {
FilterData fd;
fd.qtype = qtype;
fd.qclass = qclass;
fd.qname = qname;
fd.src_addr = src_addr;
fd.dst_addr = dst_addr;
fd.rcode = qh.rcode;
if (0 == Filter(&fd))
return 0;
}
/* gather stats */
if (0 == opt_count_replies || 1 == qh.qr) {
rcode_counts[qh.rcode]++;
if ((agent = AgentAddr_lookup_or_add(Destinations, dst_addr)) != NULL)
agent->count++;
}
if (0 == opt_count_queries || 0 == qh.qr) {
qtype_counts[qtype]++;
qclass_counts[qclass]++;
opcode_counts[qh.opcode]++;
if ((agent = AgentAddr_lookup_or_add(Sources, src_addr)) != NULL)
agent->count++;
for (lvl = 1; lvl <= max_level; lvl++) {
s = QnameToNld(qname, lvl);
sc = StringCounter_lookup_or_add(Domains[lvl], s);
sc->count++;
if (opt_count_domsrc) {
StringAddrCounter *ssc;
ssc = StringAddrCounter_lookup_or_add(DomSrcs[lvl], src_addr, s);
ssc->count++;
}
}
}
if (0 == qh.qr) {
query_count_intvl++;
query_count_total++;
} else {
reply_count_intvl++;
reply_count_total++;
}
return 1;
}
int
handle_udp(const struct udphdr *udp, int len,
const inX_addr * src_addr,
const inX_addr * dst_addr)
{
if (check_port && check_port != udp->uh_dport && check_port != udp->uh_sport)
return 0;
if (0 == handle_dns((char *)(udp + 1), len - sizeof(*udp), src_addr, dst_addr))
return 0;
return 1;
}
#if USE_IPV6
int
handle_ipv6(struct ip6_hdr *ipv6, int len)
{
int offset;
int nexthdr;
inX_addr src_addr;
inX_addr dst_addr;
uint16_t payload_len;
if (0 == opt_count_ipv6)
return 0;
offset = sizeof(struct ip6_hdr);
nexthdr = ipv6->ip6_nxt;
inXaddr_assign_v6(&src_addr, &ipv6->ip6_src);
inXaddr_assign_v6(&dst_addr, &ipv6->ip6_dst);
payload_len = ntohs(ipv6->ip6_plen);
if (ignore_list_match(&src_addr))
return (0);
/*
* Parse extension headers. This only handles the standard headers, as
* defined in RFC 2460, correctly. Fragments are discarded.
*/
while ((IPPROTO_ROUTING == nexthdr) /* routing header */
||(IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
||(IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
||(IPPROTO_DSTOPTS == nexthdr) /* destination options. */
||(IPPROTO_DSTOPTS == nexthdr) /* destination options. */
||(IPPROTO_AH == nexthdr) /* destination options. */
||(IPPROTO_ESP == nexthdr)) { /* encapsulating security payload. */
struct {
uint8_t nexthdr;
uint8_t length;
} ext_hdr;
uint16_t ext_hdr_len;
/* Catch broken packets */
if ((offset + sizeof(ext_hdr)) > len)
return (0);
/* Cannot handle fragments. */
if (IPPROTO_FRAGMENT == nexthdr)
return (0);
memcpy(&ext_hdr, (char *)ipv6 + offset, sizeof(ext_hdr));
nexthdr = ext_hdr.nexthdr;
ext_hdr_len = (8 * (ntohs(ext_hdr.length) + 1));
/* This header is longer than the packets payload.. WTF? */
if (ext_hdr_len > payload_len)
return (0);
offset += ext_hdr_len;
payload_len -= ext_hdr_len;
} /* while */
/* Catch broken and empty packets */
if (((offset + payload_len) > len)
|| (payload_len == 0))
return (0);
if (IPPROTO_UDP != nexthdr)
return (0);
if (handle_udp((struct udphdr *)((char *)ipv6 + offset), payload_len, &src_addr, &dst_addr) == 0)
return (0);
return (1); /* Success */
}
#endif
int
handle_ipv4(const struct ip *ip, int len)
{
int offset = ip->ip_hl << 2;
inX_addr src_addr;
inX_addr dst_addr;
#if USE_IPV6
if (ip->ip_v == 6)
return (handle_ipv6((struct ip6_hdr *)ip, len));
#endif
if (0 == opt_count_ipv4)
return 0;
inXaddr_assign_v4(&src_addr, &ip->ip_src);
inXaddr_assign_v4(&dst_addr, &ip->ip_dst);
if (ignore_list_match(&src_addr))
return (0);
if (IPPROTO_UDP != ip->ip_p)
return 0;
if (0 == handle_udp((struct udphdr *)((char *)ip + offset), len - offset, &src_addr, &dst_addr))
return 0;
return 1;
}
#ifdef HAVE_NET_IF_PPP_H
int
handle_ppp(const u_char * pkt, int len)
{
unsigned short us;
unsigned short proto;
if (len < 2)
return 0;
if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
pkt += 2; /* ACFC not used */
len -= 2;
}
if (len < 2)
return 0;
if (*pkt % 2) {
proto = *pkt; /* PFC is used */
pkt++;
len--;
} else {
memcpy(&us, pkt, sizeof(us));
proto = ntohs(us);
pkt += 2;
len -= 2;
}
if (ETHERTYPE_IP != proto && PPP_IP != proto)
return 0;
return handle_ipv4((struct ip *)pkt, len);
}
#endif
int
handle_null(const u_char * pkt, int len)
{
unsigned int family;
memcpy(&family, pkt, sizeof(family));
if (AF_INET == family)
return handle_ipv4((struct ip *)(pkt + 4), len - 4);
#if USE_IPV6
if (AF_INET6 == family)
return handle_ipv6((struct ip6_hdr *)(pkt + 4), len - 4);
#endif
return 0;
}
#ifdef DLT_LOOP
int
handle_loop(const u_char * pkt, int len)
{
unsigned int family;
memcpy(&family, pkt, sizeof(family));
if (AF_INET == ntohl(family))
return handle_ipv4((struct ip *)(pkt + 4), len - 4);
#if USE_IPV6
if (AF_INET6 == ntohl(family))
return handle_ipv6((struct ip6_hdr *)(pkt + 4), len - 4);
#endif
return 0;
}
#endif
#ifdef DLT_RAW
int
handle_raw(const u_char * pkt, int len)
{
return handle_ipv4((struct ip *)pkt, len);
}
#endif
int
handle_ip(const u_char * pkt, int len, unsigned short etype)
{
#if USE_IPV6
if (ETHERTYPE_IPV6 == etype) {
return (handle_ipv6((struct ip6_hdr *)pkt, len));
} else
#endif
if (ETHERTYPE_IP == etype) {
return handle_ipv4((struct ip *)pkt, len);
}
return 0;
}
int
handle_ether(const u_char * pkt, int len)
{
struct ether_header *e = (void *)pkt;
unsigned short etype = ntohs(e->ether_type);
if (len < ETHER_HDR_LEN)
return 0;
pkt += ETHER_HDR_LEN;
len -= ETHER_HDR_LEN;
if (ETHERTYPE_8021Q == etype) {
etype = ntohs(*(unsigned short *)(pkt + 2));
pkt += 4;
len -= 4;
}
return handle_ip(pkt, len, etype);
}
#ifdef DLT_LINUX_SLL
static int
handle_linux_sll(const u_char * pkt, int len)
{
struct sll_header {
uint16_t pkt_type;
uint16_t dev_type;
uint16_t addr_len;
uint8_t addr[8];
uint16_t proto_type;
} *hdr;
uint16_t etype;
if (len < sizeof(struct sll_header))
return (0);
hdr = (struct sll_header *)pkt;
pkt = (u_char *) (hdr + 1);
len -= sizeof(struct sll_header);
etype = ntohs(hdr->proto_type);
return handle_ip(pkt, len, etype);
}
#endif /* DLT_LINUX_SLL */
void
handle_pcap(u_char * udata, const struct pcap_pkthdr *hdr, const u_char * pkt)
{
if (hdr->caplen < ETHER_HDR_LEN)
return;
if (0 == handle_datalink(pkt, hdr->caplen))
return;
last_ts = hdr->ts;
}
void
cron_pre(void)
{
(void)0;
}
void
cron_post(void)
{
query_count_intvl = 0;
reply_count_intvl = 0;
}
void
redraw()
{
cron_pre();
report();
cron_post();
do_redraw = 0;
}
void
keyboard(void)
{
int ch;
int old_do_redraw = do_redraw;
/*
* The screen should be redrawn after any valid key is pressed.
*/
do_redraw = 1;
ch = getch() & 0xff;
if (ch >= 'A' && ch <= 'Z')
ch += 'a' - 'A';
switch (ch) {
case 's':
SubReport = Sources_report;
break;
case 'd':
SubReport = Destinatioreport;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
SubReport = Domain_report;
cur_level = ch - '0';
break;
case '!':
SubReport = DomSrc_report;
cur_level = 1;
break;
case 'c':
case '@':
SubReport = DomSrc_report;
cur_level = 2;
break;
case '#':
SubReport = DomSrc_report;
cur_level = 3;
break;
case '$':
SubReport = DomSrc_report;
cur_level = 4;
break;
case '%':
SubReport = DomSrc_report;
cur_level = 5;
break;
case '^':
SubReport = DomSrc_report;
cur_level = 6;
break;
case '&':
SubReport = DomSrc_report;
cur_level = 7;
break;
case '*':
SubReport = DomSrc_report;
cur_level = 8;
break;