Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Dec 8, 2023
1 parent 3aa51d1 commit 9aefb14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
32 changes: 18 additions & 14 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Source uint8

// Clients information sources. The order determines the priority.
const (
SourceWHOIS Source = iota
SourceWHOIS Source = iota + 1
SourceARP
SourceRDNS
SourceDHCP
Expand Down Expand Up @@ -60,19 +60,23 @@ type Runtime struct {
whois *whois.Info

// arp is the ARP information of a client. nil indicates that there is no
// information from the source.
// information from the source. Empty non-nil slice indicates that the data
// from the source is present, but empty.
arp []string

// rdns is the RDNS information of a client. nil indicates that there is no
// information from the source.
// information from the source. Empty non-nil slice indicates that the data
// from the source is present, but empty.
rdns []string

// dhcp is the DHCP information of a client. nil indicates that there is no
// information from the source.
// information from the source. Empty non-nil slice indicates that the data
// from the source is present, but empty.
dhcp []string

// hostsFile is the information from the hosts file. nil indicates that
// there is no information from the source.
// there is no information from the source. Empty non-nil slice indicates
// that the data from the source is present, but empty.
hostsFile []string
}

Expand All @@ -91,25 +95,25 @@ func (r *Runtime) Info() (cs Source, host string) {
cs, info = SourceARP, r.arp
}

if len(info) == 0 {
return cs, ""
}

// TODO(s.chzhen): Return the full information.
return cs, info[0]
}

// SetInfo sets a host as a client information from the cs.
//
// TODO(s.chzhen): Improve API. Use []string.
func (r *Runtime) SetInfo(cs Source, host string) {
info := []string{host}

func (r *Runtime) SetInfo(cs Source, hosts []string) {
switch cs {
case SourceARP:
r.arp = info
r.arp = hosts
case SourceRDNS:
r.rdns = info
r.rdns = hosts
case SourceDHCP:
r.dhcp = info
r.dhcp = hosts
case SourceHostsFile:
r.hostsFile = info
r.hostsFile = hosts
}
}

Expand Down
9 changes: 3 additions & 6 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ type clientsContainer struct {
list map[string]*Client // name -> client
idIndex map[string]*Client // ID -> client

// ipToRC is a map that contains the runtime clients according to their IP
// addresses.
// ipToRC maps IP addresses to runtime client information.
ipToRC map[netip.Addr]*client.Runtime

allTags *stringutil.Set
Expand Down Expand Up @@ -574,7 +573,7 @@ func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Ru
rc = &client.Runtime{}
}

rc.SetInfo(client.SourceDHCP, host)
rc.SetInfo(client.SourceDHCP, []string{host})

return rc, true
}
Expand Down Expand Up @@ -767,8 +766,6 @@ func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
return
}

// TODO(e.burkov): Consider storing WHOIS information separately and
// potentially get rid of [RuntimeClient].
rc, ok := clients.ipToRC[ip]
if !ok {
// Create a RuntimeClient implicitly so that we don't do this check
Expand Down Expand Up @@ -845,7 +842,7 @@ func (clients *clientsContainer) addHostLocked(
clients.ipToRC[ip] = rc
}

rc.SetInfo(src, host)
rc.SetInfo(src, []string{host})

log.Debug("clients: adding client info %s -> %q %q [%d]", ip, src, host, len(clients.ipToRC))

Expand Down

0 comments on commit 9aefb14

Please sign in to comment.