C++ WinSock choose local interface for connection - c++

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 ?

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.

Related

C++ / Qt: How can I detect when a hostname or IP address refers to the current system?

I'm writing a macOS C++ application using Qt that acts as both a UDP client and UDP server. The server functionality allows the user to configure which port the UDP packets will be received on, and the client functionality allows specifying both a target host and a port number. The host can be either a hostname or an IP address, including addresses or hostnames that resolve to a multicast or broadcast address.
In order to help prevent circularity, when the server functionality is enabled I need to be able to warn the user when the host and port they've entered for the client would send the packets directly to the app's server. Of course it's easy to check if the port numbers match, but this means I need to know whether the host they've entered refers to the current system.
Examples of hostnames or IP addresses that would typically be problematic:
127.0.0.1
localhost
192.168.1.255 (assuming the system is on a 192.168.1.0/24 subnet)
any of the IP addresses assigned to the current system's network interfaces
the system's local DNS name
any other loopback addresses that may be configured other than 127.0.0.1
How could I go about detecting this?
Since this app is being written using Qt, a solution that exclusively uses Qt's framework would be ideal. But if that's not possible (since Qt doesn't handle every possible use case) a macOS-specific solution will work too.
QNetworkInterface class should provide the most information you may need.
You can obtain a list of IP addresses using QNetworkInterface::allAddresses() static function.
You can also get a list of all interfaces using QNetworkInterface::allInterfaces().
Calling QNetworkInterface::addressEntries() for each QNetworkInterface returned by QNetworkInterface::allInterfaces() will give you more information about address entries for each interface.
auto ifs = QNetworkInterface::allInterfaces();
foreach (auto interface , ifs){
auto addresses = interface.addressEntries();
foreach ( auto addy , addresses){
///play with the addy here.
}
}
You can also test hostnames against those ip addresses which you are going to ban, using QDnsLookup class.

GetIpNetTable alternative for IPv6

I am trying to discover devices connected within a local area network. I have been successful using the GetIpNetTable function. However, this only returns IPv4 devices and I need to detect devices using IPv6 as well (such as mobile phones connected to the network). Is there a way that this can be achieved?
You're looking for the GetIpNetTable2 function, which can return both the IPv4 ARP table and the IPv6 neighbor table or both, depending on which address family you pass to it. This function requires Windows Vista/2008 or later, and there is absolutely nothing you can do on XP or earlier (except upgrade).

get IP address of local machine on c++

I want to retrieve IP address of my computer (same as I get on http://www.whatsmyip.org/)
I have a win32 project.
This is the code that I am using, as I didnt find any tutorial on this, I could get following info, but not the IP address which I saw for my computer on the whatsmyip.org :(
The IP I got on whatsmyip.org starts with 116.x.x.x
Your code gets adapter addresses, which are local. If you want your Internet address, you need to use the Internet, not your local network. You need to replicate the functionality of asking an external site what IP it sees you connecting from. See here for some suggestions for how to do that.
Retrieving http://icanhazip.com will do it. You can use whatever HTTP library you like.
The IP which assigned to your machine is not necessarily the IP that you see outside of your local network (e.g. in whatsmyip.org).
Your machine is not directly connected to the Internet with a valid and static IP. Maybe you are behind a NAT. So you can not determine your valid IP over Internet by listing your local assigned IPs in many situations.
To findout what IP address you have in Internet, you can do two ways. Ask from someone over Internet (for example, using whatsmyip.org). Or, query your local network recursivly (which is not easy task)

Is there any API in C++ to find the loopback IP address of a machine?

I am using a time synchronization method to sync local time with a server time. In some cases both the server and the local machine are the same. In this case, I need to find the loopback IP address of the local machine and check if the server IP address is the same. This is the need so I just need an API to find the loopback IP address.
The loopback address is guaranteed to be 127.x.x.x (any will work, 0.0.1 is standard) on any IPv4 machine and ::1 on any IPv6 machine. There's no need to look this up either - it's always going to work, and on each machine it's going to refer to itself.
That doesn't work. You're assuming that a single machine is represented by a single IP address. That's just not true. An adapter is, and the "loopback" adapter is a virtual one present on every system. But the loopback adapter is unique and distinct from any physical one, and therefore the loopback IP address (127.0.0.1) is distinct from any physical adapter's IP address.
You can get the complete list of local IPv4 addresses with GetAdaptersInfo. Check if the server address appears in that list. Or simply use NTP, as Jan Hudec suggested.
The loop back address in any in the range of addresses from 127.0.0.1 to 127.255.255.255. You can chose any one you wish. Most people usually chose 127.0.0.1. No API is required.

Get the ip addresses of an interface

Using the output of "ipconfig /all" i can get what I need but I want a more reliable technique to get ip of an interface (practically the interface has to be identified by it's name) in case of ipconfig was corrupted or was not available for any reason.
I'm not sure what you mean by "server and client", but if you're looking for an API that tells you the IP address(es) associated with the local machine's network interfaces, check out getifaddrs() (for Linux/MacOSX/POSIX) and GetAdaptersAddresses() (for Windows). If you want a usage example for those calls, have a look at the GetNetworkInterfaceInfos() function in this file.
(If you wanted to get the IP address of a client connected to the local machine, call getpeername() on the socket that is associated with that client's connection)
Since you mentioned ipconfig, I assume you want to do this on windows.
Windows has a set of IP Helper apis designed for retrieval and modify networking settings.
And here is a simple sample: http://msdn.microsoft.com/en-us/library/aa366298(VS.85).aspx