Skip to content

Commit

Permalink
all: imp code; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Apr 22, 2024
1 parent 155b2fe commit 5025717
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
17 changes: 17 additions & 0 deletions internal/client/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ func (ci *Index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
return nil, false
}

// FindByIPWithoutZone finds a persistent client by IP address without zone. It
// strips the IPv6 zone index from the stored IP addresses before comparing,
// because querylog entries don't have it. See TODO on [querylog.logEntry.IP].
func (ci *Index) FindByIPWithoutZone(ip netip.Addr) (c *Persistent) {
if (ip == netip.Addr{}) {
return nil
}

for addr, uid := range ci.ipToUID {
if addr.WithZone("") == ip {
return ci.uidToClient[uid]
}
}

return nil
}

// find finds persistent client by MAC.
func (ci *Index) findByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
k := macToKey(mac)
Expand Down
56 changes: 56 additions & 0 deletions internal/client/index_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,59 @@ func TestMACToKey(t *testing.T) {
_ = macToKey(mac)
})
}

func TestIndex_FindByIPWithoutZone(t *testing.T) {
var (
ip = netip.MustParseAddr("fe80::a098:7654:32ef:ff1")
ipWithZone = netip.MustParseAddr("fe80::1ff:fe23:4567:890a%eth2")
)

var (
Client = &Persistent{
Name: "client",
IPs: []netip.Addr{ip},
}

ClientWithZone = &Persistent{
Name: "client_with_zone",
IPs: []netip.Addr{ipWithZone},
}
)

ci := newIDIndex([]*Persistent{
Client,
ClientWithZone,
})

testCases := []struct {
ip netip.Addr
want *Persistent
name string
}{{
name: "without_zone",
ip: ip,
want: Client,
}, {
name: "with_zone",
ip: ipWithZone,
want: ClientWithZone,
}, {
name: "zero_address",
ip: netip.Addr{},
want: nil,
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := ci.FindByIPWithoutZone(tc.ip.WithZone(""))

if tc.want == nil {
require.Nil(t, c)

return
}

require.Equal(t, tc.want, c)
})
}
}
6 changes: 5 additions & 1 deletion internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ func (clients *clientsContainer) clientOrArtificial(
}()

cli, ok := clients.find(id)
if ok {
if !ok {
cli = clients.clientIndex.FindByIPWithoutZone(ip)
}

if cli != nil {
return &querylog.Client{
Name: cli.Name,
IgnoreQueryLog: cli.IgnoreQueryLog,
Expand Down
1 change: 1 addition & 0 deletions internal/querylog/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type logEntry struct {
Answer []byte `json:",omitempty"`
OrigAnswer []byte `json:",omitempty"`

// TODO(s.chzhen): Use netip.Addr.
IP net.IP `json:"IP"`

Result filtering.Result
Expand Down

0 comments on commit 5025717

Please sign in to comment.