[英]ARP Request in C#
我試圖讓我的 Android 設備認為我是路由器,在 C# 中使用一個簡單的 ARP 請求(使用 C# 將 arp 從我的筆記本電腦發送到我的 android 設備)。 我想如果我使用 SendArp 方法(來自 Iphlpapi.dll),它會起作用:
SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")),
ConvertIPToInt32(IPAddress.Parse("192.168.1.1")), macAddr, ref macAddrLen)
但我無法發送請求。*但是,如果我寫 '0' 而不是ConvertIPToInt32(IPAddress.Parse("192.168.1.1"))
:
SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")), 0,
macAddr, ref macAddrLen)
它會起作用:
因此,如果源 ip 為“0”,則它正在工作,但如果源是路由器 IP 地址,則它不是。
我正在使用此 pinvoke 方法發送 ARP:
[System.Runtime.InteropServices.DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
internal extern static Int32 SendArp(Int32 destIpAddress, Int32 srcIpAddress,
byte[] macAddress, ref Int32 macAddressLength);
這個方法將字符串 IP 轉換為 Int32:
private static Int32 ConvertIPToInt32(IPAddress pIPAddr)
{
byte[] lByteAddress = pIPAddr.GetAddressBytes();
return BitConverter.ToInt32(lByteAddress, 0);
}
謝謝你。
我想你誤解了第二個參數的含義。
1) ARP 請求不是發送到特定 IP(例如 Android 設備)而是廣播到網絡中的所有計算機。
2)看一下SendARP函數的說明,第二個參數是接口IP,不是目的IP。 如果我理解正確的話,如果您的計算機中有多個 LAN 卡,您可以選擇一張,它將發送 ARP 請求
SrcIP [in] 發送方的源 IPv4 地址,采用 IPAddr 結構的形式。 此參數是可選的,用於選擇發送 ARP 條目請求的接口。 調用者可以為此參數指定與 INADDR_ANY IPv4 地址相對應的零。
這是我使用的方法,似乎沒有問題。
正如其他答案所述,第二個參數是源 IP 的選擇。
將其設置為 0 只會使用您計算機上的任何接口。
//You'll need this pinvoke signature as it is not part of the .Net framework
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP,
byte[] pMacAddr, ref uint PhyAddrLen);
//These vars are needed, if the the request was a success
//the MAC address of the host is returned in macAddr
private byte[] macAddr = new byte[6];
private uint macAddrLen;
//Here you can put the IP that should be checked
private IPAddress Destination = IPAddress.Parse("127.0.0.1");
//Send Request and check if the host is there
if (SendARP((int)Destination.Address, 0, macAddr, ref macAddrLen) == 0)
{
//SUCCESS! Igor it's alive!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.