Skip to content

Commit

Permalink
Refactoring the variable name Iface to Interface and ifaceIndex
Browse files Browse the repository at this point in the history
… to `ifIndex`(keeping it consistent with `ControlMessage`)

Signed-off-by: ilolicon <97431110@qq.com>
  • Loading branch information
ilolicon committed Apr 12, 2023
1 parent d55df2c commit 60b7449
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func main() {
pinger.Interval = *interval
pinger.Timeout = *timeout
pinger.TTL = *ttl
pinger.Iface = *iface
pinger.Interface = *iface
pinger.SetPrivileged(*privileged)

fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
Expand Down
24 changes: 12 additions & 12 deletions packetconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type packetConn interface {
SetReadDeadline(t time.Time) error
WriteTo(b []byte, dst net.Addr) (int, error)
SetTTL(ttl int)
SetIfaceIndex(ifaceIndex int)
SetIfIndex(ifIndex int)
}

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

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

func (c *icmpConn) SetIfaceIndex(ifaceIndex int) {
c.ifaceIndex = ifaceIndex
func (c *icmpConn) SetIfIndex(ifIndex int) {
c.ifIndex = ifIndex
}

func (c *icmpConn) SetReadDeadline(t time.Time) error {
Expand Down Expand Up @@ -69,12 +69,12 @@ func (c *icmpv4Conn) WriteTo(b []byte, dst net.Addr) (int, error) {
return 0, err
}
var cm *ipv4.ControlMessage
if 1 <= c.ifaceIndex {
// c.ifaceIndex == 0 if not set interface
if 1 <= c.ifIndex {
// c.ifIndex == 0 if not set interface
if err := c.c.IPv4PacketConn().SetControlMessage(ipv4.FlagInterface, true); err != nil {
return 0, err
}
cm = &ipv4.ControlMessage{IfIndex: c.ifaceIndex}
cm = &ipv4.ControlMessage{IfIndex: c.ifIndex}
}

return c.c.IPv4PacketConn().WriteTo(b, cm, dst)
Expand Down Expand Up @@ -110,12 +110,12 @@ func (c *icmpV6Conn) WriteTo(b []byte, dst net.Addr) (int, error) {
return 0, err
}
var cm *ipv6.ControlMessage
if 1 <= c.ifaceIndex {
// c.ifaceIndex == 0 if not set interface
if 1 <= c.ifIndex {
// c.ifIndex == 0 if not set interface
if err := c.c.IPv6PacketConn().SetControlMessage(ipv6.FlagInterface, true); err != nil {
return 0, err
}
cm = &ipv6.ControlMessage{IfIndex: c.ifaceIndex}
cm = &ipv6.ControlMessage{IfIndex: c.ifIndex}
}

return c.c.IPv6PacketConn().WriteTo(b, cm, dst)
Expand Down
10 changes: 5 additions & 5 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ type Pinger struct {
// Source is the source IP address
Source string

// Iface used to send/recv ICMP messages
Iface string
// Interface used to send/recv ICMP messages
Interface string

// Channel and mutex used to communicate when the Pinger should stop between goroutines.
done chan interface{}
Expand Down Expand Up @@ -429,12 +429,12 @@ func (p *Pinger) RunWithContext(ctx context.Context) error {
defer conn.Close()

conn.SetTTL(p.TTL)
if p.Iface != "" {
iface, err := net.InterfaceByName(p.Iface)
if p.Interface != "" {
iface, err := net.InterfaceByName(p.Interface)
if err != nil {
return err
}
conn.SetIfaceIndex(iface.Index)
conn.SetIfIndex(iface.Index)
}
return p.run(ctx, conn)
}
Expand Down
8 changes: 4 additions & 4 deletions ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ func TestStatisticsLossy(t *testing.T) {
}
}

func TestSetIfaceName(t *testing.T) {
func TestSetInterfaceName(t *testing.T) {
pinger := New("localhost")
pinger.Count = 1
pinger.Timeout = time.Second

// Set loopback interface
pinger.Iface = "lo"
pinger.Interface = "lo"
err := pinger.Run()
if runtime.GOOS == "linux" {
AssertNoError(t, err)
Expand All @@ -493,7 +493,7 @@ func TestSetIfaceName(t *testing.T) {
}

// Set fake interface
pinger.Iface = "L()0pB@cK"
pinger.Interface = "L()0pB@cK"
err = pinger.Run()
AssertError(t, err, "device not found")
}
Expand Down Expand Up @@ -664,7 +664,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) SetIfaceIndex(t int) {}
func (c testPacketConn) SetIfIndex(ifIndex 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 60b7449

Please sign in to comment.