Reputation: 2682
Let's say my computer A is connected to multiple networks. On one of these networks is another host, B.
Is it possible, through C# from host A, to determine how host B would reach host A? The solution must handle both IPv4 and IPv6. It doesn't need to handle NAT.
One could say I want to figure out which one of host A's network interfaces is connected to host B.
Upvotes: 0
Views: 422
Reputation: 1494
your catch is How to detemine which network interface (ip address) will be used to send a packet to a specific ip address?
you can find pinvoke for GetBestAddress . i am unable to find some pinvoke example for GetBestAddressEx that supports IPv6
Upvotes: 1
Reputation: 3356
You might trying looking at http://msdn.microsoft.com/en-us/library/system.net.networkinformation.tcpconnectioninformation.aspx as well.
Upvotes: 1
Reputation: 81
It is never possible to tell how another host will reach you because it is up to that host's route table.
However, usually routes are symmetric which means a remote host will reach you the same way you will reach that remote host. You can examine the route table. On Windows that command would be "route -n". This is probably exposed via some API too.
If host A and B are already connected, that connection will show up in netstat.
If you control the code that has the connected socket then you just need to get the peer. Sorry I don't have the C# code to do that.
Upvotes: 1