| // Copyright 2014 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package icmp_test |
| |
| import ( |
| "log" |
| "net" |
| "os" |
| |
| "golang.org/x/net/internal/iana" |
| "golang.org/x/net/internal/icmp" |
| "golang.org/x/net/ipv6" |
| ) |
| |
| func ExamplePacketConn_nonPrivilegedPing() { |
| 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(iana.ProtocolIPv6ICMP, 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) |
| } |
| } |