conn.go 738 B

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