diff options
-rw-r--r-- | dhcpv4/dhcpv4.go | 11 |
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") |