2021-09-06 23:07:34 +08:00
|
|
|
package dhcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2024-04-10 15:34:28 +08:00
|
|
|
"net/netip"
|
2021-09-06 23:07:34 +08:00
|
|
|
"runtime"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
2021-09-06 23:07:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ListenDHCPClient(ctx context.Context, ifaceName string) (net.PacketConn, error) {
|
|
|
|
listenAddr := "0.0.0.0:68"
|
|
|
|
if runtime.GOOS == "linux" || runtime.GOOS == "android" {
|
|
|
|
listenAddr = "255.255.255.255:68"
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:27:38 +08:00
|
|
|
options := []dialer.Option{
|
|
|
|
dialer.WithInterface(ifaceName),
|
|
|
|
dialer.WithAddrReuse(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback bind on windows, because syscall bind can not receive broadcast
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
options = append(options, dialer.WithFallbackBind(true))
|
|
|
|
}
|
|
|
|
|
2024-04-10 15:34:28 +08:00
|
|
|
return dialer.ListenPacket(ctx, "udp4", listenAddr, netip.AddrPortFrom(netip.AddrFrom4([4]byte{255, 255, 255, 255}), 67), options...)
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|