dhcpv4

package module
Version: v0.0.0-...-f3ab8f7 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

VMware has ended active development of this project, this repository will no longer be updated.

godhcpv4

DHCPv4 packet serialization/deserialization.

Includes a handler to create your own DHCPv4 server with (see handler.go).

RFCs

Other RFCs are informational or obsoleted by newer versions.

  • 2131: Dynamic Host Configuration Protocol
  • 3396: Encoding Long Options in the Dynamic Host Configuration Protocol (DHCPv4)

License

This project is available under the Apache 2.0 license.

Documentation

Overview

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2014 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	MessageTypeDHCPDiscover = MessageType(1)
	MessageTypeDHCPOffer    = MessageType(2)
	MessageTypeDHCPRequest  = MessageType(3)
	MessageTypeDHCPDecline  = MessageType(4)
	MessageTypeDHCPAck      = MessageType(5)
	MessageTypeDHCPNak      = MessageType(6)
	MessageTypeDHCPRelease  = MessageType(7)
	MessageTypeDHCPInform   = MessageType(8)
)
View Source
const (
	// RFC2132 Section 3: RFC 1497 Vendor Extensions
	OptionPad           = Option(0)
	OptionEnd           = Option(255)
	OptionSubnetMask    = Option(1)
	OptionTimeOffset    = Option(2)
	OptionRouter        = Option(3)
	OptionTimeServer    = Option(4)
	OptionNameServer    = Option(5)
	OptionDomainServer  = Option(6)
	OptionLogServer     = Option(7)
	OptionQuotesServer  = Option(8)
	OptionLPRServer     = Option(9)
	OptionImpressServer = Option(10)
	OptionRLPServer     = Option(11)
	OptionHostname      = Option(12)
	OptionBootFileSize  = Option(13)
	OptionMeritDumpFile = Option(14)
	OptionDomainName    = Option(15)
	OptionSwapServer    = Option(16)
	OptionRootPath      = Option(17)
	OptionExtensionFile = Option(18)

	// RFC2132 Section 4: IP Layer Parameters per Host
	OptionForwardOnOff  = Option(19)
	OptionSrcRteOnOff   = Option(20)
	OptionPolicyFilter  = Option(21)
	OptionMaxDGAssembly = Option(22)
	OptionDefaultIPTTL  = Option(23)
	OptionMTUTimeout    = Option(24)
	OptionMTUPlateau    = Option(25)

	// RFC2132 Section 5: IP Layer Parameters per Interface
	OptionMTUInterface     = Option(26)
	OptionMTUSubnet        = Option(27)
	OptionBroadcastAddress = Option(28)
	OptionMaskDiscovery    = Option(29)
	OptionMaskSupplier     = Option(30)
	OptionRouterDiscovery  = Option(31)
	OptionRouterRequest    = Option(32)
	OptionStaticRoute      = Option(33)

	// RFC2132 Section 6: Link Layer Parameters per Interface
	OptionTrailers   = Option(34)
	OptionARPTimeout = Option(35)
	OptionEthernet   = Option(36)

	// RFC2132 Section 7: TCP Parameters
	OptionDefaultTCPTTL = Option(37)
	OptionKeepaliveTime = Option(38)
	OptionKeepaliveData = Option(39)

	// RFC2132 Section 8: Application and Service Parameters
	OptionNISDomain        = Option(40)
	OptionNISServers       = Option(41)
	OptionNTPServers       = Option(42)
	OptionVendorSpecific   = Option(43)
	OptionNETBIOSNameSrv   = Option(44)
	OptionNETBIOSDistSrv   = Option(45)
	OptionNETBIOSNodeType  = Option(46)
	OptionNETBIOSScope     = Option(47)
	OptionXWindowFont      = Option(48)
	OptionXWindowManager   = Option(49)
	OptionNISDomainName    = Option(64)
	OptionNISServerAddr    = Option(65)
	OptionHomeAgentAddrs   = Option(68)
	OptionSMTPServer       = Option(69)
	OptionPOP3Server       = Option(70)
	OptionNNTPServer       = Option(71)
	OptionWWWServer        = Option(72)
	OptionFingerServer     = Option(73)
	OptionIRCServer        = Option(74)
	OptionStreetTalkServer = Option(75)
	OptionSTDAServer       = Option(76)

	// RFC2132 Section 9: DHCP Extensions
	OptionAddressRequest = Option(50)
	OptionAddressTime    = Option(51)
	OptionOverload       = Option(52)
	OptionServerName     = Option(66)
	OptionBootfileName   = Option(67)
	OptionDHCPMsgType    = Option(53)
	OptionDHCPServerID   = Option(54)
	OptionParameterList  = Option(55)
	OptionDHCPMessage    = Option(56)
	OptionDHCPMaxMsgSize = Option(57)
	OptionRenewalTime    = Option(58)
	OptionRebindingTime  = Option(59)
	OptionClassID        = Option(60)
	OptionClientID       = Option(61)
)

From RFC2132: DHCP Options and BOOTP Vendor Extensions

View Source
const (
	OptionNDSServers  = Option(85)
	OptionNDSTreeName = Option(86)
	OptionNDSContext  = Option(87)
)

From RFC2241: DHCP Options for Novell Directory Services

View Source
const (
	OptionNetWareIPDomain = Option(62)
	OptionNetWareIPOption = Option(63)
)

From RFC2242: NetWare/IP Domain Name and Information

View Source
const (
	OptionDirectoryAgent = Option(78)
	OptionServiceScope   = Option(79)
)

From RFC2610: DHCP Options for Service Location Protocol

View Source
const (
	OptionLDAP           = Option(95)
	OptionNetinfoAddress = Option(112)
	OptionNetinfoTag     = Option(113)
	OptionURL            = Option(114)
)

From RFC3679: Unused Dynamic Host Configuration Protocol (DHCP) Option Codes

View Source
const (
	OptionVIVendorClass               = Option(124)
	OptionVIVendorSpecificInformation = Option(125)
)

From RFC3925: Vendor-Identifying Vendor Options for Dynamic Host Configuration Protocol version 4 (DHCPv4)

View Source
const (
	OptionBCMCSControllerDomainNameList    = Option(88)
	OptionBCMCSControllerIPv4AddressOption = Option(89)
)

From RFC4280: Dynamic Host Configuration Protocol (DHCP) Options for Broadcast and Multicast Control Servers

View Source
const (
	OptionClientLastTransactionTimeOption = Option(91)
	OptionAssociatedIPOption              = Option(92)
)

From RFC4388: Dynamic Host Configuration Protocol (DHCP) Leasequery

View Source
const (
	OptionClientSystem    = Option(93)
	OptionClientNDI       = Option(94)
	OptionUUIDGUID        = Option(97)
	OptionPXEUndefined128 = Option(128)
	OptionPXEUndefined129 = Option(129)
	OptionPXEUndefined130 = Option(130)
	OptionPXEUndefined131 = Option(131)
	OptionPXEUndefined132 = Option(132)
	OptionPXEUndefined133 = Option(133)
	OptionPXEUndefined134 = Option(134)
	OptionPXEUndefined135 = Option(135)
)

From RFC4578: Dynamic Host Configuration Protocol (DHCP) Options for the Intel Preboot eXecution Environment (PXE)

View Source
const (
	OptionPCode = Option(100)
	OptionTCode = Option(101)
)

From RFC4833: Timezone Options for DHCP

View Source
const (
	OptionGeoConfOption = Option(123)
	OptionGeoLoc        = Option(144)
)

From RFC6225: Dynamic Host Configuration Protocol Options for Coordinate-Based Location Configuration Information

View Source
const (
	BootRequest = OpCode(1)
	BootReply   = OpCode(2)
)

Message op codes defined in RFC2132.

View Source
const (
	OptionAuthentication = Option(90)
)

From RFC3118: Authentication for DHCP Messages

View Source
const (
	OptionAutoConfig = Option(116)
)

From RFC2563: DHCP Option to Disable Stateless Auto-Configuration in IPv4 Clients

View Source
const (
	OptionCCC = Option(122)
)

From RFC3495: Dynamic Host Configuration Protocol (DHCP) Option for CableLabs Client Configuration

View Source
const (
	OptionClasslessStaticRouteOption = Option(121)
)

From RFC3442: The Classless Static Route Option for Dynamic Host Configuration Protocol (DHCP) version 4

View Source
const (
	OptionClientFQDN = Option(81)
)

From RFC4702: The Dynamic Host Configuration Protocol (DHCP) Client Fully Qualified Domain Name (FQDN) Option

View Source
const (
	OptionDomainSearch = Option(119)
)

From RFC3397: Dynamic Host Configuration Protocol (DHCP) Domain Search Option

View Source
const (
	OptionGeoConfCivic = Option(99)
)

From RFC4776: Dynamic Host Configuration Protocol (DHCPv4 and DHCPv6) Option for Civic Addresses Configuration Information

View Source
const (
	OptionNameServiceSearch = Option(117)
)

From RFC2937: The Name Service Search Option for DHCP

View Source
const (
	OptionRapidCommit = Option(80)
)

From RFC4039: Rapid Commit Option for the Dynamic Host Configuration Protocol version 4 (DHCPv4)

View Source
const (
	OptionRelayAgentInformation = Option(82)
)

From RFC3046: DHCP Relay Agent Information Option

View Source
const (
	OptionSIPServersDHCPOption = Option(120)
)

From RFC3361: Dynamic Host Configuration Protocol (DHCP-for-IPv4) Option for Session Initiation Protocol (SIP) Servers

View Source
const (
	OptionSubnetSelectionOption = Option(118)
)

From RFC3011: The IPv4 Subnet Selection Option for DHCP

View Source
const (
	OptionUserAuth = Option(98)
)

From RFC2485: DHCP Option for The Open Group\x27s User Authentication Protocol

View Source
const (
	OptionUserClass = Option(77)
)

From RFC3004: The User Class Option for DHCP

View Source
const (
	OptioniSNS = Option(83)
)

From RFC4174: The IPv4 Dynamic Host Configuration Protocol (DHCP) Option for the Internet Storage Name Service

Variables

View Source
var (
	ErrShortPacket   = errors.New("dhcpv4: short packet")
	ErrInvalidPacket = errors.New("dhcpv4: invalid packet")
)

Functions

func PacketToBytes

func PacketToBytes(p Packet, opts *packetToBytesOptions) ([]byte, error)

PacketToBytes serializes the DHCP packet pointed to by p into its wire-level representation. The function may return an error if it cannot successfully serialize the packet. Otherwise, it returns a newly created byte slice.

func Serve

func Serve(pc PacketConn, h Handler) error

Serve reads packets off the network and calls the specified handler.

func Validate

func Validate(p Packet, vs []Validation) error

Types

type DHCPAck

type DHCPAck struct {
	Packet
	// contains filtered or unexported fields
}

DHCPAck is a server to client packet with configuration parameters, including committed network address.

func CreateDHCPAck

func CreateDHCPAck(req Request) DHCPAck

func (DHCPAck) Request

func (d DHCPAck) Request() Request

func (DHCPAck) ToBytes

func (d DHCPAck) ToBytes() ([]byte, error)

func (DHCPAck) Validate

func (d DHCPAck) Validate() error

type DHCPDecline

type DHCPDecline struct {
	Packet
}

DHCPDecline is a client to server packet indicating network address is already in use.

type DHCPDiscover

type DHCPDiscover struct {
	Packet
	ReplyWriter
}

DHCPDiscover is a client broadcast packet to locate available servers.

type DHCPInform

type DHCPInform struct {
	Packet
	ReplyWriter
}

DHCPInform is a client to server packet, asking only for local configuration parameters; client already has externally configured network address.

type DHCPNak

type DHCPNak struct {
	Packet
	// contains filtered or unexported fields
}

DHCPNak is a server to client packet indicating client's notion of network address is incorrect (e.g., client has moved to new subnet) or client's lease as expired.

func CreateDHCPNak

func CreateDHCPNak(req Request) DHCPNak

func (DHCPNak) Request

func (d DHCPNak) Request() Request

func (DHCPNak) ToBytes

func (d DHCPNak) ToBytes() ([]byte, error)

func (DHCPNak) Validate

func (d DHCPNak) Validate() error

type DHCPOffer

type DHCPOffer struct {
	Packet
	// contains filtered or unexported fields
}

DHCPOffer is a server to client packet in response to DHCPDISCOVER with offer of configuration parameters.

func CreateDHCPOffer

func CreateDHCPOffer(req Request) DHCPOffer

func (DHCPOffer) Request

func (d DHCPOffer) Request() Request

func (DHCPOffer) ToBytes

func (d DHCPOffer) ToBytes() ([]byte, error)

func (DHCPOffer) Validate

func (d DHCPOffer) Validate() error

type DHCPRelease

type DHCPRelease struct {
	Packet
}

DHCPRelease is a client to server packet relinquishing network address and cancelling remaining lease.

type DHCPRequest

type DHCPRequest struct {
	Packet
	ReplyWriter
}

DHCPRequest is a client message to servers either (a) requesting offered parameters from one server and implicitly declining offers from all others, (b) confirming correctness of previously allocated address after, e.g., system reboot, or (c) extending the lease on a particular network address.

type Handler

type Handler interface {
	ServeDHCP(req Request)
}

Handler defines the interface an object needs to implement to handle DHCP packets. The handler should do a type switch on the Request object that is passed as argument to determine what kind of packet it is dealing with. It can use the WriteReply function on the request to send a reply back to the peer responsible for sending the request packet. While the handler may be blocking, it is not encouraged. Rather, the handler should return as soon as possible to avoid blocking the serve loop. If blocking operations need to be executed to determine if the request packet needs a reply, and if so, what kind of reply, it is recommended to handle this in separate goroutines. The WriteReply function can be called from multiple goroutines without needing extra synchronization.

type MessageType

type MessageType byte

MessageType is the type for the various DHCP messages defined in RFC2132.

type OpCode

type OpCode byte

type Option

type Option byte

Option is the type for DHCP option tags.

type OptionGetter

type OptionGetter interface {
	GetOption(Option) ([]byte, bool)
	GetMessageType() MessageType
	GetUint8(Option) (uint8, bool)
	GetUint16(Option) (uint16, bool)
	GetUint32(Option) (uint32, bool)
	GetString(Option) (string, bool)
	GetIP(Option) (net.IP, bool)
	GetDuration(Option) (time.Duration, bool)
}

OptionGetter defines a bag of functions that can be used to get options.

type OptionMap

type OptionMap map[Option][]byte

OptionMap maps DHCP option tags to their values.

func (OptionMap) Decode

func (om OptionMap) Decode(dst interface{})

func (OptionMap) Deserialize

func (om OptionMap) Deserialize(x []byte, opts *OptionMapDeserializeOptions) error

Deserialize reads options from the []byte into the option map.

func (OptionMap) Encode

func (om OptionMap) Encode(dst interface{})

func (OptionMap) GetDuration

func (om OptionMap) GetDuration(o Option) (time.Duration, bool)

GetDuration gets the duration value of an option, stored as a 32 bit unsigned integer.

func (OptionMap) GetIP

func (om OptionMap) GetIP(o Option) (net.IP, bool)

GetIP gets the IP value of an option.

func (OptionMap) GetMessageType

func (om OptionMap) GetMessageType() MessageType

GetMessageType gets the message type from the DHCPMsgType option field.

func (OptionMap) GetOption

func (om OptionMap) GetOption(o Option) ([]byte, bool)

GetOption gets the []byte value of an option.

func (OptionMap) GetString

func (om OptionMap) GetString(o Option) (string, bool)

GetString gets the string value of an option.

func (OptionMap) GetUint16

func (om OptionMap) GetUint16(o Option) (uint16, bool)

GetUint16 gets the 16 bit unsigned integer value of an option.

func (OptionMap) GetUint32

func (om OptionMap) GetUint32(o Option) (uint32, bool)

GetUint32 gets the 32 bit unsigned integer value of an option.

func (OptionMap) GetUint8

func (om OptionMap) GetUint8(o Option) (uint8, bool)

GetUint8 gets the 8 bit unsigned integer value of an option.

func (OptionMap) Serialize

func (om OptionMap) Serialize() []byte

Serialize writes the contents of the option map to a byte slice.

func (OptionMap) SetDuration

func (om OptionMap) SetDuration(o Option, v time.Duration)

SetDuration sets the duration value of an option, stored as a 32 bit unsigned integer.

func (OptionMap) SetIP

func (om OptionMap) SetIP(o Option, v net.IP)

GetIP sets the IP value of an option.

func (OptionMap) SetMessageType

func (om OptionMap) SetMessageType(m MessageType)

SetMessageType sets the message type in the DHCPMsgType option field.

func (OptionMap) SetOption

func (om OptionMap) SetOption(o Option, v []byte)

SetOption sets the []byte value of an option.

func (OptionMap) SetString

func (om OptionMap) SetString(o Option, v string)

SetString sets the string value of an option.

func (OptionMap) SetUint16

func (om OptionMap) SetUint16(o Option, v uint16)

SetUint16 sets the 16 bit unsigned integer value of an option.

func (OptionMap) SetUint32

func (om OptionMap) SetUint32(o Option, v uint32)

SetUint32 sets the 32 bit unsigned integer value of an option.

func (OptionMap) SetUint8

func (om OptionMap) SetUint8(o Option, v uint8)

SetUint8 sets the 8 bit unsigned integer value of an option.

type OptionMapDeserializeOptions

type OptionMapDeserializeOptions struct {
	IgnoreMissingEndTag bool
}

type OptionSetter

type OptionSetter interface {
	SetOption(Option, []byte)
	SetMessageType(MessageType)
	SetUint8(Option, uint8)
	SetUint16(Option, uint16)
	SetUint32(Option, uint32)
	SetString(Option, string)
	SetIP(Option, net.IP)
	SetDuration(Option, time.Duration)
}

OptionSetter defines a bag of functions that can be used to set options.

type Packet

type Packet struct {
	RawPacket
	OptionMap
	// contains filtered or unexported fields
}

func NewPacket

func NewPacket(o OpCode) Packet

NewPacket creates and returns a new packet with the specified OpCode.

func NewReply

func NewReply(req PacketGetter) Packet

NewReply creates and returns a new reply packet given a request.

func PacketFromBytes

func PacketFromBytes(b []byte) (Packet, error)

PacketFromBytes deserializes the wire-level representation of a DHCP packet contained in the []byte b into a Packet struct. The function returns an error if the packet is malformed. The contents of []byte b is copied into the resulting structure and can be reused after this function has returned.

func (Packet) InterfaceIndex

func (p Packet) InterfaceIndex() int

InterfaceIndex returns the interface index this packet was received on.

type PacketConn

type PacketConn interface {
	PacketReader
	PacketWriter

	Close() error
	LocalAddr() net.Addr
}

PacketConn groups PacketReader and PacketWriter to form a subset of net.PacketConn.

func NewPacketConn

func NewPacketConn(pc net.PacketConn) (PacketConn, error)

NewPacketConn returns a PacketConn based on the specified net.PacketConn. It adds functionality to return the interface index from calls to ReadFrom and include the interface index argument in calls to WriteTo.

type PacketGetter

type PacketGetter interface {
	GetHType() uint8
	GetHLen() uint8
	GetXID() []byte
	GetFlags() []byte
	GetCHAddr() []byte

	GetCIAddr() net.IP
	GetYIAddr() net.IP
	GetSIAddr() net.IP
	GetGIAddr() net.IP
}

type PacketReader

type PacketReader interface {
	ReadFrom(b []byte) (n int, addr net.Addr, ifindex int, err error)
}

PacketReader defines an adaptation of the ReadFrom function (as defined net.PacketConn) that includes the interface index the packet arrived on.

type PacketSetter

type PacketSetter interface {
	SetCIAddr(ip net.IP)
	SetYIAddr(ip net.IP)
	SetSIAddr(ip net.IP)
	SetGIAddr(ip net.IP)
}

type PacketWriter

type PacketWriter interface {
	WriteTo(b []byte, addr net.Addr, ifindex int) (n int, err error)
}

PacketWriter defines an adaptation of the WriteTo function (as defined net.PacketConn) that includes the interface index the packet should be sent on.

type RawPacket

type RawPacket []byte

func (RawPacket) CHAddr

func (p RawPacket) CHAddr() []byte

func (RawPacket) CIAddr

func (p RawPacket) CIAddr() []byte

func (RawPacket) Cookie

func (p RawPacket) Cookie() []byte

Cookie returns the fixed-value prefix to the `options` portion of the packet. According to the RFC, this should equal the 4-octet { 99, 130, 83, 99 }.

func (RawPacket) File

func (p RawPacket) File() []byte

File returns the `file` portion of the packet. This field can be used as extra space to extend the DHCP options, if necessary. To enable this, the "Option Overload" option needs to be set in the regular options. Also see RFC2132, section 9.3.

func (RawPacket) Flags

func (p RawPacket) Flags() []byte

func (RawPacket) GIAddr

func (p RawPacket) GIAddr() []byte

func (RawPacket) GetCHAddr

func (p RawPacket) GetCHAddr() []byte

GetCHAddr gets the client's hardware address.

func (RawPacket) GetCIAddr

func (p RawPacket) GetCIAddr() net.IP

GetCIAddr gets the current IP address of the client.

func (RawPacket) GetFlags

func (p RawPacket) GetFlags() []byte

GetFlags gets the packet's flags.

func (RawPacket) GetGIAddr

func (p RawPacket) GetGIAddr() net.IP

GetGIAddr gets the IP address of the relay agent.

func (RawPacket) GetHLen

func (p RawPacket) GetHLen() uint8

GetHLen gets the hardware address length.

func (RawPacket) GetHType

func (p RawPacket) GetHType() uint8

GetHType gets the hardware address type.

func (RawPacket) GetSIAddr

func (p RawPacket) GetSIAddr() net.IP

GetSIAddr gets the IP address of the next server to use in bootstrap.

func (RawPacket) GetXID

func (p RawPacket) GetXID() []byte

GetXID gets the packet's transaction ID.

func (RawPacket) GetYIAddr

func (p RawPacket) GetYIAddr() net.IP

GetYIAddr gets the IP address offered or assigned to the client.

func (RawPacket) HLen

func (p RawPacket) HLen() []byte

func (RawPacket) HType

func (p RawPacket) HType() []byte

func (RawPacket) Hops

func (p RawPacket) Hops() []byte

func (RawPacket) Op

func (p RawPacket) Op() []byte

func (RawPacket) Options

func (p RawPacket) Options() []byte

Options returns the variable-sized `options` portion of the packet.

func (RawPacket) ParseOptions

func (p RawPacket) ParseOptions() (OptionMap, error)

func (RawPacket) SIAddr

func (p RawPacket) SIAddr() []byte

func (RawPacket) SName

func (p RawPacket) SName() []byte

SName returns the `sname` portion of the packet. This field can be used as extra space to extend the DHCP options, if necessary. To enable this, the "Option Overload" option needs to be set in the regular options. Also see RFC2132, section 9.3.

func (RawPacket) Secs

func (p RawPacket) Secs() []byte

func (RawPacket) SetCIAddr

func (p RawPacket) SetCIAddr(ip net.IP)

SetCIAddr sets the current IP address of the client.

From RFC2131 section 3.5: The client fills in the 'ciaddr' field only when correctly configured with an IP address in BOUND, RENEWING or REBINDING state.

func (RawPacket) SetGIAddr

func (p RawPacket) SetGIAddr(ip net.IP)

SetGIAddr sets the IP address of the relay agent.

From RFC2131 section 2: Relay agent IP address, used in booting via a relay agent.

func (RawPacket) SetSIAddr

func (p RawPacket) SetSIAddr(ip net.IP)

SetSIAddr sets the IP address of the next server to use in bootstrap.

From RFC2131 section 2: DHCP clarifies the interpretation of the 'siaddr' field as the address of the server to use in the next step of the client's bootstrap process. Returned in DHCPOFFER, DHCPACK by server.

func (RawPacket) SetYIAddr

func (p RawPacket) SetYIAddr(ip net.IP)

SetYIAddr sets the IP address offered or assigned to the client.

From RFC2131 section 3.1: Each server may respond with a DHCPOFFER message that includes an available network address in the 'yiaddr' field.

func (RawPacket) XID

func (p RawPacket) XID() []byte

func (RawPacket) YIAddr

func (p RawPacket) YIAddr() []byte

type Reply

type Reply interface {
	Validate() error
	ToBytes() ([]byte, error)
	Request() Request

	PacketSetter
	OptionSetter
}

Reply defines an interface implemented by DHCP replies.

type ReplyWriter

type ReplyWriter interface {
	WriteReply(r Reply) error
}

ReplyWriter defines an interface for the object that writes a reply to the network to the intended received, be it via broadcast or unicast.

type Request

type Request interface {
	PacketGetter
	OptionGetter

	// Every request can tell where it came from.
	InterfaceIndex() int
}

Request

type Validation

type Validation interface {
	Validate(p Packet) error
}

func ValidateAllowedOptions

func ValidateAllowedOptions(os []Option) Validation

func ValidateMust

func ValidateMust(o Option) Validation

func ValidateMustNot

func ValidateMustNot(o Option) Validation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL