-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicmp_mac_resolver.h
More file actions
248 lines (206 loc) · 6.63 KB
/
icmp_mac_resolver.h
File metadata and controls
248 lines (206 loc) · 6.63 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define MAX_PACKETS 200
#define DEFAULT_INTERFACE "enp3s0"
#define TIMEOUT 2
class ICMPMacResolver {
public:
friend void test_checksum_calculation();
friend void test_socket_initialization();
ICMPMacResolver(std::string target_ip)
: recv_sock(-1), send_sock(-1), target_ip_(target_ip) {}
~ICMPMacResolver() {
if (recv_sock != -1) close(recv_sock);
if (send_sock != -1) close(send_sock);
}
bool get_mac(unsigned char* mac) {
if (!init_receive_socket()) return false;
if (!init_send_socket()) return false;
if (!send_icmp_request(target_ip_.c_str())) return false;
return receive_icmp_reply(mac, target_ip_.c_str());
}
private:
int recv_sock;
int send_sock;
std::string target_ip_;
unsigned short checksum(void* b, int len) {
unsigned short* buf = static_cast<unsigned short*>(b);
unsigned int sum = 0;
// Обрабатываем по 2 байта
while (len > 1) {
sum += *buf++;
len -= 2;
}
// Если остался 1 байт
if (len == 1) {
sum += *(unsigned char*)buf;
}
// Сворачиваем 32-битную сумму в 16 бит
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return static_cast<unsigned short>(~sum);
}
bool init_receive_socket() {
// Получаем автоматически определенный интерфейс
std::string iface = find_appropriate_interface(target_ip_.c_str());
recv_sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (recv_sock < 0) {
perror("receive socket");
return false;
}
struct ifreq ifr;
std::memset(&ifr, 0, sizeof(ifr));
std::strncpy(ifr.ifr_name, iface.c_str(), IFNAMSIZ);
if (setsockopt(recv_sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) <
0) {
perror("setsockopt SO_BINDTODEVICE");
return false;
}
// Set timeout
struct timeval tv;
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(recv_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt SO_RCVTIMEO");
return false;
}
return true;
}
bool init_send_socket() {
send_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (send_sock < 0) {
perror("send socket");
return false;
}
// Set send timeout (2 seconds)
struct timeval tv;
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(send_sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt SO_SNDTIMEO");
close(send_sock);
return false;
}
return true;
}
bool send_icmp_request(const char* ip_address) {
struct icmphdr icmp;
std::memset(&icmp, 0, sizeof(icmp));
icmp.type = ICMP_ECHO;
icmp.code = 0;
icmp.un.echo.id = htons(getpid());
icmp.un.echo.sequence = 1;
icmp.checksum = checksum(&icmp, sizeof(icmp));
struct sockaddr_in dest;
std::memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr(ip_address);
int result =
sendto(send_sock, &icmp, sizeof(icmp), 0,
reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest));
if (result <= 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
std::cerr << "Send operation timed out\n";
} else {
perror("sendto");
}
return false;
}
return true;
}
std::string find_appropriate_interface(const char* target_ip) {
struct ifaddrs *ifaddr, *ifa;
std::string best_interface;
in_addr_t target = inet_addr(target_ip);
in_addr_t best_mask = 0;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return DEFAULT_INTERFACE; // fallback
}
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr || ifa->ifa_addr->sa_family != AF_INET)
continue;
in_addr_t local = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
in_addr_t mask = ((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr;
if ((local & mask) == (target & mask) && mask > best_mask) {
best_mask = mask;
best_interface = ifa->ifa_name;
}
}
freeifaddrs(ifaddr);
return best_interface.empty() ? DEFAULT_INTERFACE : best_interface;
}
bool receive_icmp_reply(unsigned char* mac, const char* target_ip) {
char packet[IP_MAXPACKET];
struct sockaddr saddr;
socklen_t saddr_size = sizeof(saddr);
const int expected_id = htons(getpid());
const unsigned long expected_ip = inet_addr(target_ip);
unsigned int packets_count = MAX_PACKETS;
while (packets_count--) {
int bytes =
recvfrom(recv_sock, packet, sizeof(packet), 0, &saddr, &saddr_size);
if (bytes < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Timeout reached
return false;
}
perror("recvfrom");
return false;
}
// Check minimum packet size
if (bytes <
static_cast<int>(sizeof(struct ethhdr) + sizeof(struct iphdr) +
sizeof(struct icmphdr))) {
continue;
}
struct ethhdr* eth = reinterpret_cast<struct ethhdr*>(packet);
struct iphdr* ip =
reinterpret_cast<struct iphdr*>(packet + sizeof(struct ethhdr));
struct icmphdr* icmp = reinterpret_cast<struct icmphdr*>(
packet + sizeof(struct ethhdr) + sizeof(struct iphdr));
// Verify source IP address
if (ip->saddr != expected_ip) {
continue;
}
// Verify ICMP Destination Unreachable (type 3)
if (icmp->type == ICMP_DEST_UNREACH) {
std::cerr << "Host unreachable (ICMP Destination Unreachable)\n";
return false;
}
// Verify ICMP Time Exceeded (type 11)
if (icmp->type == ICMP_TIME_EXCEEDED) {
std::cerr << "Host unreachable (ICMP Time Exceeded)\n";
return false;
}
// Verify ICMP type and code
if (icmp->type != ICMP_ECHOREPLY || icmp->code != 0) {
continue;
}
// Verify ID and sequence number
if (icmp->un.echo.id != expected_id || icmp->un.echo.sequence != 1) {
continue;
}
// All checks passed - copy MAC address
std::memcpy(mac, eth->h_source, 6);
return true;
}
std::cerr << "Maximum number of packets reached\n";
return false;
}
};