dhcpd: added dhcpd-failover service, only keep linux support

This commit is contained in:
nadoo committed 2022-02-23 22:20:06 +08:00
1 parent 1f196c9cf5
commit c261e5989c
6 files changed
+69 -33

No files matched your search

+2 -12
View File
@@ -3,40 +3,30 @@ package dhcpd
import (
"context"
"net"
"runtime"
"time"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/nadoo/glider/pkg/log"
"github.com/nadoo/glider/pkg/sockopt"
)
func existsServer(intf *net.Interface) (exists bool) {
func discovery(intf *net.Interface) (found bool) {
lc := &net.ListenConfig{}
lc.Control = sockopt.BindControl(intf)
addr := "0.0.0.0:68"
if runtime.GOOS == "linux" || runtime.GOOS == "android" {
addr = "255.255.255.255:68"
}
pc, err := lc.ListenPacket(context.Background(), "udp4", addr)
pc, err := lc.ListenPacket(context.Background(), "udp4", "255.255.255.255:68")
if err != nil {
log.F("[dhcpd] failed in dhcp client ListenPacket: %s", err)
return
}
defer pc.Close()
discovery, err := dhcpv4.NewDiscovery(intf.HardwareAddr, dhcpv4.WithBroadcast(true))
if err != nil {
log.F("[dhcpd] failed in dhcp client NewDiscovery: %s", err)
return
}
_, err = pc.WriteTo(discovery.ToBytes(), &net.UDPAddr{IP: net.IPv4bcast, Port: 67})
if err != nil {
log.F("[dhcpd] failed in dhcp client WriteTo: %s", err)
return
}