Pull request 2271: AGDNS-2374-slog-arpdb

Squashed commit of the following:

commit 355136e6e2f3e77b483d97fbc01fbef562c319eb
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Aug 27 18:09:51 2024 +0300

    arpdb: imp docs

commit 27383833033216ab938b0896dbc39e5af8d4dde9
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Aug 26 19:11:10 2024 +0300

    all: slog arpdb
This commit is contained in:
Stanislav Chzhen
2024-08-27 20:42:10 +03:00
parent 738958d90a
commit 0b8bf13453
8 changed files with 110 additions and 142 deletions

View File

@@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"fmt"
"log/slog"
"net"
"net/netip"
"slices"
@@ -12,7 +13,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/osutil"
)
@@ -38,8 +39,8 @@ type Interface interface {
}
// New returns the [Interface] properly initialized for the OS.
func New() (arp Interface) {
return newARPDB()
func New(logger *slog.Logger) (arp Interface) {
return newARPDB(logger)
}
// Empty is the [Interface] implementation that does nothing.
@@ -69,6 +70,30 @@ type Neighbor struct {
MAC net.HardwareAddr
}
// newNeighbor returns the new initialized [Neighbor] by parsing string
// representations of IP and MAC addresses.
func newNeighbor(host, ipStr, macStr string) (n *Neighbor, err error) {
defer func() { err = errors.Annotate(err, "getting arp neighbor: %w") }()
ip, err := netip.ParseAddr(ipStr)
if err != nil {
// Don't wrap the error, as it will get annotated.
return nil, err
}
mac, err := net.ParseMAC(macStr)
if err != nil {
// Don't wrap the error, as it will get annotated.
return nil, err
}
return &Neighbor{
Name: host,
IP: ip,
MAC: mac,
}, nil
}
// Clone returns the deep copy of n.
func (n Neighbor) Clone() (clone Neighbor) {
return Neighbor{
@@ -80,10 +105,10 @@ func (n Neighbor) Clone() (clone Neighbor) {
// validatedHostname returns h if it's a valid hostname, or an empty string
// otherwise, logging the validation error.
func validatedHostname(h string) (host string) {
func validatedHostname(logger *slog.Logger, h string) (host string) {
err := netutil.ValidateHostname(h)
if err != nil {
log.Debug("arpdb: parsing arp output: host: %s", err)
logger.Debug("parsing host of arp output", slogutil.KeyError, err)
return ""
}
@@ -132,15 +157,18 @@ func (ns *neighs) reset(with []Neighbor) {
// parseNeighsFunc parses the text from sc as if it'd be an output of some
// ARP-related command. lenHint is a hint for the size of the allocated slice
// of Neighbors.
type parseNeighsFunc func(sc *bufio.Scanner, lenHint int) (ns []Neighbor)
//
// TODO(s.chzhen): Return []*Neighbor instead.
type parseNeighsFunc func(logger *slog.Logger, sc *bufio.Scanner, lenHint int) (ns []Neighbor)
// cmdARPDB is the implementation of the [Interface] that uses command line to
// retrieve data.
type cmdARPDB struct {
parse parseNeighsFunc
ns *neighs
cmd string
args []string
logger *slog.Logger
parse parseNeighsFunc
ns *neighs
cmd string
args []string
}
// type check
@@ -158,7 +186,7 @@ func (arp *cmdARPDB) Refresh() (err error) {
}
sc := bufio.NewScanner(bytes.NewReader(out))
ns := arp.parse(sc, arp.ns.len())
ns := arp.parse(arp.logger, sc, arp.ns.len())
if err = sc.Err(); err != nil {
// TODO(e.burkov): This error seems unreachable. Investigate.
return fmt.Errorf("scanning the output: %w", err)