Skip to content

Commit

Permalink
Pull request: 5035-netip-arp-hosts
Browse files Browse the repository at this point in the history
Updates #5035.

Squashed commit of the following:

commit d1c4493
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Oct 25 14:26:52 2022 +0300

    aghnet: imp hosts rec equal

commit 0a7f40a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Oct 24 18:10:09 2022 +0300

    aghnet: move arp and hosts to netip.Addr
  • Loading branch information
ainar-g committed Oct 25, 2022
1 parent cebbb69 commit 04c8e3b
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 194 deletions.
5 changes: 3 additions & 2 deletions internal/aghnet/arpdb.go
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"net"
"net/netip"
"sync"

"github.com/AdguardTeam/golibs/errors"
Expand Down Expand Up @@ -54,7 +55,7 @@ type Neighbor struct {
Name string

// IP contains either IPv4 or IPv6.
IP net.IP
IP netip.Addr

// MAC contains the hardware address.
MAC net.HardwareAddr
Expand All @@ -64,7 +65,7 @@ type Neighbor struct {
func (n Neighbor) Clone() (clone Neighbor) {
return Neighbor{
Name: n.Name,
IP: slices.Clone(n.IP),
IP: n.IP,
MAC: slices.Clone(n.MAC),
}
}
Expand Down
15 changes: 11 additions & 4 deletions internal/aghnet/arpdb_bsd.go
Expand Up @@ -5,6 +5,7 @@ package aghnet
import (
"bufio"
"net"
"net/netip"
"strings"
"sync"

Expand Down Expand Up @@ -47,22 +48,28 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

if ipStr := fields[1]; len(ipStr) < 2 {
continue
} else if ip := net.ParseIP(ipStr[1 : len(ipStr)-1]); ip == nil {
} else if ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1]); err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)

continue
} else {
n.IP = ip
}

hwStr := fields[3]
if mac, err := net.ParseMAC(hwStr); err != nil {
mac, err := net.ParseMAC(hwStr)
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)

continue
} else {
n.MAC = mac
}

host := fields[0]
if err := netutil.ValidateDomainName(host); err != nil {
log.Debug("parsing arp output: %s", err)
err = netutil.ValidateDomainName(host)
if err != nil {
log.Debug("arpdb: parsing arp output: host: %s", err)
} else {
n.Name = host
}
Expand Down
7 changes: 4 additions & 3 deletions internal/aghnet/arpdb_bsd_test.go
Expand Up @@ -4,6 +4,7 @@ package aghnet

import (
"net"
"net/netip"
)

const arpAOutput = `
Expand All @@ -17,14 +18,14 @@ hostname.two (::ffff:ffff) at ef:cd:ab:ef:cd:ab on em0 expires in 1198 seconds [

var wantNeighs = []Neighbor{{
Name: "hostname.one",
IP: net.IPv4(192, 168, 1, 2),
IP: netip.MustParseAddr("192.168.1.2"),
MAC: net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF},
}, {
Name: "hostname.two",
IP: net.ParseIP("::ffff:ffff"),
IP: netip.MustParseAddr("::ffff:ffff"),
MAC: net.HardwareAddr{0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB},
}, {
Name: "",
IP: net.ParseIP("::1234"),
IP: netip.MustParseAddr("::1234"),
MAC: net.HardwareAddr{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
}}
32 changes: 22 additions & 10 deletions internal/aghnet/arpdb_linux.go
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/fs"
"net"
"net/netip"
"strings"
"sync"

Expand Down Expand Up @@ -94,7 +95,8 @@ func (arp *fsysARPDB) Refresh() (err error) {
}

n := Neighbor{}
if n.IP = net.ParseIP(fields[0]); n.IP == nil || n.IP.IsUnspecified() {
n.IP, err = netip.ParseAddr(fields[0])
if err != nil || n.IP.IsUnspecified() {
continue
} else if n.MAC, err = net.ParseMAC(fields[3]); err != nil {
continue
Expand Down Expand Up @@ -135,15 +137,19 @@ func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

n := Neighbor{}

if ip := net.ParseIP(fields[0]); ip == nil || n.IP.IsUnspecified() {
ip, err := netip.ParseAddr(fields[0])
if err != nil || n.IP.IsUnspecified() {
log.Debug("arpdb: parsing arp output: ip: %s", err)

continue
} else {
n.IP = ip
}

hwStr := fields[3]
if mac, err := net.ParseMAC(hwStr); err != nil {
log.Debug("parsing arp output: %s", err)
mac, err := net.ParseMAC(hwStr)
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)

continue
} else {
Expand Down Expand Up @@ -174,15 +180,17 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

if ipStr := fields[1]; len(ipStr) < 2 {
continue
} else if ip := net.ParseIP(ipStr[1 : len(ipStr)-1]); ip == nil {
} else if ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1]); err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)

continue
} else {
n.IP = ip
}

hwStr := fields[3]
if mac, err := net.ParseMAC(hwStr); err != nil {
log.Debug("parsing arp output: %s", err)
log.Debug("arpdb: parsing arp output: mac: %s", err)

continue
} else {
Expand All @@ -191,7 +199,7 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

host := fields[0]
if verr := netutil.ValidateDomainName(host); verr != nil {
log.Debug("parsing arp output: %s", verr)
log.Debug("arpdb: parsing arp output: host: %s", verr)
} else {
n.Name = host
}
Expand All @@ -218,14 +226,18 @@ func parseIPNeigh(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

n := Neighbor{}

if ip := net.ParseIP(fields[0]); ip == nil {
ip, err := netip.ParseAddr(fields[0])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)

continue
} else {
n.IP = ip
}

if mac, err := net.ParseMAC(fields[4]); err != nil {
log.Debug("parsing arp output: %s", err)
mac, err := net.ParseMAC(fields[4])
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)

continue
} else {
Expand Down
5 changes: 3 additions & 2 deletions internal/aghnet/arpdb_linux_test.go
Expand Up @@ -4,6 +4,7 @@ package aghnet

import (
"net"
"net/netip"
"sync"
"testing"
"testing/fstest"
Expand Down Expand Up @@ -33,10 +34,10 @@ const ipNeighOutput = `
::ffff:ffff dev enp0s3 lladdr ef:cd:ab:ef:cd:ab router STALE`

var wantNeighs = []Neighbor{{
IP: net.IPv4(192, 168, 1, 2),
IP: netip.MustParseAddr("192.168.1.2"),
MAC: net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF},
}, {
IP: net.ParseIP("::ffff:ffff"),
IP: netip.MustParseAddr("::ffff:ffff"),
MAC: net.HardwareAddr{0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB},
}}

Expand Down
11 changes: 8 additions & 3 deletions internal/aghnet/arpdb_openbsd.go
Expand Up @@ -5,6 +5,7 @@ package aghnet
import (
"bufio"
"net"
"net/netip"
"strings"
"sync"

Expand Down Expand Up @@ -50,14 +51,18 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

n := Neighbor{}

if ip := net.ParseIP(fields[0]); ip == nil {
ip, err := netip.ParseAddr(fields[0])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)

continue
} else {
n.IP = ip
}

if mac, err := net.ParseMAC(fields[1]); err != nil {
log.Debug("parsing arp output: %s", err)
mac, err := net.ParseMAC(fields[1])
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)

continue
} else {
Expand Down
5 changes: 3 additions & 2 deletions internal/aghnet/arpdb_openbsd_test.go
Expand Up @@ -4,6 +4,7 @@ package aghnet

import (
"net"
"net/netip"
)

const arpAOutput = `
Expand All @@ -15,9 +16,9 @@ Host Ethernet Address Netif Expire Flags
`

var wantNeighs = []Neighbor{{
IP: net.IPv4(192, 168, 1, 2),
IP: netip.MustParseAddr("192.168.1.2"),
MAC: net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF},
}, {
IP: net.ParseIP("::ffff:ffff"),
IP: netip.MustParseAddr("::ffff:ffff"),
MAC: net.HardwareAddr{0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB},
}}
3 changes: 2 additions & 1 deletion internal/aghnet/arpdb_test.go
Expand Up @@ -2,6 +2,7 @@ package aghnet

import (
"net"
"net/netip"
"sync"
"testing"

Expand Down Expand Up @@ -35,7 +36,7 @@ func (arp *TestARPDB) Neighbors() (ns []Neighbor) {
}

func TestARPDBS(t *testing.T) {
knownIP := net.IP{1, 2, 3, 4}
knownIP := netip.MustParseAddr("1.2.3.4")
knownMAC := net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF}

succRefrCount, failRefrCount := 0, 0
Expand Down
7 changes: 5 additions & 2 deletions internal/aghnet/arpdb_windows.go
Expand Up @@ -5,6 +5,7 @@ package aghnet
import (
"bufio"
"net"
"net/netip"
"strings"
"sync"
)
Expand Down Expand Up @@ -43,13 +44,15 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {

n := Neighbor{}

if ip := net.ParseIP(fields[0]); ip == nil {
ip, err := netip.ParseAddr(fields[0])
if err != nil {
continue
} else {
n.IP = ip
}

if mac, err := net.ParseMAC(fields[1]); err != nil {
mac, err := net.ParseMAC(fields[1])
if err != nil {
continue
} else {
n.MAC = mac
Expand Down
5 changes: 3 additions & 2 deletions internal/aghnet/arpdb_windows_test.go
Expand Up @@ -4,6 +4,7 @@ package aghnet

import (
"net"
"net/netip"
)

const arpAOutput = `
Expand All @@ -14,9 +15,9 @@ Interface: 192.168.1.1 --- 0x7
::ffff:ffff ef-cd-ab-ef-cd-ab static`

var wantNeighs = []Neighbor{{
IP: net.IPv4(192, 168, 1, 2),
IP: netip.MustParseAddr("192.168.1.2"),
MAC: net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF},
}, {
IP: net.ParseIP("::ffff:ffff"),
IP: netip.MustParseAddr("::ffff:ffff"),
MAC: net.HardwareAddr{0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB},
}}

0 comments on commit 04c8e3b

Please sign in to comment.