Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARP Request in C#

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:

enter image description here

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.

like image 553
albeck Avatar asked Oct 24 '25 08:10

albeck


1 Answers

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.

like image 83
VladL Avatar answered Oct 26 '25 22:10

VladL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!