Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithContext versions of functions that use contexts. #555

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 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,13 +139,29 @@ 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make New be a wrapper of NewWithContext as well? just so changes to one aren't forgotten in the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with e0b42be.

xid, err := GenerateTransactionID()
if err != nil {
return nil, err
}
return newDHCPv4(xid, modifiers...), 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
}
return newDHCPv4(xid, modifiers...), nil
}

func newDHCPv4(xid TransactionID, modifiers ...Modifier) *DHCPv4 {
d := DHCPv4{
OpCode: OpcodeBootRequest,
HWType: iana.HWTypeEthernet,
Expand All @@ -157,7 +179,7 @@ func New(modifiers ...Modifier) (*DHCPv4, error) {
for _, mod := range modifiers {
mod(&d)
}
return &d, nil
return &d
}

// NewDiscoveryForInterface builds a new DHCPv4 Discovery message, with a default
Expand Down
Loading