From 8dffa7b9eae154294da25dcac379be192e9b9940 Mon Sep 17 00:00:00 2001 From: ilolicon <97431110@qq.com> Date: Wed, 12 Apr 2023 16:23:06 +0800 Subject: [PATCH] Refactoring the variable name `Iface` to `Interface` and `ifaceIndex` to `ifIndex`(keeping it consistent with `ControlMessage`) Signed-off-by: ilolicon <97431110@qq.com> --- cmd/ping/ping.go | 2 +- packetconn.go | 24 ++++++++++++------------ ping.go | 10 +++++----- ping_test.go | 8 ++++---- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cmd/ping/ping.go b/cmd/ping/ping.go index aae6b98..8839851 100644 --- a/cmd/ping/ping.go +++ b/cmd/ping/ping.go @@ -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()) diff --git a/packetconn.go b/packetconn.go index 80336a8..dfdbee6 100644 --- a/packetconn.go +++ b/packetconn.go @@ -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 { @@ -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 { @@ -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) @@ -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) diff --git a/ping.go b/ping.go index b2013ca..2ee772a 100644 --- a/ping.go +++ b/ping.go @@ -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{} @@ -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) } diff --git a/ping_test.go b/ping_test.go index d0396e3..5c4d5c7 100644 --- a/ping_test.go +++ b/ping_test.go @@ -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) @@ -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") } @@ -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