summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2019-06-19 15:03:24 -0700
committerinsomniac <insomniacslk@users.noreply.github.com>2019-06-27 18:16:03 +0100
commit498b45cac80e2856fdcebfc0b4ebd446cc7ef87e (patch)
tree21ce5effdb00dc0e278120a7fe04e057ff249abc /dhcpv4
parentb4283850189193486e09713e2a1b505e4ce2d067 (diff)
dhcpv4: actually use random number timeout
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv4')
-rw-r--r--dhcpv4/dhcpv4.go11
1 files changed, 9 insertions, 2 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go
index a6085d5..d76a656 100644
--- a/dhcpv4/dhcpv4.go
+++ b/dhcpv4/dhcpv4.go
@@ -17,6 +17,7 @@ package dhcpv4
import (
"bytes"
+ "context"
"errors"
"fmt"
"net"
@@ -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}
@@ -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")