summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
authorAndrea Barberio <insomniac@slackware.it>2019-04-17 00:03:45 +0100
committerinsomniac <insomniacslk@users.noreply.github.com>2019-04-17 11:38:11 +0100
commitbbe0e65b24bf53191fc56b67d0d98624030e3d26 (patch)
treed2c009516c5f86b1a44b6c566602e2714e81919b /dhcpv6
parentac4db3f5fb61a34cf65d81fb4efe6e3d2627b461 (diff)
[server] Serve returns an error, and closes when done
Server6 and Server4 had Serve methods that return without reporting an error, changed in this PR. Serve now also closes the connection when done. Signed-off-by: Andrea Barberio <insomniac@slackware.it>
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/server6/server.go58
1 files changed, 56 insertions, 2 deletions
diff --git a/dhcpv6/server6/server.go b/dhcpv6/server6/server.go
index f6fb826..e4fcf91 100644
--- a/dhcpv6/server6/server.go
+++ b/dhcpv6/server6/server.go
@@ -1,5 +1,58 @@
package server6
+/*
+ To use the DHCPv6 server code you have to call NewServer with two arguments:
+ - an address to listen on, and
+ - a handler function, that will be called every time a valid DHCPv6 packet is
+ received.
+
+ The address to listen on is used to know IP address, port and optionally the
+ scope to create and UDP socket to listen on for DHCPv6 traffic.
+
+ 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.
+
+ Optionally, NewServer can receive options that will modify the server object.
+ Some options already exist, for example WithConn. If this option is passed with
+ a valid connection, the listening address argument is ignored.
+
+ Example program:
+
+
+package main
+
+import (
+ "log"
+ "net"
+
+ "github.com/insomniacslk/dhcp/dhcpv6"
+ "github.com/insomniacslk/dhcp/dhcpv6/server6"
+)
+
+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())
+}
+
+func main() {
+ laddr := net.UDPAddr{
+ IP: net.ParseIP("::1"),
+ Port: 547,
+ }
+ server, err := server6.NewServer(&laddr, handler)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // This never returns. If you want to do other stuff, dump it into a
+ // goroutine.
+ server.Serve()
+}
+
+*/
+
import (
"log"
"net"
@@ -19,16 +72,17 @@ type Server struct {
// Serve starts the DHCPv6 server. The listener will run in background, and can
// be interrupted with `Server.Close`.
-func (s *Server) Serve() {
+func (s *Server) Serve() error {
log.Printf("Server listening on %s", s.conn.LocalAddr())
log.Print("Ready to handle requests")
+ defer s.Close()
for {
rbuf := make([]byte, 4096) // FIXME this is bad
n, peer, err := s.conn.ReadFrom(rbuf)
if err != nil {
log.Printf("Error reading from packet conn: %v", err)
- return
+ return err
}
log.Printf("Handling request from %v", peer)