-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharprev.c
70 lines (63 loc) · 1.95 KB
/
arprev.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
//learn form network
//this program have Segmentation fault,need find
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <string.h>
#define ARP_REQUEST 1
typedef struct aprhdr{
u_int16_t htype; //Hardware type
u_int16_t ptype; //protocol type
u_char hlen; //Hardware address length
u_char plen; //protocol address length
u_int16_t oper; //operation code
u_char sha[6]; //sender hardware address
u_char spa[4]; //sender ip address
u_char tha[6]; //target hardware address
u_char tpa[4]; //target ip address
}arphdr_t;
#define MAX 2048
int main(int argc, char const *argv[]){
int i = 0;
bpf_u_int32 net = 0, mast = 0;
struct bpf_program filter;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *des = NULL;
struct pcap_pkthdr hk;
const unsigned char *packet = NULL;
arphdr_t *arp = NULL;
memset(errbuf, 0, PCAP_ERRBUF_SIZE);
des = pcap_open_live(argv[1], MAX, 0, 512, errbuf); //参数argv[1]不能写成"argv[1]"
pcap_lookupnet(argv[1], &net, &mast, errbuf);
pcap_compile(des, &filter, "arp", 1, 0); //这里出现段错误
pcap_setfilter(des, &filter);
while(1){
packet = pcap_next(des, &hk);
arp = (struct arphdr*)(packet + 14);
if(arp != NULL){
printf("\nRecived packet size:\t%d", hk.len);
printf("-Hardware type:\t%s", (ntohs(arp->htype) == 1) ? "Ethernet" : "Unknown");
printf("-Protocol type:\t%s", (ntohs(arp->ptype) == 0x0800) ? "IPv4" : "Unknown");
printf("-Operation:\t%s", (ntohs(arp->oper) == ARP_REQUEST) ? "ARP Request" : "ARP Reply");
if(ntohs(arp->htype) == 1 && ntohs(arp->ptype) == 0x0800){
printf("-Sender MAC:\t");
for(i = 0; i < 6; i++){
printf("%02X:", arp->sha[i]);
}
printf("-Sender IP:\t");
for(i = 0; i < 4; i++){
printf("%d.", arp->spa[i]);
}
printf("-Target MAC\t");
for(i = 0; i < 6; i++){
printf("%02X:", arp->tha[i]);
}
printf("-Target IP:\t");
for(i = 0; i < 4; i++){
printf("%d.", arp->tpa[i]);
}
}
}
}
return 0;
}