
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?
Antwort1
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
(netmask255.255.255.255
) to interfaceLoopback
(Your own addresses are always routed through the loopback interface, they never go to the network.)
10.2.0.0/16
(netmask255.255.0.0
) to interfaceLocal Area Connection
(Addresses in your own subnet are, by definition, local.)
0.0.0.0/0
(netmask0.0.0.0
) to gateway10.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) andip -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.
Antwort2
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
Auch diese IP-Adressen sind Loopbacks:
127.0.0.0/8
Andere sind öffentlich.
Die meisten Tools müssen keine öffentlichen oder privaten IP-Adressen kennen.