f-46: dhcpapi: add the control socket, its protocol and its client #46
Le contrat et le listener vont dans internal/api/dhcp (package dhcpapi), sur la forme de internal/api/agent, et le client dans internal/client/dhcp. Chaînage des imports : statefile <- dhcpd <- dhcpapi <- dhcpclient, sans cycle. dhcpd parle net.IP et net.IPNet et garde ses structs disque privées ; dhcpapi parle chaînes JSON et convertit à la frontière. Un même type portait jusqu'ici le format du fil, la signature du Store et le format du .state — ce qui couplait le fichier au protocole alors que le ticket le décrit comme un détail interne. Le digest est calculé sur une forme canonique partagée par les deux côtés : MAC, IP et CIDR normalisés, hôtes triés, doublon de MAC refusé. Un écart de digest signale donc une vraie divergence, pas une différence d'écriture. Le listener pose un recover par connexion, plafonne les lignes à 64 Kio, refuse une ligne malformée sans fermer la connexion, écoute en 0600 et supprime une socket résiduelle avant le bind. Le client pose une deadline. 101 tests au total, -race propre, treize mutations toutes détectées. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
8802de4fb0
commit
0c0b58820c
13 changed files with 1482 additions and 445 deletions
83
internal/api/dhcp/convert.go
Normal file
83
internal/api/dhcp/convert.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package dhcpapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/dhcpd"
|
||||
)
|
||||
|
||||
func (s Subnet) toConfig() (dhcpd.SubnetConfig, error) {
|
||||
_, network, err := net.ParseCIDR(s.Network)
|
||||
if err != nil {
|
||||
return dhcpd.SubnetConfig{}, fmt.Errorf("invalid network %q: %w", s.Network, err)
|
||||
}
|
||||
interfaceIP := net.ParseIP(s.InterfaceIP)
|
||||
if interfaceIP == nil {
|
||||
return dhcpd.SubnetConfig{}, fmt.Errorf("invalid interface ip %q", s.InterfaceIP)
|
||||
}
|
||||
|
||||
c := dhcpd.SubnetConfig{Network: network, InterfaceIP: interfaceIP}
|
||||
|
||||
if s.VPCRoute != "" {
|
||||
if _, c.VPCRoute, err = net.ParseCIDR(s.VPCRoute); err != nil {
|
||||
return dhcpd.SubnetConfig{}, fmt.Errorf("invalid vpc route %q: %w", s.VPCRoute, err)
|
||||
}
|
||||
}
|
||||
if s.DefaultGateway != "" {
|
||||
if c.DefaultGateway = net.ParseIP(s.DefaultGateway); c.DefaultGateway == nil {
|
||||
return dhcpd.SubnetConfig{}, fmt.Errorf("invalid default gateway %q", s.DefaultGateway)
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (h Host) toHost() (dhcpd.Host, error) {
|
||||
mac, err := net.ParseMAC(h.MAC)
|
||||
if err != nil {
|
||||
return dhcpd.Host{}, fmt.Errorf("invalid mac %q: %w", h.MAC, err)
|
||||
}
|
||||
ip := net.ParseIP(h.IP)
|
||||
if ip == nil {
|
||||
return dhcpd.Host{}, fmt.Errorf("invalid host ip %q", h.IP)
|
||||
}
|
||||
return dhcpd.Host{MAC: mac, IP: ip, VM: h.VM, DefaultRoute: h.DefaultRoute}, nil
|
||||
}
|
||||
|
||||
func subnetFromConfig(c dhcpd.SubnetConfig) Subnet {
|
||||
s := Subnet{
|
||||
Network: c.Network.String(),
|
||||
InterfaceIP: c.InterfaceIP.String(),
|
||||
}
|
||||
if c.VPCRoute != nil {
|
||||
s.VPCRoute = c.VPCRoute.String()
|
||||
}
|
||||
if c.DefaultGateway != nil {
|
||||
s.DefaultGateway = c.DefaultGateway.String()
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func hostFromHost(h dhcpd.Host) Host {
|
||||
return Host{
|
||||
MAC: h.MAC.String(),
|
||||
IP: h.IP.String(),
|
||||
VM: h.VM,
|
||||
DefaultRoute: h.DefaultRoute,
|
||||
}
|
||||
}
|
||||
|
||||
func stateFromStore(store *dhcpd.Store) State {
|
||||
state := State{Hosts: make([]Host, 0)}
|
||||
|
||||
if config, configured := store.Subnet(); configured {
|
||||
subnet := subnetFromConfig(config)
|
||||
state.Subnet = &subnet
|
||||
}
|
||||
for _, h := range store.Hosts() {
|
||||
state.Hosts = append(state.Hosts, hostFromHost(h))
|
||||
}
|
||||
SortHosts(state.Hosts)
|
||||
|
||||
return state
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue