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
|
package dhcp6server
import (
"sync"
"github.com/mdlayher/dhcp6"
)
// ServeMux is a DHCP request multiplexer, which implements Handler. ServeMux
// matches handlers based on their MessageType, enabling different handlers
// to be used for different types of DHCP messages. ServeMux can be helpful
// for structuring your application, but may not be needed for very simple
// DHCP servers.
type ServeMux struct {
mu sync.RWMutex
m map[dhcp6.MessageType]Handler
}
// NewServeMux creates a new ServeMux which is ready to accept Handlers.
func NewServeMux() *ServeMux {
return &ServeMux{
m: make(map[dhcp6.MessageType]Handler),
}
}
// ServeDHCP implements Handler for ServeMux, and serves a DHCP request using
// the appropriate handler for an input Request's MessageType. If the
// MessageType does not match a valid Handler, ServeDHCP does not invoke any
// handlers, ignoring a client's request.
func (mux *ServeMux) ServeDHCP(w ResponseSender, r *Request) {
mux.mu.RLock()
defer mux.mu.RUnlock()
h, ok := mux.m[r.MessageType]
if !ok {
return
}
h.ServeDHCP(w, r)
}
// Handle registers a MessageType and Handler with a ServeMux, so that
// future requests with that MessageType will invoke the Handler.
func (mux *ServeMux) Handle(mt dhcp6.MessageType, handler Handler) {
mux.mu.Lock()
mux.m[mt] = handler
mux.mu.Unlock()
}
// HandleFunc registers a MessageType and function as a HandlerFunc with a
// ServeMux, so that future requests with that MessageType will invoke the
// HandlerFunc.
func (mux *ServeMux) HandleFunc(mt dhcp6.MessageType, handler func(ResponseSender, *Request)) {
mux.Handle(mt, HandlerFunc(handler))
}
|