Skip to content

Commit

Permalink
Merge pull request #1 from basen/tos-support
Browse files Browse the repository at this point in the history
Add TOS support
  • Loading branch information
kajtzu authored Sep 19, 2022
2 parents b89bb75 + 63b43a3 commit 1c7ace2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packetconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ type packetConn interface {
SetReadDeadline(t time.Time) error
WriteTo(b []byte, dst net.Addr) (int, error)
SetTTL(ttl int)
SetTOS(tos int)
}

type icmpConn struct {
c *icmp.PacketConn
ttl int
tos int
}

func (c *icmpConn) Close() error {
Expand All @@ -33,6 +35,10 @@ func (c *icmpConn) SetTTL(ttl int) {
c.ttl = ttl
}

func (c *icmpConn) SetTOS(tos int) {
c.tos = tos
}

func (c *icmpConn) SetReadDeadline(t time.Time) error {
return c.c.SetReadDeadline(t)
}
Expand All @@ -42,11 +48,17 @@ func (c *icmpConn) WriteTo(b []byte, dst net.Addr) (int, error) {
if err := c.c.IPv6PacketConn().SetHopLimit(c.ttl); err != nil {
return 0, err
}
if err := c.c.IPv6PacketConn().SetTrafficClass(c.tos); err != nil {
return 0, err
}
}
if c.c.IPv4PacketConn() != nil {
if err := c.c.IPv4PacketConn().SetTTL(c.ttl); err != nil {
return 0, err
}
if err := c.c.IPv4PacketConn().SetTOS(c.tos); err != nil {
return 0, err
}
}

return c.c.WriteTo(b, dst)
Expand Down
8 changes: 7 additions & 1 deletion ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
// it calls the OnFinish callback.
//
// For a full ping example, see "cmd/ping/ping.go".
//
package ping

import (
Expand Down Expand Up @@ -107,6 +106,7 @@ func New(addr string) *Pinger {
protocol: "udp",
awaitingSequences: firstSequence,
TTL: 64,
TOS: 0,
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
}
}
Expand Down Expand Up @@ -205,6 +205,8 @@ type Pinger struct {
logger Logger

TTL int

TOS int
}

type packet struct {
Expand Down Expand Up @@ -233,6 +235,9 @@ type Packet struct {
// TTL is the Time To Live on the packet.
Ttl int

// ToS is the Type of Service on the packet.
Tos int

// ID is the ICMP identifier.
ID int
}
Expand Down Expand Up @@ -419,6 +424,7 @@ func (p *Pinger) Run() error {
defer conn.Close()

conn.SetTTL(p.TTL)
conn.SetTOS(p.TOS)
return p.run(conn)
}

Expand Down
1 change: 1 addition & 0 deletions ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ func (c testPacketConn) ICMPRequestType() icmp.Type { return ipv4.ICMPTyp
func (c testPacketConn) SetFlagTTL() error { return nil }
func (c testPacketConn) SetReadDeadline(t time.Time) error { return nil }
func (c testPacketConn) SetTTL(t int) {}
func (c testPacketConn) SetTOS(t int) {}

func (c testPacketConn) ReadFrom(b []byte) (n int, ttl int, src net.Addr, err error) {
return 0, 0, nil, nil
Expand Down

0 comments on commit 1c7ace2

Please sign in to comment.