update for dhcp4client changes
This commit is contained in:
parent
2079aea3a1
commit
c28d816805
@ -3,7 +3,6 @@ package dhcp4
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -36,7 +35,7 @@ type Client struct {
|
|||||||
hardwareAddr net.HardwareAddr
|
hardwareAddr net.HardwareAddr
|
||||||
cfg Config
|
cfg Config
|
||||||
timeNow func() time.Time
|
timeNow func() time.Time
|
||||||
randRead func([]byte) (int, error)
|
generateXID func([]byte)
|
||||||
|
|
||||||
// last DHCPACK packet for renewal/release
|
// last DHCPACK packet for renewal/release
|
||||||
ack dhcp4.Packet
|
ack dhcp4.Packet
|
||||||
@ -48,9 +47,6 @@ func (c *Client) ObtainOrRenew() bool {
|
|||||||
if c.timeNow == nil {
|
if c.timeNow == nil {
|
||||||
c.timeNow = time.Now
|
c.timeNow = time.Now
|
||||||
}
|
}
|
||||||
if c.randRead == nil {
|
|
||||||
c.randRead = rand.Read
|
|
||||||
}
|
|
||||||
if c.connection == nil && c.Interface != nil {
|
if c.connection == nil && c.Interface != nil {
|
||||||
pktsock, err := dhcp4client.NewPacketSock(c.Interface.Index)
|
pktsock, err := dhcp4client.NewPacketSock(c.Interface.Index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,14 +67,15 @@ func (c *Client) ObtainOrRenew() bool {
|
|||||||
dhcp4client.Timeout(5*time.Second),
|
dhcp4client.Timeout(5*time.Second),
|
||||||
dhcp4client.Broadcast(false),
|
dhcp4client.Broadcast(false),
|
||||||
dhcp4client.Connection(c.connection),
|
dhcp4client.Connection(c.connection),
|
||||||
|
dhcp4client.GenerateXID(c.generateXID),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.err = err
|
c.err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dhcp.RandRead = c.randRead
|
|
||||||
c.dhcp = dhcp
|
c.dhcp = dhcp
|
||||||
})
|
})
|
||||||
|
c.err = nil // clear previous error
|
||||||
// TODO: renew if c.ack != nil, fall back if renewal fails
|
// TODO: renew if c.ack != nil, fall back if renewal fails
|
||||||
ok, ack, err := c.dhcpRequest()
|
ok, ack, err := c.dhcpRequest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -64,13 +64,12 @@ func TestDHCP4(t *testing.T) {
|
|||||||
hardwareAddr: mac,
|
hardwareAddr: mac,
|
||||||
timeNow: func() time.Time { return now },
|
timeNow: func() time.Time { return now },
|
||||||
connection: &replayer{pcapr: pcapr},
|
connection: &replayer{pcapr: pcapr},
|
||||||
randRead: func(b []byte) (n int, err error) {
|
generateXID: func(b []byte) {
|
||||||
if got, want := len(b), 4; got != want {
|
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
|
// TODO: read the transaction ID from the pcap file
|
||||||
copy(b, []byte{0x77, 0x08, 0xd7, 0x24})
|
copy(b, []byte{0x77, 0x08, 0xd7, 0x24})
|
||||||
return len(b), nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user