2
votes

I need to provide a function that assembles a UDP packet to send when my user provides local and remote IP:Port pairs. I have an Ethernet card API which sends a Ethernet frame for me, do I have to fill a complete Ethernet frame which contains MAC addresses, IP and UDP.

Question: Given an IPV4 address on the Internet, how to find out the MAC address the IP corresponds to so that I can fill it in the Ethernet frame header. How can I use C to lookup this from the router's ARP table. Should I send out an ARP packet to my router (I do not know how to do this either).

When I call UDP sendto(), who fills the MAC address in the ethernet frame? How does that accomplishes?

2
There is no portable C interface for this. For Linux, see here for example.indiv
@indiv When I call UDP sendto(), who fills the MAC address in the ethernet frame? How does that accomplishes? Thank you a lot.Peng Zhang
The operating system's network stack fills it in. If you build the packets yourself (via a raw socket), then you have to emulate that part of the network stack. As my link above shows, Linux exposes the internals of its network stack's ARP table via the proc file system. Another API to the network stack on Linux is called Netlink.indiv
@indiv Thank you a lot. Your comment and the answer of this post helps me realize I was probably doing things in the wrong way. I will talk to senior dev and we may just fill the destination MAC by hand. Considering our use case, this is doable.Peng Zhang

2 Answers

5
votes

The short answer is: You can't.

The MAC address only has meaning on the local network. There is no way to ask a remote IPv4 endpoint (not on your network), what its MAC address is. If you want to send a UDP packet to a remote endpoint, use your OS socket interface to send to that IPv4 address. It will take care of the details.

Based on your comments below, if you have an interface to send Ethernet frames, then you will need to look up the MAC address of your local IPv4 gateway address (by sending an ARP request). The MAC address of the gateway is what you put in the Ethernet frame. The MAC address of the destination endpoint does not go in the Ethernet frame.

0
votes

Your operating system does that. How?: Your operating system fills the macaddress for the host that you send something to or the router that is between the host.

What is the macadress when you send something in your local network

That is the host's computer macadress. How to get the macaddress from only a ipaddress?

You broadcast the ipadress to the network for macadress. Send an ARP packet to macadress FF:FF:FF:FF:FF:FF with your ipadress and wait for response. Everyone on your network will see your packet. Everybody ignores the packet except the computer with your target ip. He will response with the macaddress. Take a look at wiresharp how an ARP packet is constructed. For speed, it is important that you cache the macaddress with the ipaddress for later use.

What is the MAC address when you send/recive something outside your network

You can't get macaddresses from computers outside your network. And there is no reason to do that. If you send a packet outside your local network the MAC adress is the address of your NAT router and the ipadress outside your network. How to get the MAC address for your NAT router you just send an ARP packet with the router's IP address. In the response contains the NAT mac address. If you have the macaddress for the router you can send a packet outside your network. The NAT router will then send your packet out over the WAN side of the router.