summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/server6/server.go
diff options
context:
space:
mode:
authorAndrea Barberio <insomniac@slackware.it>2019-04-29 13:52:22 +0100
committerinsomniac <insomniacslk@users.noreply.github.com>2019-04-29 15:44:25 +0100
commit03e9ac918544b248cd2d660195934d7c943cb4d7 (patch)
treea9b43d893419db1d567934dc56f23b2eaaf16e0d /dhcpv6/server6/server.go
parent5c6ea15a40cc06ac0af833fa3ef883de3f32037b (diff)
[server6] Server should join multicast address
The previous logic was wrong - there's no "listening" on multicast address, the server should listen on the given address, and join the multicast group. This PR fixes it. Also moved the multicast addresses to a common package. Tested with unit/integ tests, and with coredhcp. Signed-off-by: Andrea Barberio <insomniac@slackware.it>
Diffstat (limited to 'dhcpv6/server6/server.go')
-rw-r--r--dhcpv6/server6/server.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/dhcpv6/server6/server.go b/dhcpv6/server6/server.go
index e4fcf91..ab9cb32 100644
--- a/dhcpv6/server6/server.go
+++ b/dhcpv6/server6/server.go
@@ -58,6 +58,7 @@ import (
"net"
"github.com/insomniacslk/dhcp/dhcpv6"
+ "golang.org/x/net/ipv6"
)
// Handler is a type that defines the handler function to be called every time a
@@ -111,8 +112,12 @@ func WithConn(conn net.PacketConn) ServerOpt {
}
}
-// NewServer initializes and returns a new Server object
-func NewServer(addr *net.UDPAddr, handler Handler, opt ...ServerOpt) (*Server, error) {
+// NewServer initializes and returns a new Server object, listening on `addr`,
+// and joining the multicast group ff02::1:2 . If `addr` is nil, IPv6 unspec is
+// used. If `WithConn` is used with a non-nil address, `addr` and `ifname` have
+// no effect. In such case, joining the multicast group is the caller's
+// responsibility.
+func NewServer(ifname string, addr *net.UDPAddr, handler Handler, opt ...ServerOpt) (*Server, error) {
s := &Server{
handler: handler,
}
@@ -122,10 +127,29 @@ func NewServer(addr *net.UDPAddr, handler Handler, opt ...ServerOpt) (*Server, e
}
if s.conn == nil {
+ // no connection provided by the user, create a new one
conn, err := net.ListenUDP("udp6", addr)
if err != nil {
return nil, err
}
+ // join multicast group on the specified interface
+ var iface *net.Interface
+ if ifname == "" {
+ iface = nil
+ } else {
+ iface, err = net.InterfaceByName(ifname)
+ if err != nil {
+ return nil, err
+ }
+ }
+ group := net.UDPAddr{
+ IP: dhcpv6.AllDHCPRelayAgentsAndServers,
+ Port: dhcpv6.DefaultServerPort,
+ }
+ p := ipv6.NewPacketConn(conn)
+ if err := p.JoinGroup(iface, &group); err != nil {
+ return nil, err
+ }
s.conn = conn
}
return s, nil