dhcp4client

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MPL-2.0 Imports: 15 Imported by: 0

README

dhcp4client GoDoc

DHCP Client

Thanks to:

@eyakubovich For AF_PACKET support.

Documentation

Index

Examples

Constants

View Source
const (
	//The Maximum size of a DHCP4 response. Used to initialise read buffers.
	MaxDHCPLen = 576
)

Variables

This section is empty.

Functions

func Connection

func Connection(co connections.Transport) func(*Client) error

Connection is a function option that allows you to configure which connection(s) type to utilise.

func CryptoGenerateXID

func CryptoGenerateXID(b []byte)

func GenerateXID

func GenerateXID(g func([]byte)) func(*Client) error

GenerateXID is a function option that allows you to set the function responsible for producing a random XID.

func HardwareAddr

func HardwareAddr(h net.HardwareAddr) func(*Client) error

HardwareAddr is a function option to set the Hardware(MAC) address of the client.

func IgnoreServers

func IgnoreServers(s []net.IP) func(*Client) error

IgnoreServers is a function option that allow you to pass an array s of IP's which the client will ignore when the DHCP Server respond with a dhcp4.OptionServerIdentifier (Code 54) in this IP array.

func MathGenerateXID

func MathGenerateXID(b []byte)

func NewInetSock

func NewInetSock() (connections.Transport, error)

func NewPacketSock

func NewPacketSock(ifindex int) (connections.Transport, error)

func Timeout

func Timeout(t time.Duration) func(*Client) error

Timout is a functional options that lets you set all network timeouts on the client.

Types

type Client

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

The Main DHCP Control Client

Example

Basic usage example of using the client to complete a DHCP4 request and getting the allocated IP.

package main

import (
	"fmt"
	"net"

	"github.com/zukadong/dhcp4client"
)

func main() {
	var err error

	m, err := net.ParseMAC("E4-B3-18-64-DC-14")
	if err != nil {
		fmt.Printf("Error: Error Parsing MAC Address:%v\n", err)
		return
	}

	//Create a connection to use
	exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m))
	if err != nil {
		fmt.Printf("Error: Error Creating Client:%v\n", err)
		return
	}
	defer exampleClient.Close()

	//Complete a full DHCP4 Cycle, Discover, Request, Offer, Acknowledgement.
	success, acknowledgementpacket, err := exampleClient.Request()

	if err != nil {
		networkError, ok := err.(net.Error)
		if ok && networkError.Timeout() {
			fmt.Printf("Warning: Didn't get a valid respose from a DHCP server in time.")
			return
		}
		fmt.Printf("Error: Network error when making request %v\n", err)
		return
	}

	if !success {
		fmt.Printf("Warning: We didn't sucessfully get a DHCP Lease")
	} else {
		fmt.Printf("IP address received which can be found in acknowledgementpacket.YIAddr()")

		//Print our IP to stderr as the basic example will get any IP so we can't
		//included it in the tested output.
		fmt.Errorf("Info: IP Received:%s\n", acknowledgementpacket.YIAddr())
	}

}
Output:

IP address received which can be found in acknowledgementpacket.YIAddr()

func New

func New(options ...func(*Client) error) (*Client, error)

Creates a DHCP4Client instance using functional options to configure settings. More information about function options [https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis]

func (*Client) BroadcastPacket

func (c *Client) BroadcastPacket(packet dhcp4.Packet) (i int, err error)

BroadcastPacket Allows you to Broadcast the DHCP4 packets over the UDP Connection.

func (*Client) Close

func (c *Client) Close() error

Close function closes the Client.

func (*Client) DeclinePacket

func (c *Client) DeclinePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet

Create Decline Packet

func (*Client) DiscoverPacket

func (c *Client) DiscoverPacket() dhcp4.Packet

Create Discover Packet

func (*Client) GetAcknowledgement

func (c *Client) GetAcknowledgement(requestPacket *dhcp4.Packet) (dhcp4.Packet, error)

Retrieve Acknowledgement Wait for the offer for a specific Request Packet.

func (*Client) GetAcknowledgementWithOptions

func (c *Client) GetAcknowledgementWithOptions(requestPacket *dhcp4.Packet, opts DHCP4ClientOptions) (dhcp4.Packet, error)

func (*Client) GetOffer

func (c *Client) GetOffer(discoverPacket *dhcp4.Packet) (dhcp4.Packet, error)

Retreive Offer... Wait for the offer for a specific Discovery Packet. UDP address contain content of option 54.

func (*Client) GetOfferWithOptions

func (c *Client) GetOfferWithOptions(xid []byte, opts DHCP4ClientOptions) (dhcp4.Packet, error)

func (*Client) Release

func (c *Client) Release(dhcpaddr net.UDPAddr, acknowledgement dhcp4.Packet) error

Release a lease backed on the Acknowledgement Packet. Returns Any Errors

func (*Client) ReleasePacket

func (c *Client) ReleasePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet

Create Release Packet For a Release

func (*Client) Renew

func (c *Client) Renew(dhcpaddr net.UDPAddr, acknowledgement dhcp4.Packet) (bool, dhcp4.Packet, error)

Renew a lease backed on the Acknowledgement Packet. Returns Sucessfull, The AcknoledgementPacket, Any Errors BUG(d2g): Which directly contradicts [RFC 2131 p28] BUG(d2g): ACK packet doesn't have to contain the server identifier. https://tools.ietf.org/html/rfc2132#section-9.7

func (*Client) RenewalRequestPacket

func (c *Client) RenewalRequestPacket(l net.IP, s net.IP) dhcp4.Packet

func (*Client) RenewalRequestPacketFromAcknowledgment

func (c *Client) RenewalRequestPacketFromAcknowledgment(a *dhcp4.Packet) dhcp4.Packet

RenewalRequestPacket Generates Create Request Packet For a Renew

func (*Client) RenewalRequestPacketWithOptions

func (c *Client) RenewalRequestPacketWithOptions(l net.IP, s net.IP, opts DHCP4ClientOptions) dhcp4.Packet

func (*Client) Request

func (c *Client) Request() (bool, dhcp4.Packet, error)

Lets do a Full DHCP Request. The Returned UDP Address is the content of Option 54 of the Offer packet. TODO:

func (*Client) RequestPacket

func (c *Client) RequestPacket(r net.IP, s net.IP) dhcp4.Packet

Create Request Packet r is the IP to request, s is server to request it from

func (*Client) RequestPacketFromOfferPacket

func (c *Client) RequestPacketFromOfferPacket(offerPacket *dhcp4.Packet) dhcp4.Packet

func (*Client) RequestPacketWithOptions

func (c *Client) RequestPacketWithOptions(r net.IP, s net.IP, opts DHCP4ClientOptions) dhcp4.Packet

func (*Client) RequestWithOptions

func (c *Client) RequestWithOptions(opts DHCP4ClientOptions) (bool, dhcp4.Packet, error)

Full DCHP Call With Client Options The Options Should Contain the requested values and the responded values

func (*Client) SendDecline

func (c *Client) SendDecline(acknowledgementPacket *dhcp4.Packet) (declinePacket dhcp4.Packet, err error)

Send Decline to the received acknowledgement.

func (*Client) SendDiscoverPacket

func (c *Client) SendDiscoverPacket() (dhcp4.Packet, error)

Send the Discovery Packet to the Broadcast Channel

func (*Client) SendDiscoverPacketWithOptions

func (c *Client) SendDiscoverPacketWithOptions(opts DHCP4ClientOptions) (dhcp4.Packet, error)

func (*Client) SendRequestFromOfferPacket

func (c *Client) SendRequestFromOfferPacket(offerPacket *dhcp4.Packet) (dhcp4.Packet, error)

Send Request Based On the offer Received.

func (*Client) SendRequestFromOfferPacketWithOptions

func (c *Client) SendRequestFromOfferPacketWithOptions(offerPacket *dhcp4.Packet, opts DHCP4ClientOptions) (requestPacket dhcp4.Packet, err error)

func (*Client) SetLaddr

func (c *Client) SetLaddr(l *net.UDPAddr)

SetLaddr allows you to change the Local Address used by the Client to make request. This is normally used after you've obtained an IP and brought your interface up to make renewal requests to the DHCP Server.

func (*Client) SetOption

func (c *Client) SetOption(options ...func(*Client) error) error

SetOptions allows you to configure the client. Generally you should be setting the functional options on the creation of the instance.

func (*Client) UnicastPacket

func (c *Client) UnicastPacket(p dhcp4.Packet) (i int, err error)

UnicastPacket Allows you to Unicast Packets to the server passed in the dhcp4.OptionServerIdentifier DHCP4 Packet option. BUG(d2g): Hardcoded to contect the server on port 67.

type DHCP4ClientOptions

type DHCP4ClientOptions map[dhcp4.MessageType][]*dhcp4.Option

DHCP4ClientOptions is used to GET & SET custom DHCP Options in the requests and response packets. The dhcp4.Options is passed in with the values you want to set and those that you want to retreive. The reason that the value is a map is so that all options can be GET at any stage of the request lifecycle in a single call to the RequestWithOptions().

func (DHCP4ClientOptions) String

func (o DHCP4ClientOptions) String() string

TODO(d2g): Tidy Up

type DHCP4Error

type DHCP4Error struct {
	Err         error    // Child Error
	OpCode      int      // The DHCP option code of the action
	Src         net.Addr // connection source
	Dest        net.Addr // destination source
	IsTimeout   bool     // if true, timed out; not all timeouts set this
	IsTemporary bool     // if true, error is temporary; not all errors set this
}

An Error Type with aditoin DHCP4 information.

func (*DHCP4Error) Error

func (e *DHCP4Error) Error() string

func (*DHCP4Error) Temporary

func (e *DHCP4Error) Temporary() bool

func (*DHCP4Error) Timeout

func (e *DHCP4Error) Timeout() bool

Notes

Bugs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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