Search code examples
c#udpethernet

How is the destination MAC address automatically updated when the destination IP address is geven for an UDP frame in C#


I have tried a program shown below:-

 class Program
{
    static IPEndPoint Mypoint = new IPEndPoint(IPAddress.Parse("10.169.20.30"), 8050);
    static IPEndPoint UrPoint = new IPEndPoint(IPAddress.Parse("10.169.20.15"), 8051);
    static UdpClient TxClient;
    static void Main(string[] args)
    {
        int i = 0;
        byte[] data= new byte[1472];
        TxClient = new UdpClient(Mypoint);
        while (i < 500)
        {
            data[i]++;
            try
            {
                TxClient.Send(data, data.Length, UrPoint);
            }
            catch { }
            Console.WriteLine("Sent frame " + ++i + " times\n");

        }
        Console.ReadKey();
    }
}

In this I am sending a series of frames to system with IP address 10.169.20.15. I am not giving any MAC ID for this system. But when I view the transmission of the frames through wireshark, I find that the destination MAC Id is automatically getting updated to the MAC Id of that system.

Could anyone please let me know how this is happening. Does the System automatically find out the MAC id corresponding to the IP address, or is there some other reason.

The reason I am asking this is because, I need to now communicate to a micro controller in UDP protocol. As I cannot update the destination MAC address in C#, will it be enough if I give just the IP address. Will the MAC Id be automatically resolved?

Hope the question is clear and thanks for your help !!!

Edit:- I tried using arp -s ipaddress mac address in command prompt and then running my program, but still the frame being sent is not being transmitted to the given mac address.

Could anyone please let me know how to resolve this. Thanks for your help


Solution

  • In short, IP doesn't care about MAC addresses, ethernet software/drivers do.

    The IP layer and above (which include TCP/UDP), is totally independent from the lower layers. It doesn't even know that ethernet is below, and it shouldn't (maybe it's not ethernet ?). The software for IP just build its packets and send them to the layer just below, and it doesn't matter what this layer is. This means there is no concept of MAC address on the IP level, and it's a good thing, it allows running IP on network with no MAC address.

    Now, how is this MAC address updated to the correct one as you observed ? This is actually very simple. Each device on the network knows the next one towards the packet destination, and just fills the MAC source and destination accordingly. Each device relays the packet on the ethernet network by filling correct MAC address.