Skip to content

Commit

Permalink
Merge pull request #18 from stapelberg/rand
Browse files Browse the repository at this point in the history
Default to math/rand in all places, add RandRead option
  • Loading branch information
d2g authored Jun 11, 2018
2 parents 30bb0ed + ac8e065 commit e612998
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package dhcp4client

import (
"bytes"
"hash/fnv"
"math/rand"
"net"
"sync"
"time"

"github.com/d2g/dhcp4"
Expand Down Expand Up @@ -31,16 +34,35 @@ type ConnectionInt interface {

func New(options ...func(*Client) error) (*Client, error) {
c := Client{
timeout: time.Second * 10,
broadcast: true,
generateXID: CryptoGenerateXID,
timeout: time.Second * 10,
broadcast: true,
}

err := c.SetOption(options...)
if err != nil {
return nil, err
}

if c.generateXID == nil {
// https://tools.ietf.org/html/rfc2131#section-4.1 explains:
//
// A DHCP client MUST choose 'xid's in such a way as to minimize the chance
// of using an 'xid' identical to one used by another client.
//
// Hence, seed a random number generator with the current time and hardware
// address.
h := fnv.New64()
h.Sum(c.hardwareAddr)
seed := int64(h.Sum64()) + time.Now().Unix()
rnd := rand.New(rand.NewSource(seed))
var rndMu sync.Mutex
c.generateXID = func(b []byte) {
rndMu.Lock()
defer rndMu.Unlock()
rnd.Read(b)
}
}

//if connection hasn't been set as an option create the default.
if c.connection == nil {
conn, err := NewInetSock()
Expand Down Expand Up @@ -299,7 +321,6 @@ func (c *Client) DeclinePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet {
return packet
}


//Lets do a Full DHCP Request.
func (c *Client) Request() (bool, dhcp4.Packet, error) {
discoveryPacket, err := c.SendDiscoverPacket()
Expand Down
2 changes: 1 addition & 1 deletion pktsock_linux.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dhcp4client

import (
"crypto/rand"
"encoding/binary"
"math/rand"
"net"
"time"

Expand Down

0 comments on commit e612998

Please sign in to comment.