1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/net/ipv4"
14 "golang.org/x/net/ipv6"
17 var _ net.PacketConn = &PacketConn{}
19 // A PacketConn represents a packet network endpoint that uses either
21 type PacketConn struct {
27 func (c *PacketConn) ok() bool { return c != nil && c.c != nil }
29 // IPv4PacketConn returns the ipv4.PacketConn of c.
30 // It returns nil when c is not created as the endpoint for ICMPv4.
31 func (c *PacketConn) IPv4PacketConn() *ipv4.PacketConn {
38 // IPv6PacketConn returns the ipv6.PacketConn of c.
39 // It returns nil when c is not created as the endpoint for ICMPv6.
40 func (c *PacketConn) IPv6PacketConn() *ipv6.PacketConn {
47 // ReadFrom reads an ICMP message from the connection.
48 func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
50 return 0, nil, syscall.EINVAL
52 // Please be informed that ipv4.NewPacketConn enables
53 // IP_STRIPHDR option by default on Darwin.
54 // See golang.org/issue/9395 for further information.
55 if runtime.GOOS == "darwin" && c.p4 != nil {
56 n, _, peer, err := c.p4.ReadFrom(b)
59 return c.c.ReadFrom(b)
62 // WriteTo writes the ICMP message b to dst.
63 // Dst must be net.UDPAddr when c is a non-privileged
64 // datagram-oriented ICMP endpoint. Otherwise it must be net.IPAddr.
65 func (c *PacketConn) WriteTo(b []byte, dst net.Addr) (int, error) {
67 return 0, syscall.EINVAL
69 return c.c.WriteTo(b, dst)
72 // Close closes the endpoint.
73 func (c *PacketConn) Close() error {
80 // LocalAddr returns the local network address.
81 func (c *PacketConn) LocalAddr() net.Addr {
85 return c.c.LocalAddr()
88 // SetDeadline sets the read and write deadlines associated with the
90 func (c *PacketConn) SetDeadline(t time.Time) error {
94 return c.c.SetDeadline(t)
97 // SetReadDeadline sets the read deadline associated with the
99 func (c *PacketConn) SetReadDeadline(t time.Time) error {
101 return syscall.EINVAL
103 return c.c.SetReadDeadline(t)
106 // SetWriteDeadline sets the write deadline associated with the
108 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
110 return syscall.EINVAL
112 return c.c.SetWriteDeadline(t)