...
1
2
3
4
5 package icmp
6
7 import (
8 "net"
9 "runtime"
10 "time"
11
12 "golang.org/x/net/ipv4"
13 "golang.org/x/net/ipv6"
14 )
15
16 var _ net.PacketConn = &PacketConn{}
17
18
19
20 type PacketConn struct {
21 c net.PacketConn
22 p4 *ipv4.PacketConn
23 p6 *ipv6.PacketConn
24 }
25
26 func (c *PacketConn) ok() bool { return c != nil && c.c != nil }
27
28
29
30 func (c *PacketConn) IPv4PacketConn() *ipv4.PacketConn {
31 if !c.ok() {
32 return nil
33 }
34 return c.p4
35 }
36
37
38
39 func (c *PacketConn) IPv6PacketConn() *ipv6.PacketConn {
40 if !c.ok() {
41 return nil
42 }
43 return c.p6
44 }
45
46
47 func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
48 if !c.ok() {
49 return 0, nil, errInvalidConn
50 }
51
52
53
54 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && c.p4 != nil {
55 n, _, peer, err := c.p4.ReadFrom(b)
56 return n, peer, err
57 }
58 return c.c.ReadFrom(b)
59 }
60
61
62
63
64
65 func (c *PacketConn) WriteTo(b []byte, dst net.Addr) (int, error) {
66 if !c.ok() {
67 return 0, errInvalidConn
68 }
69 return c.c.WriteTo(b, dst)
70 }
71
72
73 func (c *PacketConn) Close() error {
74 if !c.ok() {
75 return errInvalidConn
76 }
77 return c.c.Close()
78 }
79
80
81 func (c *PacketConn) LocalAddr() net.Addr {
82 if !c.ok() {
83 return nil
84 }
85 return c.c.LocalAddr()
86 }
87
88
89
90 func (c *PacketConn) SetDeadline(t time.Time) error {
91 if !c.ok() {
92 return errInvalidConn
93 }
94 return c.c.SetDeadline(t)
95 }
96
97
98
99 func (c *PacketConn) SetReadDeadline(t time.Time) error {
100 if !c.ok() {
101 return errInvalidConn
102 }
103 return c.c.SetReadDeadline(t)
104 }
105
106
107
108 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
109 if !c.ok() {
110 return errInvalidConn
111 }
112 return c.c.SetWriteDeadline(t)
113 }
114
View as plain text