dhcp6server

package
v0.0.0-...-2a67805 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AllRelayAgentsAndServersAddr is the multicast address group which is
	// used to communicate with neighboring (on-link) DHCP servers and relay
	// agents, as defined in RFC 3315, Section 5.1.  All DHCP servers
	// and relay agents are members of this multicast group.
	AllRelayAgentsAndServersAddr = &net.IPAddr{
		IP: net.ParseIP("ff02::1:2"),
	}

	// AllServersAddr is the multicast address group which is used by a
	// DHCP relay agent to communicate with DHCP servers, if the relay agent
	// wishes to send messages to all servers, or does not know the unicast
	// address of a server.  All DHCP servers are members of this multicast
	// group.
	AllServersAddr = &net.IPAddr{
		IP: net.ParseIP("ff05::1:3"),
	}
)

Functions

func ListenAndServe

func ListenAndServe(iface string, handler Handler) error

ListenAndServe listens for UDP6 connections on the specified address of the specified interface, using the default Server configuration and specified handler to handle DHCPv6 connections. The Handler must not be nil.

Any traffic which reaches the Server, and is not bound for the specified network interface, will be filtered out and ignored.

In this configuration, the server acts as a DHCP server, but NOT as a DHCP relay agent. For more information on DHCP relay agents, see RFC 3315, Section 20.

Types

type Handler

type Handler interface {
	ServeDHCP(ResponseSender, *Request)
}

Handler provides an interface which allows structs to act as DHCPv6 server handlers. ServeDHCP implementations receive a copy of the incoming DHCP request via the Request parameter, and allow outgoing communication via the ResponseSender.

ServeDHCP implementations can choose to write a response packet using the ResponseSender interface, or choose to not write anything at all. If no packet is sent back to the client, it may choose to back off and retry, or attempt to pursue communication with other DHCP servers.

type HandlerFunc

type HandlerFunc func(ResponseSender, *Request)

HandlerFunc is an adapter type which allows the use of normal functions as DHCP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler struct that calls f.

func (HandlerFunc) ServeDHCP

func (f HandlerFunc) ServeDHCP(w ResponseSender, r *Request)

ServeDHCP calls f(w, r), allowing regular functions to implement Handler.

type PacketConn

type PacketConn interface {
	ReadFrom(b []byte) (n int, cm *ipv6.ControlMessage, src net.Addr, err error)
	WriteTo(b []byte, cm *ipv6.ControlMessage, dst net.Addr) (n int, err error)

	Close() error

	JoinGroup(ifi *net.Interface, group net.Addr) error
	LeaveGroup(ifi *net.Interface, group net.Addr) error

	SetControlMessage(cf ipv6.ControlFlags, on bool) error
}

PacketConn is an interface which types must implement in order to serve DHCP connections using Server.Serve.

type Request

type Request struct {
	// DHCP message type, such as Solicit, Request, or Renew.
	MessageType dhcp6.MessageType

	// Unique transaction ID, which should be preserved across
	// multiple requests to the same DHCP server.  ServeDHCP
	// implementations must manually verify that the same
	// transaction ID is used.
	TransactionID [3]byte

	// Map of options sent by client, carrying additional
	// information or requesting additional information from
	// the server.  Its methods can be used to check for and parse
	// additional information relating to a request.
	Options dhcp6.Options

	// Length of the DHCP request, in bytes.
	Length int64

	// Network address which was used to contact the DHCP server.
	RemoteAddr string
}

Request represents a processed DHCP request received by a server. Its struct members contain information regarding the request's message type, transaction ID, client ID, options, etc.

func ParseRequest

func ParseRequest(b []byte, remoteAddr *net.UDPAddr) (*Request, error)

ParseRequest creates a new Request from an input byte slice and UDP address. It populates the basic struct members which can be used in a DHCP handler.

If the input byte slice is not a valid DHCP packet, ErrInvalidPacket is returned.

type ResponseSender

type ResponseSender interface {
	// Options returns the Options map that will be sent to a client
	// after a call to Send.
	Options() dhcp6.Options

	// Send generates a DHCP response packet using the input message type
	// and any options set by Options.  Send returns the number of bytes
	// sent and any errors which occurred.
	Send(dhcp6.MessageType) (int, error)
}

ResponseSender provides an interface which allows a DHCP handler to construct and send a DHCP response packet. In addition, the server automatically handles copying certain options from a client Request to a ResponseSender's Options, including:

  • Client ID (OptionClientID)
  • Server ID (OptionServerID)

ResponseSender implementations should use the same transaction ID sent in a client Request.

type ServeMux

type ServeMux struct {
	// contains filtered or unexported fields
}

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.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux creates a new ServeMux which is ready to accept Handlers.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(mt dhcp6.MessageType, handler Handler)

Handle registers a MessageType and Handler with a ServeMux, so that future requests with that MessageType will invoke the Handler.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(mt dhcp6.MessageType, handler func(ResponseSender, *Request))

HandleFunc registers a MessageType and function as a HandlerFunc with a ServeMux, so that future requests with that MessageType will invoke the HandlerFunc.

func (*ServeMux) ServeDHCP

func (mux *ServeMux) ServeDHCP(w ResponseSender, r *Request)

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.

type Server

type Server struct {
	// Iface is the the network interface on which this server should
	// listen.  Traffic from any other network interface will be filtered out
	// and ignored by the server.
	Iface *net.Interface

	// Addr is the network address which this server should bind to.  The
	// default value is [::]:547, as specified in RFC 3315, Section 5.2.
	Addr string

	// Handler is the handler to use while serving DHCP requests.  If this
	// value is nil, the Server will panic.
	Handler Handler

	// MulticastGroups designates which IPv6 multicast groups this server
	// will join on start-up.  Because the default configuration acts as a
	// DHCP server, most servers will typically join both
	// AllRelayAgentsAndServersAddr, and AllServersAddr. If configuring a
	// DHCP relay agent, only the former value should be used.
	MulticastGroups []*net.IPAddr

	// ServerID is the the server's DUID, which uniquely identifies this
	// server to clients.  If no DUID is specified, a DUID-LL will be
	// generated using Iface's hardware type and address.  If possible,
	// servers with persistent storage available should generate a DUID-LLT
	// and store it for future use.
	ServerID dhcp6opts.DUID

	// ErrorLog is an optional logger which can be used to report errors and
	// erroneous behavior while the server is accepting client requests.
	// If ErrorLog is nil, logging goes to os.Stderr via the log package's
	// standard logger.
	ErrorLog *log.Logger
}

Server represents a DHCP server, and is used to configure a DHCP server's behavior.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe listens on the address specified by s.Addr using the network interface defined in s.Iface. Traffic from any other interface will be filtered out and ignored. Serve is called to handle serving DHCP traffic once ListenAndServe opens a UDP6 packet connection.

func (*Server) Serve

func (s *Server) Serve(p PacketConn) error

Serve configures and accepts incoming connections on PacketConn p, creating a new goroutine for each. Serve configures IPv6 control message settings, joins the appropriate multicast groups, and begins listening for incoming connections.

The service goroutine reads requests, generate the appropriate Request and ResponseSender values, then calls s.Handler to handle the request.

Notes

Bugs

  • determine if error can be temporary

  • consider using a sync.Pool with many buffers available to avoid allocating a new one on each connection

  • decide to log or handle other request errors

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL