github.com/metacubex/mihomo@v1.18.5/component/dhcp/conn.go (about)

     1  package dhcp
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/netip"
     7  	"runtime"
     8  
     9  	"github.com/metacubex/mihomo/component/dialer"
    10  )
    11  
    12  func ListenDHCPClient(ctx context.Context, ifaceName string) (net.PacketConn, error) {
    13  	listenAddr := "0.0.0.0:68"
    14  	if runtime.GOOS == "linux" || runtime.GOOS == "android" {
    15  		listenAddr = "255.255.255.255:68"
    16  	}
    17  
    18  	options := []dialer.Option{
    19  		dialer.WithInterface(ifaceName),
    20  		dialer.WithAddrReuse(true),
    21  	}
    22  
    23  	// fallback bind on windows, because syscall bind can not receive broadcast
    24  	if runtime.GOOS == "windows" {
    25  		options = append(options, dialer.WithFallbackBind(true))
    26  	}
    27  
    28  	return dialer.ListenPacket(ctx, "udp4", listenAddr, netip.AddrPortFrom(netip.AddrFrom4([4]byte{255, 255, 255, 255}), 67), options...)
    29  }