insomniacslk / dhcp

DHCPv6 and DHCPv4 packet library, client and server written in Go
BSD 3-Clause "New" or "Revised" License
685 stars 168 forks source link

Support Windows #521

Open QQ2017 opened 6 months ago

QQ2017 commented 6 months ago

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

pmazzini commented 6 months ago

It looks to be a dup of #497

QQ2017 commented 5 months ago

@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
}
pmazzini commented 5 months ago

Can you create a pull request?