PeppeDx
PeppeDx

Reputation: 167

C++ WinSock choose local interface for connection

I have a system with multiple NIC and so multiple ip addresses and I've to use an SDK whose initialization that needs my local address and the remote address.

I want to autoselect the local endpoint. At the moment I'm enumerating all local addresses (via GetAdaptersAddresses) checking for the best match ( to be correct I should use the subnet mask).

But given that this job is done by the routing table is there any Windows API that given a remote address gives me back the right local endpoint ?

Upvotes: 0

Views: 1243

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595971

is there any Windows API that given a remote address gives me back the right local endpoint ?

Have a look at GetBestInterface() and GetBestInterfaceEx().

Both functions return an index to an interface. You can then retrieve the interface's IP by enumerating network interfaces with GetIpAddrTable(), or enumerating network adapters with GetAdaptersInfo()/GetAdaptersAddresses(), until you find an entry with a matching interface index.

Upvotes: 3

Related Questions