-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.c
70 lines (62 loc) · 1.9 KB
/
dump.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
#include <stdio.h>
#include <pcap.h>
#define ETHER_ADDR_LEN 6
#define SIZE_ETHERNET 14
#define SIZE_BOARDDATA 5
struct pkt_ethernet {
u_char ether_dhost[ETHER_ADDR_LEN];
u_char ether_shost[ETHER_ADDR_LEN];
u_short ether_type;
};
struct pkt_boarddata {
u_char board_tag;
u_char board_op;
u_short board_addr;
u_char board_paircount;
};
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet){
const struct pkt_ethernet *ethernet;
const struct pkt_boarddata *boarddata;
const u_char *payload;
ethernet = (struct pkt_ethernet*)(packet);
boarddata = (struct pkt_boarddata*)(packet + SIZE_ETHERNET);
payload = (u_char*)(packet + SIZE_ETHERNET + SIZE_BOARDDATA);
unsigned int count = boarddata->board_paircount + 1;
unsigned int i;
for(i = 0; i < count; ++i){
printf("%.2x%.2x%.2x", (unsigned int)payload[3*i+0], (unsigned int)payload[3*i+1], (unsigned int)payload[3*i+2]);
}
printf("\n");
}
int main(int argc, char *argv[]){
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
struct bpf_program fp;
char filter_exp[] = "ether broadcast";
struct pcap_pkthdr header;
const u_char *packet;
if(argc != 2){
fprintf(stderr, "Usage: %s pcapng-file\n", argv[0]);
return 1;
}
handle = pcap_open_offline(argv[1], errbuf);
if(handle == NULL){
fprintf(stderr, "Couldn't open capture file: %s\n", errbuf);
return 1;
}
if(pcap_datalink(handle) != DLT_EN10MB){
fprintf(stderr, "No Ethernet headers found\n");
return 1;
}
if(pcap_compile(handle, &fp, filter_exp, 0, PCAP_NETMASK_UNKNOWN) == -1){
fprintf(stderr, "Couldn't parse filter '%s': %s\n", filter_exp, pcap_geterr(handle));
return 1;
}
if(pcap_setfilter(handle, &fp) == -1){
fprintf(stderr, "Couldn't install filter '%s': %s\n", filter_exp, pcap_geterr(handle));
return 1;
}
pcap_loop(handle, -1, process_packet, NULL);
pcap_close(handle);
return 0;
}