Search code examples
c#udpipethernetosi

Send UDP packet, not using ARP, known IP and MAC


I am trying to send UDP over a network to a microcontroller. The microcontroller has had almost all of the IP stack stripped out to keep it lean, so cannot engage in ARP. The microcontroller is outputting broadcast packets, which I can receive so I know its IP address and MAC address. I can send broadcast UDP packets to it, which it receives fine, but when I try to send packets using its IP address, I can see ARP messages in wireshark, "Who has 192.168.1.205? Tell 192.168.1.18", 205 being the micro, 18 is my PC. The micro does not respond with its MAC address, so I cant send the packet, but I know the MAC address from the packets the micro is sending out anyway.

So my question is can I specify the IP and MAC address using the UdpClient so it does not need to use ARP.. is there anything else I'm not considering?

The broadcast method that works looks like this:

myUdpClient.EnableBroadcast = true;
myUdpClient.Connect(IPAddress.Broadcast, port);
myUdpClient.Send(dataBytes, dataBytes.Length);

The fixed IP method that doesnt currently work looks like this

IPEndPoint ep = new IPEndPoint("192.168.1.205", port);
myUdpClient.Send(dataBytes, dataBytes.Length, ep);

I have looked at using things like

myUdpClient.Client.SendTo(dataBytes, SocketFlags.DontRoute, ep);

but this doesnt work, and is still not making use of the microcontrollers MAC address.. which my application knows and needs to use (I think). I was also hoping some ARP table on the network would have known where the MAC address was seeing as packets with this info are being sent from it..

As a note, I want to keep broadcast to a minimum to reduce the load on other microcontrollers on the network.

Thanks in advance, Gareth


Solution

  • As far as I can see, you have two options, I'm sure someone else has more;

    • Set a static ARP entry in the OS before launching your application. arp -s <ip> <mac> or similar will set it on most OS's and bypass ARP for further requests to the IP.

    • Use Jpcap to bypass the IP stack entirely, building and sending your own raw packets.