-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap-print-time-qname-qtype-rcode.c
104 lines (92 loc) · 2.08 KB
/
pcap-print-time-qname-qtype-rcode.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <assert.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <ldns/ldns.h>
#include <getopt.h>
#include <pcap.h>
#include "pcap_layers.h"
#ifdef __GLIBC__
#define __u6_addr __in6_u
#endif
static pcap_t *in = NULL;
static struct pcap_pkthdr hdr;
static int include_queries = 0;
int
my_dns_handler(const u_char * buf, int len, void *userdata)
{
ldns_pkt *pkt = 0;
ldns_rr_list *qd = 0;
ldns_rr *q = 0;
ldns_rdf *qn = 0;
ldns_rr_type qt;
char *qn_str = 0;
if (LDNS_STATUS_OK != ldns_wire2pkt(&pkt, buf, len))
goto done;
if (1 != ldns_pkt_qr(pkt) && !include_queries)
goto done;
qd = ldns_pkt_question(pkt);
if (0 == qd)
goto done;
q = ldns_rr_list_rr(qd, 0);
if (0 == q)
goto done;
qn = ldns_rr_owner(q);
if (0 == qn)
goto done;
qn_str = ldns_rdf2str(qn);
qt = ldns_rr_get_type(q);
printf("%10lu.%06lu %s %d %d\n", hdr.ts.tv_sec, hdr.ts.tv_usec, qn_str, qt, ldns_pkt_get_rcode(pkt));
done:
ldns_pkt_free(pkt);
LDNS_FREE(qn_str);
return 0;
}
int
main(int argc, char *argv[])
{
char errbuf[PCAP_ERRBUF_SIZE + 1];
const u_char *data;
int i;
while ((i = getopt(argc, argv, "q")) != -1) {
switch (i) {
case 'q':
include_queries = 1;
break;
case '?':
default:
fprintf(stderr, "usage: %s [-q] < pcap-in\n", argv[0]);
exit(1);
}
}
argc -= optind;
argv += optind;
setbuf(stdout, NULL);
setbuf(stderr, NULL);
in = pcap_open_offline("-", errbuf);
if (NULL == in) {
fprintf(stderr, "stdin: %s", errbuf);
exit(1);
}
pcap_layers_init(pcap_datalink(in), 0);
callback_l7 = my_dns_handler;
while ((data = pcap_next(in, &hdr))) {
handle_pcap(0, &hdr, data);
}
return 0;
}