summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
authorinsomniac <insomniacslk@users.noreply.github.com>2018-09-27 20:05:34 +0100
committerGitHub <noreply@github.com>2018-09-27 20:05:34 +0100
commit705562ea82b711f372f75ad325d180372ae2ac40 (patch)
tree44b57f2679ade2563f8a2adfcf9e959d0d359db8 /dhcpv6
parentf10e01a9bfb08a7b74bd9d322b26bc354cb04ad1 (diff)
parent93e07e83bb197ce356a596dc8e57f134c3ce5fef (diff)
Implemented basic DHCPv6 server handler (#157)
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/client.go7
-rw-r--r--dhcpv6/client_test.go14
-rw-r--r--dhcpv6/server.go138
-rw-r--r--dhcpv6/server_test.go77
4 files changed, 217 insertions, 19 deletions
diff --git a/dhcpv6/client.go b/dhcpv6/client.go
index 10a20c9..24b6e0d 100644
--- a/dhcpv6/client.go
+++ b/dhcpv6/client.go
@@ -127,6 +127,13 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT
return nil, err
}
defer conn.Close()
+ // wait for the listener to be ready
+ for {
+ if conn.LocalAddr() != nil {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
// send the packet out
conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
diff --git a/dhcpv6/client_test.go b/dhcpv6/client_test.go
new file mode 100644
index 0000000..d2efa1f
--- /dev/null
+++ b/dhcpv6/client_test.go
@@ -0,0 +1,14 @@
+package dhcpv6
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestNewClient(t *testing.T) {
+ c := NewClient()
+ require.NotNil(t, c)
+ require.Equal(t, DefaultReadTimeout, c.ReadTimeout)
+ require.Equal(t, DefaultWriteTimeout, c.WriteTimeout)
+}
diff --git a/dhcpv6/server.go b/dhcpv6/server.go
index e12136a..3fade87 100644
--- a/dhcpv6/server.go
+++ b/dhcpv6/server.go
@@ -4,43 +4,124 @@ import (
"fmt"
"log"
"net"
+ "sync"
+ "time"
)
-type ResponseWriter interface {
- LocalAddr() net.Addr
- RemoteAddr() net.Addr
- WriteMsg(DHCPv6) error
- Write([]byte) (int, error)
- Close() error
+/*
+ To use the DHCPv6 server code you have to call NewServer with two arguments:
+ - a handler function, that will be called every time a valid DHCPv6 packet is
+ received, and
+ - an address to listen on.
+
+ The handler is a function that takes as input a packet connection, that can be
+ used to reply to the client; a peer address, that identifies the client sending
+ the request, and the DHCPv6 packet itself. Just implement your custom logic in
+ the handler.
+
+ The address to listen on is used to know IP address, port and optionally the
+ scope to create and UDP6 socket to listen on for DHCPv6 traffic.
+
+ Example program:
+
+
+package main
+
+import (
+ "log"
+ "net"
+
+ "github.com/insomniacslk/dhcp/dhcpv6"
+)
+
+func handler(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
+ // this function will just print the received DHCPv6 message, without replying
+ log.Print(m.Summary())
}
-type Handler interface {
- ServeDHCP(w ResponseWriter, m *DHCPv6)
+func main() {
+ laddr := net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Port: 547,
+ }
+ server := dhcpv6.NewServer(laddr, handler)
+
+ defer server.Close()
+ if err := server.ActivateAndServe(); err != nil {
+ log.Panic(err)
+ }
}
+*/
+
+// Handler is a type that defines the handler function to be called every time a
+// valid DHCPv6 message is received
+type Handler func(conn net.PacketConn, peer net.Addr, m DHCPv6)
+
+// Server represents a DHCPv6 server object
type Server struct {
- PacketConn net.PacketConn
+ conn net.PacketConn
+ connMutex sync.Mutex
+ shouldStop chan bool
Handler Handler
+ localAddr net.UDPAddr
+}
+
+// LocalAddr returns the local address of the listening socket, or nil if not
+// listening
+func (s *Server) LocalAddr() net.Addr {
+ s.connMutex.Lock()
+ defer s.connMutex.Unlock()
+ if s.conn == nil {
+ return nil
+ }
+ return s.conn.LocalAddr()
}
+// ActivateAndServe starts the DHCPv6 server
func (s *Server) ActivateAndServe() error {
- if s.PacketConn == nil {
- return fmt.Errorf("Error: no packet connection specified")
+ s.connMutex.Lock()
+ if s.conn == nil {
+ conn, err := net.ListenUDP("udp6", &s.localAddr)
+ if err != nil {
+ return err
+ }
+ s.conn = conn
}
- var pc *net.UDPConn
- var ok bool
- if pc, ok = s.PacketConn.(*net.UDPConn); !ok {
+ defer func() {
+ s.conn.Close()
+ s.conn = nil
+ }()
+ s.connMutex.Unlock()
+ var (
+ pc *net.UDPConn
+ ok bool
+ )
+ if pc, ok = s.conn.(*net.UDPConn); !ok {
return fmt.Errorf("Error: not an UDPConn")
}
if pc == nil {
return fmt.Errorf("ActivateAndServe: Invalid nil PacketConn")
}
- log.Print("Handling requests")
+ log.Printf("Server listening on %s", pc.LocalAddr())
+ log.Print("Ready to handle requests")
for {
- rbuf := make([]byte, 1024) // FIXME this is bad
+ select {
+ case <-s.shouldStop:
+ break
+ case <-time.After(time.Millisecond):
+ }
+ pc.SetReadDeadline(time.Now().Add(time.Second))
+ rbuf := make([]byte, 4096) // FIXME this is bad
n, peer, err := pc.ReadFrom(rbuf)
if err != nil {
- log.Printf("Error reading from packet conn: %v", err)
+ switch err.(type) {
+ case net.Error:
+ // silently skip and continue
+ default:
+ //complain and continue
+ log.Printf("Error reading from packet conn: %v", err)
+ }
continue
}
log.Printf("Handling request from %v", peer)
@@ -49,8 +130,27 @@ func (s *Server) ActivateAndServe() error {
log.Printf("Error parsing DHCPv6 request: %v", err)
continue
}
- log.Print(m.Summary())
- // FIXME use s.Handler
+ s.Handler(pc, peer, m)
}
return nil
}
+
+// Close sends a termination request to the server, and closes the UDP listener
+func (s *Server) Close() error {
+ s.shouldStop <- true
+ s.connMutex.Lock()
+ defer s.connMutex.Unlock()
+ if s.conn != nil {
+ return s.conn.Close()
+ }
+ return nil
+}
+
+// NewServer initializes and returns a new Server object
+func NewServer(addr net.UDPAddr, handler Handler) *Server {
+ return &Server{
+ localAddr: addr,
+ Handler: handler,
+ shouldStop: make(chan bool, 1),
+ }
+}
diff --git a/dhcpv6/server_test.go b/dhcpv6/server_test.go
new file mode 100644
index 0000000..066fe3a
--- /dev/null
+++ b/dhcpv6/server_test.go
@@ -0,0 +1,77 @@
+package dhcpv6
+
+import (
+ "log"
+ "net"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+)
+
+// utility function to set up a client and a server instance and run it in
+// background. The caller needs to call Server.Close() once finished.
+func setUpClientAndServer(handler Handler) (*Client, *Server) {
+ laddr := net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Port: 0,
+ Zone: "lo",
+ }
+ s := NewServer(laddr, handler)
+ go s.ActivateAndServe()
+
+ c := NewClient()
+ c.LocalAddr = &net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Zone: "lo",
+ }
+ for {
+ if s.LocalAddr() != nil {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ log.Printf("Waiting for server to run...")
+ }
+ c.RemoteAddr = &net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Port: s.LocalAddr().(*net.UDPAddr).Port,
+ Zone: "lo",
+ }
+
+ return c, s
+}
+
+func TestNewServer(t *testing.T) {
+ laddr := net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Port: 0,
+ Zone: "lo",
+ }
+ handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) {}
+ s := NewServer(laddr, handler)
+ defer s.Close()
+
+ require.NotNil(t, s)
+ require.Nil(t, s.conn)
+ require.Equal(t, laddr, s.localAddr)
+ require.NotNil(t, s.Handler)
+}
+
+func TestServerActivateAndServe(t *testing.T) {
+ handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) {
+ adv, err := NewAdvertiseFromSolicit(m)
+ if err != nil {
+ log.Printf("NewAdvertiseFromSolicit failed: %v", err)
+ return
+ }
+ if _, err := conn.WriteTo(adv.ToBytes(), peer); err != nil {
+ log.Printf("Cannot reply to client: %v", err)
+ }
+ }
+ c, s := setUpClientAndServer(handler)
+ defer s.Close()
+
+ _, _, err := c.Solicit("lo", nil)
+
+ require.NoError(t, err)
+}