-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcap-test.c
84 lines (70 loc) · 2.27 KB
/
pcap-test.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
/*
* pcap-test.c
*/
#include<pcap/pcap.h>
#include<stdint.h>
#include"crc32.h"
//typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
void callback(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
char src[24];
char dst[24];
sprintf(dst,"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
bytes[0],bytes[1],bytes[2],bytes[3],
bytes[4],bytes[5],bytes[6],bytes[7]);
sprintf(src,"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
bytes[8],bytes[9],bytes[10],bytes[11],
bytes[12],bytes[13],bytes[14],bytes[15]);
//uint32_t crc32(uint32_t crc, const void *buf, size_t size);
uint32_t crc = crc32(0/*initial value*/, bytes, h->caplen);
printf("Got packet SRC: %s DST: %s CRC32: %08X\n",src,dst,crc);
}
#define IFACE_A "eth0"
int main(int argc, char* argv[])
{
char errbuf[PCAP_ERRBUF_SIZE];
int r;
pcap_t* p_pcap;
// get a new packet capture handle
p_pcap = pcap_create(IFACE_A, errbuf);
if (p_pcap==NULL)
{
fprintf(stderr,"Error: Failed to create pcap handle: %s",errbuf);
return -1;
}
// capture packets of any size
pcap_set_snaplen(p_pcap, 65535);
// set promiscuous mode
pcap_set_promisc(p_pcap, 1);
// use high-precision host-synchronized timestamps from adaptor
switch (pcap_set_tstamp_type(p_pcap, PCAP_TSTAMP_ADAPTER))
{
case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
fprintf(stderr,"Warning: Interface does not support this timestamp type.\n");
break;
case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
fprintf(stderr,"Warning: Interface does not support setting the timestamp type.\n");
break;
}
// activate the handle
r = pcap_activate(p_pcap);
if (r!=0)
{
fprintf(stderr,"Warning: Non-zero return from pcap_activate: %02X\n",r);
}
if (pcap_setdirection(p_pcap, PCAP_D_IN) != 0)
{
pcap_perror(p_pcap, (char*) "Error: Failed to set capture direction");
// if we were to continue, we would cause a packet storm
return -1;
}
// get linktype (should be LINKTYPE_ETHERNET)
int i_linktype = pcap_datalink(p_pcap);
fprintf(stderr,"Info: Linktype is %02X.\n",i_linktype);
// capture packets until interrupt
pcap_loop(p_pcap, 0/*infinity*/, callback, NULL/*user*/);
printf("Finished.");
// cleanup
pcap_close(p_pcap);
return 0;
}