dhcp4

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package dhcp4 provides building blocks for DHCP clients and servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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) (*Conn, error)

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

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 (*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.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, 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 MessageType

type MessageType int

MessageType is the type of a DHCP message.

const (
	MsgDiscover MessageType = iota + 1
	MsgOffer
	MsgRequest
	MsgDecline
	MsgAck
	MsgNack
	MsgRelease
	MsgInform
)

Message types as described in RFC 2131.

func (MessageType) String

func (mt MessageType) String() string

type Option

type Option byte

Option is a DHCP option.

const (
	OptSubnetMask         Option = 1  // IPMask
	OptTimeOffset         Option = 2  // int32
	OptRouters            Option = 3  // IPs
	OptDNSServers         Option = 6  // IPs
	OptHostname           Option = 12 // string
	OptBootFileSize       Option = 13 // uint16
	OptDomainName         Option = 15 // string
	OptInterfaceMTU       Option = 26 // uint16
	OptBroadcastAddr      Option = 28 // IP
	OptNTPServers         Option = 42 // IP
	OptVendorSpecific     Option = 43 // []byte
	OptRequestedIP        Option = 50 // IP
	OptLeaseTime          Option = 51 // uint32
	OptOverload           Option = 52 // byte
	OptServerIdentifier   Option = 54 // IP
	OptRequestedOptions   Option = 55 // []byte
	OptMessage            Option = 56 // string
	OptMaximumMessageSize Option = 57 // uint16
	OptRenewalTime        Option = 58 // uint32
	OptRebindingTime      Option = 59 // uint32
	OptVendorIdentifier   Option = 60 // string
	OptClientIdentifier   Option = 61 // string
	OptFQDN               Option = 81 // string
	OptAgentInformation   Option = 82 // struct

	OptTFTPServer      Option = 66 // string
	OptBootFile        Option = 67 // string
	OptDHCPMessageType Option = 53 // byte
)

Some of the more commonly seen DHCP options. Refer to http://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.xhtml for the full authoritative list.

type Options

type Options map[Option][]byte

Options stores DHCP options.

func (Options) Byte

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

Byte returns the value of option n as a byte.

func (Options) Bytes

func (o Options) Bytes(n Option) ([]byte, error)

Bytes returns the value of option n as a byte slice.

func (Options) Copy

func (o Options) Copy() Options

Copy returns a shallow copy of o.

func (Options) GUID

func (o Options) GUID(n Option) (string, error)

GUID returns the value of option n as a guid string.

func (Options) IP

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

IP returns the value of option n as an IPv4 address.

func (Options) IPMask

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

IPMask returns the value of option n as a net.IPMask.

func (Options) IPs

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

IPs returns the value of option n as a list of IPv4 addresses.

func (Options) Int32

func (o Options) Int32(n Option) (int32, error)

Int32 returns the value of option n as an int32.

func (Options) Marshal

func (o Options) Marshal() ([]byte, error)

Marshal returns the wire encoding of o.

func (Options) String

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

String returns the value of option n as a string.

func (Options) Uint16

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

Uint16 returns the value of option n as a uint16.

func (Options) Uint32

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

Uint32 returns the value of option n as a uint32.

func (Options) Unmarshal

func (o Options) Unmarshal(bs []byte) error

Unmarshal parses DHCP options into o.

type Packet

type Packet struct {
	Type          MessageType
	TransactionID []byte // Always 4 bytes
	Broadcast     bool
	HardwareAddr  net.HardwareAddr // Only ethernet supported at the moment

	ClientAddr net.IP // Client's current IP address (it will respond to ARP for this IP)
	YourAddr   net.IP // Client IP address offered/assigned by server
	ServerAddr net.IP // Responding server's IP address
	RelayAddr  net.IP // IP address of DHCP relay agent, if an agent forwarded the request

	BootServerName string
	BootFilename   string

	Options Options
}

Packet represents a DHCP packet.

func Unmarshal

func Unmarshal(bs []byte) (*Packet, error)

Unmarshal parses a DHCP message and returns a Packet.

func (*Packet) DebugString

func (p *Packet) DebugString() string

DebugString prints the contents of a DHCP packet for human consumption.

func (*Packet) Marshal

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

Marshal returns the wire encoding of p.

Jump to

Keyboard shortcuts

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