Documentation ¶
Index ¶
- Constants
- func Connection(co connections.Transport) func(*Client) error
- func CryptoGenerateXID(b []byte)
- func GenerateXID(g func([]byte)) func(*Client) error
- func HardwareAddr(h net.HardwareAddr) func(*Client) error
- func IgnoreServers(s []net.IP) func(*Client) error
- func MathGenerateXID(b []byte)
- func NewInetSock() (connections.Transport, error)
- func NewPacketSock(ifindex int) (connections.Transport, error)
- func Timeout(t time.Duration) func(*Client) error
- type Client
- func (c *Client) BroadcastPacket(packet dhcp4.Packet) (i int, err error)
- func (c *Client) Close() error
- func (c *Client) DeclinePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet
- func (c *Client) DiscoverPacket() dhcp4.Packet
- func (c *Client) GetAcknowledgement(requestPacket *dhcp4.Packet) (dhcp4.Packet, error)
- func (c *Client) GetAcknowledgementWithOptions(requestPacket *dhcp4.Packet, opts DHCP4ClientOptions) (dhcp4.Packet, error)
- func (c *Client) GetOffer(discoverPacket *dhcp4.Packet) (dhcp4.Packet, error)
- func (c *Client) GetOfferWithOptions(xid []byte, opts DHCP4ClientOptions) (dhcp4.Packet, error)
- func (c *Client) Release(dhcpaddr net.UDPAddr, acknowledgement dhcp4.Packet) error
- func (c *Client) ReleasePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet
- func (c *Client) Renew(dhcpaddr net.UDPAddr, acknowledgement dhcp4.Packet) (bool, dhcp4.Packet, error)
- func (c *Client) RenewalRequestPacket(l net.IP, s net.IP) dhcp4.Packet
- func (c *Client) RenewalRequestPacketFromAcknowledgment(a *dhcp4.Packet) dhcp4.Packet
- func (c *Client) RenewalRequestPacketWithOptions(l net.IP, s net.IP, opts DHCP4ClientOptions) dhcp4.Packet
- func (c *Client) Request() (bool, dhcp4.Packet, error)
- func (c *Client) RequestPacket(r net.IP, s net.IP) dhcp4.Packet
- func (c *Client) RequestPacketFromOfferPacket(offerPacket *dhcp4.Packet) dhcp4.Packet
- func (c *Client) RequestPacketWithOptions(r net.IP, s net.IP, opts DHCP4ClientOptions) dhcp4.Packet
- func (c *Client) RequestWithOptions(opts DHCP4ClientOptions) (bool, dhcp4.Packet, error)
- func (c *Client) SendDecline(acknowledgementPacket *dhcp4.Packet) (declinePacket dhcp4.Packet, err error)
- func (c *Client) SendDiscoverPacket() (dhcp4.Packet, error)
- func (c *Client) SendDiscoverPacketWithOptions(opts DHCP4ClientOptions) (dhcp4.Packet, error)
- func (c *Client) SendRequestFromOfferPacket(offerPacket *dhcp4.Packet) (dhcp4.Packet, error)
- func (c *Client) SendRequestFromOfferPacketWithOptions(offerPacket *dhcp4.Packet, opts DHCP4ClientOptions) (requestPacket dhcp4.Packet, err error)
- func (c *Client) SetLaddr(l *net.UDPAddr)
- func (c *Client) SetOption(options ...func(*Client) error) error
- func (c *Client) UnicastPacket(p dhcp4.Packet) (i int, err error)
- type DHCP4ClientOptions
- type DHCP4Error
- Bugs
Examples ¶
Constants ¶
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 ¶
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 ¶
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)
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/d2g/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 ¶
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 ¶
BroadcastPacket Allows you to Broadcast the DHCP4 packets over the UDP Connection.
func (*Client) DeclinePacket ¶
Create Decline Packet
func (*Client) DiscoverPacket ¶
Create Discover Packet
func (*Client) GetAcknowledgement ¶
Retrieve Acknowledgement Wait for the offer for a specific Request Packet.
func (*Client) GetAcknowledgementWithOptions ¶
func (*Client) GetOffer ¶
Retreive Offer... Wait for the offer for a specific Discovery Packet. UDP address contain content of option 54.
func (*Client) GetOfferWithOptions ¶
func (*Client) ReleasePacket ¶
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 (*Client) RenewalRequestPacketFromAcknowledgment ¶
RenewalRequestPacket Generates Create Request Packet For a Renew
func (*Client) RenewalRequestPacketWithOptions ¶
func (*Client) Request ¶
Lets do a Full DHCP Request. The Returned UDP Address is the content of Option 54 of the Offer packet. TODO:
func (*Client) RequestPacket ¶
Create Request Packet r is the IP to request, s is server to request it from
func (*Client) RequestPacketFromOfferPacket ¶
func (*Client) RequestPacketWithOptions ¶
func (*Client) RequestWithOptions ¶
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 ¶
Send the Discovery Packet to the Broadcast Channel
func (*Client) SendDiscoverPacketWithOptions ¶
func (c *Client) SendDiscoverPacketWithOptions(opts DHCP4ClientOptions) (dhcp4.Packet, error)
func (*Client) SendRequestFromOfferPacket ¶
Send Request Based On the offer Received.
func (*Client) SendRequestFromOfferPacketWithOptions ¶
func (*Client) SetLaddr ¶
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.
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().
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 ¶
Hardcoded to contect the server on port 67.
Which directly contradicts [RFC 2131 p28]
ACK packet doesn't have to contain the server identifier. https://tools.ietf.org/html/rfc2132#section-9.7
Should Use the Server Identifier Option