2

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?

Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
  • There is no portable C interface for this. For Linux, see [here](http://stackoverflow.com/questions/15407354/python-get-mac-address-of-default-gateway) for example. – indiv Oct 29 '14 at 22:55
  • @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 Oct 29 '14 at 23:00
  • 1
    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](http://en.wikipedia.org/wiki/Netlink). – indiv Oct 29 '14 at 23:05
  • @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 Oct 29 '14 at 23:31

2 Answers2

5

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    I think my question is misunderstood. I need to assemble a UDP packet. Just like the BSD socket does. – Peng Zhang Oct 29 '14 at 22:43
  • 1
    Even if you want to do that, you still don't need the MAC address of the remote endpoint. UDP packets do not contain MAC addresses, they contain IPv4 addresses. – Greg Hewgill Oct 29 '14 at 22:44
  • @PengZhang: Sounds like you want the [gateway](http://en.wikipedia.org/wiki/Gateway_address) address. The address could be specific for a route, or if no route is specified, the default gateway. And then you have to deal with multiple network interfaces... – indiv Oct 29 '14 at 22:49
  • @GregHewgill I just clarified my post. I need MAC to fill in the Ethernet header. – Peng Zhang Oct 29 '14 at 22:53
  • @GregHewgill When I call UDP sendto(), who fills the MAC address in the ethernet frame? How does that accomplishes? – Peng Zhang Oct 29 '14 at 23:04
  • 2
    @PengZhang: The OS socket layer fills in the MAC address from the ARP table, if it exists. If not, then the socket layer sends an ARP request to the network to request the MAC address for the IPv4 address of the gateway. Then it sends the Ethernet frame containing the UDP packet using the MAC address of the gateway. – Greg Hewgill Oct 29 '14 at 23:05
  • @GregHewgill Thank you. I am confused by "Then it sends the Ethernet frame containing the UDP packet using the MAC address of the gateway.". My current code fills the MAC address of the other end and sends it successfully. The two endpoints are connected through a router. And in the network course, what I learned is filling the MAC of the destination. If I fill the MAC of the gateway, when the packet gets to the gateway, how the gateway will handle it? – Peng Zhang Oct 29 '14 at 23:10
  • 1
    That is the purpose of the gateway. It uses the IPv4 address to decide what to do next. If the gateway is connected directly to the destination network, then it fills in the MAC address of the destination (using an ARP request if necessary) and then sends the UDP packet to its destination. If the gateway is not directly connected to the destination network, then it sends the UDP packet up to the next gateway, which repeats the procedure. – Greg Hewgill Oct 29 '14 at 23:13
  • @GregHewgill Thank you. Another question, if I directly and correctly fills the destination MAC address and IP, the gateway will just pass it to the next gateway, right? Could you recommend me a book on network. I studied math at school but my current project is developing over ethernet card API. – Peng Zhang Oct 29 '14 at 23:24
  • [TCP/IP Illustrated](http://www.amazon.com/TCP-Illustrated-Vol-Addison-Wesley-Professional/dp/0201633469) is the book from which I learned about the IP protocol stack. It's excellent. – Greg Hewgill Oct 29 '14 at 23:27
0

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.

SmileDeveloper
  • 366
  • 3
  • 10