浏览代码

icmp: prevent stripping off ipv4 header twice on darwin

We cannot use net.ReadFrom on darwin because a) net.ReadFrom and
net.ReadFromIP unconditionally try to strip off ipv4 header due to
and c) icmp.ListenPacket calls ipv4.PacketConn internally.

Instead, we use ipv4.PacketConn.ReadFrom that calls net.ReadMsgIP
internally to prevent icmp.ReadFrom from returning corrupted ICMP
message.

Fixes #9395.

Change-Id: I269f3724de72bf37eb26921c251c2acb1c720fa3
Mikio Hara 11 年之前
父节点
当前提交
2399668107
共有 1 个文件被更改,包括 10 次插入0 次删除
  1. 10 0
      icmp/endpoint.go

+ 10 - 0
icmp/endpoint.go

@@ -6,6 +6,7 @@ package icmp
 
 
 import (
 import (
 	"net"
 	"net"
+	"runtime"
 	"syscall"
 	"syscall"
 	"time"
 	"time"
 
 
@@ -51,6 +52,15 @@ func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
 	if !c.ok() {
 	if !c.ok() {
 		return 0, nil, syscall.EINVAL
 		return 0, nil, syscall.EINVAL
 	}
 	}
+	// Please be informed that ipv4.NewPacketConn enables
+	// IP_STRIPHDR option by default on Darwin.
+	// See golang.org/issue/9395 for futher information.
+	if runtime.GOOS == "darwin" {
+		if p, _ := c.ipc.(*ipv4.PacketConn); p != nil {
+			n, _, peer, err := p.ReadFrom(b)
+			return n, peer, err
+		}
+	}
 	return c.c.ReadFrom(b)
 	return c.c.ReadFrom(b)
 }
 }