| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package probing |
| 15 |
|
| 16 |
import ( |
| 17 |
"net" |
| 18 |
"runtime" |
| 19 |
"time" |
| 20 |
|
| 21 |
"golang.org/x/net/icmp" |
| 22 |
"golang.org/x/net/ipv4" |
| 23 |
"golang.org/x/net/ipv6" |
| 24 |
) |
| 25 |
|
| 26 |
type packetConn interface { |
| 27 |
Close() error |
| 28 |
ICMPRequestType() icmp.Type |
| 29 |
ReadFrom(b []byte) (n int, ttl int, src net.Addr, err error) |
| 30 |
SetFlagTTL() error |
| 31 |
SetReadDeadline(t time.Time) error |
| 32 |
WriteTo(b []byte, dst net.Addr) (int, error) |
| 33 |
SetTTL(ttl int) |
| 34 |
SetMark(m uint) error |
| 35 |
SetDoNotFragment() error |
| 36 |
SetBroadcastFlag() error |
| 37 |
SetIfIndex(ifIndex int) |
| 38 |
SetSource(source net.IP) |
| 39 |
SetTrafficClass(uint8) error |
| 40 |
InstallICMPIDFilter(id int) error |
| 41 |
} |
| 42 |
|
| 43 |
type icmpConn struct { |
| 44 |
c *icmp.PacketConn |
| 45 |
ttl int |
| 46 |
ifIndex int |
| 47 |
source net.IP |
| 48 |
} |
| 49 |
|
| 50 |
func (c *icmpConn) Close() error { |
| 51 |
return c.c.Close() |
| 52 |
} |
| 53 |
|
| 54 |
func (c *icmpConn) SetTTL(ttl int) { |
| 55 |
c.ttl = ttl |
| 56 |
} |
| 57 |
|
| 58 |
func (c *icmpConn) SetIfIndex(ifIndex int) { |
| 59 |
c.ifIndex = ifIndex |
| 60 |
} |
| 61 |
|
| 62 |
func (c *icmpConn) SetSource(source net.IP) { |
| 63 |
c.source = source |
| 64 |
} |
| 65 |
|
| 66 |
func (c *icmpConn) SetReadDeadline(t time.Time) error { |
| 67 |
return c.c.SetReadDeadline(t) |
| 68 |
} |
| 69 |
|
| 70 |
type icmpv4Conn struct { |
| 71 |
icmpConn |
| 72 |
} |
| 73 |
|
| 74 |
func (c *icmpv4Conn) SetFlagTTL() error { |
| 75 |
err := c.c.IPv4PacketConn().SetControlMessage(ipv4.FlagTTL, true) |
| 76 |
if runtime.GOOS == "windows" { |
| 77 |
return nil |
| 78 |
} |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
func (c *icmpv4Conn) SetTrafficClass(tclass uint8) error { |
| 83 |
return c.c.IPv4PacketConn().SetTOS(int(tclass)) |
| 84 |
} |
| 85 |
|
| 86 |
func (c *icmpv4Conn) ReadFrom(b []byte) (int, int, net.Addr, error) { |
| 87 |
ttl := -1 |
| 88 |
n, cm, src, err := c.c.IPv4PacketConn().ReadFrom(b) |
| 89 |
if cm != nil { |
| 90 |
ttl = cm.TTL |
| 91 |
} |
| 92 |
return n, ttl, src, err |
| 93 |
} |
| 94 |
|
| 95 |
func (c *icmpv4Conn) WriteTo(b []byte, dst net.Addr) (int, error) { |
| 96 |
if err := c.c.IPv4PacketConn().SetTTL(c.ttl); err != nil { |
| 97 |
return 0, err |
| 98 |
} |
| 99 |
var cm *ipv4.ControlMessage |
| 100 |
if 1 <= c.ifIndex { |
| 101 |
|
| 102 |
if err := c.c.IPv4PacketConn().SetControlMessage(ipv4.FlagInterface, true); err != nil { |
| 103 |
return 0, err |
| 104 |
} |
| 105 |
cm = &ipv4.ControlMessage{IfIndex: c.ifIndex} |
| 106 |
} |
| 107 |
|
| 108 |
if c.source != nil { |
| 109 |
if cm == nil { |
| 110 |
cm = &ipv4.ControlMessage{} |
| 111 |
} |
| 112 |
cm.Src = c.source |
| 113 |
} |
| 114 |
|
| 115 |
return c.c.IPv4PacketConn().WriteTo(b, cm, dst) |
| 116 |
} |
| 117 |
|
| 118 |
func (c icmpv4Conn) ICMPRequestType() icmp.Type { |
| 119 |
return ipv4.ICMPTypeEcho |
| 120 |
} |
| 121 |
|
| 122 |
type icmpV6Conn struct { |
| 123 |
icmpConn |
| 124 |
} |
| 125 |
|
| 126 |
func (c *icmpV6Conn) SetFlagTTL() error { |
| 127 |
err := c.c.IPv6PacketConn().SetControlMessage(ipv6.FlagHopLimit, true) |
| 128 |
if runtime.GOOS == "windows" { |
| 129 |
return nil |
| 130 |
} |
| 131 |
return err |
| 132 |
} |
| 133 |
|
| 134 |
func (c *icmpV6Conn) SetTrafficClass(tclass uint8) error { |
| 135 |
return c.c.IPv6PacketConn().SetTrafficClass(int(tclass)) |
| 136 |
} |
| 137 |
|
| 138 |
func (c *icmpV6Conn) ReadFrom(b []byte) (int, int, net.Addr, error) { |
| 139 |
ttl := -1 |
| 140 |
n, cm, src, err := c.c.IPv6PacketConn().ReadFrom(b) |
| 141 |
if cm != nil { |
| 142 |
ttl = cm.HopLimit |
| 143 |
} |
| 144 |
return n, ttl, src, err |
| 145 |
} |
| 146 |
|
| 147 |
func (c *icmpV6Conn) WriteTo(b []byte, dst net.Addr) (int, error) { |
| 148 |
if err := c.c.IPv6PacketConn().SetHopLimit(c.ttl); err != nil { |
| 149 |
return 0, err |
| 150 |
} |
| 151 |
var cm *ipv6.ControlMessage |
| 152 |
if 1 <= c.ifIndex { |
| 153 |
|
| 154 |
if err := c.c.IPv6PacketConn().SetControlMessage(ipv6.FlagInterface, true); err != nil { |
| 155 |
return 0, err |
| 156 |
} |
| 157 |
cm = &ipv6.ControlMessage{IfIndex: c.ifIndex} |
| 158 |
} |
| 159 |
|
| 160 |
if c.source != nil { |
| 161 |
if cm == nil { |
| 162 |
cm = &ipv6.ControlMessage{} |
| 163 |
} |
| 164 |
cm.Src = c.source |
| 165 |
} |
| 166 |
|
| 167 |
return c.c.IPv6PacketConn().WriteTo(b, cm, dst) |
| 168 |
} |
| 169 |
|
| 170 |
func (c icmpV6Conn) ICMPRequestType() icmp.Type { |
| 171 |
return ipv6.ICMPTypeEchoRequest |
| 172 |
} |