AdGuardHome/internal/dhcpsvc/dhcpsvc.go
Eugene Burkov f40ef76c79 Pull request 1861: 4923-gopacket-dhcp vol.1
Merge in DNS/adguard-home from 4923-gopacket-dhcp to master

Updates #4923.

Squashed commit of the following:

commit edf36ce8b1873272c3daebe8cc8f8132793aac44
Merge: a17513d3e 123ca8738
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jun 22 14:14:39 2023 +0300

    Merge branch 'master' into 4923-gopacket-dhcp

commit a17513d3e0a9e596d56444dfa46478eee15631de
Merge: f04727c29 994906fbd
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Jun 21 17:49:09 2023 +0300

    Merge branch 'master' into 4923-gopacket-dhcp

commit f04727c29eaf22f9eb53f3aa33d42d00e177b224
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Jun 20 15:42:31 2023 +0300

    home: revert clients container

commit c58284ac6b5b2274da5eed2e853847d757709e5b
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Jun 19 21:10:36 2023 +0300

    all: imp code, names, docs

commit 4c4613c939e1325d11655822d9dbc3f05a6d203c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Jun 13 18:51:12 2023 +0300

    all: imp code

commit 0b4a6e0dd561d9b7bb78dea21dcc947bcd0bd583
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Jun 7 18:40:15 2023 +0300

    all: imp api

commit 0425edea03d6ca0859657df683bef6ec45bfc399
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Jun 5 15:57:23 2023 +0300

    dhcpsvc: introduce package

commit 5628ebe6cccf91e2c48778966730bcbbe9e1d9f2
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jun 1 17:49:12 2023 +0300

    WIP
2023-06-22 14:18:43 +03:00

120 lines
3.8 KiB
Go

// Package dhcpsvc contains the AdGuard Home DHCP service.
//
// TODO(e.burkov): Add tests.
package dhcpsvc
import (
"context"
"net"
"net/netip"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
)
// Lease is a DHCP lease.
//
// TODO(e.burkov): Consider it to [agh], since it also may be needed in
// [websvc]. Also think of implementing iterating methods with appropriate
// signatures.
type Lease struct {
// IP is the IP address leased to the client.
IP netip.Addr
// Expiry is the expiration time of the lease.
Expiry time.Time
// Hostname of the client.
Hostname string
// HWAddr is the physical hardware address (MAC address).
HWAddr net.HardwareAddr
// IsStatic defines if the lease is static.
IsStatic bool
}
type Interface interface {
agh.ServiceWithConfig[*Config]
// Enabled returns true if DHCP provides information about clients.
Enabled() (ok bool)
// HostByIP returns the hostname of the DHCP client with the given IP
// address. The address will be netip.Addr{} if there is no such client,
// due to an assumption that a DHCP client must always have an IP address.
HostByIP(ip netip.Addr) (host string)
// MACByIP returns the MAC address for the given IP address leased. It
// returns nil if there is no such client, due to an assumption that a DHCP
// client must always have a MAC address.
MACByIP(ip netip.Addr) (mac net.HardwareAddr)
// IPByHost returns the IP address of the DHCP client with the given
// hostname. The hostname will be an empty string if there is no such
// client, due to an assumption that a DHCP client must always have a
// hostname, either set by the client or assigned automatically.
IPByHost(host string) (ip netip.Addr)
// Leases returns all the DHCP leases.
Leases() (leases []*Lease)
// AddLease adds a new DHCP lease. It returns an error if the lease is
// invalid or already exists.
AddLease(l *Lease) (err error)
// EditLease changes an existing DHCP lease. It returns an error if there
// is no lease equal to old or if new is invalid or already exists.
EditLease(old, new *Lease) (err error)
// RemoveLease removes an existing DHCP lease. It returns an error if there
// is no lease equal to l.
RemoveLease(l *Lease) (err error)
// Reset removes all the DHCP leases.
Reset() (err error)
}
// Empty is an [Interface] implementation that does nothing.
type Empty struct{}
// type check
var _ Interface = Empty{}
// Start implements the [Service] interface for Empty.
func (Empty) Start() (err error) { return nil }
// Shutdown implements the [Service] interface for Empty.
func (Empty) Shutdown(_ context.Context) (err error) { return nil }
var _ agh.ServiceWithConfig[*Config] = Empty{}
// Config implements the [ServiceWithConfig] interface for Empty.
func (Empty) Config() (conf *Config) { return nil }
// Enabled implements the [Interface] interface for Empty.
func (Empty) Enabled() (ok bool) { return false }
// HostByIP implements the [Interface] interface for Empty.
func (Empty) HostByIP(_ netip.Addr) (host string) { return "" }
// MACByIP implements the [Interface] interface for Empty.
func (Empty) MACByIP(_ netip.Addr) (mac net.HardwareAddr) { return nil }
// IPByHost implements the [Interface] interface for Empty.
func (Empty) IPByHost(_ string) (ip netip.Addr) { return netip.Addr{} }
// Leases implements the [Interface] interface for Empty.
func (Empty) Leases() (leases []*Lease) { return nil }
// AddLease implements the [Interface] interface for Empty.
func (Empty) AddLease(_ *Lease) (err error) { return nil }
// EditLease implements the [Interface] interface for Empty.
func (Empty) EditLease(_, _ *Lease) (err error) { return nil }
// RemoveLease implements the [Interface] interface for Empty.
func (Empty) RemoveLease(_ *Lease) (err error) { return nil }
// Reset implements the [Interface] interface for Empty.
func (Empty) Reset() (err error) { return nil }