github.com/igoogolx/clash@v1.19.8/component/dhcp/conn.go (about)

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