dhcp icon indicating copy to clipboard operation
dhcp copied to clipboard

Support Windows

Open QQ2017 opened this issue 1 year ago • 3 comments

The DHCP4 server prompts "not implemented on Windows" when running on Windows system. Can it support Windows? Thanks

QQ2017 avatar Jan 18 '24 14:01 QQ2017

It looks to be a dup of #497

pmazzini avatar Jan 18 '24 16:01 pmazzini

@pmazzini Hello, here is the code of \dhcpv4\server4\conn_windows.go

// +build windows

package server4

import (
	"fmt"
	"net"
)

// NewIPv4UDPConn returns a UDP connection bound to both the interface and port
// given based on a IPv4 UDP socket. The UDP connection allows broadcasting.
func NewIPv4UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error) {
	// For Windows, we use the ListenPacket method to create a UDP connection.
	// This method automatically handles socket creation and configuration.
	laddr := net.UDPAddr{
		IP:   net.IPv4zero, // Use IPv4zero for listening on all interfaces.
		Port: addr.Port,
	}
	conn, err := net.ListenPacket("udp4", laddr.String())
	if err != nil {
		return nil, fmt.Errorf("cannot create UDP listener: %v", err)
	}
	udpConn, ok := conn.(*net.UDPConn)
	if !ok {
		return nil, fmt.Errorf("incorrect connection type: %T, expected *net.UDPConn", conn)
	}
	// On Windows, binding to a specific interface is not as straightforward
	// as on Unix-based systems. It might require additional steps or be
	// managed differently depending on the use case and network configuration.
	// Setting socket options like SO_BROADCAST is not directly exposed in the
	// net package, but the ListenPacket should create a socket that is
	// capable of broadcasting by default.
	return udpConn, nil
}

QQ2017 avatar Feb 11 '24 08:02 QQ2017

Can you create a pull request?

pmazzini avatar Feb 11 '24 09:02 pmazzini