blob: e12136aabd64083a0db096454574fd7091c9f99b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package dhcpv6
import (
"fmt"
"log"
"net"
)
type ResponseWriter interface {
LocalAddr() net.Addr
RemoteAddr() net.Addr
WriteMsg(DHCPv6) error
Write([]byte) (int, error)
Close() error
}
type Handler interface {
ServeDHCP(w ResponseWriter, m *DHCPv6)
}
type Server struct {
PacketConn net.PacketConn
Handler Handler
}
func (s *Server) ActivateAndServe() error {
if s.PacketConn == nil {
return fmt.Errorf("Error: no packet connection specified")
}
var pc *net.UDPConn
var ok bool
if pc, ok = s.PacketConn.(*net.UDPConn); !ok {
return fmt.Errorf("Error: not an UDPConn")
}
if pc == nil {
return fmt.Errorf("ActivateAndServe: Invalid nil PacketConn")
}
log.Print("Handling requests")
for {
rbuf := make([]byte, 1024) // FIXME this is bad
n, peer, err := pc.ReadFrom(rbuf)
if err != nil {
log.Printf("Error reading from packet conn: %v", err)
continue
}
log.Printf("Handling request from %v", peer)
m, err := FromBytes(rbuf[:n])
if err != nil {
log.Printf("Error parsing DHCPv6 request: %v", err)
continue
}
log.Print(m.Summary())
// FIXME use s.Handler
}
return nil
}
|