dhcp

package
v0.3.10-0-alpha2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 17 Imported by: 4

Documentation

Overview

Package dhcp provides building blocks for DHCP clients and servers.

Index

Constants

View Source
const (
	// DHCP client architecture values
	// - https://datatracker.ietf.org/doc/html/rfc4578#section-2.1
	// - https://github.com/ipxe/ipxe/blob/236299baa32452c79a59138c44eca5fcf4a918f9/src/include/ipxe/dhcp.h#L275-L305
	// 0. Intel x86 PC
	CLIENT_ARCH_INTEL_X86PC = iota
	// 1. NEC/PC98
	CLIENT_ARCH_NEC_PC98
	// 2. EFI Itenium
	CLIENT_ARCH_EFI_ITANIUM
	// 3. DEC alpha
	CLIENT_ARCH_DEC_ALPHA
	// 4. Arc x86
	CLIENT_ARCH_ARC_X86
	// 5. Intel Lean Client
	CLIENT_ARCH_INTEL_LEAN_CLIENT
	// 6. EFI IA32
	CLIENT_ARCH_EFI_IA32
	// 7. EFI BC
	CLIENT_ARCH_EFI_BC
	// 8. EFI Xscale
	CLIENT_ARCH_EFI_XSCALE
	// 9. EFI x86_64
	CLIENT_ARCH_EFI_X86_64
	// 10. EFI 32-bit ARM
	CLIENT_ARCH_EFI_ARM32
	// 11. EFI 64-bit ARM
	CLIENT_ARCH_EFI_ARM64
)

Variables

View Source
var (
	ErrOptionWrongSize = errors.New("option value is the wrong size")
)

Functions

func GetOptIP

func GetOptIP(ip net.IP) []byte

func GetOptIPs

func GetOptIPs(ips []net.IP) []byte

func GetOptTime

func GetOptTime(d time.Duration) []byte

func GetOptUint16

func GetOptUint16(val uint16) []byte

func IsPXERequest

func IsPXERequest(pkt Packet) bool

func IsUEFIPxeArch

func IsUEFIPxeArch(arch uint16) bool

func NewDHCPServer2

func NewDHCPServer2(iface string, dhcpServerPort, dhcpRelayPort uint16) (*DHCPServer, *Conn, error)

raw socket

Types

type Conn

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

Conn is a DHCP-oriented packet socket.

Multiple goroutines may invoke methods on a Conn simultaneously.

func NewConn

func NewConn(addr string, disableBroadcast bool) (*Conn, error)

NewConn creates a Conn bound to the given UDP ip:port.

func NewRawSocketConn

func NewRawSocketConn(iface string, filter []bpf.RawInstruction, dhcpServerPort uint16) (*Conn, error)

func NewSnooperConn

func NewSnooperConn(addr string) (*Conn, error)

NewSnooperConn creates a Conn that listens on the given UDP ip:port.

Unlike NewConn, NewSnooperConn does not bind to the ip:port, enabling the Conn to coexist with other services on the machine.

func NewSocketConn

func NewSocketConn(addr string, port int) (*Conn, error)

func (*Conn) Close

func (c *Conn) Close() error

Close closes the DHCP socket. Any blocked Read or Write operations will be unblocked and return errors.

func (*Conn) RecvDHCP

func (c *Conn) RecvDHCP() (Packet, *net.UDPAddr, net.HardwareAddr, *net.Interface, error)

RecvDHCP reads a Packet from the connection. It returns the packet and the interface it was received on.

func (*Conn) SendDHCP

func (c *Conn) SendDHCP(pkt Packet, addr *net.UDPAddr, mac net.HardwareAddr, intf *net.Interface) error

SendDHCP sends pkt. The precise transmission mechanism depends on pkt.txType(). intf should be the net.Interface returned by RecvDHCP if responding to a DHCP client, or the interface for which configuration is desired if acting as a client.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls. If the deadline is reached, Read will fail with a timeout (see net.Error) instead of blocking. A zero value for t means Read will not time out.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls. If the deadline is reached, Write will fail with a timeout (see net.Error) instead of blocking. A zero value for t means Write will not time out.

type DHCPHandler

type DHCPHandler interface {
	ServeDHCP(pkt Packet, addr *net.UDPAddr, intf *net.Interface) (Packet, []string, error)
}

type DHCPServer

type DHCPServer struct {
	Address string
	Port    int
	// contains filtered or unexported fields
}

func NewDHCPServer

func NewDHCPServer(address string, port int) (*DHCPServer, error)

net.ListenPacket

func NewDHCPServer3

func NewDHCPServer3(address string, port int) (*DHCPServer, error)

udp socket

func (*DHCPServer) GetConn

func (s *DHCPServer) GetConn() *Conn

func (*DHCPServer) ListenAndServe

func (s *DHCPServer) ListenAndServe(handler DHCPHandler) error

type MessageType

type MessageType byte // Option 53
const (
	Discover MessageType = 1 // Broadcast Packet From Client - Can I have an IP?
	Offer    MessageType = 2 // Broadcast From Server - Here's an IP
	Request  MessageType = 3 // Broadcast From Client - I'll take that IP (Also start for renewals)
	Decline  MessageType = 4 // Broadcast From Client - Sorry I can't use that IP
	ACK      MessageType = 5 // From Server, Yes you can have that IP
	NAK      MessageType = 6 // From Server, No you cannot have that IP
	Release  MessageType = 7 // From Client, I don't need that IP anymore
	Inform   MessageType = 8 // From Client, I have this IP and there's nothing you can do about it
)

DHCP Message Type 53

type OpCode

type OpCode byte
const (
	BootRequest OpCode = 1 // From Client
	BootReply   OpCode = 2 // From Server
)

OpCodes

type Option

type Option struct {
	Code  OptionCode
	Value []byte
}

Option is a DHCP option.

type OptionCode

type OptionCode byte
const (
	PXECLIENT = "PXEClient"

	OptClasslessRouteLin OptionCode = OptionClasslessRouteFormat //Classless Static Route Option
	OptClasslessRouteWin OptionCode = 249
)
const (
	End                          OptionCode = 255
	Pad                          OptionCode = 0
	OptionSubnetMask             OptionCode = 1
	OptionTimeOffset             OptionCode = 2
	OptionRouter                 OptionCode = 3
	OptionTimeServer             OptionCode = 4
	OptionNameServer             OptionCode = 5
	OptionDomainNameServer       OptionCode = 6
	OptionLogServer              OptionCode = 7
	OptionCookieServer           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

	// IP Layer Parameters per Host
	OptionIPForwardingEnableDisable          OptionCode = 19
	OptionNonLocalSourceRoutingEnableDisable OptionCode = 20
	OptionPolicyFilter                       OptionCode = 21
	OptionMaximumDatagramReassemblySize      OptionCode = 22
	OptionDefaultIPTimeToLive                OptionCode = 23
	OptionPathMTUAgingTimeout                OptionCode = 24
	OptionPathMTUPlateauTable                OptionCode = 25

	// IP Layer Parameters per Interface
	OptionInterfaceMTU              OptionCode = 26
	OptionAllSubnetsAreLocal        OptionCode = 27
	OptionBroadcastAddress          OptionCode = 28
	OptionPerformMaskDiscovery      OptionCode = 29
	OptionMaskSupplier              OptionCode = 30
	OptionPerformRouterDiscovery    OptionCode = 31
	OptionRouterSolicitationAddress OptionCode = 32
	OptionStaticRoute               OptionCode = 33

	// Link Layer Parameters per Interface
	OptionTrailerEncapsulation  OptionCode = 34
	OptionARPCacheTimeout       OptionCode = 35
	OptionEthernetEncapsulation OptionCode = 36

	// TCP Parameters
	OptionTCPDefaultTTL        OptionCode = 37
	OptionTCPKeepaliveInterval OptionCode = 38
	OptionTCPKeepaliveGarbage  OptionCode = 39

	// Application and Service Parameters
	OptionNetworkInformationServiceDomain            OptionCode = 40
	OptionNetworkInformationServers                  OptionCode = 41
	OptionNetworkTimeProtocolServers                 OptionCode = 42
	OptionVendorSpecificInformation                  OptionCode = 43
	OptionNetBIOSOverTCPIPNameServer                 OptionCode = 44
	OptionNetBIOSOverTCPIPDatagramDistributionServer OptionCode = 45
	OptionNetBIOSOverTCPIPNodeType                   OptionCode = 46
	OptionNetBIOSOverTCPIPScope                      OptionCode = 47
	OptionXWindowSystemFontServer                    OptionCode = 48
	OptionXWindowSystemDisplayManager                OptionCode = 49
	OptionNetworkInformationServicePlusDomain        OptionCode = 64
	OptionNetworkInformationServicePlusServers       OptionCode = 65
	OptionMobileIPHomeAgent                          OptionCode = 68
	OptionSimpleMailTransportProtocol                OptionCode = 69
	OptionPostOfficeProtocolServer                   OptionCode = 70
	OptionNetworkNewsTransportProtocol               OptionCode = 71
	OptionDefaultWorldWideWebServer                  OptionCode = 72
	OptionDefaultFingerServer                        OptionCode = 73
	OptionDefaultInternetRelayChatServer             OptionCode = 74
	OptionStreetTalkServer                           OptionCode = 75
	OptionStreetTalkDirectoryAssistance              OptionCode = 76

	OptionRelayAgentInformation OptionCode = 82

	// DHCP Extensions
	OptionRequestedIPAddress     OptionCode = 50
	OptionIPAddressLeaseTime     OptionCode = 51
	OptionOverload               OptionCode = 52
	OptionDHCPMessageType        OptionCode = 53
	OptionServerIdentifier       OptionCode = 54
	OptionParameterRequestList   OptionCode = 55
	OptionMessage                OptionCode = 56
	OptionMaximumDHCPMessageSize OptionCode = 57
	OptionRenewalTimeValue       OptionCode = 58
	OptionRebindingTimeValue     OptionCode = 59
	OptionVendorClassIdentifier  OptionCode = 60
	OptionClientIdentifier       OptionCode = 61

	OptionTFTPServerName OptionCode = 66
	OptionBootFileName   OptionCode = 67

	OptionUserClass OptionCode = 77

	OptionClientArchitecture               OptionCode = 93
	OptionClientNetworkInterfaceIdentifier OptionCode = 94
	OptionClientMachineIdentifier          OptionCode = 97

	OptionTZPOSIXString    OptionCode = 100
	OptionTZDatabaseString OptionCode = 101

	OptionDomainSearch OptionCode = 119

	OptionClasslessRouteFormat OptionCode = 121

	// From RFC3942 - Options Used by PXELINUX
	OptionPxelinuxMagic      OptionCode = 208
	OptionPxelinuxConfigfile OptionCode = 209
	OptionPxelinuxPathprefix OptionCode = 210
	OptionPxelinuxReboottime OptionCode = 211
)

DHCP Options

type Options

type Options map[OptionCode][]byte

Map of DHCP options

func (Options) Byte

func (o Options) Byte(n OptionCode) (byte, error)

func (Options) Bytes

func (o Options) Bytes(code OptionCode) ([]byte, error)

func (Options) IP

func (o Options) IP(n OptionCode) (net.IP, error)

func (Options) IPMask

func (o Options) IPMask(n OptionCode) (net.IPMask, error)

func (Options) IPs

func (o Options) IPs(n OptionCode) ([]net.IP, error)

func (Options) String

func (o Options) String(n OptionCode) (string, error)

func (Options) Uint16

func (o Options) Uint16(n OptionCode) (uint16, error)

func (Options) Uint32

func (o Options) Uint32(n OptionCode) (uint32, error)

type Packet

type Packet []byte

A DHCP packet

func MakeReplyPacket

func MakeReplyPacket(pkt Packet, conf *ResponseConfig) (Packet, error)

func NewPacket

func NewPacket(opCode OpCode) Packet

func ReplyPacket

func ReplyPacket(req Packet, mt MessageType, serverId, yIAddr net.IP, leaseDuration time.Duration, options []Option) Packet

ReplyPacket creates a reply packet that a Server would send to a client. It uses the req Packet param to copy across common/necessary fields to associate the reply the request.

func RequestPacket

func RequestPacket(mt MessageType, chAddr net.HardwareAddr, cIAddr net.IP, xId []byte, broadcast bool, options []Option) Packet

Creates a request packet that a Client would send to a server.

func Unmarshal

func Unmarshal(b []byte) Packet

func (*Packet) AddOption

func (p *Packet) AddOption(o OptionCode, value []byte)

Appends a DHCP option to the end of a packet

func (Packet) Broadcast

func (p Packet) Broadcast() bool

func (Packet) CHAddr

func (p Packet) CHAddr() net.HardwareAddr

func (Packet) CIAddr

func (p Packet) CIAddr() net.IP

func (Packet) Cookie

func (p Packet) Cookie() []byte

func (Packet) File

func (p Packet) File() []byte

BOOTP legacy

func (Packet) Flags

func (p Packet) Flags() []byte

func (Packet) GIAddr

func (p Packet) GIAddr() net.IP

func (Packet) GetOptionValue

func (p Packet) GetOptionValue(code OptionCode) []byte

func (Packet) HLen

func (p Packet) HLen() byte

func (Packet) HType

func (p Packet) HType() byte

func (Packet) Hops

func (p Packet) Hops() byte

func (Packet) Marshal

func (p Packet) Marshal() []byte

func (Packet) OpCode

func (p Packet) OpCode() OpCode

func (Packet) Options

func (p Packet) Options() []byte

func (*Packet) PadToMinSize

func (p *Packet) PadToMinSize()

func (Packet) ParseOptions

func (p Packet) ParseOptions() Options

Parses the packet's options into an Options map

func (Packet) RelayAddr

func (p Packet) RelayAddr() net.IP

func (Packet) SIAddr

func (p Packet) SIAddr() net.IP

func (Packet) SName

func (p Packet) SName() []byte

BOOTP legacy

func (Packet) Secs

func (p Packet) Secs() []byte

func (Packet) SetBroadcast

func (p Packet) SetBroadcast(broadcast bool)

func (Packet) SetCHAddr

func (p Packet) SetCHAddr(a net.HardwareAddr)

func (Packet) SetCIAddr

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

func (Packet) SetCookie

func (p Packet) SetCookie(cookie []byte)

func (Packet) SetFile

func (p Packet) SetFile(file []byte)

BOOTP legacy

func (Packet) SetFlags

func (p Packet) SetFlags(flags []byte)

func (Packet) SetGIAddr

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

func (Packet) SetHType

func (p Packet) SetHType(hType byte)

func (Packet) SetHops

func (p Packet) SetHops(hops byte)

func (Packet) SetOpCode

func (p Packet) SetOpCode(c OpCode)

func (Packet) SetSIAddr

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

func (Packet) SetSName

func (p Packet) SetSName(sName []byte)

BOOTP legacy

func (Packet) SetSecs

func (p Packet) SetSecs(secs []byte)

func (Packet) SetXId

func (p Packet) SetXId(xId []byte)

func (Packet) SetYIAddr

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

func (*Packet) StripOptions

func (p *Packet) StripOptions()

Removes all options from packet.

func (Packet) TransactionID

func (p Packet) TransactionID() string

func (Packet) Type

func (p Packet) Type() MessageType

func (Packet) XId

func (p Packet) XId() []byte

func (Packet) YIAddr

func (p Packet) YIAddr() net.IP

type ResponseConfig

type ResponseConfig struct {
	OsName        string
	ServerIP      net.IP // OptServerIdentifier 54
	ClientIP      net.IP
	Gateway       net.IP        // OptRouters 3
	Domain        string        // OptDomainName 15
	LeaseTime     time.Duration // OptLeaseTime 51
	RenewalTime   time.Duration // OptRenewalTime 58
	BroadcastAddr net.IP        // OptBroadcastAddr 28
	Hostname      string        // OptHostname 12
	SubnetMask    net.IP        // OptSubnetMask 1
	DNSServers    []net.IP      // OptDNSServers
	Routes        [][]string    // TODO: 249 for windows, 121 for linux
	NTPServers    []net.IP      // OptNTPServers 42
	MTU           uint16        // OptMTU 26

	// Relay Info https://datatracker.ietf.org/doc/html/rfc3046
	RelayInfo []byte

	// TFTP config
	BootServer string
	BootFile   string
	BootBlock  uint16
}

http://www.networksorcery.com/enp/rfc/rfc2132.txt

func (ResponseConfig) GetHostname

func (conf ResponseConfig) GetHostname() string

Jump to

Keyboard shortcuts

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