From c378e705e94364c766d04a164d2ce38dc29cc7e5 Mon Sep 17 00:00:00 2001 From: Anatole Denis Date: Tue, 1 Oct 2019 13:54:35 +0200 Subject: server{4,6}: Return UDPConn from NewIPv*UDPConn The concrete type under the interface is known here since we create the connection in the same function. Since *net.UDPConn implements net.PacketConn anyway, returning the concrete type here is more powerful and less risky than having downstream users cast the value themselves There should be no code change for downstream users, with the exception of explicit casts (`udpc := conn.(*net.UDPConn)`), which can simply be removed Signed-off-by: Anatole Denis --- dhcpv4/server4/conn.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'dhcpv4/server4/conn.go') diff --git a/dhcpv4/server4/conn.go b/dhcpv4/server4/conn.go index 84d6aa3..0a4c73a 100644 --- a/dhcpv4/server4/conn.go +++ b/dhcpv4/server4/conn.go @@ -1,6 +1,7 @@ package server4 import ( + "errors" "fmt" "net" "os" @@ -13,7 +14,7 @@ import ( // given based on a IPv4 DGRAM socket. The UDP connection allows broadcasting. // // The interface must already be configured. -func NewIPv4UDPConn(iface string, port int) (net.PacketConn, error) { +func NewIPv4UDPConn(iface string, port int) (*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) @@ -41,5 +42,13 @@ func NewIPv4UDPConn(iface string, port int) (net.PacketConn, error) { return nil, fmt.Errorf("cannot bind to port %d: %v", port, err) } - return net.FilePacketConn(f) + 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 } -- cgit v1.2.3