dhcpv4

package
v0.0.0-...-99f0e32 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package dhcpv4 provides encoding and decoding of DHCPv4 packets and options.

Example Usage:

p, err := dhcpv4.New(
  dhcpv4.WithClientIP(net.IP{192, 168, 0, 1}),
  dhcpv4.WithMessageType(dhcpv4.MessageTypeInform),
)
p.UpdateOption(dhcpv4.OptServerIdentifier(net.IP{192, 110, 110, 110}))

// Retrieve the DHCP Message Type option.
m := p.MessageType()

bytesOnTheWire := p.ToBytes()
longSummary := p.Summary()

Index

Constants

View Source
const (
	ServerPort = 67
	ClientPort = 68
)
View Source
const (

	// MaxHWAddrLen is the maximum hardware address length of the ClientHWAddr
	// (client hardware address) according to RFC 2131, Section 2. This is the
	// link-layer destination a server must send responses to.
	MaxHWAddrLen = 16

	// MaxMessageSize is the maximum size in bytes that a DHCPv4 packet can hold.
	MaxMessageSize = 576
)
View Source
const (
	AgentCircuitIDSubOption                raiSubOptionCode = 1   // RFC 3046
	AgentRemoteIDSubOption                 raiSubOptionCode = 2   // RFC 3046
	DOCSISDeviceClassSubOption             raiSubOptionCode = 4   // RFC 3256
	LinkSelectionSubOption                 raiSubOptionCode = 5   // RFC 3527
	SubscriberIDSubOption                  raiSubOptionCode = 6   // RFC 3993
	RADIUSAttributesSubOption              raiSubOptionCode = 7   // RFC 4014
	AuthenticationSubOption                raiSubOptionCode = 8   // RFC 4030
	VendorSpecificInformationSubOption     raiSubOptionCode = 9   // RFC 4243
	RelayAgentFlagsSubOption               raiSubOptionCode = 10  // RFC 5010
	ServerIdentifierOverrideSubOption      raiSubOptionCode = 11  // RFC 5107
	RelaySourcePortSubOption               raiSubOptionCode = 19  // RFC 8357
	VirtualSubnetSelectionSubOption        raiSubOptionCode = 151 // RFC 6607
	VirtualSubnetSelectionControlSubOption raiSubOptionCode = 152 // RFC 6607
)

Option 82 Relay Agention Information Sub Options

View Source
const (
	OptionPad                                        optionCode = 0
	OptionSubnetMask                                 optionCode = 1
	OptionTimeOffset                                 optionCode = 2
	OptionRouter                                     optionCode = 3
	OptionTimeServer                                 optionCode = 4
	OptionNameServer                                 optionCode = 5
	OptionDomainNameServer                           optionCode = 6
	OptionLogServer                                  optionCode = 7
	OptionQuoteServer                                optionCode = 8
	OptionLPRServer                                  optionCode = 9
	OptionImpressServer                              optionCode = 10
	OptionResourceLocationServer                     optionCode = 11
	OptionHostName                                   optionCode = 12
	OptionBootFileSize                               optionCode = 13
	OptionMeritDumpFile                              optionCode = 14
	OptionDomainName                                 optionCode = 15
	OptionSwapServer                                 optionCode = 16
	OptionRootPath                                   optionCode = 17
	OptionExtensionsPath                             optionCode = 18
	OptionIPForwarding                               optionCode = 19
	OptionNonLocalSourceRouting                      optionCode = 20
	OptionPolicyFilter                               optionCode = 21
	OptionMaximumDatagramAssemblySize                optionCode = 22
	OptionDefaultIPTTL                               optionCode = 23
	OptionPathMTUAgingTimeout                        optionCode = 24
	OptionPathMTUPlateauTable                        optionCode = 25
	OptionInterfaceMTU                               optionCode = 26
	OptionAllSubnetsAreLocal                         optionCode = 27
	OptionBroadcastAddress                           optionCode = 28
	OptionPerformMaskDiscovery                       optionCode = 29
	OptionMaskSupplier                               optionCode = 30
	OptionPerformRouterDiscovery                     optionCode = 31
	OptionRouterSolicitationAddress                  optionCode = 32
	OptionStaticRoutingTable                         optionCode = 33
	OptionTrailerEncapsulation                       optionCode = 34
	OptionArpCacheTimeout                            optionCode = 35
	OptionEthernetEncapsulation                      optionCode = 36
	OptionDefaulTCPTTL                               optionCode = 37
	OptionTCPKeepaliveInterval                       optionCode = 38
	OptionTCPKeepaliveGarbage                        optionCode = 39
	OptionNetworkInformationServiceDomain            optionCode = 40
	OptionNetworkInformationServers                  optionCode = 41
	OptionNTPServers                                 optionCode = 42
	OptionVendorSpecificInformation                  optionCode = 43
	OptionNetBIOSOverTCPIPNameServer                 optionCode = 44
	OptionNetBIOSOverTCPIPDatagramDistributionServer optionCode = 45
	OptionNetBIOSOverTCPIPNodeType                   optionCode = 46
	OptionNetBIOSOverTCPIPScope                      optionCode = 47
	OptionXWindowSystemFontServer                    optionCode = 48
	OptionXWindowSystemDisplayManger                 optionCode = 49
	OptionRequestedIPAddress                         optionCode = 50
	OptionIPAddressLeaseTime                         optionCode = 51
	OptionOptionOverload                             optionCode = 52
	OptionDHCPMessageType                            optionCode = 53
	OptionServerIdentifier                           optionCode = 54
	OptionParameterRequestList                       optionCode = 55
	OptionMessage                                    optionCode = 56
	OptionMaximumDHCPMessageSize                     optionCode = 57
	OptionRenewTimeValue                             optionCode = 58
	OptionRebindingTimeValue                         optionCode = 59
	OptionClassIdentifier                            optionCode = 60
	OptionClientIdentifier                           optionCode = 61
	OptionNetWareIPDomainName                        optionCode = 62
	OptionNetWareIPInformation                       optionCode = 63
	OptionNetworkInformationServicePlusDomain        optionCode = 64
	OptionNetworkInformationServicePlusServers       optionCode = 65
	OptionTFTPServerName                             optionCode = 66
	OptionBootfileName                               optionCode = 67
	OptionMobileIPHomeAgent                          optionCode = 68
	OptionSimpleMailTransportProtocolServer          optionCode = 69
	OptionPostOfficeProtocolServer                   optionCode = 70
	OptionNetworkNewsTransportProtocolServer         optionCode = 71
	OptionDefaultWorldWideWebServer                  optionCode = 72
	OptionDefaultFingerServer                        optionCode = 73
	OptionDefaultInternetRelayChatServer             optionCode = 74
	OptionStreetTalkServer                           optionCode = 75
	OptionStreetTalkDirectoryAssistanceServer        optionCode = 76
	OptionUserClassInformation                       optionCode = 77
	OptionSLPDirectoryAgent                          optionCode = 78
	OptionSLPServiceScope                            optionCode = 79
	OptionRapidCommit                                optionCode = 80
	OptionFQDN                                       optionCode = 81
	OptionRelayAgentInformation                      optionCode = 82
	OptionInternetStorageNameService                 optionCode = 83
	// Option 84 returned in RFC 3679
	OptionNDSServers                       optionCode = 85
	OptionNDSTreeName                      optionCode = 86
	OptionNDSContext                       optionCode = 87
	OptionBCMCSControllerDomainNameList    optionCode = 88
	OptionBCMCSControllerIPv4AddressList   optionCode = 89
	OptionAuthentication                   optionCode = 90
	OptionClientLastTransactionTime        optionCode = 91
	OptionAssociatedIP                     optionCode = 92
	OptionClientSystemArchitectureType     optionCode = 93
	OptionClientNetworkInterfaceIdentifier optionCode = 94
	OptionLDAP                             optionCode = 95
	// Option 96 returned in RFC 3679
	OptionClientMachineIdentifier     optionCode = 97
	OptionOpenGroupUserAuthentication optionCode = 98
	OptionGeoConfCivic                optionCode = 99
	OptionIEEE10031TZString           optionCode = 100
	OptionReferenceToTZDatabase       optionCode = 101
	// Option 108 returned in RFC 8925
	OptionIPv6OnlyPreferred optionCode = 108
	// Options 102-111 returned in RFC 3679
	OptionNetInfoParentServerAddress optionCode = 112
	OptionNetInfoParentServerTag     optionCode = 113
	OptionURL                        optionCode = 114
	// Option 115 returned in RFC 3679
	OptionAutoConfigure                   optionCode = 116
	OptionNameServiceSearch               optionCode = 117
	OptionSubnetSelection                 optionCode = 118
	OptionDNSDomainSearchList             optionCode = 119
	OptionSIPServers                      optionCode = 120
	OptionClasslessStaticRoute            optionCode = 121
	OptionCCC                             optionCode = 122
	OptionGeoConf                         optionCode = 123
	OptionVendorIdentifyingVendorClass    optionCode = 124
	OptionVendorIdentifyingVendorSpecific optionCode = 125
	// Options 126-127 returned in RFC 3679
	OptionTFTPServerIPAddress                   optionCode = 128
	OptionCallServerIPAddress                   optionCode = 129
	OptionDiscriminationString                  optionCode = 130
	OptionRemoteStatisticsServerIPAddress       optionCode = 131
	Option8021PVLANID                           optionCode = 132
	Option8021QL2Priority                       optionCode = 133
	OptionDiffservCodePoint                     optionCode = 134
	OptionHTTPProxyForPhoneSpecificApplications optionCode = 135
	OptionPANAAuthenticationAgent               optionCode = 136
	OptionLoSTServer                            optionCode = 137
	OptionCAPWAPAccessControllerAddresses       optionCode = 138
	OptionOPTIONIPv4AddressMoS                  optionCode = 139
	OptionOPTIONIPv4FQDNMoS                     optionCode = 140
	OptionSIPUAConfigurationServiceDomains      optionCode = 141
	OptionOPTIONIPv4AddressANDSF                optionCode = 142
	OptionOPTIONIPv6AddressANDSF                optionCode = 143
	// Options 144-149 returned in RFC 3679
	OptionTFTPServerAddress optionCode = 150
	OptionStatusCode        optionCode = 151
	OptionBaseTime          optionCode = 152
	OptionStartTimeOfState  optionCode = 153
	OptionQueryStartTime    optionCode = 154
	OptionQueryEndTime      optionCode = 155
	OptionDHCPState         optionCode = 156
	OptionDataSource        optionCode = 157
	// Options 158-174 returned in RFC 3679
	OptionEtherboot                        optionCode = 175
	OptionIPTelephone                      optionCode = 176
	OptionEtherbootPacketCableAndCableHome optionCode = 177
	// Options 178-207 returned in RFC 3679
	OptionPXELinuxMagicString  optionCode = 208
	OptionPXELinuxConfigFile   optionCode = 209
	OptionPXELinuxPathPrefix   optionCode = 210
	OptionPXELinuxRebootTime   optionCode = 211
	OptionOPTION6RD            optionCode = 212
	OptionOPTIONv4AccessDomain optionCode = 213
	// Options 214-219 returned in RFC 3679
	OptionSubnetAllocation        optionCode = 220
	OptionVirtualSubnetAllocation optionCode = 221
	// Options 222-223 returned in RFC 3679
	// Options 224-254 are reserved for private use
	OptionEnd optionCode = 255
)

DHCPv4 Options

Variables

View Source
var (
	// ErrShortByteStream is an error that is thrown any time a short byte stream is
	// detected during option parsing.
	ErrShortByteStream = errors.New("short byte stream")

	// ErrZeroLengthByteStream is an error that is thrown any time a zero-length
	// byte stream is encountered.
	ErrZeroLengthByteStream = errors.New("zero-length byte stream")
)
View Source
var MaxLeaseTime = math.MaxUint32 * time.Second

MaxLeaseTime is the maximum lease time that can be encoded.

View Source
var RandomTimeout = 2 * time.Minute

RandomTimeout is the amount of time to wait until random number generation is canceled.

Functions

func BindToInterface

func BindToInterface(fd int, ifname string) error

BindToInterface (deprecated) redirects to interfaces.BindToInterface

func GetByte

func GetByte(code OptionCode, o Options) (byte, error)

GetByte parses any single-byte option

func GetExternalIPv4Addrs

func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error)

GetExternalIPv4Addrs obtains the currently-configured, non-loopback IPv4 addresses from `addrs` coming from a particular interface (e.g. net.Interface.Addrs).

func GetIP

func GetIP(code OptionCode, o Options) net.IP

GetIP returns code out of o parsed as an IP.

func GetIPs

func GetIPs(code OptionCode, o Options) []net.IP

GetIPs parses a list of IPs from code in o.

func GetString

func GetString(code OptionCode, o Options) string

GetString parses an RFC 2132 string from o[code].

func GetUint16

func GetUint16(code OptionCode, o Options) (uint16, error)

GetUint16 parses a uint16 from code in o.

func IPv4AddrsForInterface

func IPv4AddrsForInterface(iface *net.Interface) ([]net.IP, error)

IPv4AddrsForInterface obtains the currently-configured, non-loopback IPv4 addresses for iface.

func WithNetboot

func WithNetboot(d *DHCPv4)

WithNetboot adds bootfile URL and bootfile param options to a DHCPv4 packet.

Types

type AutoConfiguration

type AutoConfiguration byte

AutoConfiguration implements encoding and decoding functions for a byte enumeration as used in RFC 2563, Section 2.

const (
	DoNotAutoConfigure AutoConfiguration = 0
	AutoConfigure      AutoConfiguration = 1
)

func (*AutoConfiguration) FromBytes

func (o *AutoConfiguration) FromBytes(data []byte) error

FromBytes parses a a single byte into AutoConfiguration

func (AutoConfiguration) String

func (o AutoConfiguration) String() string

String returns a human-readable string for this option.

func (AutoConfiguration) ToBytes

func (o AutoConfiguration) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type DHCPv4

type DHCPv4 struct {
	OpCode         OpcodeType
	HWType         iana.HWType
	HopCount       uint8
	TransactionID  TransactionID
	NumSeconds     uint16
	Flags          uint16
	ClientIPAddr   net.IP
	YourIPAddr     net.IP
	ServerIPAddr   net.IP
	GatewayIPAddr  net.IP
	ClientHWAddr   net.HardwareAddr
	ServerHostName string
	BootFileName   string
	Options        Options
}

DHCPv4 represents a DHCPv4 packet header and options. See the New* functions to build DHCPv4 packets.

func FromBytes

func FromBytes(q []byte) (*DHCPv4, error)

FromBytes decodes a DHCPv4 packet from a sequence of bytes, and returns an error if the packet is not valid.

func New

func New(modifiers ...Modifier) (*DHCPv4, error)

New creates a new DHCPv4 structure and fill it up with default values. It won't be a valid DHCPv4 message so you will need to adjust its fields. See also NewDiscovery, NewRequest, NewAcknowledge, NewInform and NewRelease.

func NewDiscovery

func NewDiscovery(hwaddr net.HardwareAddr, modifiers ...Modifier) (*DHCPv4, error)

NewDiscovery builds a new DHCPv4 Discovery message, with a default Ethernet HW type and specified hardware address.

func NewDiscoveryForInterface

func NewDiscoveryForInterface(ifname string, modifiers ...Modifier) (*DHCPv4, error)

NewDiscoveryForInterface builds a new DHCPv4 Discovery message, with a default Ethernet HW type and the hardware address obtained from the specified interface.

func NewInform

func NewInform(hwaddr net.HardwareAddr, localIP net.IP, modifiers ...Modifier) (*DHCPv4, error)

NewInform builds a new DHCPv4 Informational message with the specified hardware address.

func NewInformForInterface

func NewInformForInterface(ifname string, needsBroadcast bool) (*DHCPv4, error)

NewInformForInterface builds a new DHCPv4 Informational message with default Ethernet HW type and the hardware address obtained from the specified interface.

func NewReleaseFromACK

func NewReleaseFromACK(ack *DHCPv4, modifiers ...Modifier) (*DHCPv4, error)

NewReleaseFromACK creates a DHCPv4 Release message from ACK. default Release message without any Modifer is created as following:

  • option Message Type is Release
  • ClientIP is set to ack.YourIPAddr
  • ClientHWAddr is set to ack.ClientHWAddr
  • Unicast
  • option Server Identifier is set to ack's ServerIdentifier

func NewRenewFromAck

func NewRenewFromAck(ack *DHCPv4, modifiers ...Modifier) (*DHCPv4, error)

NewRenewFromAck builds a DHCPv4 RENEW-style request from the ACK of a lease. RENEW requests have minor changes to their options compared to SELECT requests as specified by RFC 2131, section 4.3.2.

func NewReplyFromRequest

func NewReplyFromRequest(request *DHCPv4, modifiers ...Modifier) (*DHCPv4, error)

NewReplyFromRequest builds a DHCPv4 reply from a request.

func NewRequestFromOffer

func NewRequestFromOffer(offer *DHCPv4, modifiers ...Modifier) (*DHCPv4, error)

NewRequestFromOffer builds a DHCPv4 request from an offer. It assumes the SELECTING state by default, see Section 4.3.2 in RFC 2131 for more details.

func (*DHCPv4) AutoConfigure

func (d *DHCPv4) AutoConfigure() (AutoConfiguration, bool)

AutoConfigure returns the value of the AutoConfigure option, and a boolean indicating if it was present.

The AutoConfigure option is described by RFC 2563, Section 2.

func (*DHCPv4) BootFileNameOption

func (d *DHCPv4) BootFileNameOption() string

BootFileNameOption parses the DHCPv4 Bootfile Name option if present.

The Bootfile Name option is described by RFC 2132, Section 9.5.

func (*DHCPv4) BroadcastAddress

func (d *DHCPv4) BroadcastAddress() net.IP

GetBroadcastAddress returns the DHCPv4 Broadcast Address value in d.

The broadcast address option is described in RFC 2132, Section 5.3.

func (*DHCPv4) ClassIdentifier

func (d *DHCPv4) ClassIdentifier() string

ClassIdentifier parses the DHCPv4 Class Identifier option if present.

The Vendor Class Identifier option is described by RFC 2132, Section 9.13.

func (*DHCPv4) ClasslessStaticRoute

func (d *DHCPv4) ClasslessStaticRoute() []*Route

ClasslessStaticRoute parses the DHCPv4 Classless Static Route option if present.

The Classless Static Route option is described by RFC 3442.

func (*DHCPv4) ClientArch

func (d *DHCPv4) ClientArch() []iana.Arch

ClientArch returns the Client System Architecture Type option.

func (*DHCPv4) DNS

func (d *DHCPv4) DNS() []net.IP

DNS parses the DHCPv4 Domain Name Server option if present.

The DNS server option is described by RFC 2132, Section 3.8.

func (*DHCPv4) DeleteOption

func (d *DHCPv4) DeleteOption(code OptionCode)

DeleteOption deletes an existing option with the given option code.

func (*DHCPv4) DomainName

func (d *DHCPv4) DomainName() string

DomainName parses the DHCPv4 Domain Name option if present.

The Domain Name option is described by RFC 2132, Section 3.17.

func (*DHCPv4) DomainSearch

func (d *DHCPv4) DomainSearch() *rfc1035label.Labels

DomainSearch returns the domain search list if present.

The domain search option is described by RFC 3397, Section 2.

func (*DHCPv4) FlagsToString

func (d *DHCPv4) FlagsToString() string

FlagsToString returns a human-readable representation of the flags field.

func (*DHCPv4) GetOneOption

func (d *DHCPv4) GetOneOption(code OptionCode) []byte

GetOneOption returns the option that matches the given option code.

According to RFC 3396, options that are specified more than once are concatenated, and hence this should always just return one option.

func (*DHCPv4) HostName

func (d *DHCPv4) HostName() string

HostName parses the DHCPv4 Host Name option if present.

The Host Name option is described by RFC 2132, Section 3.14.

func (*DHCPv4) IPAddressLeaseTime

func (d *DHCPv4) IPAddressLeaseTime(def time.Duration) time.Duration

IPAddressLeaseTime returns the IP address lease time or the given default duration if not present.

The IP address lease time option is described by RFC 2132, Section 9.2.

func (*DHCPv4) IPAddressRebindingTime

func (d *DHCPv4) IPAddressRebindingTime(def time.Duration) time.Duration

IPAddressRebindingTime returns the IP address rebinding time or the given default duration if not present.

The IP address rebinding time option is described by RFC 2132, Section 9.12.

func (*DHCPv4) IPAddressRenewalTime

func (d *DHCPv4) IPAddressRenewalTime(def time.Duration) time.Duration

IPAddressRenewalTime returns the IP address renewal time or the given default duration if not present.

The IP address renewal time option is described by RFC 2132, Section 9.11.

func (*DHCPv4) IPv6OnlyPreferred

func (d *DHCPv4) IPv6OnlyPreferred() (time.Duration, bool)

IPv6OnlyPreferred returns the V6ONLY_WAIT duration, and a boolean indicating whether this option was present.

The IPv6-Only Preferred option is described by RFC 8925, Section 3.1.

func (*DHCPv4) IsBroadcast

func (d *DHCPv4) IsBroadcast() bool

IsBroadcast indicates whether the packet is a broadcast packet.

func (*DHCPv4) IsOptionRequested

func (d *DHCPv4) IsOptionRequested(requested OptionCode) bool

IsOptionRequested returns true if that option is within the requested options of the DHCPv4 message.

func (*DHCPv4) IsUnicast

func (d *DHCPv4) IsUnicast() bool

IsUnicast indicates whether the packet is a unicast packet.

func (*DHCPv4) MaxMessageSize

func (d *DHCPv4) MaxMessageSize() (uint16, error)

MaxMessageSize returns the DHCP Maximum Message Size if present.

The Maximum DHCP Message Size option is described by RFC 2132, Section 9.10.

func (*DHCPv4) Message

func (d *DHCPv4) Message() string

Message returns the DHCPv4 (Error) Message option.

The message options is described in RFC 2132, Section 9.9.

func (*DHCPv4) MessageType

func (d *DHCPv4) MessageType() MessageType

MessageType returns the DHCPv4 Message Type option.

func (*DHCPv4) NTPServers

func (d *DHCPv4) NTPServers() []net.IP

NTPServers parses the DHCPv4 NTP Servers option if present.

The NTP servers option is described by RFC 2132, Section 8.3.

func (*DHCPv4) ParameterRequestList

func (d *DHCPv4) ParameterRequestList() OptionCodeList

ParameterRequestList returns the DHCPv4 Parameter Request List.

The parameter request list option is described by RFC 2132, Section 9.8.

func (*DHCPv4) RelayAgentInfo

func (d *DHCPv4) RelayAgentInfo() *RelayOptions

RelayAgentInfo returns options embedded by the relay agent.

The relay agent info option is described by RFC 3046.

func (*DHCPv4) RequestedIPAddress

func (d *DHCPv4) RequestedIPAddress() net.IP

RequestedIPAddress returns the DHCPv4 Requested IP Address value in d.

The requested IP address option is described by RFC 2132, Section 9.1.

func (*DHCPv4) RootPath

func (d *DHCPv4) RootPath() string

RootPath parses the DHCPv4 Root Path option if present.

The Root Path option is described by RFC 2132, Section 3.19.

func (*DHCPv4) Router

func (d *DHCPv4) Router() []net.IP

Router parses the DHCPv4 Router option if present.

The Router option is described by RFC 2132, Section 3.5.

func (*DHCPv4) ServerIdentifier

func (d *DHCPv4) ServerIdentifier() net.IP

ServerIdentifier returns the DHCPv4 Server Identifier value in d.

The server identifier option is described by RFC 2132, Section 9.7.

func (*DHCPv4) SetBroadcast

func (d *DHCPv4) SetBroadcast()

SetBroadcast sets the packet to be a broadcast packet.

func (*DHCPv4) SetUnicast

func (d *DHCPv4) SetUnicast()

SetUnicast sets the packet to be a unicast packet.

func (*DHCPv4) String

func (d *DHCPv4) String() string

String implements fmt.Stringer.

func (*DHCPv4) SubnetMask

func (d *DHCPv4) SubnetMask() net.IPMask

SubnetMask returns a subnet mask option contained if present.

The subnet mask option is described by RFC 2132, Section 3.3.

func (*DHCPv4) Summary

func (d *DHCPv4) Summary() string

Summary prints detailed information about the packet.

func (*DHCPv4) SummaryWithVendor

func (d *DHCPv4) SummaryWithVendor(vendorDecoder OptionDecoder) string

SummaryWithVendor prints a summary of the packet, interpreting the vendor-specific info option using the given parser (can be nil).

func (*DHCPv4) TFTPServerName

func (d *DHCPv4) TFTPServerName() string

TFTPServerName parses the DHCPv4 TFTP Server Name option if present.

The TFTP Server Name option is described by RFC 2132, Section 9.4.

func (*DHCPv4) ToBytes

func (d *DHCPv4) ToBytes() []byte

ToBytes writes the packet to binary.

func (*DHCPv4) UpdateOption

func (d *DHCPv4) UpdateOption(opt Option)

UpdateOption replaces an existing option with the same option code with the given one, adding it if not already present.

func (*DHCPv4) UserClass

func (d *DHCPv4) UserClass() []string

UserClass returns the user class if present.

The user class information option is defined by RFC 3004.

func (*DHCPv4) VIVC

func (d *DHCPv4) VIVC() VIVCIdentifiers

VIVC returns the vendor-identifying vendor class option if present.

type Duration

type Duration time.Duration

Duration implements the IP address lease time option described by RFC 2132, Section 9.2.

func (*Duration) FromBytes

func (d *Duration) FromBytes(data []byte) error

FromBytes parses a duration from a byte stream according to RFC 2132, Section 9.2.

func (Duration) String

func (d Duration) String() string

String returns a human-readable string for this option.

func (Duration) ToBytes

func (d Duration) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type GenericOptionCode

type GenericOptionCode uint8

GenericOptionCode is an unnamed option code.

func (GenericOptionCode) Code

func (o GenericOptionCode) Code() uint8

Code implements OptionCode.Code.

func (GenericOptionCode) String

func (o GenericOptionCode) String() string

String returns the option's name.

type IP

type IP net.IP

IP implements DHCPv4 IP option marshaling and unmarshaling as described by RFC 2132, Sections 5.3, 9.1, 9.7, and others.

func (*IP) FromBytes

func (i *IP) FromBytes(data []byte) error

FromBytes parses an IP from data in binary form.

func (IP) String

func (i IP) String() string

String returns a human-readable IP.

func (IP) ToBytes

func (i IP) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type IPMask

type IPMask net.IPMask

IPMask represents an option encapsulating the subnet mask.

This option implements the subnet mask option in RFC 2132, Section 3.3.

func (*IPMask) FromBytes

func (im *IPMask) FromBytes(data []byte) error

FromBytes parses im from data per RFC 2132.

func (IPMask) String

func (im IPMask) String() string

String returns a human-readable string.

func (IPMask) ToBytes

func (im IPMask) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type IPs

type IPs []net.IP

IPs are IPv4 addresses from a DHCP packet as used and specified by options in RFC 2132, Sections 3.5 through 3.13, 8.2, 8.3, 8.5, 8.6, 8.9, and 8.10.

IPs implements the OptionValue type.

func (*IPs) FromBytes

func (i *IPs) FromBytes(data []byte) error

FromBytes parses an IPv4 address from a DHCP packet as used and specified by options in RFC 2132, Sections 3.5 through 3.13, 8.2, 8.3, 8.5, 8.6, 8.9, and 8.10.

func (IPs) String

func (i IPs) String() string

String returns a human-readable representation of a list of IPs.

func (IPs) ToBytes

func (i IPs) ToBytes() []byte

ToBytes marshals IPv4 addresses to a DHCP packet as specified by RFC 2132, Section 3.5 et al.

type MessageType

type MessageType byte

MessageType represents the possible DHCP message types - DISCOVER, OFFER, etc

const (
	// MessageTypeNone is not a real message type, it is used by certain
	// functions to signal that no explicit message type is requested
	MessageTypeNone     MessageType = 0
	MessageTypeDiscover MessageType = 1
	MessageTypeOffer    MessageType = 2
	MessageTypeRequest  MessageType = 3
	MessageTypeDecline  MessageType = 4
	MessageTypeAck      MessageType = 5
	MessageTypeNak      MessageType = 6
	MessageTypeRelease  MessageType = 7
	MessageTypeInform   MessageType = 8
)

DHCP message types

func (*MessageType) FromBytes

func (m *MessageType) FromBytes(data []byte) error

FromBytes reads a message type from data as described by RFC 2132, Section 9.6.

func (MessageType) String

func (m MessageType) String() string

String prints a human-readable message type name.

func (MessageType) ToBytes

func (m MessageType) ToBytes() []byte

ToBytes returns the serialized version of this option described by RFC 2132, Section 9.6.

type Modifier

type Modifier func(d *DHCPv4)

Modifier defines the signature for functions that can modify DHCPv4 structures. This is used to simplify packet manipulation

func PrependModifiers

func PrependModifiers(m []Modifier, other ...Modifier) []Modifier

PrependModifiers prepends other to m.

func WithBroadcast

func WithBroadcast(broadcast bool) Modifier

WithBroadcast sets the packet to be broadcast or unicast

func WithClientIP

func WithClientIP(ip net.IP) Modifier

WithClientIP sets the Client IP for a DHCPv4 packet.

func WithDNS

func WithDNS(servers ...net.IP) Modifier

WithDNS modifies a packet with the DHCPv4 Domain Name Server option.

func WithDomainSearchList

func WithDomainSearchList(searchList ...string) Modifier

WithDomainSearchList adds or updates an OptionDomainSearch

func WithGatewayIP

func WithGatewayIP(ip net.IP) Modifier

WithGatewayIP sets the Gateway IP for the DHCPv4 packet.

func WithGeneric

func WithGeneric(code OptionCode, value []byte) Modifier

func WithHWType

func WithHWType(hwt iana.HWType) Modifier

WithHWType sets the Hardware Type for a DHCPv4 packet.

func WithHwAddr

func WithHwAddr(hwaddr net.HardwareAddr) Modifier

WithHwAddr sets the hardware address for a packet

func WithIPv6OnlyPreferred

func WithIPv6OnlyPreferred(v6OnlyWait uint32) Modifier

WithIPv6OnlyPreferred adds or updates an OptIPv6OnlyPreferred

func WithLeaseTime

func WithLeaseTime(leaseTime uint32) Modifier

WithLeaseTime adds or updates an OptIPAddressLeaseTime

func WithMessageType

func WithMessageType(m MessageType) Modifier

WithMessageType adds the DHCPv4 message type m to a packet.

func WithNetmask

func WithNetmask(mask net.IPMask) Modifier

WithNetmask adds or updates an OptSubnetMask

func WithOption

func WithOption(opt Option) Modifier

WithOption appends a DHCPv4 option provided by the user

func WithOptionCopied

func WithOptionCopied(request *DHCPv4, opt OptionCode) Modifier

WithOptionCopied copies the value of option opt from request.

func WithRelay

func WithRelay(ip net.IP) Modifier

WithRelay adds parameters required for DHCPv4 to be relayed by the relay server with given ip

func WithReply

func WithReply(request *DHCPv4) Modifier

WithReply fills in opcode, hwtype, xid, clienthwaddr, and flags from the given packet.

func WithRequestedOptions

func WithRequestedOptions(optionCodes ...OptionCode) Modifier

WithRequestedOptions adds requested options to the packet.

func WithRouter

func WithRouter(routers ...net.IP) Modifier

WithRouter updates a packet with the DHCPv4 Router option.

func WithServerIP

func WithServerIP(ip net.IP) Modifier

WithServerIP sets the Server IP for a DHCPv4 packet.

func WithTransactionID

func WithTransactionID(xid TransactionID) Modifier

WithTransactionID sets the Transaction ID for the DHCPv4 packet

func WithUserClass

func WithUserClass(uc string, rfc bool) Modifier

WithUserClass adds a user class option to the packet. The rfc parameter allows you to specify if the userclass should be rfc compliant or not. More details in issue #113

func WithYourIP

func WithYourIP(ip net.IP) Modifier

WithYourIP sets the Your IP for a DHCPv4 packet.

func WithoutOption

func WithoutOption(code OptionCode) Modifier

WithoutOption removes the DHCPv4 option with the given code

type OpcodeType

type OpcodeType uint8

OpcodeType represents a DHCPv4 opcode.

const (
	OpcodeBootRequest OpcodeType = 1
	OpcodeBootReply   OpcodeType = 2
)

constants that represent valid values for OpcodeType

func (OpcodeType) String

func (o OpcodeType) String() string

type Option

type Option struct {
	Code  OptionCode
	Value OptionValue
}

Option is a DHCPv4 option and consists of a 1-byte option code and a value stream of bytes.

The value is to be interpreted based on the option code.

func OptAutoConfigure

func OptAutoConfigure(autoconf AutoConfiguration) Option

OptAutoConfigure returns a new AutoConfigure option.

The AutoConfigure option is described by RFC 2563, Section 2.

func OptBootFileName

func OptBootFileName(name string) Option

OptBootFileName returns a new DHCPv4 Boot File Name option.

The Bootfile Name option is described by RFC 2132, Section 9.5.

func OptBroadcastAddress

func OptBroadcastAddress(ip net.IP) Option

OptBroadcastAddress returns a new DHCPv4 Broadcast Address option.

The broadcast address option is described in RFC 2132, Section 5.3.

func OptClassIdentifier

func OptClassIdentifier(name string) Option

OptClassIdentifier returns a new DHCPv4 Class Identifier option.

The Vendor Class Identifier option is described by RFC 2132, Section 9.13.

func OptClasslessStaticRoute

func OptClasslessStaticRoute(routes ...*Route) Option

OptClasslessStaticRoute returns a new DHCPv4 Classless Static Route option.

The Classless Static Route option is described by RFC 3442.

func OptClientArch

func OptClientArch(archs ...iana.Arch) Option

OptClientArch returns a new Client System Architecture Type option.

func OptClientIdentifier

func OptClientIdentifier(ident []byte) Option

OptClientIdentifier returns a new Client Identifier option.

func OptDNS

func OptDNS(servers ...net.IP) Option

OptDNS returns a new DHCPv4 Domain Name Server option.

The DNS server option is described by RFC 2132, Section 3.8.

func OptDomainName

func OptDomainName(name string) Option

OptDomainName returns a new DHCPv4 Domain Name option.

The Domain Name option is described by RFC 2132, Section 3.17.

func OptDomainSearch

func OptDomainSearch(labels *rfc1035label.Labels) Option

OptDomainSearch returns a new domain search option.

The domain search option is described by RFC 3397, Section 2.

func OptGeneric

func OptGeneric(code OptionCode, value []byte) Option

OptGeneric returns a generic option.

func OptHostName

func OptHostName(name string) Option

OptHostName returns a new DHCPv4 Host Name option.

The Host Name option is described by RFC 2132, Section 3.14.

func OptIPAddressLeaseTime

func OptIPAddressLeaseTime(d time.Duration) Option

OptIPAddressLeaseTime returns a new IP address lease time option.

The IP address lease time option is described by RFC 2132, Section 9.2.

func OptIPv6OnlyPreferred

func OptIPv6OnlyPreferred(d time.Duration) Option

The IPv6-Only Preferred option is described by RFC 8925, Section 3.1

func OptMaxMessageSize

func OptMaxMessageSize(size uint16) Option

OptMaxMessageSize returns a new DHCP Maximum Message Size option.

The Maximum DHCP Message Size option is described by RFC 2132, Section 9.10.

func OptMessage

func OptMessage(msg string) Option

OptMessage returns a new DHCPv4 (Error) Message option.

func OptMessageType

func OptMessageType(m MessageType) Option

OptMessageType returns a new DHCPv4 Message Type option.

func OptNTPServers

func OptNTPServers(ntpServers ...net.IP) Option

OptNTPServers returns a new DHCPv4 NTP Server option.

The NTP servers option is described by RFC 2132, Section 8.3.

func OptParameterRequestList

func OptParameterRequestList(codes ...OptionCode) Option

OptParameterRequestList returns a new DHCPv4 Parameter Request List.

The parameter request list option is described by RFC 2132, Section 9.8.

func OptRFC3004UserClass

func OptRFC3004UserClass(v []string) Option

OptRFC3004UserClass returns a new user class option according to RFC 3004.

func OptRebindingTimeValue

func OptRebindingTimeValue(d time.Duration) Option

The IP address rebinding time option as described by RFC 2132, Section 9.12.

func OptRelayAgentInfo

func OptRelayAgentInfo(o ...Option) Option

OptRelayAgentInfo returns a new DHCP Relay Agent Info option.

The relay agent info option is described by RFC 3046.

func OptRenewTimeValue

func OptRenewTimeValue(d time.Duration) Option

The IP address renew time option as described by RFC 2132, Section 9.11.

func OptRequestedIPAddress

func OptRequestedIPAddress(ip net.IP) Option

OptRequestedIPAddress returns a new DHCPv4 Requested IP Address option.

The requested IP address option is described by RFC 2132, Section 9.1.

func OptRootPath

func OptRootPath(name string) Option

OptRootPath returns a new DHCPv4 Root Path option.

The Root Path option is described by RFC 2132, Section 3.19.

func OptRouter

func OptRouter(routers ...net.IP) Option

OptRouter returns a new DHCPv4 Router option.

The Router option is described by RFC 2132, Section 3.5.

func OptServerIdentifier

func OptServerIdentifier(ip net.IP) Option

OptServerIdentifier returns a new DHCPv4 Server Identifier option.

The server identifier option is described by RFC 2132, Section 9.7.

func OptSubnetMask

func OptSubnetMask(mask net.IPMask) Option

OptSubnetMask returns a new DHCPv4 SubnetMask option per RFC 2132, Section 3.3.

func OptTFTPServerName

func OptTFTPServerName(name string) Option

OptTFTPServerName returns a new DHCPv4 TFTP Server Name option.

The TFTP Server Name option is described by RFC 2132, Section 9.4.

func OptUserClass

func OptUserClass(name string) Option

OptUserClass returns a new DHCPv4 User Class option.

func OptVIVC

func OptVIVC(identifiers ...VIVCIdentifier) Option

OptVIVC returns a new vendor-identifying vendor class option.

The option is described by RFC 3925.

func (Option) String

func (o Option) String() string

String returns a human-readable version of this option.

type OptionCode

type OptionCode interface {
	// Code is the 1 byte option code for the wire.
	Code() uint8

	// String returns the option's name.
	String() string
}

OptionCode is a single byte representing the code for a given Option.

OptionCode is an interface purely to support different stringers on options with the same Code value, as vendor-specific options use option codes that have the same value, but mean a different thing.

type OptionCodeList

type OptionCodeList []OptionCode

OptionCodeList is a list of DHCP option codes.

func (*OptionCodeList) Add

func (ol *OptionCodeList) Add(cs ...OptionCode)

Add adds option codes in cs to ol.

func (*OptionCodeList) FromBytes

func (ol *OptionCodeList) FromBytes(data []byte) error

FromBytes parses a byte stream for this option as described by RFC 2132, Section 9.8.

func (OptionCodeList) Has

func (ol OptionCodeList) Has(c OptionCode) bool

Has returns whether c is in the list.

func (OptionCodeList) String

func (ol OptionCodeList) String() string

String returns a human-readable string for the option names.

func (OptionCodeList) ToBytes

func (ol OptionCodeList) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option as defined by RFC 2132, Section 9.8.

type OptionDecoder

type OptionDecoder interface {
	fmt.Stringer
	FromBytes([]byte) error
}

OptionDecoder can decode a byte stream into a human-readable option.

type OptionGeneric

type OptionGeneric struct {
	Data []byte
}

OptionGeneric is an option that only contains the option code and associated data. Every option that does not have a specific implementation will fall back to this option.

func (OptionGeneric) String

func (o OptionGeneric) String() string

String returns a human-readable representation of a generic option.

func (OptionGeneric) ToBytes

func (o OptionGeneric) ToBytes() []byte

ToBytes returns a serialized generic option as a slice of bytes.

type OptionHumanizer

type OptionHumanizer struct {
	ValueHumanizer OptionParser
	CodeHumanizer  func(code uint8) OptionCode
}

OptionHumanizer is used to interpret a set of Options for their option code name and values.

There should be separate OptionHumanizers for each Option "space": DHCP, BSDP, Relay Agent Info, and others.

func (OptionHumanizer) Stringify

func (oh OptionHumanizer) Stringify(code uint8, data []byte) string

Stringify returns a human-readable interpretation of the option code and its associated data.

type OptionParser

type OptionParser func(code OptionCode, data []byte) fmt.Stringer

OptionParser gives a human-legible interpretation of data for the given option code.

type OptionValue

type OptionValue interface {
	ToBytes() []byte
	String() string
}

OptionValue is an interface that all DHCP v4 options adhere to.

type Options

type Options map[uint8][]byte

Options is a collection of options.

func OptionsFromList

func OptionsFromList(o ...Option) Options

OptionsFromList adds all given options to an options map.

func (Options) Del

func (o Options) Del(opcode OptionCode)

Del deletes the option matching the option code.

func (Options) FromBytes

func (o Options) FromBytes(data []byte) error

FromBytes parses a sequence of bytes until the end and builds a list of options from it.

The sequence should not contain the DHCP magic cookie.

Returns an error if any invalid option or length is found.

func (Options) Get

func (o Options) Get(code OptionCode) []byte

Get will attempt to get all options that match a DHCPv4 option from its OptionCode. If the option was not found it will return an empty list.

According to RFC 3396, options that are specified more than once are concatenated, and hence this should always just return one option. This currently returns a list to be API compatible.

func (Options) Has

func (o Options) Has(opcode OptionCode) bool

Has checks whether o has the given opcode.

func (Options) Marshal

func (o Options) Marshal(b *uio.Lexer)

Marshal writes options binary representations to b.

func (Options) String

func (o Options) String() string

String prints options using DHCP-specified option codes.

func (Options) Summary

func (o Options) Summary(vendorDecoder OptionDecoder) string

Summary prints options in human-readable values.

Summary uses vendorParser to interpret the OptionVendorSpecificInformation option.

func (Options) ToBytes

func (o Options) ToBytes() []byte

ToBytes makes Options usable as an OptionValue as well.

Used in the case of vendor-specific and relay agent options.

func (Options) ToString

func (o Options) ToString(humanizer OptionHumanizer) string

ToString uses parse to parse options into human-readable values.

func (Options) Update

func (o Options) Update(option Option)

Update updates the existing options with the passed option, adding it at the end if not present already

type RelayOptions

type RelayOptions struct {
	Options
}

RelayOptions is like Options, but stringifies using the Relay Agent Specific option space.

func (*RelayOptions) FromBytes

func (r *RelayOptions) FromBytes(data []byte) error

FromBytes parses relay agent options from data.

func (RelayOptions) String

func (r RelayOptions) String() string

String prints the contained options using Relay Agent-specific option code parsing.

type Route

type Route struct {
	// Dest is the destination network.
	Dest *net.IPNet

	// Router is the router to use for the given destination network.
	Router net.IP
}

Route is a classless static route as per RFC 3442.

func (Route) Marshal

func (r Route) Marshal(buf *uio.Lexer)

Marshal implements uio.Marshaler.

Format described in RFC 3442:

<size of mask in number of bits> <destination address, omitting octets that must be zero per mask> <route IP>

func (*Route) String

func (r *Route) String() string

String prints the destination network and router IP.

func (*Route) Unmarshal

func (r *Route) Unmarshal(buf *uio.Lexer) error

Unmarshal implements uio.Unmarshaler.

type Routes

type Routes []*Route

Routes is a collection of network routes.

func (*Routes) FromBytes

func (r *Routes) FromBytes(p []byte) error

FromBytes parses routes from a set of bytes as described by RFC 3442.

func (Routes) String

func (r Routes) String() string

String prints all routes.

func (Routes) ToBytes

func (r Routes) ToBytes() []byte

ToBytes marshals a set of routes as described by RFC 3442.

type String

type String string

String represents an option encapsulating a string in IPv4 DHCP.

This representation is shared by multiple options specified by RFC 2132, Sections 3.14, 3.16, 3.17, 3.19, and 3.20.

func (*String) FromBytes

func (o *String) FromBytes(data []byte) error

FromBytes parses a serialized stream of bytes into o.

func (String) String

func (o String) String() string

String returns a human-readable string.

func (String) ToBytes

func (o String) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type Strings

type Strings []string

Strings represents an option encapsulating a list of strings in IPv4 DHCP as specified in RFC 3004

Strings implements the OptionValue type.

func (*Strings) FromBytes

func (o *Strings) FromBytes(data []byte) error

FromBytes parses Strings from a DHCP packet as specified by RFC 3004.

func (Strings) String

func (o Strings) String() string

String returns a human-readable representation of a list of Strings.

func (Strings) ToBytes

func (o Strings) ToBytes() []byte

ToBytes marshals Strings to a DHCP packet as specified by RFC 3004.

type TransactionID

type TransactionID [4]byte

TransactionID represents a 4-byte DHCP transaction ID as defined in RFC 951, Section 3.

The TransactionID is used to match DHCP replies to their original request.

func GenerateTransactionID

func GenerateTransactionID() (TransactionID, error)

GenerateTransactionID generates a random 32-bits number suitable for use as TransactionID

func (TransactionID) String

func (xid TransactionID) String() string

String prints a hex transaction ID.

type Uint16

type Uint16 uint16

Uint16 implements encoding and decoding functions for a uint16 as used in RFC 2132, Section 9.10.

func (*Uint16) FromBytes

func (o *Uint16) FromBytes(data []byte) error

FromBytes decodes data into o as per RFC 2132, Section 9.10.

func (Uint16) String

func (o Uint16) String() string

String returns a human-readable string for this option.

func (Uint16) ToBytes

func (o Uint16) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

type VIVCIdentifier

type VIVCIdentifier struct {
	// EntID is the enterprise ID.
	EntID iana.EnterpriseID
	Data  []byte
}

VIVCIdentifier implements the vendor-identifying vendor class option described by RFC 3925.

type VIVCIdentifiers

type VIVCIdentifiers []VIVCIdentifier

VIVCIdentifiers implements encoding and decoding methods for a DHCP option described in RFC 3925.

func (*VIVCIdentifiers) FromBytes

func (ids *VIVCIdentifiers) FromBytes(data []byte) error

FromBytes parses data into ids per RFC 3925.

func (VIVCIdentifiers) String

func (ids VIVCIdentifiers) String() string

String returns a human-readable string for this option.

func (VIVCIdentifiers) ToBytes

func (ids VIVCIdentifiers) ToBytes() []byte

ToBytes returns a serialized stream of bytes for this option.

Directories

Path Synopsis
Package client4 is deprecated.
Package client4 is deprecated.
Package nclient4 is a small, minimum-functionality client for DHCPv4.
Package nclient4 is a small, minimum-functionality client for DHCPv4.
Package server4 is a basic, extensible DHCPv4 server.
Package server4 is a basic, extensible DHCPv4 server.

Jump to

Keyboard shortcuts

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