Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How does tools like ping, or any other tool that uses the tcp/ip protocol know that for example 192.168.1.1 or 10.0.0.1 is a local ip address while 8.8.8.8 or 74.142.23.95 are public? are 192.168.x.x and 10.0.x.x hardcoded to be preserved for local use?

share|improve this question
5  
They do not know it and they do not care. Do you have a specific problem ? –  BatchyX Aug 10 '13 at 14:28
    
@BatchyX I am just trying to understand how things work. And some program somewhere have to care if the adress is a part of the LAN or WAN. –  totokaka Aug 10 '13 at 15:09
    
Why should they care ? –  BatchyX Aug 10 '13 at 18:57

2 Answers 2

up vote 8 down vote accepted

Well, they are reserved by RFC 1918 for use in private networks.

But that doesn't actually matter much. You can obtain a block of "public" IP addresses from RIPE or whatever, and use it for your private network, and everything will still work. The reservation is needed only for political reasons, to allow admins to set up their own private networks without any trouble.

Tools like ping do not care whether an address is "private" or "local" or "public". They simply send a packet to the given address, and your OS looks at the routing table to decide where to send it next.

For example, when you configure an Ethernet card on Windows with IP address 10.2.3.4/16 (in netmask format: 255.255.0.0) and gateway 10.2.0.1, it adds the following entries to the routing table:

  • 10.2.3.4/32 (netmask 255.255.255.255) to interface Loopback

    (Your own addresses are always routed through the loopback interface, they never go to the network.)

  • 10.2.0.0/16 (netmask 255.255.0.0) to interface Local Area Connection

    (Addresses in your own subnet are, by definition, local.)

  • 0.0.0.0/0 (netmask 0.0.0.0) to gateway 10.2.0.1

    (Everything else is not local.)

In other words, you told the OS that all addresses within the 10.2.0.0/16 range are local, and the OS takes care of everything.


To view the routing table:

  • on Linux, ip route (IPv4) and ip -6 route (IPv6)
  • on Windows, route print (IPv4 on ≤XP, both v4/v6 on ≥Vista)
  • on Windows XP, netsh interface ipv6 show route (IPv6)
  • on Windows, Linux, BSD, and other Unix-likes, netstat -r -n (IPv4)
  • on Linux and some Unix-likes, netstat -r -n -6 (IPv6)

Editing the routing table can be done with the same tools. For example, to mark all of 172.16.0.0/16 as local, you can use ip route add 172.16.0.0/16 dev eth0 on Linux.

share|improve this answer
1  
Aha, this explains much. I'll accept the answer tomorrow if no better answer has come. Thank you! –  totokaka Aug 10 '13 at 15:07
    
It might be worth mentioning that you can also manually edit the routing table. It isn't often needed, but can be very useful sometimes. –  Michael Kjörling Aug 10 '13 at 15:48
    
What can you get out of changing the routing table? –  totokaka Aug 10 '13 at 16:04
    
@totokaka: It's really not as useful on hosts as it is on routers (or on PCs working as routers), but it still has some use – for example, if you have multiple Internet connections, or are connected to a VPN, you could tell the OS that you only want to access a few corporate servers over the VPN but not everything else – instead of all-or-nothing. –  grawity Aug 10 '13 at 16:25

Public and private IP addresses are defined in RFC. These are private IP addresses:

10.0.0.0/8

172.16.0.0/12

192.168.0.0/16

Also these IP addresses are loopback:

127.0.0.0/8

Others are public.

Most of tools does not need to know public or private IP addresses.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.