Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/net/icmp: PacketConn.NonPrivilegedPing example is not correct in final expectation #31204

Closed
labulakalia opened this issue Apr 2, 2019 · 2 comments

Comments

@labulakalia
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.11.5 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

What did you expect to see?

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")
if err != nil {
    log.Fatal(err)
}
defer c.Close()

wm := icmp.Message{
    Type: ipv6.ICMPTypeEchoRequest, Code: 0,
    Body: &icmp.Echo{
        ID: os.Getpid() & 0xffff, Seq: 1,
        Data: []byte("HELLO-R-U-THERE"),
    },
}
wb, err := wm.Marshal(nil)
if err != nil {
    log.Fatal(err)
}
if _, err := c.WriteTo(wb, &net.UDPAddr{IP: net.ParseIP("ff02::1"), Zone: "en0"}); err != nil {
    log.Fatal(err)
}

rb := make([]byte, 1500)
n, peer, err := c.ReadFrom(rb)
if err != nil {
    log.Fatal(err)
}
rm, err := icmp.ParseMessage(58, rb[:n])
if err != nil {
    log.Fatal(err)
}
switch rm.Type {
case ipv6.ICMPTypeEchoReply:
    log.Printf("got reflection from %v", peer)
default:
    log.Printf("got %+v; want echo reply", rm)
}

output

2019/04/02 20:41:11 got &{Type:echo request Code:0 Checksum:10393 Body:0xc00008ea20}; want echo reply

in this case , it match default option in last,

but i think it should match ipv6.ICMPTypeEchoReply, if i change

switch rm.Type {
case ipv6.ICMPTypeEchoReply:
    log.Printf("got reflection from %v", peer)
default:
    log.Printf("got %+v; want echo reply", rm)
}

to

switch rm.Type {
case ipv6.ICMPTypeEchoRequest:
    log.Printf("got reflection from %v", peer)
default:
    log.Printf("got %+v; want echo reply", rm)
}

default is not be matched, i thinks it wrong?

What did you see instead?

@odeke-em odeke-em changed the title https://godoc.org/golang.org/x/net/icmp#example-PacketConn--NonPrivilegedPing example is wrong x/net/icmp: PacketConn.NonPrivilegedPing example is wrong Apr 3, 2019
@gopherbot gopherbot added this to the Unreleased milestone Apr 3, 2019
@odeke-em
Copy link
Member

odeke-em commented Apr 3, 2019

Thank you for the report @labulaka521 and welcome to the Go project!

To me, yes in deed, it doesn't look correct that we expect from our sender a packet of type of ipv6.ICMPTypeEchoReply yet they sent a ipv6.ICMPTypeEchoRequest

Kindly paging @mikioh @nigeltao

@odeke-em odeke-em changed the title x/net/icmp: PacketConn.NonPrivilegedPing example is wrong x/net/icmp: PacketConn.NonPrivilegedPing example is not correct in final expectation Apr 3, 2019
@mikioh
Copy link
Contributor

mikioh commented Apr 3, 2019

I believe that you modified and ran the example on your node, so your modification is the key because fe80::1 is assigned to only loopback interfaces on darwin by default.

Please be informed that IPv6 multicast packet delivery via loopback interface depends on each implementation. Some might send back the original packet and ICMP never touches the packet because "loopback interface is just for the sake of maintenance and handling unicast/anycast packets is enough for the purpose." FWIW, you can control the multicast loopback by using icmp.PacektConn.IPv6PacketConn.

Please try the example w/ non-loopback links. If you have a better example, not a dumb unicast one, because we already have two unicast ICMP examples in ipv4 and ipv6 packages, feel free to send a CL. Thanks.

@mikioh mikioh closed this as completed Apr 3, 2019
@golang golang locked and limited conversation to collaborators Apr 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants