Skip to content

Commit 86bc934

Browse files
marksuterhugelgupf
authored andcommitted
Add WithContext versions of functions that use contexts.
These new functions are equivalent, but allow callers to pass a context down from main (or equivalent). This is necessary to comply with certain style guides, for example, https://google.github.io/styleguide/go/decisions#contexts. We considered replacing dhcpv4.GenerateTransactionID with rand.Read(xid[:]) based on the godoc saying that Read call "never returns an error", but this package is used in embedded situations where it can fail. Signed-off-by: Mark Suter <[email protected]>
1 parent b56fa0d commit 86bc934

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

dhcpv4/dhcpv4.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error) {
117117
}
118118

119119
// GenerateTransactionID generates a random 32-bits number suitable for use as
120-
// TransactionID
120+
// TransactionID.
121121
func GenerateTransactionID() (TransactionID, error) {
122+
return GenerateTransactionIDWithContext(context.Background())
123+
}
124+
125+
// GenerateTransactionIDWithContext generates a random 32-bits number suitable
126+
// for use as TransactionID.
127+
func GenerateTransactionIDWithContext(ctx context.Context) (TransactionID, error) {
122128
var xid TransactionID
123-
ctx, cancel := context.WithTimeout(context.Background(), RandomTimeout)
129+
ctx, cancel := context.WithTimeout(ctx, RandomTimeout)
124130
defer cancel()
125131
n, err := rand.ReadContext(ctx, xid[:])
126132
if err != nil {
@@ -133,8 +139,8 @@ func GenerateTransactionID() (TransactionID, error) {
133139
}
134140

135141
// New creates a new DHCPv4 structure and fill it up with default values. It
136-
// won't be a valid DHCPv4 message so you will need to adjust its fields.
137-
// See also NewDiscovery, NewRequest, NewAcknowledge, NewInform and NewRelease.
142+
// won't be a valid DHCPv4 message so you will need to adjust its fields. See
143+
// also NewDiscovery, NewRequest, NewAcknowledge, NewInform and NewRelease.
138144
func New(modifiers ...Modifier) (*DHCPv4, error) {
139145
xid, err := GenerateTransactionID()
140146
if err != nil {
@@ -160,6 +166,35 @@ func New(modifiers ...Modifier) (*DHCPv4, error) {
160166
return &d, nil
161167
}
162168

169+
// NewWithContext creates a new DHCPv4 structure and fill it up with default
170+
// values. It won't be a valid DHCPv4 message so you will need to adjust its
171+
// fields. See also NewDiscovery, NewRequest, NewAcknowledge, NewInform and
172+
// NewRelease.
173+
func NewWithContext(ctx context.Context, modifiers ...Modifier) (*DHCPv4, error) {
174+
xid, err := GenerateTransactionIDWithContext(ctx)
175+
if err != nil {
176+
return nil, err
177+
}
178+
d := DHCPv4{
179+
OpCode: OpcodeBootRequest,
180+
HWType: iana.HWTypeEthernet,
181+
ClientHWAddr: make(net.HardwareAddr, 6),
182+
HopCount: 0,
183+
TransactionID: xid,
184+
NumSeconds: 0,
185+
Flags: 0,
186+
ClientIPAddr: net.IPv4zero,
187+
YourIPAddr: net.IPv4zero,
188+
ServerIPAddr: net.IPv4zero,
189+
GatewayIPAddr: net.IPv4zero,
190+
Options: make(Options),
191+
}
192+
for _, mod := range modifiers {
193+
mod(&d)
194+
}
195+
return &d, nil
196+
}
197+
163198
// NewDiscoveryForInterface builds a new DHCPv4 Discovery message, with a default
164199
// Ethernet HW type and the hardware address obtained from the specified
165200
// interface.

0 commit comments

Comments
 (0)