2024-01-31 14:50:27 +03:00
|
|
|
package dhcpsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-03 15:29:54 +03:00
|
|
|
"log/slog"
|
2024-07-10 16:17:56 +03:00
|
|
|
"net"
|
2024-01-31 14:50:27 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
// macKey contains hardware address as byte array of 6, 8, or 20 bytes.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Move to aghnet or even to netutil.
|
|
|
|
type macKey any
|
|
|
|
|
|
|
|
// macToKey converts mac into macKey, which is used as the key for the lease
|
|
|
|
// maps. mac must be a valid hardware address of length 6, 8, or 20 bytes, see
|
|
|
|
// [netutil.ValidateMAC].
|
|
|
|
func macToKey(mac net.HardwareAddr) (key macKey) {
|
|
|
|
switch len(mac) {
|
|
|
|
case 6:
|
|
|
|
return [6]byte(mac)
|
|
|
|
case 8:
|
|
|
|
return [8]byte(mac)
|
|
|
|
case 20:
|
|
|
|
return [20]byte(mac)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid mac address %#v", mac))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// netInterface is a common part of any interface within the DHCP server.
|
2024-01-31 14:50:27 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Add other methods as [DHCPServer] evolves.
|
|
|
|
type netInterface struct {
|
2024-07-03 15:29:54 +03:00
|
|
|
// logger logs the events related to the network interface.
|
|
|
|
logger *slog.Logger
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
// leases is the set of DHCP leases assigned to this interface.
|
|
|
|
leases map[macKey]*Lease
|
|
|
|
|
2024-01-31 14:50:27 +03:00
|
|
|
// name is the name of the network interface.
|
|
|
|
name string
|
|
|
|
|
|
|
|
// leaseTTL is the default Time-To-Live value for leases.
|
|
|
|
leaseTTL time.Duration
|
|
|
|
}
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
// newNetInterface creates a new netInterface with the given name, leaseTTL, and
|
|
|
|
// logger.
|
|
|
|
func newNetInterface(name string, l *slog.Logger, leaseTTL time.Duration) (iface *netInterface) {
|
|
|
|
return &netInterface{
|
|
|
|
logger: l,
|
|
|
|
leases: map[macKey]*Lease{},
|
|
|
|
name: name,
|
|
|
|
leaseTTL: leaseTTL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:50:27 +03:00
|
|
|
// reset clears all the slices in iface for reuse.
|
|
|
|
func (iface *netInterface) reset() {
|
2024-07-10 16:17:56 +03:00
|
|
|
clear(iface.leases)
|
2024-01-31 14:50:27 +03:00
|
|
|
}
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
// addLease inserts the given lease into iface. It returns an error if the
|
2024-01-31 14:50:27 +03:00
|
|
|
// lease can't be inserted.
|
2024-07-10 16:17:56 +03:00
|
|
|
func (iface *netInterface) addLease(l *Lease) (err error) {
|
|
|
|
mk := macToKey(l.HWAddr)
|
|
|
|
_, found := iface.leases[mk]
|
2024-01-31 14:50:27 +03:00
|
|
|
if found {
|
|
|
|
return fmt.Errorf("lease for mac %s already exists", l.HWAddr)
|
|
|
|
}
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
iface.leases[mk] = l
|
2024-01-31 14:50:27 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
// updateLease replaces an existing lease within iface with the given one. It
|
|
|
|
// returns an error if there is no lease with such hardware address.
|
|
|
|
func (iface *netInterface) updateLease(l *Lease) (prev *Lease, err error) {
|
2024-07-10 16:17:56 +03:00
|
|
|
mk := macToKey(l.HWAddr)
|
|
|
|
prev, found := iface.leases[mk]
|
2024-02-20 14:52:38 +03:00
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("no lease for mac %s", l.HWAddr)
|
|
|
|
}
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
iface.leases[mk] = l
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
return prev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeLease removes an existing lease from iface. It returns an error if
|
|
|
|
// there is no lease equal to l.
|
|
|
|
func (iface *netInterface) removeLease(l *Lease) (err error) {
|
2024-07-10 16:17:56 +03:00
|
|
|
mk := macToKey(l.HWAddr)
|
|
|
|
_, found := iface.leases[mk]
|
2024-02-20 14:52:38 +03:00
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("no lease for mac %s", l.HWAddr)
|
|
|
|
}
|
|
|
|
|
2024-07-10 16:17:56 +03:00
|
|
|
delete(iface.leases, mk)
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|