What source address is in the packet? The local, private IP? or the public
internet IP?
The IP address from which the packet arrived. In other words, for
internet hosts, the packet will contain the external public address, for
internal network hosts, the packet will contain the internal address.
This seems to be the information you're trying to handle yourself, when
in fact the network layer is already handling it for you.
More importantly, for external internet hosts what you really want is
the NATted address, and that's what the packet will contain, since a NAT
firewall will replace the source address in the header.
Ok. I'm talking to multiple peers, some inside and some outside my
router. If I send out my private IP (192.168.0.14) to all peers as a
return address, then only those INSIDE my router will be able to
respond. If I send out my public internet IP to all peers, only those
OUTSIDE my router will be able to respond. I need to send the
appropriate return IP address to each peer determined by their
location relative to my router.
What I'm suggesting is don't send any IP address in your data packet.
Let the IP stack worry about putting the correct address in the
header. Unless you have special needs, this isn't something the
application layer should be worrying about.
I don't think I have to worry about this at all. Since I don't control
the corporate firewall at work (port forwarding & such), I wouldn't be
able to communicate outside the company boundaries anyway. I would
simply send my local IP (10.x.x.x) to all other peers.
Host 1 has address 10.0.0.1/255.255.255.0 (address/subnet)
Host 2 has address 10.0.1.1/255.255.255.0
Now these two hosts are NOT on the same subnet, in fact they must be
routed, and your algorithm will correctly identify that. But in reality
both hosts are probably behind the firewall, and you'd have to use the
internal IP addresses to communicate between them.
IAW, routed != NAT.