Browse Source

icmp: don't refer internal packages in examples

Also fixes ping tests. Latest Linux kernels prohibit to access ICMPv6
properties with non-privileged sockets.

Fixes golang/go#12163.

Change-Id: I439b41556e8d4c2f3a9f725131267469e08c9599
Reviewed-on: https://go-review.googlesource.com/13653
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Mikio Hara 10 years ago
parent
commit
ea66ef9485
2 changed files with 12 additions and 3 deletions
  1. 11 2
      icmp/example_test.go
  2. 1 1
      icmp/ping_test.go

+ 11 - 2
icmp/example_test.go

@@ -8,13 +8,22 @@ import (
 	"log"
 	"log"
 	"net"
 	"net"
 	"os"
 	"os"
+	"runtime"
 
 
 	"golang.org/x/net/icmp"
 	"golang.org/x/net/icmp"
-	"golang.org/x/net/internal/iana"
 	"golang.org/x/net/ipv6"
 	"golang.org/x/net/ipv6"
 )
 )
 
 
 func ExamplePacketConn_nonPrivilegedPing() {
 func ExamplePacketConn_nonPrivilegedPing() {
+	switch runtime.GOOS {
+	case "darwin":
+	case "linux":
+		log.Println("you may need to adjust the net.ipv4.ping_group_range kernel state")
+	default:
+		log.Println("not supported on", runtime.GOOS)
+		return
+	}
+
 	c, err := icmp.ListenPacket("udp6", "fe80::1%en0")
 	c, err := icmp.ListenPacket("udp6", "fe80::1%en0")
 	if err != nil {
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
@@ -41,7 +50,7 @@ func ExamplePacketConn_nonPrivilegedPing() {
 	if err != nil {
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 	}
-	rm, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n])
+	rm, err := icmp.ParseMessage(58, rb[:n])
 	if err != nil {
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 	}

+ 1 - 1
icmp/ping_test.go

@@ -115,7 +115,7 @@ func doPing(tt pingTest, seq int) error {
 		return err
 		return err
 	}
 	}
 
 
-	if tt.protocol == iana.ProtocolIPv6ICMP {
+	if tt.network != "udp6" && tt.protocol == iana.ProtocolIPv6ICMP {
 		var f ipv6.ICMPFilter
 		var f ipv6.ICMPFilter
 		f.SetAll(true)
 		f.SetAll(true)
 		f.Accept(ipv6.ICMPTypeDestinationUnreachable)
 		f.Accept(ipv6.ICMPTypeDestinationUnreachable)