views:

65

answers:

2

If I use arp and arping on machines in my local network I get the mac addresses from them. In the same way I can construct and send a ARP request and collect the response to these machines. This is used since I build raw packets completely from scratchy (to allow spoofing of every possible field, including mac addresses if needed). But, when I try arping or arp on external ip's and hosts such as google.com it doesn't get any reply. What should the destination mac address be set to when sending packets to targets outside my local network? I guess the router since that's what passes it on... am I correct? Is there a quick way in ANSI C to collect the mac address of the router in use by the computer? Or at least the IP so I can send a ARP request to it.

Thanx in advance

A: 

It should be set to the gateway (assuming ethernet on that link...).

Brian Knoblauch
Yes, it's a normal ethernet network. I know the gateway address, but if I were to install this application on another computer it would be nice not to have to manually enter the address of the gateway. Is there a quick function in ANSI C to retrieve the address of the default gateway?
inquam
ANSI C has *no* functions dealing with networking. You'll have to use something OS specific. So what OS are you on ?
nos
+2  A: 

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.

Amardeep
The ICMP approach seems like a smart way. Feels like that to should work for both static and dhcp cases to right? Do that when the application starts up and store the gateway ip and arp it for the mac address.
inquam
It will work for both static and DHCP. You won't even have to ARP it because the packet will forwarded from the router and its source MAC address will be what you are looking for.
Amardeep
So very true... That was a very simple solution. Thanx!
inquam
Of course it ain't necessarily so that 1) the router for outgoing packets is the same as that for incoming packets, or 2) the router doesn't change while your program is running, or 3) the same router should be used for all destinations. Ideally you would consult the routing table each time.
caf
@caf: And is there a good and fast way to do that programaticly using ANSI C?
inquam
@inquam: No - as pointed out in another comment, ANSI C has no networking functions at all. You will have to use operating-system specific calls to read the routing table.
caf