diff options
author | Anatole Denis <natolumin@unverle.fr> | 2019-10-01 13:54:35 +0200 |
---|---|---|
committer | Anatole Denis <natolumin@unverle.fr> | 2019-10-01 15:16:41 +0200 |
commit | c378e705e94364c766d04a164d2ce38dc29cc7e5 (patch) | |
tree | 2ee45342fd3b22272bd78a5445409558e4780705 /dhcpv6/server6 | |
parent | f4eaaf1f283fe3dc826012a75e7ba0dc4ddc4c1e (diff) |
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 <natolumin@unverle.fr>
Diffstat (limited to 'dhcpv6/server6')
-rw-r--r-- | dhcpv6/server6/conn.go | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/dhcpv6/server6/conn.go b/dhcpv6/server6/conn.go index 08c54e8..cbf72b4 100644 --- a/dhcpv6/server6/conn.go +++ b/dhcpv6/server6/conn.go @@ -15,7 +15,7 @@ import ( // As a bonus, you can actually listen on a multicast address instead of being punted to the wildcard // // The interface must already be configured. -func NewIPv6UDPConn(iface string, addr *net.UDPAddr) (net.PacketConn, error) { +func NewIPv6UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error) { fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_UDP) if err != nil { return nil, fmt.Errorf("cannot get a UDP socket: %v", err) @@ -58,5 +58,13 @@ func NewIPv6UDPConn(iface string, addr *net.UDPAddr) (net.PacketConn, error) { return nil, fmt.Errorf("cannot bind to address %v: %v", addr, 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(dhcp6): incorrect socket type, expected UDP") + } + return udpconn, nil } |