-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.c
68 lines (57 loc) · 1.27 KB
/
demo.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcap.h>
#include <err.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pcap_layers.h"
struct _foo {
struct in_addr src;
struct in_addr dst;
unsigned short qtype;
/* etc... */
};
/*
* this will only be called if 'ip' is a complete IPv header
*/
int
my_ip4_handler(const struct ip *ip4, int len, void *userdata)
{
fprintf(stderr, "got IPv4 layer, src=%s\n", inet_ntoa(ip4->ip_src));
return 0;
}
int
my_dns_handler(const u_char *buf, int len, void *userdata)
{
fprintf(stderr, "got DNS layer, %d bytes\n", len);
return 0;
}
int
main(int argc, char *argv[])
{
pcap_t *in = NULL;
char errbuf[PCAP_ERRBUF_SIZE + 1];
struct pcap_pkthdr hdr;
const u_char *data;
in = pcap_open_offline("-", errbuf);
if (NULL == in) {
fprintf(stderr, "stdin: %s", errbuf);
exit(1);
}
pcap_layers_init(pcap_datalink(in), 1);
callback_ipv4 = my_ip4_handler;
callback_l7 = my_dns_handler;
while ((data = pcap_next(in, &hdr))) {
struct _foo f;
memset(&f, 0, sizeof(f));
handle_pcap((u_char *) &f, &hdr, data);
}
pcap_close(in);
exit(0);
}