-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARP.cpp
More file actions
51 lines (39 loc) · 964 Bytes
/
ARP.cpp
File metadata and controls
51 lines (39 loc) · 964 Bytes
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
#include <iostream>
#include <WinSock2.h>
#include <iphlpapi.h>
#include <WS2tcpip.h>
#include <Windows.h>
#pragma comment(lib, "WS2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
using namespace std;
int getMacByArp(char* srcIp, char* destIp) {
DWORD arp;
IPAddr dest = 0;
IPAddr src = 0;
ULONG mac[2];
ULONG maclen = 6;
const char* strSrcIp = srcIp;
const char* strDestIp = destIp;
inet_pton(AF_INET, strSrcIp, &src);
inet_pton(AF_INET, strDestIp, &dest);
memset(&mac, 0xff, sizeof(mac));
arp = SendARP(dest, src, &mac, &maclen);
BYTE* bMac = (BYTE*)&mac;
//printf("%-20s", strDestIp);
for (int i = 0; i < (int)maclen; i++) {
if (i == (maclen - 1)) {
printf("%.2x\n", (int)bMac[i]);
}
else {
printf("%.2x-", (int)bMac[i]);
}
}
return 0;
}
int main(int argc, char *argv[]) {
int ret;
if (argv[1] != NULL or argv[1] != "" and argv[2] != NULL or argv[2] != "") {
ret = getMacByArp(argv[1], argv[2]);
}
return ret;
}