Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Sep 24, 2024
1 parent 23896ae commit df7492e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
21 changes: 17 additions & 4 deletions internal/client/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type HostsContainer interface {

// StorageConfig is the client storage configuration structure.
type StorageConfig struct {
// DHCP is used to update [SourceDHCP] runtime client information. It must
// not be nil.
// DHCP is used to match IPs against MACs of persistent clients and update
// [SourceDHCP] runtime client information. It must not be nil.
DHCP DHCP

// EtcHosts is used to update [SourceHostsFile] runtime client information.
Expand All @@ -100,6 +100,10 @@ type StorageConfig struct {
// ARPClientsUpdatePeriod defines how often [SourceARP] runtime client
// information is updated.
ARPClientsUpdatePeriod time.Duration

// RuntimeSourceDHCP specifies whether to update [SourceDHCP] information
// of runtime clients.
RuntimeSourceDHCP bool
}

// Storage contains information about persistent and runtime clients.
Expand Down Expand Up @@ -131,6 +135,10 @@ type Storage struct {
// arpClientsUpdatePeriod defines how often [SourceARP] runtime client
// information is updated. It must be greater than zero.
arpClientsUpdatePeriod time.Duration

// runtimeSourceDHCP specifies whether to update [SourceDHCP] information
// of runtime clients.
runtimeSourceDHCP bool
}

// NewStorage returns initialized client storage. conf must not be nil.
Expand All @@ -143,8 +151,9 @@ func NewStorage(conf *StorageConfig) (s *Storage, err error) {
dhcp: conf.DHCP,
etcHosts: conf.EtcHosts,
arpDB: conf.ARPDB,
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
done: make(chan struct{}),
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
runtimeSourceDHCP: conf.RuntimeSourceDHCP,
}

for i, p := range conf.InitialClients {
Expand Down Expand Up @@ -306,7 +315,7 @@ func (s *Storage) UpdateAddress(ip netip.Addr, host string, info *whois.Info) {

// UpdateDHCP updates [SourceDHCP] runtime client information.
func (s *Storage) UpdateDHCP() {
if s.dhcp == nil {
if s.dhcp == nil || !s.runtimeSourceDHCP {
return
}

Expand Down Expand Up @@ -545,6 +554,10 @@ func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
return rc.clone()
}

if !s.runtimeSourceDHCP {
return nil
}

host := s.dhcp.HostByIP(ip)
if host == "" {
return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/client/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ func TestClientsDHCP(t *testing.T) {
}

storage, err := client.NewStorage(&client.StorageConfig{
DHCP: d,
DHCP: d,
RuntimeSourceDHCP: true,
})
require.NoError(t, err)

Expand Down
7 changes: 1 addition & 6 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ type clientsContainer struct {
// storage stores information about persistent clients.
storage *client.Storage

// dhcp is the DHCP service implementation.
dhcp client.DHCP

// clientChecker checks if a client is blocked by the current access
// settings.
clientChecker BlockedClientChecker
Expand Down Expand Up @@ -75,9 +72,6 @@ func (clients *clientsContainer) Init(
return errors.Error("clients container already initialized")
}

// TODO(e.burkov): Use [dhcpsvc] implementation when it's ready.
clients.dhcp = dhcpServer

confClients := make([]*client.Persistent, 0, len(objects))
for i, o := range objects {
var p *client.Persistent
Expand Down Expand Up @@ -106,6 +100,7 @@ func (clients *clientsContainer) Init(
EtcHosts: EtcHosts,
ARPDB: arpDB,
ARPClientsUpdatePeriod: arpClientsUpdatePeriod,
RuntimeSourceDHCP: config.Clients.Sources.DHCP,
})
if err != nil {
return fmt.Errorf("init client storage: %w", err)
Expand Down

0 comments on commit df7492e

Please sign in to comment.