Skip to content

Commit

Permalink
Add WithContext versions of functions that use contexts.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
marksuter authored and hugelgupf committed Jan 9, 2025
1 parent b56fa0d commit 86bc934
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions dhcpv4/dhcpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ func GetExternalIPv4Addrs(addrs []net.Addr) ([]net.IP, error) {
}

// GenerateTransactionID generates a random 32-bits number suitable for use as
// TransactionID
// TransactionID.
func GenerateTransactionID() (TransactionID, error) {
return GenerateTransactionIDWithContext(context.Background())
}

// GenerateTransactionIDWithContext generates a random 32-bits number suitable
// for use as TransactionID.
func GenerateTransactionIDWithContext(ctx context.Context) (TransactionID, error) {
var xid TransactionID
ctx, cancel := context.WithTimeout(context.Background(), RandomTimeout)
ctx, cancel := context.WithTimeout(ctx, RandomTimeout)
defer cancel()
n, err := rand.ReadContext(ctx, xid[:])
if err != nil {
Expand All @@ -133,8 +139,8 @@ func GenerateTransactionID() (TransactionID, error) {
}

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

// NewWithContext creates a new DHCPv4 structure and fill it up with default
// values. It won't be a valid DHCPv4 message so you will need to adjust its
// fields. See also NewDiscovery, NewRequest, NewAcknowledge, NewInform and
// NewRelease.
func NewWithContext(ctx context.Context, modifiers ...Modifier) (*DHCPv4, error) {
xid, err := GenerateTransactionIDWithContext(ctx)
if err != nil {
return nil, err
}
d := DHCPv4{
OpCode: OpcodeBootRequest,
HWType: iana.HWTypeEthernet,
ClientHWAddr: make(net.HardwareAddr, 6),
HopCount: 0,
TransactionID: xid,
NumSeconds: 0,
Flags: 0,
ClientIPAddr: net.IPv4zero,
YourIPAddr: net.IPv4zero,
ServerIPAddr: net.IPv4zero,
GatewayIPAddr: net.IPv4zero,
Options: make(Options),
}
for _, mod := range modifiers {
mod(&d)
}
return &d, nil
}

// NewDiscoveryForInterface builds a new DHCPv4 Discovery message, with a default
// Ethernet HW type and the hardware address obtained from the specified
// interface.
Expand Down

0 comments on commit 86bc934

Please sign in to comment.