mirror of
https://github.com/go-ping/ping.git
synced 2025-09-20 09:50:22 +00:00
Add ability to set TTL (#186)
This commit is contained in:
@@ -17,21 +17,38 @@ type packetConn interface {
|
||||
SetFlagTTL() error
|
||||
SetReadDeadline(t time.Time) error
|
||||
WriteTo(b []byte, dst net.Addr) (int, error)
|
||||
SetTTL(ttl int)
|
||||
}
|
||||
|
||||
type icmpConn struct {
|
||||
c *icmp.PacketConn
|
||||
c *icmp.PacketConn
|
||||
ttl int
|
||||
}
|
||||
|
||||
func (c *icmpConn) Close() error {
|
||||
return c.c.Close()
|
||||
}
|
||||
|
||||
func (c *icmpConn) SetTTL(ttl int) {
|
||||
c.ttl = ttl
|
||||
}
|
||||
|
||||
func (c *icmpConn) SetReadDeadline(t time.Time) error {
|
||||
return c.c.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (c *icmpConn) WriteTo(b []byte, dst net.Addr) (int, error) {
|
||||
if c.c.IPv6PacketConn() != nil {
|
||||
if err := c.c.IPv6PacketConn().SetHopLimit(c.ttl); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if c.c.IPv4PacketConn() != nil {
|
||||
if err := c.c.IPv4PacketConn().SetTTL(c.ttl); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return c.c.WriteTo(b, dst)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user