summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/server4/conn.go
diff options
context:
space:
mode:
authorGuillaume Rose <guillaume.rose@gmail.com>2021-06-08 01:53:46 -0700
committerGitHub <noreply@github.com>2021-06-08 09:53:46 +0100
commit465dd6c35f6c983206a5bc21fbfff4819ce21eb0 (patch)
treeb1ecb516857e5162249184d59fd65e5015785cc9 /dhcpv4/server4/conn.go
parentfb4eaaa00ad29a188328380690494d9267dc1ccc (diff)
Make server4 package compile on Windows (#428)
Diffstat (limited to 'dhcpv4/server4/conn.go')
-rw-r--r--dhcpv4/server4/conn.go63
1 files changed, 0 insertions, 63 deletions
diff --git a/dhcpv4/server4/conn.go b/dhcpv4/server4/conn.go
deleted file mode 100644
index 3e49669..0000000
--- a/dhcpv4/server4/conn.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package server4
-
-import (
- "errors"
- "fmt"
- "net"
- "os"
-
- "github.com/insomniacslk/dhcp/dhcpv4"
- "golang.org/x/sys/unix"
-)
-
-// NewIPv4UDPConn returns a UDP connection bound to both the interface and port
-// given based on a IPv4 DGRAM socket. The UDP connection allows broadcasting.
-//
-// The interface must already be configured.
-func NewIPv4UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error) {
- fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
- if err != nil {
- return nil, fmt.Errorf("cannot get a UDP socket: %v", err)
- }
- f := os.NewFile(uintptr(fd), "")
- // net.FilePacketConn dups the FD, so we have to close this in any case.
- defer f.Close()
-
- // Allow broadcasting.
- if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_BROADCAST, 1); err != nil {
- return nil, fmt.Errorf("cannot set broadcasting on socket: %v", err)
- }
- // Allow reusing the addr to aid debugging.
- if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
- return nil, fmt.Errorf("cannot set reuseaddr on socket: %v", err)
- }
- if len(iface) != 0 {
- // Bind directly to the interface.
- if err := dhcpv4.BindToInterface(fd, iface); err != nil {
- return nil, fmt.Errorf("cannot bind to interface %s: %v", iface, err)
- }
- }
-
- if addr == nil {
- addr = &net.UDPAddr{Port: dhcpv4.ServerPort}
- }
- // Bind to the port.
- saddr := unix.SockaddrInet4{Port: addr.Port}
- if addr.IP != nil && addr.IP.To4() == nil {
- return nil, fmt.Errorf("wrong address family (expected v4) for %s", addr.IP)
- }
- copy(saddr.Addr[:], addr.IP.To4())
- if err := unix.Bind(fd, &saddr); err != nil {
- return nil, fmt.Errorf("cannot bind to port %d: %v", addr.Port, err)
- }
-
- conn, err := net.FilePacketConn(f)
- if err != nil {
- return nil, err
- }
- udpconn, ok := conn.(*net.UDPConn)
- if !ok {
- return nil, errors.New("BUG(dhcp4): incorrect socket type, expected UDP")
- }
- return udpconn, nil
-}