Skip to content

Commit 498b45c

Browse files
hugelgupfinsomniacslk
authored andcommitted
dhcpv4: actually use random number timeout
Signed-off-by: Chris Koch <[email protected]>
1 parent b428385 commit 498b45c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

dhcpv4/dhcpv4.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package dhcpv4
1717

1818
import (
1919
"bytes"
20+
"context"
2021
"errors"
2122
"fmt"
2223
"net"
@@ -45,6 +46,10 @@ const (
4546
bootpMinLen = 300
4647
)
4748

49+
// RandomTimeout is the amount of time to wait until random number generation
50+
// is canceled.
51+
var RandomTimeout = 2 * time.Minute
52+
4853
// magicCookie is the magic 4-byte value at the beginning of the list of options
4954
// in a DHCPv4 packet.
5055
var magicCookie = [4]byte{99, 130, 83, 99}
@@ -115,9 +120,11 @@ func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error) {
115120
// TransactionID
116121
func GenerateTransactionID() (TransactionID, error) {
117122
var xid TransactionID
118-
n, err := rand.Read(xid[:])
123+
ctx, cancel := context.WithTimeout(context.Background(), RandomTimeout)
124+
defer cancel()
125+
n, err := rand.ReadContext(ctx, xid[:])
119126
if err != nil {
120-
return xid, err
127+
return xid, fmt.Errorf("could not get random number: %v", err)
121128
}
122129
if n != 4 {
123130
return xid, errors.New("invalid random sequence for transaction ID: smaller than 32 bits")

0 commit comments

Comments
 (0)