How to Know an IP Address is Local or not on Linux Server - c++

We are developing a Server software on Linux using C/C++, this software will limit the download rate for those requests which are from the Internet, but for those from local machines (intranet) it won't set any limit.
The problem is how to judge an IP address is local or not, is it possible to do it through c/c++ by reading some network number settings (maybe from router?)?
UPDATE
When I say local ip, I mean it is from within the company. For example, suppose the company has three subnets (this company only has a DSL link to the internet), they are 10.123.1.xxx, 172.16.1.xxx and 192.168.1.xxx, then all ip addresses from these three subnets should be considered as local address.

The private address ranges are:
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
You might also want to filter out link-local addresses (169.254/16)
You could then parse the ip address in your code(to get the addresses you could use avahi or something similiar and save all the addresses to a file and then parse each address individually)and check it matches these addresses. If it does not then limit its connection
Edit
You could also look into using the getifaddrs function that will list local addresses

If you run a traceroute on the IP address of the machine requesting a connection, you should be able to see whether the route takes you through the "gateway outside the company" (typically your ISP). A simple example in my house would be the Time Warner gateway that my internal router connects to. If the route to the client does not go through the ISP (as you mentioned, you have a DSL link; so the IP address of the DSL endpoint should be known), then it's an internal request. This doesn't require you to know the full map of IP addresses inside the company - you can assume your routers have it figured out.
To get this information you can run a system command from inside your program and parse the response.
To start with, run it from the command line (with a known "internal" and "external" IP address), and look at the difference. If you need further help after that, please update your question with the information you gathered.

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 to get my computer external IP address?

Lets say my IP currently is: 123.123.123.123
How can I get that string programmatically?
Note: I don't want to get this IP: 192.168.0.10, I want that IP which others can use to connect into my computer via HTTP or anything.
NOTE: I dont want to open some web-page such as ip4.me to get the address, I want to get it with just C++.
I tried to google but every suggestion was "load a page and read the IP it tells you". Is that really the only way?!
You can fetch it from http://api.externalip.net/ip/ or some similar services, but I'm not sure how reliable these sites are, in means of availability
Any other way would be extremely complicated, as in general, no network equipment has api to tell external IP, and even if it had, you can not tell is there a simple xDSL router in front of you or Cisco ASA nating outbound traffic
My recommendation is to send a packet with the record route option.
If you know your upstream gateway, you should be able to find a ping command that allows you to set record route, and then either stores that data in an array or something you can regex.
Your WAN IP should be either record 0 or record 1, I believe.
What network library are you using?

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.