summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2019-10-02 22:14:18 +0100
committerGitHub <noreply@github.com>2019-10-02 22:14:18 +0100
commit651e0846f78086900cc734e93f57e3bc2eca0265 (patch)
tree2ee45342fd3b22272bd78a5445409558e4780705
parentf4eaaf1f283fe3dc826012a75e7ba0dc4ddc4c1e (diff)
parentc378e705e94364c766d04a164d2ce38dc29cc7e5 (diff)
Merge pull request #323 from Natolumin/return_concrete_type_from_conn_creation
server{4,6}: Return UDPConn from NewIPv*UDPConn
-rw-r--r--dhcpv4/server4/conn.go13
-rw-r--r--dhcpv6/server6/conn.go12
2 files changed, 21 insertions, 4 deletions
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
}
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
}