dhcp6

package module
v0.0.0-...-269b0be Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2020 License: MIT Imports: 17 Imported by: 0

README

dhcp6 Build Status Coverage Status GoDoc

Package dhcp6 implements a DHCPv6 server, as described in IETF RFC 3315. MIT Licensed.

At this time, the API is not stable, and may change over time. The eventual goal is to implement a server, client, and testing facilities for consumers of this package.

The design of this package is inspired by Go's net/http package. The Go standard library is Copyright (c) 2012 The Go Authors. All rights reserved. The Go license can be found at https://golang.org/LICENSE.

Documentation

Overview

Package dhcp6 implements a DHCPv6 server, as described in RFC 3315.

Unless otherwise stated, any reference to "DHCP" in this package refers to DHCPv6 only.

Index

Constants

View Source
const (
	FQDNFlagS = 1
	FQDNFlagO = 2
	FQDNFlagN = 4
)

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"),
	}
)
View Source
var DefaultServeMux = NewServeMux()

DefaultServeMux is the default ServeMux used by Serve. When the Handle and HandleFunc functions are called, handlers are applied to DefaultServeMux.

View Source
var ErrHardwareTypeNotImplemented = errors.New("hardware type detection not implemented on this platform")

ErrHardwareTypeNotImplemented is returned when HardwareType is not implemented on the current platform.

View Source
var ErrInvalidDUIDLLTTime = errors.New("DUID-LLT time must be after midnight (UTC), January 1, 2000")

ErrInvalidDUIDLLTTime is returned when a time before midnight (UTC), January 1, 2000 is used in NewDUIDLLT.

View Source
var ErrInvalidIP = errors.New("IP must be an IPv6 address")

ErrInvalidIP is returned when an input net.IP value is not recognized as a valid IPv6 address.

View Source
var ErrInvalidLifetimes = errors.New("preferred lifetime must be less than valid lifetime")

ErrInvalidLifetimes is returned when an input preferred lifetime is shorter than a valid lifetime parameter.

View Source
var ErrInvalidPacket = errors.New("not enough bytes for valid packet")

ErrInvalidPacket is returned when a byte slice does not contain enough data to create a valid Packet. A Packet must have at least a message type and transaction ID.

View Source
var ErrParseHardwareType = errors.New("could not parse hardware type for interface")

ErrParseHardwareType is returned when a valid hardware type could not be found for a given interface.

Functions

func Handle

func Handle(mt MessageType, handler Handler)

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

func HandleFunc

func HandleFunc(mt MessageType, handler func(ResponseSender, *Request))

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

func HardwareType

func HardwareType(ifi *net.Interface) (uint16, error)

HardwareType returns the IANA ARP parameter Hardware Type value for an input network interface. A table of known values can be found here: http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml.

If an error occurs, a syscall error is returned. If no hardware type is found, ErrParseHardwareType is returned.

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. If the handler is nil, DefaultServeMux is used instead.

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 ArchType

type ArchType uint16

An ArchType is a client system architecture type, as defined in RFC 4578, Section 2.1. Though this RFC indicates these constants are for DHCPv4, they are carried over for use in DHCPv6 in RFC 5970, Section 3.3.

const (
	// RFC 4578
	ArchTypeIntelx86PC      ArchType = 0
	ArchTypeNECPC98         ArchType = 1
	ArchTypeEFIItanium      ArchType = 2
	ArchTypeDECAlpha        ArchType = 3
	ArchtypeArcx86          ArchType = 4
	ArchTypeIntelLeanClient ArchType = 5
	ArchTypeEFIIA32         ArchType = 6
	ArchTypeEFIBC           ArchType = 7
	ArchTypeEFIXscale       ArchType = 8
	ArchTypeEFIx8664        ArchType = 9
)

ArchType constants which indicate the client system architecture types described in RFC 4578, Section 2.1.

func (ArchType) String

func (i ArchType) String() string

type ArchTypes

type ArchTypes []ArchType

ArchTypes is a slice of ArchType values. It is provided for convenient marshaling and unmarshaling of a slice of ArchType values from an Options map.

func (ArchTypes) MarshalBinary

func (a ArchTypes) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from ArchTypes.

func (*ArchTypes) UnmarshalBinary

func (a *ArchTypes) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into an ArchTypes slice.

If the byte slice is less than 2 bytes in length, or is not a length that is divisible by 2, io.ErrUnexpectedEOF is returned.

type Authentication

type Authentication struct {
	// The authentication protocol used in this authentication option
	Protocol byte

	// The algorithm used in the authentication protocol
	Algorithm byte

	// The replay detection method used in this authentication option
	RDM byte

	// The replay detection information for the RDM
	ReplayDetection uint64

	// The authentication information,
	// as specified by the protocol and
	// algorithm used in this authentication
	// option
	AuthenticationInformation []byte
}

The Authentication option carries authentication information to authenticate the identity and contents of DHCP messages. The use of the Authentication option is described in section 21.

func (*Authentication) MarshalBinary

func (a *Authentication) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a Authentication.

func (*Authentication) UnmarshalBinary

func (a *Authentication) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a Authentication. If the byte slice does not contain enough data to form a valid Authentication, io.ErrUnexpectedEOF is returned.

type DNSNames

type DNSNames []string

func (DNSNames) MarshalBinary

func (d DNSNames) MarshalBinary() ([]byte, error)

func (*DNSNames) UnmarshalBinary

func (d *DNSNames) UnmarshalBinary(b []byte) error

type DNSServers

type DNSServers []net.IP

func (DNSServers) MarshalBinary

func (d DNSServers) MarshalBinary() ([]byte, error)

func (*DNSServers) UnmarshalBinary

func (d *DNSServers) UnmarshalBinary(b []byte) error

type DUID

DUID represents a DHCP Unique Identifier, as defined in RFC 3315, Section 9. A DUID is used by a DHCP server to identify unique clients. A DUID can also be used by a DHCP client to identify a unique server, when needed.

The DUID interface represents a generic DUID, but DUIDs can be type-asserted to one of four specific types outlined in RFC 3315 and RFC 6355:

  • DUIDLLT - DUID Based on Link-layer Address Plus Time
  • DUIDEN - DUID Assigned by Vendor Based on Enterprise Number
  • DUIDLL - DUID Based on Link-layer Address
  • DUIDUUID - DUID Based on Universally Unique Identifier

If further introspection of the DUID is needed, a type switch is recommended:

switch d := duid.(type) {
case *dhcp6.DUIDLLT:
	fmt.Println(d.Time)
case *dhcp6.DUIDEN:
	fmt.Println(d.EnterpriseNumber)
case *dhcp6.DUIDLL:
	fmt.Println(d.HardwareAddr)
case *dhcp6.DUIDUUID:
	fmt.Println(d.UUID)
}

type DUIDEN

type DUIDEN struct {
	// Type specifies the DUID type.  For a DUIDEN, this should always be
	// DUIDTypeEN.
	Type DUIDType

	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// Identifier specifies a unique identifier of arbitrary length.  This
	// value is typically assigned when a device is manufactured.
	Identifier []byte
}

DUIDEN represents a DUID Assigned by Vendor Based on Enterprise Number [DUID-EN], as defined in RFC 3315, Section 9.3. This DUID type uses an IANA-assigned Private Enterprise Number for a given vendor.

func NewDUIDEN

func NewDUIDEN(enterpriseNumber uint32, identifier []byte) *DUIDEN

NewDUIDEN generates a new DUIDEN from an input IANA-assigned Private Enterprise Number and a variable length unique identifier byte slice.

func (*DUIDEN) MarshalBinary

func (d *DUIDEN) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a DUIDEN.

func (*DUIDEN) UnmarshalBinary

func (d *DUIDEN) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a DUIDEN. If the byte slice does not contain enough data to form a valid DUIDEN, or another DUID type is indicated, errInvalidDUIDEN is returned.

type DUIDLL

type DUIDLL struct {
	// Type specifies the DUID type.  For a DUIDLL, this should always be
	// DUIDTypeLL.
	Type DUIDType

	// HardwareType specifies an IANA-assigned hardware type, as described
	// in RFC 826.
	HardwareType uint16

	// HardwareAddr specifies the hardware address for an arbitrary link-layer
	// interface on a device, used in generating the DUIDLL.  This value
	// could represent any arbitrary interface on a system, and should not be
	// treated as a client or server's communicating hardware address.
	HardwareAddr net.HardwareAddr
}

DUIDLL represents a DUID Based on Link-layer Address [DUID-LL], as defined in RFC 3315, Section 9.4.

This DUID type is recommended for devices with a permanently-connected network interface, but without stable, persistent storage.

DUIDLL values are generated automatically for Servers which are not created with a ServerID, using the hardware type found by HardwareType and the hardware address of the listening network interface.

func NewDUIDLL

func NewDUIDLL(hardwareType uint16, hardwareAddr net.HardwareAddr) *DUIDLL

NewDUIDLL generates a new DUIDLL from an input IANA-assigned hardware type and a hardware address.

func (*DUIDLL) MarshalBinary

func (d *DUIDLL) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a DUIDLL.

func (*DUIDLL) UnmarshalBinary

func (d *DUIDLL) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a DUIDLL. If the byte slice does not contain enough data to form a valid DUIDLL, or another DUID type is indicated, errInvalidDUIDLL is returned.

type DUIDLLT

type DUIDLLT struct {
	// Type specifies the DUID type.  For a DUIDLLT, this should always be
	// DUIDTypeLLT.
	Type DUIDType

	// HardwareType specifies an IANA-assigned hardware type, as described
	// in RFC 826.
	HardwareType uint16

	// Time specifies the duration of the time this DUID was generated, minus
	// midnight (UTC), January 1, 2000.
	Time time.Duration

	// HardwareAddr specifies the hardware address for an arbitrary link-layer
	// interface on a device, used in generating the DUIDLLT.  This value
	// could represent any arbitrary interface on a system, and should not be
	// treated as a client or server's communicating hardware address.
	HardwareAddr net.HardwareAddr
}

DUIDLLT represents a DUID Based on Link-layer Address Plus Time [DUID-LLT], as defined in RFC 3315, Section 9.2.

This DUID type must only be used with clients and servers with stable, persistent storage. It is the recommended DUID type for all general purpose computing devices.

func NewDUIDLLT

func NewDUIDLLT(hardwareType uint16, time time.Time, hardwareAddr net.HardwareAddr) (*DUIDLLT, error)

NewDUIDLLT generates a new DUIDLLT from an input IANA-assigned hardware type, time value, and a hardware address.

The time value must be greater than midnight (UTC), January 1, 2000.

func (*DUIDLLT) MarshalBinary

func (d *DUIDLLT) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a DUIDLLT.

func (*DUIDLLT) UnmarshalBinary

func (d *DUIDLLT) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a DUIDLLT. If the byte slice does not contain enough data to form a valid DUIDLLT, or another DUID type is indicated, errInvalidDUIDLLT is returned.

type DUIDType

type DUIDType uint16

DUIDType is a type of DHCP Unique Identifier, as defined in RFC 3315, Section 9. DUIDs are used to uniquely identify a client to a server, or vice-versa.

const (
	// RFC 3315
	DUIDTypeLLT DUIDType = 1
	DUIDTypeEN  DUIDType = 2
	DUIDTypeLL  DUIDType = 3

	// RFC 6355
	DUIDTypeUUID DUIDType = 4
)

DUIDType constants which indicate DUID types described in RFCs 3315 and 6355.

These DUID types are taken from IANA's DHCPv6 parameters registry: http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml.

func (DUIDType) String

func (i DUIDType) String() string

type DUIDUUID

type DUIDUUID struct {
	// Type specifies the DUID type.  For a DUIDUUID, this should always be
	// DUIDTypeUUID.
	Type DUIDType

	// UUID specifies a Universally Unique Identifier, as described in RFC 4578.
	UUID [16]byte
}

DUIDUUID represents a DUID based on Universally Unique Identifier [DUID-UUID], as defined in RFC 6355. This DUID type uses a UUID to identify clients or servers.

func NewDUIDUUID

func NewDUIDUUID(uuid [16]byte) *DUIDUUID

NewDUIDUUID generates a new DUIDUUID using an input UUID.

func (*DUIDUUID) MarshalBinary

func (d *DUIDUUID) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a DUIDUUID.

func (*DUIDUUID) UnmarshalBinary

func (d *DUIDUUID) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a DUIDUUID. If the byte slice does not contain the exact number of bytes needed to form a valid DUIDUUID, or another DUID type is indicated, errInvalidDUIDUUID is returned.

type Data

type Data [][]byte

Data is a raw collection of byte slices, typically carrying user class data, vendor class data, or PXE boot file parameters.

func (Data) MarshalBinary

func (d Data) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a Data structure.

func (*Data) UnmarshalBinary

func (d *Data) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a Data structure. Data is packed in the form:

  • 2 bytes: data length
  • N bytes: raw data

type ElapsedTime

type ElapsedTime time.Duration

An ElapsedTime is a client's elapsed request time value, as defined in RFC 3315, Section 22.9.

The duration returned reports the time elapsed during a DHCP transaction, as reported by a client.

func (ElapsedTime) MarshalBinary

func (t ElapsedTime) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from an ElapsedTime.

func (*ElapsedTime) UnmarshalBinary

func (t *ElapsedTime) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a ElapsedTime.

If the byte slice is not exactly 2 bytes in length, io.ErrUnexpectedEOF is returned.

type FQDN

type FQDN struct {
	Flags uint8
	Name  string
}

func (FQDN) MarshalBinary

func (f FQDN) MarshalBinary() ([]byte, error)

func (FQDN) String

func (f FQDN) String() string

func (*FQDN) UnmarshalBinary

func (f *FQDN) UnmarshalBinary(b []byte) error

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 IAAddr

type IAAddr struct {
	// IP specifies the IPv6 address to offer to a client.  The validity of the
	// address is controlled by the PreferredLifetime and ValidLifetime fields.
	IP net.IP

	// PreferredLifetime specifies the preferred lifetime of an IPv6 address.
	// When the preferred lifetime of an address expires, the address becomes
	// deprecated, and should not be used in new communications.
	//
	// The preferred lifetime of an address must not be greater than its
	// valid lifetime.
	PreferredLifetime time.Duration

	// ValidLifetime specifies the valid lifetime of an IPv6 address.  When the
	// valid lifetime of an address expires, the address should not be used for
	// any further communication.
	//
	// The valid lifetime of an address must be greater than its preferred
	// lifetime.
	ValidLifetime time.Duration

	// Options specifies a map of DHCP options specific to this IAAddr.
	// Its methods can be used to retrieve data from an incoming IAAddr, or
	// send data with an outgoing IAAddr.
	Options Options
}

IAAddr represents an Identity Association Address, as defined in RFC 3315, Section 22.6.

DHCP clients use identity assocation addresses (IAAddrs) to request IPv6 addresses from a DHCP server, using the lifetimes specified in the preferred lifetime and valid lifetime fields. Multiple IAAddrs may be present in a single DHCP request, but only enscapsulated within an IANA or IATA options field.

func NewIAAddr

func NewIAAddr(ip net.IP, preferred time.Duration, valid time.Duration, options Options) (*IAAddr, error)

NewIAAddr creates a new IAAddr from an IPv6 address, preferred and valid lifetime durations, and an optional Options map.

The IP must be exactly 16 bytes, the correct length for an IPv6 address. The preferred lifetime duration must be less than the valid lifetime duration. Failure to meet either of these conditions will result in an error. If an Options map is not specified, a new one will be allocated.

func (*IAAddr) MarshalBinary

func (i *IAAddr) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IAAddr.

func (*IAAddr) UnmarshalBinary

func (i *IAAddr) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a IAAddr.

If the byte slice does not contain enough data to form a valid IAAddr, io.ErrUnexpectedEOF is returned. If the preferred lifetime value in the byte slice is less than the valid lifetime, ErrInvalidLifetimes is returned.

type IANA

type IANA struct {
	// IAID specifies a DHCP identity association identifier.  The IAID
	// is a unique, client-generated identifier.
	IAID [4]byte

	// T1 specifies how long a DHCP client will wait to contact this server,
	// to extend the lifetimes of the addresses assigned to this IANA
	// by this server.
	T1 time.Duration

	// T2 specifies how long a DHCP client will wait to contact any server,
	// to extend the lifetimes of the addresses assigned to this IANA
	// by this server.
	T2 time.Duration

	// Options specifies a map of DHCP options specific to this IANA.
	// Its methods can be used to retrieve data from an incoming IANA, or send
	// data with an outgoing IANA.
	Options Options
}

IANA represents an Identity Association for Non-temporary Addresses, as defined in RFC 3315, Section 22.4.

Multiple IANAs may be present in a single DHCP request.

func NewIANA

func NewIANA(iaid [4]byte, t1 time.Duration, t2 time.Duration, options Options) *IANA

NewIANA creates a new IANA from an IAID, T1 and T2 durations, and an Options map. If an Options map is not specified, a new one will be allocated.

func (*IANA) MarshalBinary

func (i *IANA) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IANA.

func (*IANA) UnmarshalBinary

func (i *IANA) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a IANA.

If the byte slice does not contain enough data to form a valid IANA, io.ErrUnexpectedEOF is returned.

type IAPD

type IAPD struct {
	// IAID specifies a DHCP identity association identifier.  The IAID
	// is a unique, client-generated identifier.
	IAID [4]byte

	// T1 specifies how long a requesting router will wait to contact a
	// delegating router, to extend the lifetimes of the prefixes delegated
	// to this IAPD, by the delegating router.
	T1 time.Duration

	// T2 specifies how long a requesting router will wait to contact any
	// available delegating router, to extend the lifetimes of the prefixes
	// delegated to this IAPD.
	T2 time.Duration

	// Options specifies a map of DHCP options specific to this IAPD.
	// Its methods can be used to retrieve data from an incoming IAPD, or send
	// data with an outgoing IAPD.
	Options Options
}

IAPD represents an Identity Association for Prefix Delegation, as defined in RFC 3633, Section 9.

Multiple IAPDs may be present in a single DHCP request.

func NewIAPD

func NewIAPD(iaid [4]byte, t1 time.Duration, t2 time.Duration, options Options) *IAPD

NewIAPD creates a new IAPD from an IAID, T1 and T2 durations, and an Options map. If an Options map is not specified, a new one will be allocated.

func (*IAPD) MarshalBinary

func (i *IAPD) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IAPD.

func (*IAPD) UnmarshalBinary

func (i *IAPD) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a IAPD.

If the byte slice does not contain enough data to form a valid IAPD, io.ErrUnexpectedEOF is returned.

type IAPrefix

type IAPrefix struct {
	// PreferredLifetime specifies the preferred lifetime of an IPv6 prefix.
	// When the preferred lifetime of a prefix expires, the prefix becomes
	// deprecated, and addresses from the prefix should not be used in new
	// communications.
	//
	// The preferred lifetime of a prefix must not be greater than its valid
	// lifetime.
	PreferredLifetime time.Duration

	// ValidLifetime specifies the valid lifetime of an IPv6 prefix.  When the
	// valid lifetime of a prefix expires, addresses from the prefix the address
	// should not be used for any further communication.
	//
	// The valid lifetime of a prefix must be greater than its preferred
	// lifetime.
	ValidLifetime time.Duration

	// PrefixLength specifies the length in bits of an IPv6 address prefix, such
	// as 32, 64, etc.
	PrefixLength uint8

	// Prefix specifies the IPv6 address prefix from which IPv6 addresses can
	// be allocated.
	Prefix net.IP

	// Options specifies a map of DHCP options specific to this IAPrefix.
	// Its methods can be used to retrieve data from an incoming IAPrefix, or
	// send data with an outgoing IAPrefix.
	Options Options
}

IAPrefix represents an Identity Association Prefix, as defined in RFC 3633, Section 10.

Routers may use identity assocation prefixes (IAPrefixes) to request IPv6 prefixes to assign individual address to IPv6 clients, using the lifetimes specified in the preferred lifetime and valid lifetime fields. Multiple IAPrefixes may be present in a single DHCP request, but only enscapsulated within an IAPD's options.

func NewIAPrefix

func NewIAPrefix(preferred time.Duration, valid time.Duration, prefixLength uint8, prefix net.IP, options Options) (*IAPrefix, error)

NewIAPrefix creates a new IAPrefix from preferred and valid lifetime durations, an IPv6 prefix length, an IPv6 prefix, and an optional Options map.

The preferred lifetime duration must be less than the valid lifetime duration. The IPv6 prefix must be exactly 16 bytes, the correct length for an IPv6 address. Failure to meet either of these conditions will result in an error. If an Options map is not specified, a new one will be allocated.

func (*IAPrefix) MarshalBinary

func (i *IAPrefix) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IAPrefix.

func (*IAPrefix) UnmarshalBinary

func (i *IAPrefix) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a IAPrefix.

If the byte slice does not contain enough data to form a valid IAPrefix, io.ErrUnexpectedEOF is returned. If the preferred lifetime value in the byte slice is less than the valid lifetime, ErrInvalidLifetimes is returned.

type IATA

type IATA struct {
	// IAID specifies a DHCP identity association identifier.  The IAID
	// is a unique, client-generated identifier.
	IAID [4]byte

	// Options specifies a map of DHCP options specific to this IATA.
	// Its methods can be used to retrieve data from an incoming IATA, or send
	// data with an outgoing IATA.
	Options Options
}

IATA represents an Identity Association for Temporary Addresses, as defined in RFC 3315, Section 22.5.

Multiple IATAs may be present in a single DHCP request.

func NewIATA

func NewIATA(iaid [4]byte, options Options) *IATA

NewIATA creates a new IATA from an IAID and an Options map. If an Options map is not specified, a new one will be allocated.

func (*IATA) MarshalBinary

func (i *IATA) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IATA.

func (*IATA) UnmarshalBinary

func (i *IATA) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a IATA.

If the byte slice does not contain enough data to form a valid IATA, io.ErrUnexpectedEOF is returned.

type IP

type IP net.IP

An IP is an IPv6 address. The IP type is provided for convenience. It can be used to easily add IPv6 addresses to an Options map.

func (IP) MarshalBinary

func (i IP) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a IP.

func (*IP) UnmarshalBinary

func (i *IP) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into an IP.

If the byte slice is not an IPv6 address, io.ErrUnexpectedEOF is returned.

type InterfaceID

type InterfaceID []byte

An InterfaceID is an opaque value of arbitrary length generated by the relay agent to identify one of the relay agent's interfaces.

func (*InterfaceID) MarshalBinary

func (i *InterfaceID) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a InterfaceID.

func (*InterfaceID) UnmarshalBinary

func (i *InterfaceID) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a InterfaceID.

type MessageType

type MessageType uint8

MessageType represents a DHCP message type, as defined in RFC 3315, Section 5.3. Different DHCP message types are used to perform different actions between a client and server.

const (
	// RFC 3315
	MessageTypeSolicit            MessageType = 1
	MessageTypeAdvertise          MessageType = 2
	MessageTypeRequest            MessageType = 3
	MessageTypeConfirm            MessageType = 4
	MessageTypeRenew              MessageType = 5
	MessageTypeRebind             MessageType = 6
	MessageTypeReply              MessageType = 7
	MessageTypeRelease            MessageType = 8
	MessageTypeDecline            MessageType = 9
	MessageTypeReconfigure        MessageType = 10
	MessageTypeInformationRequest MessageType = 11
	MessageTypeRelayForw          MessageType = 12
	MessageTypeRelayRepl          MessageType = 13

	// RFC 5007
	MessageTypeLeasequery      MessageType = 14
	MessageTypeLeasequeryReply MessageType = 15

	// RFC 5460
	MessageTypeLeasequeryDone MessageType = 16
	MessageTypeLeasequeryData MessageType = 17

	// RFC 6977
	MessageTypeReconfigureRequest MessageType = 18
	MessageTypeReconfigureReply   MessageType = 19

	// RFC 7341
	MessageTypeDHCPv4Query    MessageType = 20
	MessageTypeDHCPv4Response MessageType = 21
)

MessageType constants which indicate the message types described in RFCs 3315, 5007, 5460, 6977, and 7341.

These message types are taken from IANA's DHCPv6 parameters registry: http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml.

func (MessageType) String

func (i MessageType) String() string

type NII

type NII struct {
	// Type specifies a network interface type.
	Type uint8

	// Major specifies the UNDI major revisision which this client supports.
	Major uint8

	// Minor specifies the UNDI minor revision which this client supports.
	Minor uint8
}

A NII is a Client Network Interface Identifier, as defined in RFC 5970, Section 3.4.

A NII is used to indicate a client's level of Universal Network Device Interface (UNDI) support.

func (*NII) MarshalBinary

func (n *NII) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a NII.

func (*NII) UnmarshalBinary

func (n *NII) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a NII.

If the byte slice is not exactly 3 bytes in length, io.ErrUnexpectedEOF is returned.

type OptionCode

type OptionCode uint16

OptionCode represents a DHCP option, as defined in RFC 3315, Section 22. Options are used to carry additional information and parameters in DHCP messages between client and server.

const (
	// RFC 3315
	OptionClientID    OptionCode = 1
	OptionServerID    OptionCode = 2
	OptionIANA        OptionCode = 3
	OptionIATA        OptionCode = 4
	OptionIAAddr      OptionCode = 5
	OptionORO         OptionCode = 6
	OptionPreference  OptionCode = 7
	OptionElapsedTime OptionCode = 8
	OptionRelayMsg    OptionCode = 9

	OptionAuth         OptionCode = 11
	OptionUnicast      OptionCode = 12
	OptionStatusCode   OptionCode = 13
	OptionRapidCommit  OptionCode = 14
	OptionUserClass    OptionCode = 15
	OptionVendorClass  OptionCode = 16
	OptionVendorOpts   OptionCode = 17
	OptionInterfaceID  OptionCode = 18
	OptionReconfMsg    OptionCode = 19
	OptionReconfAccept OptionCode = 20

	// rfc3646
	OptionDNSServers OptionCode = 23
	OptionDomainList OptionCode = 24

	// RFC 3633
	OptionIAPD     OptionCode = 25
	OptionIAPrefix OptionCode = 26

	// rfc4701
	OptionClientFQDN OptionCode = 39

	// RFC 4649
	OptionRemoteIdentifier OptionCode = 37

	// RFC 5970
	OptionBootFileURL    OptionCode = 59
	OptionBootFileParam  OptionCode = 60
	OptionClientArchType OptionCode = 61
	OptionNII            OptionCode = 62
)

OptionCode constants which indicate the option codes described in RFC 3315, RFC 3633, and RFC 5970.

These option codes are taken from IANA's DHCPv6 parameters registry: http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml.

func (OptionCode) String

func (i OptionCode) String() string

type OptionRequestOption

type OptionRequestOption []OptionCode

A OptionRequestOption is a list OptionCode, as defined in RFC 3315, Section 22.7.

The Option Request option is used to identify a list of options in a message between a client and a server.

func (OptionRequestOption) MarshalBinary

func (oro OptionRequestOption) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a OptionRequestOption.

func (*OptionRequestOption) UnmarshalBinary

func (oro *OptionRequestOption) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a OptionRequestOption.

If the length of byte slice is not be be divisible by 2, errInvalidOptionRequest is returned.

type Options

type Options map[OptionCode][][]byte

Options is a map of OptionCode keys with a slice of byte slice values. Its methods can be used to easily check for and parse additional information from a client request. If raw data is needed, the map can be accessed directly.

func (Options) Add

func (o Options) Add(key OptionCode, value encoding.BinaryMarshaler) error

Add adds a new OptionCode key and BinaryMarshaler struct's bytes to the Options map.

func (Options) Authentication

func (o Options) Authentication() (*Authentication, bool, error)

Authentication returns the Authentication Option value, as described in RFC 3315, Section 22.11.

The Authentication option carries authentication information to authenticate the identity and contents of DHCP messages.

The boolean return value indicates if Authentication was present in the Options map. The error return value indicates if a valid authentication could be parsed from the option.

func (Options) BootFileParam

func (o Options) BootFileParam() (Data, bool, error)

BootFileParam returns the Boot File Parameters Option value, described in RFC 5970, Section 3.2.

The Data structure returned contains any parameters needed for a boot file, such as a root filesystem label or a path to a configuration file for further chainloading.

The boolean return value indicates if OptionBootFileParam was present in the Options map. The error return value indicates if valid boot file parameters could not be parsed from the option.

func (Options) BootFileURL

func (o Options) BootFileURL() (*URL, bool, error)

BootFileURL returns the Boot File URL Option value, described in RFC 5970, Section 3.1.

The URL return value contains a URL which may be used by clients to obtain a boot file for PXE.

The boolean return value indicates if OptionBootFileURL was present in the Options map. The error return value indicates if a valid boot file URL could not be parsed from the option.

func (Options) ClientArchType

func (o Options) ClientArchType() (ArchTypes, bool, error)

ClientArchType returns the Client System Architecture Type Option value, described in RFC 5970, Section 3.3.

The ArchTypes slice returned contains a list of one or more ArchType values. The first ArchType listed is the client's most preferable value.

The boolean return value indicates if OptionClientArchType was present in the Options map. The error return value indicates if a valid list of ArchType values could not be parsed from the option.

func (Options) ClientFQDN

func (o Options) ClientFQDN() (FQDN, bool, error)

func (Options) ClientID

func (o Options) ClientID() (DUID, bool, error)

ClientID returns the Client Identifier Option value, as described in RFC 3315, Section 22.2.

The DUID returned allows unique identification of a client to a server.

The boolean return value indicates if OptionClientID was present in the Options map. The error return value indicates if a known, valid DUID type could be parsed from the option.

func (Options) ElapsedTime

func (o Options) ElapsedTime() (ElapsedTime, bool, error)

ElapsedTime returns the Elapsed Time Option value, as described in RFC 3315, Section 22.9.

The time.Duration returned reports the time elapsed during a DHCP transaction, as reported by a client.

The boolean return value indicates if OptionElapsedTime was present in the Options map. The error return value indicates if a valid duration could be parsed from the option.

func (Options) Get

func (o Options) Get(key OptionCode) ([]byte, bool)

Get attempts to retrieve the first value specified by an OptionCode key. If a value is found, get returns the value and boolean true. If it is not found, Get returns nil and boolean false.

func (Options) IAAddr

func (o Options) IAAddr() ([]*IAAddr, bool, error)

IAAddr returns the Identity Association Address Option value, as described in RFC 3315, Section 22.6.

The IAAddr option must always appear encapsulated in the Options map of a IANA or IATA option. Multiple IAAddr values may be present in a single DHCP request.

The boolean return value indicates if OptionIAAddr was present in the Options map. The error return value indicates if one or more valid IAAddrs could not be parsed from the option.

func (Options) IANA

func (o Options) IANA() ([]IANA, bool, error)

IANA returns the Identity Association for Non-temporary Addresses Option value, as described in RFC 3315, Section 22.4.

Multiple IANA values may be present in a single DHCP request.

The boolean return value indicates if OptionIANA was present in the Options map. The error return value indicates if one or more valid IANAs could not be parsed from the option.

func (Options) IAPD

func (o Options) IAPD() ([]*IAPD, bool, error)

IAPD returns the Identity Association for Prefix Delegation Option value, described in RFC 3633, Section 9.

Multiple IAPD values may be present in a a single DHCP request.

The boolean return value indicates if OptionIAPD was present in the Options map. The error return value indicates if one or more valid IAPDs could not be parsed from the option.

func (Options) IAPrefix

func (o Options) IAPrefix() ([]*IAPrefix, bool, error)

IAPrefix returns the Identity Association Prefix Option value, as described in RFC 3633, Section 10.

Multiple IAPrefix values may be present in a a single DHCP request.

The boolean return value indicates if OptionIAPrefix was present in the Options map. The error return value indicates if one or more valid IAPrefixes could not be parsed from the option.

func (Options) IATA

func (o Options) IATA() ([]*IATA, bool, error)

IATA returns the Identity Association for Temporary Addresses Option value, as described in RFC 3315, Section 22.5.

Multiple IATA values may be present in a single DHCP request.

The boolean return value indicates if OptionIATA was present in the Options map. The error return value indicates if one or more valid IATAs could not be parsed from the option.

func (Options) InterfaceID

func (o Options) InterfaceID() (InterfaceID, bool, error)

InterfaceID returns the Interface-Id Option value, described in RFC 3315, Section 22.18.

The InterfaceID structure returned contains any raw class data present in the option.

The boolean return value indicates if InterfaceID was present in the Options map. The error return value indicates if any errors were present in the interface-id data.

func (Options) NII

func (o Options) NII() (*NII, bool, error)

NII returns the Client Network Interface Identifier Option value, described in RFC 5970, Section 3.4.

The NII value returned indicates a client's level of Universal Network Device Interface (UNDI) support.

The boolean return value indicates if OptionNII was present in the Options map. The error return value indicates if a valid list of ArchType values could not be parsed from the option.

func (Options) OptionRequest

func (o Options) OptionRequest() (OptionRequestOption, bool, error)

OptionRequest returns the Option Request Option value, as described in RFC 3315, Section 22.7.

The slice of OptionCode values indicates the options a DHCP client is interested in receiving from a server.

The boolean return value indicates if OptionORO was present in the Options map. The error return value indicates if a valid OptionCode slice could be parsed from the option.

func (Options) Preference

func (o Options) Preference() (Preference, bool, error)

Preference returns the Preference Option value, as described in RFC 3315, Section 22.8.

The integer preference value is sent by a server to a client to affect the selection of a server by the client.

The boolean return value indicates if OptionPreference was present in the Options map. The error return value indicates if a valid integer value could not be parsed from the option.

func (Options) RapidCommit

func (o Options) RapidCommit() (bool, error)

RapidCommit returns the Rapid Commit Option value, described in RFC 3315, Section 22.14.

The boolean return value indicates if OptionRapidCommit was present in the Options map, and thus, if Rapid Commit should be used.

The error return value indicates if a valid Rapid Commit Option could not be parsed.

func (Options) RelayMessage

func (o Options) RelayMessage() (*Packet, bool, error)

func (Options) RelayMessageOption

func (o Options) RelayMessageOption() (RelayMessageOption, bool, error)

RelayMessageOption returns the Relay Message Option value, as described in RFC 3315, Section 22.10.

The RelayMessage option carries a DHCP message in a Relay-forward or Relay-reply message.

The boolean return value indicates if OptionRelayMsg was present in the Options map. The error return value indicates if a valid OptionRelayMsg could be parsed from the option.

func (Options) RemoteIdentifier

func (o Options) RemoteIdentifier() (*RemoteIdentifier, bool, error)

RemoteIdentifier returns the Remote Identifier, described in RFC 4649.

This option may be added by DHCPv6 relay agents that terminate switched or permanent circuits and have mechanisms to identify the remote host end of the circuit.

The boolean return value indicates if OptionRemoteIdentifier was present in the Options map. The error return value indicates if any errors were present in the class data.

func (Options) ServerID

func (o Options) ServerID() (DUID, bool, error)

ServerID returns the Server Identifier Option value, as described in RFC 3315, Section 22.3.

The DUID returned allows unique identification of a server to a client.

The boolean return value indicates if OptionServerID was present in the Options map. The error return value indicates if a known, valid DUID type could be parsed from the option.

func (Options) StatusCode

func (o Options) StatusCode() (*StatusCode, bool, error)

StatusCode returns the Status Code Option value, described in RFC 3315, Section 22.13.

The StatusCode return value may be used to determine a code and an explanation for the status.

The boolean return value indicates if OptionStatusCode was present in the Options map. The error return value indicates if a valid StatusCode could not be parsed from the option.

func (Options) Unicast

func (o Options) Unicast() (IP, bool, error)

Unicast returns the IP from a Unicast Option value, described in RFC 3315, Section 22.12.

The IP return value indicates a server's IPv6 address, which a client may use to contact the server via unicast.

The boolean return value indicates if OptionUnicast was present in the Options map. The error return value indicates if a valid IPv6 address could not be parsed from the option.

func (Options) UserClass

func (o Options) UserClass() (Data, bool, error)

UserClass returns the User Class Option value, described in RFC 3315, Section 22.15.

The Data structure returned contains any raw class data present in the option.

The boolean return value indicates if OptionUserClass was present in the Options map. The error return value indicates if any errors were present in the class data.

func (Options) VendorClass

func (o Options) VendorClass() (*VendorClass, bool, error)

VendorClass returns the Vendor Class Option value, described in RFC 3315, Section 22.16.

The VendorClass structure returned contains VendorClass in the option.

The boolean return value indicates if OptionVendorClass was present in the Options map. The error return value indicates if any errors were present in the VendorClass data.

func (Options) VendorOpts

func (o Options) VendorOpts() (*VendorOpts, bool, error)

VendorOpts returns the Vendor-specific Information Option value, described in RFC 3315, Section 22.17.

The VendorOpts structure returned contains Vendor-specific Information data present in the option.

The boolean return value indicates if VendorOpts was present in the Options map. The error return value indicates if any errors were present in the class data.

type Packet

type Packet struct {
	// MessageType specifies the DHCP message type constant, such as
	// MessageTypeSolicit, MessageTypeAdvertise, etc.
	MessageType MessageType

	// TransactionID specifies the DHCP transaction ID.  The transaction ID must
	// be the same for all message exchanges in one DHCP transaction.
	TransactionID [3]byte

	HopCount                 uint8
	LinkAddress, PeerAddress net.IP

	// Options specifies a map of DHCP options.  Its methods can be used to
	// retrieve data from an incoming packet, or send data with an outgoing
	// packet.
	Options Options
}

Packet represents a raw DHCPv6 packet, using the format described in RFC 3315, Section 6.

The Packet type is typically only needed for low-level operations within the client, server, or in tests.

func (*Packet) MarshalBinary

func (p *Packet) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a Packet.

func (*Packet) UnmarshalBinary

func (p *Packet) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a Packet.

If the byte slice does not contain enough data to form a valid Packet, ErrInvalidPacket is returned.

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 Preference

type Preference uint8

A Preference is a preference value, as defined in RFC 3315, Section 22.8.

A preference value is sent by a server to a client to affect the selection of a server by the client.

func (Preference) MarshalBinary

func (p Preference) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a Preference.

func (*Preference) UnmarshalBinary

func (p *Preference) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a Preference.

If the byte slice is not exactly 1 byte in length, io.ErrUnexpectedEOF is returned.

type RelayMessage

type RelayMessage struct {
	// RELAY-FORW or RELAY-REPL only
	MessageType MessageType

	// Number of relay agents that have relayed this
	// message.
	HopCount uint8

	// A global or site-local address that will be used by
	// the server to identify the link on which the client
	// is located.
	LinkAddress net.IP

	// The address of the client or relay agent from which
	// the message to be relayed was received.
	PeerAddress net.IP

	// Options specifies a map of DHCP options.  Its methods can be used to
	// retrieve data from an incoming RelayMessage, or send data with an outgoing
	// RelayMessage.
	// MUST include a "Relay Message option" (see
	// section 22.10); MAY include other options added by
	// the relay agent.
	Options Options
}

RelayMessage represents a raw RelayMessage generated by DHCPv6 relay agent, using RFC 3315, Section 7.

func (*RelayMessage) MarshalBinary

func (p *RelayMessage) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a RelayMessage.

func (*RelayMessage) UnmarshalBinary

func (p *RelayMessage) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a RelayMessage.

If the byte slice does not contain enough data to form a valid RelayMessage, ErrInvalidPacket is returned.

type RelayMessageOption

type RelayMessageOption []byte

A DHCPv6 Relay Agent relays messages between clients and servers or other relay agents through Relay-Forward and Relay-Reply message types. The original client DHCP message (i.e., the packet payload, excluding UDP and IP headers) is encapsulated in a Relay Message option

func (*RelayMessageOption) ClientServerMessage

func (r *RelayMessageOption) ClientServerMessage() (*Packet, error)

ClientServerMessage gets the client server message (e.g. Solicit, Advertise ...) into this option (when hopcount = 0 of outer RelayMessage).

func (*RelayMessageOption) MarshalBinary

func (r *RelayMessageOption) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a RelayMessageOption.

func (*RelayMessageOption) RelayMessage

func (r *RelayMessageOption) RelayMessage() (*RelayMessage, error)

RelayMessage gets the relay message (e.g. Relay Forward, Relay Reply) into this option (when hopcount > 0 of outer RelayMessage).

func (*RelayMessageOption) SetClientServerMessage

func (r *RelayMessageOption) SetClientServerMessage(p *Packet) error

Set Client Server Message (e.g. Solicit, Advertise ...) into this option.

func (*RelayMessageOption) SetRelayMessage

func (r *RelayMessageOption) SetRelayMessage(p *RelayMessage) error

Set RelayMessage (e.g. Relay Forward, Relay Reply) into this option.

func (*RelayMessageOption) UnmarshalBinary

func (r *RelayMessageOption) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a RelayMessageOption.

type RemoteIdentifier

type RemoteIdentifier struct {
	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// The opaque value for the remote-id.
	RemoteId []byte
}

The definition of the remote-id carried in this option is vendor specific. The vendor is indicated in the enterprise-number field. The remote-id field may be used to encode, for instance: - a "caller ID" telephone number for dial-up connection - a "user name" prompted for by a Remote Access Server - a remote caller ATM address - a "modem ID" of a cable data modem - the remote IP address of a point-to-point link - a remote X.25 address for X.25 connections - an interface or port identifier

func (*RemoteIdentifier) MarshalBinary

func (r *RemoteIdentifier) MarshalBinary() ([]byte, error)

RemoteIdentifier allocates a byte slice containing the data from a RemoteIdentifier.

func (*RemoteIdentifier) UnmarshalBinary

func (r *RemoteIdentifier) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a RemoteIdentifier. If the byte slice does not contain enough data to form a valid RemoteIdentifier, io.ErrUnexpectedEOF is returned.

type Request

type Request struct {
	// DHCP message type, such as Solicit, Request, or Renew.
	MessageType 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 Options

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

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

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.

func RequestFromPacket

func RequestFromPacket(p *Packet, plen int, remoteAddr *net.UDPAddr, linkAddr net.IP) (*Request, error)

type ResponseSender

type ResponseSender interface {
	// Options returns the Options map that will be sent to a client
	// after a call to Send.
	Options() 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(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 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 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, DefaultServeMux will be used in place of Handler.
	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 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 NewServer

func NewServer(iface string, handler Handler) (*Server, error)

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.

type Status

type Status uint16

Status represesents a DHCP status code, as defined in RFC 3315, Section 5.4. Status codes are used to communicate success or failure between client and server.

const (
	// RFC 3315
	StatusSuccess      Status = 0
	StatusUnspecFail   Status = 1
	StatusNoAddrsAvail Status = 2
	StatusNoBinding    Status = 3
	StatusNotOnLink    Status = 4
	StatusUseMulticast Status = 5

	// RFC 3633
	StatusNoPrefixAvail Status = 6

	// RFC 5007
	StatusUnknownQueryType Status = 7
	StatusMalformedQuery   Status = 8
	StatusNotConfigured    Status = 9
	StatusNotAllowed       Status = 10

	// RFC 5460
	StatusQueryTerminated Status = 11
)

Status constants which indicate the status codes described in RFCs 3315, 3633, 5007, and 5460.

These status codes are taken from IANA's DHCPv6 parameters registry: http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml.

func (Status) String

func (i Status) String() string

type StatusCode

type StatusCode struct {
	// Code specifies the Status value stored within this StatusCode, such as
	// StatusSuccess, StatusUnspecFail, etc.
	Code Status

	// Message specifies a human-readable message within this StatusCode, useful
	// for providing information about successes or failures.
	Message string
}

StatusCode represents a Status Code, as defined in RFC 3315, Section 5.4. DHCP clients and servers can use status codes to communicate successes or failures, and provide additional information using a message to describe specific failures.

func NewStatusCode

func NewStatusCode(code Status, message string) *StatusCode

NewStatusCode creates a new StatusCode from an input Status value and a string message.

func (*StatusCode) MarshalBinary

func (s *StatusCode) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a StatusCode.

func (*StatusCode) UnmarshalBinary

func (s *StatusCode) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a StatusCode.

If the byte slice does not contain enough data to form a valid StatusCode, errInvalidStatusCode is returned.

type URL

type URL url.URL

A URL is a uniform resource locater. The URL type is provided for convenience. It can be used to easily add URLs to an Options map.

func (URL) MarshalBinary

func (u URL) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a URL.

func (*URL) UnmarshalBinary

func (u *URL) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into an URL.

If the byte slice is not an URLv6 address, io.ErrUnexpectedEOF is returned.

type VendorClass

type VendorClass struct {
	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// The vendor-class-data is composed of a series of separate items, each
	// of which describes some characteristic of the client's hardware
	// configuration.  Examples of vendor-class-data instances might include
	// the version of the operating system the client is running or the
	// amount of memory installed on the client.
	VendorClassData Data
}

This option is used by a client to identify the vendor that manufactured the hardware on which the client is running. The information contained in the data area of this option is contained in one or more opaque fields that identify details of the hardware configuration.

func (*VendorClass) MarshalBinary

func (vc *VendorClass) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a VendorClass.

func (*VendorClass) UnmarshalBinary

func (vc *VendorClass) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a VendorClass.

If the byte slice is less than 4 bytes in length, or if VendorClassData is malformed, io.ErrUnexpectedEOF is returned.

type VendorOpts

type VendorOpts struct {
	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// An opaque object of option-len octets,
	// interpreted by vendor-specific code on the
	// clients and servers
	Options Options
}

A VendorOpts is used by clients and servers to exchange VendorOpts information.

func (*VendorOpts) MarshalBinary

func (v *VendorOpts) MarshalBinary() ([]byte, error)

MarshalBinary allocates a byte slice containing the data from a VendorOpts.

func (*VendorOpts) UnmarshalBinary

func (v *VendorOpts) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a raw byte slice into a VendorOpts. If the byte slice does not contain enough data to form a valid VendorOpts, io.ErrUnexpectedEOF is returned. If option-data are invalid, then ErrInvalidPacket is returned.

Notes

Bugs

  • add additional option code types defined by IANA

  • 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

Directories

Path Synopsis
cmd
dhcp6d
Command dhcp6d is an example DHCPv6 server.
Command dhcp6d is an example DHCPv6 server.
Package dhcp6test provides utilities for testing DHCPv6 clients and servers.
Package dhcp6test provides utilities for testing DHCPv6 clients and servers.

Jump to

Keyboard shortcuts

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