پیدا کردن MAC Address از روی IP
نویسنده: وحید نصیری
تاریخ: ۱۳۸۸/۰۴/۱۴ ۲۲:۲۵:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[System.Runtime.InteropServices.DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
internal extern static Int32 SendArp(Int32 destIpAddress, Int32 srcIpAddress, byte[] macAddress, ref Int32 macAddressLength);
public static System.Net.NetworkInformation.PhysicalAddress GetMacFromIP(System.Net.IPAddress IP)
{
if (IP.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
throw new ArgumentException("supports just IPv4 addresses");
Int32 addrInt = IpToInt(IP);
Int32 srcAddrInt = 0;
byte[] mac = new byte[6]; // 48 bit
int length = mac.Length;
int reply = SendArp(addrInt, srcAddrInt, mac, ref length);
byte[] emptyMac = new byte[12];
if (reply != 0)
{
//No MAC Address found for the IP Address
return new System.Net.NetworkInformation.PhysicalAddress(emptyMac);
}
return new System.Net.NetworkInformation.PhysicalAddress(mac);
}
private static Int32 IpToInt(System.Net.IPAddress IP)
{
byte[] bytes = IP.GetAddressBytes();
return BitConverter.ToInt32(bytes, 0);
}
khob bod mamnon az shoma