mirror of
https://github.com/go-ping/ping.git
synced 2025-04-28 10:43:30 +00:00
Add ability to set TTL (#186)
This commit is contained in:
parent
3818264768
commit
6e2b003bff
@ -41,6 +41,7 @@ func main() {
|
|||||||
interval := flag.Duration("i", time.Second, "")
|
interval := flag.Duration("i", time.Second, "")
|
||||||
count := flag.Int("c", -1, "")
|
count := flag.Int("c", -1, "")
|
||||||
size := flag.Int("s", 24, "")
|
size := flag.Int("s", 24, "")
|
||||||
|
ttl := flag.Int("l", 64, "TTL")
|
||||||
privileged := flag.Bool("privileged", false, "")
|
privileged := flag.Bool("privileged", false, "")
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Print(usage)
|
fmt.Print(usage)
|
||||||
@ -88,6 +89,7 @@ func main() {
|
|||||||
pinger.Size = *size
|
pinger.Size = *size
|
||||||
pinger.Interval = *interval
|
pinger.Interval = *interval
|
||||||
pinger.Timeout = *timeout
|
pinger.Timeout = *timeout
|
||||||
|
pinger.TTL = *ttl
|
||||||
pinger.SetPrivileged(*privileged)
|
pinger.SetPrivileged(*privileged)
|
||||||
|
|
||||||
fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
|
fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
|
||||||
|
@ -17,21 +17,38 @@ type packetConn interface {
|
|||||||
SetFlagTTL() error
|
SetFlagTTL() error
|
||||||
SetReadDeadline(t time.Time) error
|
SetReadDeadline(t time.Time) error
|
||||||
WriteTo(b []byte, dst net.Addr) (int, error)
|
WriteTo(b []byte, dst net.Addr) (int, error)
|
||||||
|
SetTTL(ttl int)
|
||||||
}
|
}
|
||||||
|
|
||||||
type icmpConn struct {
|
type icmpConn struct {
|
||||||
c *icmp.PacketConn
|
c *icmp.PacketConn
|
||||||
|
ttl int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *icmpConn) Close() error {
|
func (c *icmpConn) Close() error {
|
||||||
return c.c.Close()
|
return c.c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *icmpConn) SetTTL(ttl int) {
|
||||||
|
c.ttl = ttl
|
||||||
|
}
|
||||||
|
|
||||||
func (c *icmpConn) SetReadDeadline(t time.Time) error {
|
func (c *icmpConn) SetReadDeadline(t time.Time) error {
|
||||||
return c.c.SetReadDeadline(t)
|
return c.c.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *icmpConn) WriteTo(b []byte, dst net.Addr) (int, error) {
|
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)
|
return c.c.WriteTo(b, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
ping.go
4
ping.go
@ -106,6 +106,7 @@ func New(addr string) *Pinger {
|
|||||||
network: "ip",
|
network: "ip",
|
||||||
protocol: "udp",
|
protocol: "udp",
|
||||||
awaitingSequences: firstSequence,
|
awaitingSequences: firstSequence,
|
||||||
|
TTL: 64,
|
||||||
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
|
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -202,6 +203,8 @@ type Pinger struct {
|
|||||||
protocol string
|
protocol string
|
||||||
|
|
||||||
logger Logger
|
logger Logger
|
||||||
|
|
||||||
|
TTL int
|
||||||
}
|
}
|
||||||
|
|
||||||
type packet struct {
|
type packet struct {
|
||||||
@ -402,6 +405,7 @@ func (p *Pinger) Run() error {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
conn.SetTTL(p.TTL)
|
||||||
return p.run(conn)
|
return p.run(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -641,6 +641,7 @@ func (c testPacketConn) Close() error { return nil }
|
|||||||
func (c testPacketConn) ICMPRequestType() icmp.Type { return ipv4.ICMPTypeEcho }
|
func (c testPacketConn) ICMPRequestType() icmp.Type { return ipv4.ICMPTypeEcho }
|
||||||
func (c testPacketConn) SetFlagTTL() error { return nil }
|
func (c testPacketConn) SetFlagTTL() error { return nil }
|
||||||
func (c testPacketConn) SetReadDeadline(t time.Time) error { return nil }
|
func (c testPacketConn) SetReadDeadline(t time.Time) error { return nil }
|
||||||
|
func (c testPacketConn) SetTTL(t int) {}
|
||||||
|
|
||||||
func (c testPacketConn) ReadFrom(b []byte) (n int, ttl int, src net.Addr, err error) {
|
func (c testPacketConn) ReadFrom(b []byte) (n int, ttl int, src net.Addr, err error) {
|
||||||
return 0, 0, nil, nil
|
return 0, 0, nil, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user