File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ package dhcpv4
17
17
18
18
import (
19
19
"bytes"
20
+ "context"
20
21
"errors"
21
22
"fmt"
22
23
"net"
@@ -45,6 +46,10 @@ const (
45
46
bootpMinLen = 300
46
47
)
47
48
49
+ // RandomTimeout is the amount of time to wait until random number generation
50
+ // is canceled.
51
+ var RandomTimeout = 2 * time .Minute
52
+
48
53
// magicCookie is the magic 4-byte value at the beginning of the list of options
49
54
// in a DHCPv4 packet.
50
55
var magicCookie = [4 ]byte {99 , 130 , 83 , 99 }
@@ -115,9 +120,11 @@ func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error) {
115
120
// TransactionID
116
121
func GenerateTransactionID () (TransactionID , error ) {
117
122
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 [:])
119
126
if err != nil {
120
- return xid , err
127
+ return xid , fmt . Errorf ( "could not get random number: %v" , err )
121
128
}
122
129
if n != 4 {
123
130
return xid , errors .New ("invalid random sequence for transaction ID: smaller than 32 bits" )
You can’t perform that action at this time.
0 commit comments