Reputation: 520
I'm trying to make my Android Device think that I'm the router, using a simple ARP request in C# (Sending the arp from my laptop to my android device with C#). I thought that if i'll use SendArp Method (from Iphlpapi.dll), it'l work:
SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")),
ConvertIPToInt32(IPAddress.Parse("192.168.1.1")), macAddr, ref macAddrLen)
But I can't send the request.*However, if I'll Write '0' instead of ConvertIPToInt32(IPAddress.Parse("192.168.1.1"))
:
SendArp(ConvertIPToInt32(IPAddress.Parse("myAndroidIP")), 0,
macAddr, ref macAddrLen)
It will work:
So if the source ip is '0', it is working, but if the source is the router IP address, its NOT.
I am using this pinvoke method to send the ARP:
[System.Runtime.InteropServices.DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
internal extern static Int32 SendArp(Int32 destIpAddress, Int32 srcIpAddress,
byte[] macAddress, ref Int32 macAddressLength);
And this method to Convert The String IP to Int32:
private static Int32 ConvertIPToInt32(IPAddress pIPAddr)
{
byte[] lByteAddress = pIPAddr.GetAddressBytes();
return BitConverter.ToInt32(lByteAddress, 0);
}
Thank you.
Upvotes: 4
Views: 8922
Reputation: 2798
This is the approach I used which seems to work without a problem.
As other answers have described, the second parameter is the selection of the source IP.
Setting it to 0 just uses any interface on your computer.
//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!
}
Upvotes: 2
Reputation: 13033
I think you misunderstand the meaning of the second parameter.
1) ARP request is sent not the specific IP (e.g. Android device) but it is broadcasted to all computers of the network.
2) Take a look at description of SendARP function, the second parameter is an interface IP, not the destination IP. If I understand it correctly, if you have more than one LAN cards in your computer, you can choose the one, which will send the ARP request
SrcIP [in] The source IPv4 address of the sender, in the form of an IPAddr structure. This parameter is optional and is used to select the interface to send the request on for the ARP entry. The caller may specify zero corresponding to the INADDR_ANY IPv4 address for this parameter.
Upvotes: 2