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

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.

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.

IP address and MAC address used in DPDK

Hello Stackoverflow experts,
I have been struggling with how to use the ip fragmentation provided by DPDK. and was wondering I have the correct concept of IP address and the MAC address used in rte-mbuf ethernet header.
Is Ip address alone in the header of rte-mbuf can be used to transfer from local to remote?
I see in the DPDK sample applications that the ip address is used in the hashed tables such as IP fragment table after the packets are received, but the fact that data is actually received just by using ethernet mac address, gives me the impression that IP address is only defined by the DPDK user (developers using DPDK API) and not used in actual data transfer.
Is there something missing to what I understand?
You are right. Most DPDK examples work on the second level of the OSI model, i.e. they only care about MAC addresses, not IP.
The IP reassembly example is based on L2 forwarding example, i.e. it acts as an Ethernet bridge. Though, it requires IP addresses to be analyzed, i.e. source and destination IPs must match for all the fragments of the same flow.
Now answering your questions:
Is Ip address alone in the header of rte-mbuf can be used to transfer from local to remote?
If you mean transfer using rte_eth_tx_burst() then no, IP header is not enough. The ethernet header must be filled properly as well.
IP address is only defined by the DPDK user (developers using DPDK API) and not used in actual data transfer.
Since the reassembly example is based on L2 forwarding example, it acts as reassembling Ethernet bridge. So you have a right impression, this example does not route packets based on IP addresses. It just uses IP addresses to reassemble IP fragments.

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)

How could I convert from IP address to MAC address

I want to store all the MAC the mac address which has access to my server.
All I know are only the IP addresses. All the machines are under unique gateway.
Could I got the MAC address from their IP address?
MAC addresses are not part of any protocol that gets routed, you will never get the MAC address of a machine that is on the other side of a router or switch.
They are are the addresses of the physical ports, not of machines (which are what you're taling to using IP).
If server and client are on the same network, you will have to use ARP. This protocol is designed to get the MAC address to a given IP address.
As soon as there is something like a router between client and server, ARP will only reveal the MAC address of the router since this is the target for your ethernet packets.
Simple answer is, no You Cannot get MAC Address from an IP Address for clients connecting to your server unless both the machines are on same Physical network
It is not possible to get MAC address from IP address. MAC address is a physical address of network device, and IP address is not.
IP address is not bound to one device in general.
I suggest you go through this
the post says
An IP address is usually assigned by the network administrator or internet
service provider, you are either provided a static one at the beginning or
given a dynamic one every time you connect to the network.
This is not true with MAC addresses as it is already embedded on the device
or the network card during manufacturing. It is supposed to be permanent and could
not be changed by anyone as it was meant to identify a specific network interface
card no matter where it is in the world.
There are however ways to change mac addess, but i don't think it is possible to get mac address using IP- Address

List of all hosts on LAN network

How can I get all the IP addresses and associated host names in a LAN?
To get the list of interfaces and IP addresses, use getifaddrs().
Search for interfaces with ifa_addr->sa_family == AF_INET
The IP address is in sin_addr.s_addr.
You can then use gethostbyaddr() to look up the DNS name for that IP address.
Update:
It was pointed out to me that the OP was probably asking about discovering other hosts, rather than the addresses of interfaces on the local machine.
There is no reliable way to discover other machines on the local area network, but there are a couple of tricks.
Ping method: Use the ping utility (or a programatic equivalent) to ping the local broadcast address, then see who responds. The broadcast address can be found by listing the interfaces as shown above. I believe ICMP does not require root access under OSX. Note that many systems may have ICMP ping disabled or firewalled, so you will only get responses from the non-stealth ones.
ARP method: Check the system ARP cache to see what IP addresses have been recently active. This will only show systems which have broadcast packets on the same network segment in recent minutes.
Both methods can be blocked by firewalls, routers, and even switches, so the exact borders of the "LAN" can be pretty narrow. Both methods can be implemented programmatically, but it might be simpler and more portable to just call out to the command line ping or arp commands.