summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--dhcpv4/bsdp/client.go10
-rw-r--r--dhcpv4/client.go10
-rw-r--r--dhcpv4/server_test.go2
-rw-r--r--dhcpv6/client.go54
-rw-r--r--dhcpv6/server_test.go2
-rw-r--r--netboot/netboot.go13
7 files changed, 40 insertions, 57 deletions
diff --git a/README.md b/README.md
index 9e8850c..9d852fc 100644
--- a/README.md
+++ b/README.md
@@ -59,9 +59,9 @@ func main() {
// return a non-empty packet list even if there is an error. This is
// intended, because the transaction may fail at any point, and we
// still want to know what packets were exchanged until then.
- // The `nil` argument indicates that we want to use a default Solicit
- // packet, instead of specifying a custom one ourselves.
- conversation, err := client.Exchange("eth0", nil)
+ // A default Solicit packet will be used during the "conversation",
+ // which can be manipulated by using modifiers.
+ conversation, err := client.Exchange("eth0")
// Summary() prints a verbose representation of the exchanged packets.
for _, packet := range conversation {
diff --git a/dhcpv4/bsdp/client.go b/dhcpv4/bsdp/client.go
index 8131bec..dd9cbcd 100644
--- a/dhcpv4/bsdp/client.go
+++ b/dhcpv4/bsdp/client.go
@@ -34,7 +34,7 @@ func castVendorOpt(ack *dhcpv4.DHCPv4) {
// Exchange runs a full BSDP exchange (Inform[list], Ack, Inform[select],
// Ack). Returns a list of DHCPv4 structures representing the exchange.
-func (c *Client) Exchange(ifname string, informList *dhcpv4.DHCPv4) ([]*dhcpv4.DHCPv4, error) {
+func (c *Client) Exchange(ifname string) ([]*dhcpv4.DHCPv4, error) {
conversation := make([]*dhcpv4.DHCPv4, 0)
// Get our file descriptor for the broadcast socket.
@@ -48,11 +48,9 @@ func (c *Client) Exchange(ifname string, informList *dhcpv4.DHCPv4) ([]*dhcpv4.D
}
// INFORM[LIST]
- if informList == nil {
- informList, err = NewInformListForInterface(ifname, dhcpv4.ClientPort)
- if err != nil {
- return conversation, err
- }
+ informList, err := NewInformListForInterface(ifname, dhcpv4.ClientPort)
+ if err != nil {
+ return conversation, err
}
conversation = append(conversation, informList)
diff --git a/dhcpv4/client.go b/dhcpv4/client.go
index e9a9ff5..8ec1490 100644
--- a/dhcpv4/client.go
+++ b/dhcpv4/client.go
@@ -179,7 +179,7 @@ func (c *Client) getRemoteUDPAddr() (*net.UDPAddr, error) {
// ordered as Discovery, Offer, Request and Acknowledge. In case of errors, an
// error is returned, and the list of DHCPv4 objects will be shorted than 4,
// containing all the sent and received DHCPv4 messages.
-func (c *Client) Exchange(ifname string, discover *DHCPv4, modifiers ...Modifier) ([]*DHCPv4, error) {
+func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]*DHCPv4, error) {
conversation := make([]*DHCPv4, 0)
raddr, err := c.getRemoteUDPAddr()
if err != nil {
@@ -220,11 +220,9 @@ func (c *Client) Exchange(ifname string, discover *DHCPv4, modifiers ...Modifier
}()
// Discover
- if discover == nil {
- discover, err = NewDiscoveryForInterface(ifname)
- if err != nil {
- return conversation, err
- }
+ discover, err := NewDiscoveryForInterface(ifname)
+ if err != nil {
+ return conversation, err
}
for _, mod := range modifiers {
discover = mod(discover)
diff --git a/dhcpv4/server_test.go b/dhcpv4/server_test.go
index d455786..ab426df 100644
--- a/dhcpv4/server_test.go
+++ b/dhcpv4/server_test.go
@@ -136,7 +136,7 @@ func TestServerActivateAndServe(t *testing.T) {
WithHwAddr(hwaddr[:]),
}
- conv, err := c.Exchange(lo, nil, modifiers...)
+ conv, err := c.Exchange(lo, modifiers...)
require.NoError(t, err)
require.Equal(t, 4, len(conv))
for _, p := range conv {
diff --git a/dhcpv6/client.go b/dhcpv6/client.go
index 79653c8..a31c426 100644
--- a/dhcpv6/client.go
+++ b/dhcpv6/client.go
@@ -37,23 +37,19 @@ func NewClient() *Client {
}
}
-// Exchange executes a 4-way DHCPv6 request (SOLICIT, ADVERTISE, REQUEST,
-// REPLY). If the SOLICIT packet is nil, defaults are used. The modifiers will
-// be applied to the Request packet. A common use is to make sure that the
-// Request packet has the right options, see modifiers.go
-func (c *Client) Exchange(ifname string, solicit DHCPv6, modifiers ...Modifier) ([]DHCPv6, error) {
+// Exchange executes a 4-way DHCPv6 request (Solicit, Advertise, Request,
+// Reply). The modifiers will be applied to the Solicit and Request packets.
+// A common use is to make sure that the Solicit packet has the right options,
+// see modifiers.go
+func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]DHCPv6, error) {
conversation := make([]DHCPv6, 0)
var err error
// Solicit
- if solicit == nil {
- solicit, err = NewSolicitForInterface(ifname)
- if err != nil {
- return conversation, err
- }
+ solicit, advertise, err := c.Solicit(ifname, modifiers...)
+ if solicit != nil {
+ conversation = append(conversation, solicit)
}
- solicit, advertise, err := c.Solicit(ifname, solicit, modifiers...)
- conversation = append(conversation, solicit)
if err != nil {
return conversation, err
}
@@ -67,7 +63,7 @@ func (c *Client) Exchange(ifname string, solicit DHCPv6, modifiers ...Modifier)
return conversation, err
}
}
- request, reply, err := c.Request(ifname, advertise, nil, modifiers...)
+ request, reply, err := c.Request(ifname, advertise, modifiers...)
if request != nil {
conversation = append(conversation, request)
}
@@ -189,15 +185,13 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT
return adv, nil
}
-// Solicit sends a SOLICIT, return the solicit, an ADVERTISE (if not nil), and
-// an error if any
-func (c *Client) Solicit(ifname string, solicit DHCPv6, modifiers ...Modifier) (DHCPv6, DHCPv6, error) {
- var err error
- if solicit == nil {
- solicit, err = NewSolicitForInterface(ifname)
- if err != nil {
- return nil, nil, err
- }
+// Solicit sends a Solicit, returns the Solicit, an Advertise (if not nil), and
+// an error if any. The modifiers will be applied to the Solicit before sending
+// it, see modifiers.go
+func (c *Client) Solicit(ifname string, modifiers ...Modifier) (DHCPv6, DHCPv6, error) {
+ solicit, err := NewSolicitForInterface(ifname)
+ if err != nil {
+ return nil, nil, err
}
for _, mod := range modifiers {
solicit = mod(solicit)
@@ -206,15 +200,13 @@ func (c *Client) Solicit(ifname string, solicit DHCPv6, modifiers ...Modifier) (
return solicit, advertise, err
}
-// Request sends a REQUEST built from an ADVERTISE if no REQUEST is specified.
-// It returns the request, a reply if not nil, and an error if any
-func (c *Client) Request(ifname string, advertise, request DHCPv6, modifiers ...Modifier) (DHCPv6, DHCPv6, error) {
- if request == nil {
- var err error
- request, err = NewRequestFromAdvertise(advertise)
- if err != nil {
- return nil, nil, err
- }
+// Request sends a Request built from an Advertise. It returns the Request, a
+// Reply (if not nil), and an error if any. The modifiers will be applied to
+// the Request before sending it, see modifiers.go
+func (c *Client) Request(ifname string, advertise DHCPv6, modifiers ...Modifier) (DHCPv6, DHCPv6, error) {
+ request, err := NewRequestFromAdvertise(advertise)
+ if err != nil {
+ return nil, nil, err
}
for _, mod := range modifiers {
request = mod(request)
diff --git a/dhcpv6/server_test.go b/dhcpv6/server_test.go
index 587e249..08cb507 100644
--- a/dhcpv6/server_test.go
+++ b/dhcpv6/server_test.go
@@ -88,6 +88,6 @@ func TestServerActivateAndServe(t *testing.T) {
iface, err := getLoopbackInterface()
require.NoError(t, err)
- _, _, err = c.Solicit(iface, nil)
+ _, _, err = c.Solicit(iface)
require.NoError(t, err)
}
diff --git a/netboot/netboot.go b/netboot/netboot.go
index 8bb9ac4..b775d00 100644
--- a/netboot/netboot.go
+++ b/netboot/netboot.go
@@ -19,21 +19,16 @@ var sleeper = func(d time.Duration) {
func RequestNetbootv6(ifname string, timeout time.Duration, retries int, modifiers ...dhcpv6.Modifier) ([]dhcpv6.DHCPv6, error) {
var (
conversation []dhcpv6.DHCPv6
+ err error
)
+ modifiers = append(modifiers, dhcpv6.WithNetboot)
delay := 2 * time.Second
for i := 0; i <= retries; i++ {
log.Printf("sending request, attempt #%d", i+1)
- solicit, err := dhcpv6.NewSolicitForInterface(ifname, modifiers...)
- if err != nil {
- return nil, fmt.Errorf("failed to create SOLICIT for interface %s: %v", ifname, err)
- }
client := dhcpv6.NewClient()
client.ReadTimeout = timeout
- // WithNetboot is added only later, to avoid applying it twice (one
- // here and one in the above call to NewSolicitForInterface)
- modifiers = append(modifiers, dhcpv6.WithNetboot)
- conversation, err = client.Exchange(ifname, solicit, modifiers...)
+ conversation, err = client.Exchange(ifname, modifiers...)
if err != nil {
log.Printf("Client.Exchange failed: %v", err)
log.Printf("sleeping %v before retrying", delay)
@@ -64,7 +59,7 @@ func RequestNetbootv4(ifname string, timeout time.Duration, retries int, modifie
log.Printf("sending request, attempt #%d", i+1)
client := dhcpv4.NewClient()
client.ReadTimeout = timeout
- conversation, err = client.Exchange(ifname, nil, modifiers...)
+ conversation, err = client.Exchange(ifname, modifiers...)
if err != nil {
log.Printf("Client.Exchange failed: %v", err)
log.Printf("sleeping %v before retrying", delay)