Browse Source

Fix peer IP address version detection due to net.IP quirks.

`net/IP.To16()` returns non-nil values even for IPv4 addresses despite
what the documentation says.

This can break interoperability with prior versions.
Yawning Angel 3 years ago
parent
commit
bae94f183d
1 changed files with 6 additions and 4 deletions
  1. 6 4
      common.go

+ 6 - 4
common.go

@@ -314,12 +314,14 @@ func (c *commonConn) initConn(conn net.Conn) error {
 	// resemble what goes out on the wire depending on what the kernel
 	// does and the state of the TCP/IP stack.
 	if taddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
-		if taddr.IP.To16() != nil {
-			// Connected to an IPv6 peer.
-			c.maxRecordSize = tentp.MaxIdealIPv6Size
-		} else {
+		// For some reason, IP.To16() always returns something, regardless
+		// of if it's correct or not, so check IPv4 first.
+		if taddr.IP.To4() != nil {
 			// Connected to an IPv4 peer.
 			c.maxRecordSize = tentp.MaxIdealIPv4Size
+		} else {
+			// Connected to an IPv6 peer.
+			c.maxRecordSize = tentp.MaxIdealIPv6Size
 		}
 	} else {
 		// No idea what kind of connection this is, use the IPv4 max frame