-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeUtils.cs
27 lines (25 loc) · 889 Bytes
/
NativeUtils.cs
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
using System.Net;
using System.Runtime.InteropServices;
namespace http_client
{
public static class NativeUtils
{
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);
public static byte[] GetMacAddress(IPAddress address)
{
byte[] mac = new byte[6];
uint len = (uint)mac.Length;
byte[] addressBytes = address.GetAddressBytes();
var dest = (uint)addressBytes[3] << 24;
dest += (uint)addressBytes[2] << 16;
dest += (uint)addressBytes[1] << 8;
dest += (uint)addressBytes[0];
if (SendARP(dest, 0, mac, ref len) != 0)
{
throw new Exception("The ARP request failed.");
}
return mac;
}
}
}