diff options
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/server.go | 89 |
1 files changed, 70 insertions, 19 deletions
diff --git a/dhcpv6/server.go b/dhcpv6/server.go index e12136a..786d369 100644 --- a/dhcpv6/server.go +++ b/dhcpv6/server.go @@ -6,38 +6,82 @@ import ( "net" ) -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) + + if err := server.ActivateAndServe(); err != nil { + log.Fatal(err) + } } +*/ + +type Handler func(conn net.PacketConn, peer net.Addr, m DHCPv6) + type Server struct { - PacketConn net.PacketConn - Handler Handler + conn net.PacketConn + LocalAddr net.UDPAddr + Handler Handler } func (s *Server) ActivateAndServe() error { - if s.PacketConn == nil { - return fmt.Errorf("Error: no packet connection specified") + 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 { + 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 + log.Printf("Waiting..") + 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) @@ -49,8 +93,15 @@ 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) } + s.conn.Close() return nil } + +func NewServer(addr net.UDPAddr, handler Handler) *Server { + return &Server{ + LocalAddr: addr, + Handler: handler, + } +} |