Skip to content

Commit

Permalink
dhcpv4: actually use random number timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf authored and insomniacslk committed Jun 27, 2019
1 parent b428385 commit 498b45c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions dhcpv4/dhcpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dhcpv4

import (
"bytes"
"context"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -45,6 +46,10 @@ const (
bootpMinLen = 300
)

// RandomTimeout is the amount of time to wait until random number generation
// is canceled.
var RandomTimeout = 2 * time.Minute

// magicCookie is the magic 4-byte value at the beginning of the list of options
// in a DHCPv4 packet.
var magicCookie = [4]byte{99, 130, 83, 99}
Expand Down Expand Up @@ -115,9 +120,11 @@ func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error) {
// TransactionID
func GenerateTransactionID() (TransactionID, error) {
var xid TransactionID
n, err := rand.Read(xid[:])
ctx, cancel := context.WithTimeout(context.Background(), RandomTimeout)
defer cancel()
n, err := rand.ReadContext(ctx, xid[:])
if err != nil {
return xid, err
return xid, fmt.Errorf("could not get random number: %v", err)
}
if n != 4 {
return xid, errors.New("invalid random sequence for transaction ID: smaller than 32 bits")
Expand Down

0 comments on commit 498b45c

Please sign in to comment.