-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp_helper.cpp
More file actions
152 lines (125 loc) · 4.23 KB
/
Copy patharp_helper.cpp
File metadata and controls
152 lines (125 loc) · 4.23 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "stdafx.h"
#include "pcap.h"
#include "ether.h"
#include "arp.h"
#include "arp_helper.h"
#define CAP_MAX_TIMEOUTS 65
//#define CAP_MAX_TIMEOUTS 100
//#define CAP_MAX_PACKETS 100
#define CAP_MAX_PACKETS 65
int get_remote_mac(pcap_t *cap_dev, const u_char *if_addr, u_long sip, u_long dip, u_char *remote_mac) {
u_char arp_packet[sizeof(struct ethhdr) + sizeof(arphdr_ether)];
struct pcap_pkthdr *header;
const u_char *pkt_data;
int res = 0;
/* In case we fail */
memset(remote_mac, 0, ETH_ALEN);
/* Build the ARP request packet */
generate_arp_request(arp_packet, if_addr, sip, dip);
/* Send the ARP request */
/* There's a race condition here because the reply might arrive before we capture it */
if (pcap_sendpacket(cap_dev, arp_packet, sizeof(arp_packet)) != 0)
return -1;
int timeouts = 0;
int packets = 0;
/* Start the capture */
while ((res = pcap_next_ex(cap_dev, &header, &pkt_data)) >= 0)
{
//static int timeouts = 0;
//static int packets = 0;
/* Timeout elapsed? */
if (res == 0) {
if (++timeouts > CAP_MAX_TIMEOUTS)
break;
}
else {
/* Look for the ARP reply (source and destination are reversed) */
if (process_arp_reply(header, pkt_data, dip, sip, remote_mac) == 0)
{
printf("t:%d, p:%d\t",timeouts,packets);
return 0;
}
/* Seen too many packets without finding ours? */
if (++packets >= CAP_MAX_PACKETS)
break;
}
}
/* Didn't receive the appropriate ARP reply */
return -2;
}
/* Create an ARP request according to the given parameters */
void generate_arp_request(u_char *packet, const u_char *if_addr, u_long sip, u_long dip) {
struct ethhdr *ethp;
struct arphdr_ether *arpp;
/* Fill Ethernet data */
ethp = (struct ethhdr *)packet;
ethp->h_proto = htons(ETHERTYPE_ARP);
memcpy(ethp->h_source, if_addr, ETH_ALEN);
/* Send to the broadcast address */
memset(ethp->h_dest, 0xFF, ETH_ALEN);
/* Fill ARP data */
arpp = (struct arphdr_ether *)(packet + sizeof(struct ethhdr));
arpp->ar_hrd = htons(ARPHRD_ETHER);
arpp->ar_pro = htons(ETHERTYPE_IP);
arpp->ar_hln = ETH_ALEN;
arpp->ar_pln = sizeof(u_long);
arpp->ar_op = htons(ARPOP_REQUEST);
memcpy(arpp->ar_sha, if_addr, ETH_ALEN);
memcpy(arpp->ar_sip, &sip, sizeof(u_long));
/* We don't know who the target is */
memset(arpp->ar_tha, 0, ETH_ALEN);
memcpy(arpp->ar_tip, &dip, sizeof(u_long));
}
/* Create an ARP reply according to the given parameters */
void generate_arp_reply(u_char *packet, const u_char *if_addr, const u_char *dst_haddr, u_long sip, u_long dip) {
struct ethhdr *ethp;
struct arphdr_ether *arpp;
/* Fill Ethernet data */
ethp = (struct ethhdr *)packet;
ethp->h_proto = htons(ETHERTYPE_ARP);
memcpy(ethp->h_source, if_addr, ETH_ALEN);
/* Send to the destination address */
memcpy(ethp->h_dest, dst_haddr, ETH_ALEN);
/* Fill ARP data */
arpp = (struct arphdr_ether *)(packet + sizeof(struct ethhdr));
arpp->ar_hrd = htons(ARPHRD_ETHER);
arpp->ar_pro = htons(ETHERTYPE_IP);
arpp->ar_hln = ETH_ALEN;
arpp->ar_pln = sizeof(u_long);
arpp->ar_op = htons(ARPOP_REPLY);
memcpy(arpp->ar_sha, if_addr, ETH_ALEN);
memcpy(arpp->ar_sip, &sip, sizeof(u_long));
memcpy(arpp->ar_tha, dst_haddr, ETH_ALEN);
memcpy(arpp->ar_tip, &dip, sizeof(u_long));
}
int process_arp_reply(struct pcap_pkthdr *header, const u_char *pkt_data, u_long sip, u_long dip, u_char *remote_mac) {
const struct ethhdr *ethp;
const struct arphdr_ether *arpp;
/* Sanity checks */
if (header->caplen < sizeof(struct ethhdr) + sizeof(arphdr_ether))
/* Packet too small */
return -1;
ethp = (struct ethhdr *)pkt_data;
if (ethp->h_proto != htons(ETHERTYPE_ARP))
/* Not an ARP packet */
return -2;
arpp = (struct arphdr_ether *)(pkt_data + sizeof(struct ethhdr));
if (arpp->ar_hrd != htons(ARPHRD_ETHER))
/* ARP not for Ethernet */
return -3;
if (arpp->ar_pro != htons(ETHERTYPE_IP))
/* ARP not for IP */
return -4;
if (ntohs(arpp->ar_op) != ARPOP_REPLY)
/* Not an ARP reply */
return -5;
if (*((u_long *)&(arpp->ar_sip)) != sip)
/* Not the source we were looking for */
return -6;
if (*((u_long *)&(arpp->ar_tip)) != dip)
/* Not the destination we were looking for */
return -7;
/* Everything looks ok - copy the ethernet address of the sender */
memcpy(remote_mac, arpp->ar_sha, ETH_ALEN);
return 0;
}