连接 0.0.0.0/32 发生了什么

2 minute read

背景

根据RFC 3330, 1700 的描述, 0.0.0.0/32 可以用作当前网络的源地址。

0.0.0.0/8 - Addresses in this block refer to source hosts on "this" network.    
Address 0.0.0.0/32 may be used as a source address for this host on this network;   
other addresses within 0.0.0.0/8 may be used to refer to specified hosts on this network.  
[RFC1700, page 4].  

0.0.0.0/32 作为目标地址使用时,与127.0.0.1含义一样。

但是0.0.0.0还有更多的含义,如下

IP address numbers in Internet Protocol (IP) version 4 (IPv4) range from 0.0.0.0 up to 255.255.255.255. The IP address 0.0.0.0 has several special meanings on computer networks. It cannot be used as a general-purpose device address, however.  
  
IPv6 networks have a similar concept of an all-zeros network address.  
  
0.0.0.0 on Clients  
  
PCs and other client devices normally show an address of 0.0.0.0 when they are not connected to a TCP/IP network. A device may give itself this address by default whenever they are offline. It may also be automatically assigned by DHCP in case of address assignment failures.  When set with this address, a device cannot communicate with any other devices on that network over IP.  
  
0.0.0.0 can also theoretically set as a device's network (subnet) mask rather than its IP address. However, a subnet mask with this value has no practical purpose. Both the IP address and network maskare typically assigned as 0.0.0.0 on a client together.    
  
Software Application and Server Uses of 0.0.0.0  
Some devices, particularly network servers, possess more than one IP network interface. TCP/IP software applications use 0.0.0.0 as a programming technique to monitor network traffic across all of the IP addresses currently assigned to the interfaces on that multi-homed device.  
  
While connected computers do not use this address, messages carried over IP sometimes include 0.0.0.0 inside the protocol header when the source of the message is unknown.  
  
The Use of 0.0.0.0 vs. 127.0.0.1 on Local Networks  
  
Students of computer networks sometimes confuse the usages of 0.0.0.0 and 127.0.0.1 on IP networks. Whereas 0.0.0.0 has several defined uses as described above, 127.0.0.1 has the one very specific purpose of allowing a device to send messages to itself.  
  
Troubleshooting IP Address Problems with 0.0.0.0  
  
If a computer is properly configured for TCP/IP networking yet still shows 0.0.0.0 for an address, try the following to troubleshoot this problem and obtain a valid address:  
  
On networks with dynamic address asssignment support, release and renew the computer's IP address. Failures with DHCP assignment can be intermittent or persistent. If the failures persist, troubleshoot the DHCP server configuration: Common causes of failure include having no available addresses in the DHCP pool.  
For networks that require static IP addressing, configure a valid IP address on the computer.  

0.0.0.0/32 可以用来表示当前网络,与0.0.0.0建立连接,实际上是与回环地址建立连接。

如下

# ping 0.0.0.0  
PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.  
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.018 ms  
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.033 ms  

如果把回环地址shutdown,连接0.0.0.0会不行.

# ifdown lo  
  
$ psql -h 0.0.0.0  
psql: could not connect to server: Connection timed out  
        Is the server running on host "0.0.0.0" and accepting  
        TCP/IP connections on port 1921?  
  
# ping 0.0.0.0  
PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.  
^C  
--- 0.0.0.0 ping statistics ---  
3 packets transmitted, 0 received, 100% packet loss, time 1999ms  

所以连接0.0.0.0匹配的PostgreSQL服务端的pg_hba.conf条目是127.0.0.1/32。而不是0.0.0.0/0

pg_hba.conf  
host all all 127.0.0.1/32  trust  

参考

https://www.rfc-editor.org/rfc/rfc1700.txt

https://www.rfc-editor.org/rfc/rfc3330.txt

http://compnetworking.about.com/od/workingwithipaddresses/g/0_0_0_0_ip-address.htm

Flag Counter

digoal’s 大量PostgreSQL文章入口