update for dhcp4client changes

This commit is contained in:
Michael Stapelberg 2018-06-02 10:15:50 +02:00
parent 2079aea3a1
commit c28d816805
2 changed files with 5 additions and 9 deletions

View File

@ -3,7 +3,6 @@ package dhcp4
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"log"
@ -36,7 +35,7 @@ type Client struct {
hardwareAddr net.HardwareAddr
cfg Config
timeNow func() time.Time
randRead func([]byte) (int, error)
generateXID func([]byte)
// last DHCPACK packet for renewal/release
ack dhcp4.Packet
@ -48,9 +47,6 @@ func (c *Client) ObtainOrRenew() bool {
if c.timeNow == nil {
c.timeNow = time.Now
}
if c.randRead == nil {
c.randRead = rand.Read
}
if c.connection == nil && c.Interface != nil {
pktsock, err := dhcp4client.NewPacketSock(c.Interface.Index)
if err != nil {
@ -71,14 +67,15 @@ func (c *Client) ObtainOrRenew() bool {
dhcp4client.Timeout(5*time.Second),
dhcp4client.Broadcast(false),
dhcp4client.Connection(c.connection),
dhcp4client.GenerateXID(c.generateXID),
)
if err != nil {
c.err = err
return
}
dhcp.RandRead = c.randRead
c.dhcp = dhcp
})
c.err = nil // clear previous error
// TODO: renew if c.ack != nil, fall back if renewal fails
ok, ack, err := c.dhcpRequest()
if err != nil {

View File

@ -64,13 +64,12 @@ func TestDHCP4(t *testing.T) {
hardwareAddr: mac,
timeNow: func() time.Time { return now },
connection: &replayer{pcapr: pcapr},
randRead: func(b []byte) (n int, err error) {
generateXID: func(b []byte) {
if got, want := len(b), 4; got != want {
return 0, fmt.Errorf("github.com/d2g/dhcp4client request unexpected amount of bytes: got %d, want %d", got, want)
t.Fatalf("github.com/d2g/dhcp4client request unexpected amount of bytes: got %d, want %d", got, want)
}
// TODO: read the transaction ID from the pcap file
copy(b, []byte{0x77, 0x08, 0xd7, 0x24})
return len(b), nil
},
}