How to Determinate Destination MAC Address

destination MAC to use when sending to remote host (is it my router)?

That's right. The source MAC address in the ethernet header should be the MAC address of the interface you're sending from, and the destination MAC address should be your router's MAC on its interface that is connected to your LAN.

It's important to remember that Link-Layer addresses are used within networks (in this case, both your MAC address and your router's internal interface are on your LAN), while Network Layer (IPv4/IPv6) addressing is used to route your packet from the source host to the destination host (this is ignoring NAT and a whole bunch of other grossness that arises in IPv4).

What will happen when you send your Ethernet frame out of your interface is the following:

  1. Your router will end up with the frame after it travels through 0 or more switches, which know where your router's internal MAC address is from receiving traffic from it in the past. Your router knows the frame is for it because its internal MAC address is the destination MAC.
  2. Your router will strip off the Ethernet header. Then, it will inspect the destination IP address, and determine how to forward that packet based on that address. If we're talking about your home router, it is probably also changing the source IP address (something called Network Address Translation, or NAT), but since it isn't in the scope of your question I'll leave that alone.
  3. Before it forwards the packet to the next router on the path toward the destination IP address, the router needs to put another Link-Layer header back on the IP datagram because it stripped off the one you had put on it. So, it will create a new Link-Layer header using its outgoing interface's MAC address as the source MAC, and the MAC address of the next router's internal MAC address as the destination. In this way, the Link-Layer header is re-written "hop-by-hop".

how to determinate destination MAC address

It looks like what you want is ioctl and SIOCGARP. That should let you query your arp cache.
I'm assuming that the host in question is on your local network or all you're going to get is your router.

You can also read from /proc/net/arp, which seems easier. You'll need to get an arp request returned first but you'll be doing that whether your tool does it or some third-party makes the request.

What should the destination mac address be set to when sending packet outside of your local network?

MAC operations are limited to machines directly connected within your subnet. So you should use the router's MAC address for packets intended for hosts outside your subnet.

There are numerous ways to obtain the router's IP address.

  1. You can parse the configuration files on your local host if the interface is statically configured.

  2. You can see if your compute platform has an API that lets you access the interface configuration information directly. This would work in both static and dhcp cases.

  3. You can write socket code to send an ICMP message to an outside address then parse the incoming responses. They will be from the router. The stack will, in this case, find the router for you.

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

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.

Is there a way to get destination mac addres in ANSI C

What I decided to do was to send my own ARP packets and caching it internaly in my application. If the destination is outside my local network I parse /proc/net/route and extract the gateway for the given interface and send an arp packet to the gateway to get it's macaddress (since that is the destination macaddress of packets destined outside the local network).

IP packet and MAC Destination Address

If the X doesn't know the MAC address of Y it will first send an ARP request to ff:ff:ff:ff:ff:ff (broadcast) requesting the MAC address for the IP address of Y. Y will respond with it's MAC address which X will then use as a destination MAC address to send the frame.

Btw, since the bridge is in between they're actually on the same LAN, not A and B.



Related Topics



Leave a reply



Submit