Wallace
Wallace

Reputation: 651

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

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.

Upvotes: 1

Views: 1345

Answers (2)

Floris
Floris

Reputation: 46365

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.

Upvotes: 0

const_ref
const_ref

Reputation: 4096

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

Upvotes: 1

Related Questions