Skip to content

Commit b203261

Browse files
marksuterhugelgupf
authored andcommitted
Reduce duplication between New and NewWithContext, per hugelgupf@'s comment.
Signed-off-by: Mark Suter <[email protected]>
1 parent 86bc934 commit b203261

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

dhcpv4/dhcpv4.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,7 @@ func New(modifiers ...Modifier) (*DHCPv4, error) {
146146
if err != nil {
147147
return nil, err
148148
}
149-
d := DHCPv4{
150-
OpCode: OpcodeBootRequest,
151-
HWType: iana.HWTypeEthernet,
152-
ClientHWAddr: make(net.HardwareAddr, 6),
153-
HopCount: 0,
154-
TransactionID: xid,
155-
NumSeconds: 0,
156-
Flags: 0,
157-
ClientIPAddr: net.IPv4zero,
158-
YourIPAddr: net.IPv4zero,
159-
ServerIPAddr: net.IPv4zero,
160-
GatewayIPAddr: net.IPv4zero,
161-
Options: make(Options),
162-
}
163-
for _, mod := range modifiers {
164-
mod(&d)
165-
}
166-
return &d, nil
149+
return newDHCPv4(xid, modifiers), nil
167150
}
168151

169152
// NewWithContext creates a new DHCPv4 structure and fill it up with default
@@ -175,6 +158,10 @@ func NewWithContext(ctx context.Context, modifiers ...Modifier) (*DHCPv4, error)
175158
if err != nil {
176159
return nil, err
177160
}
161+
return newDHCPv4(xid, modifiers), nil
162+
}
163+
164+
func newDHCPv4(xid TransactionID, modifiers ...Modifier) *DHCPv4 {
178165
d := DHCPv4{
179166
OpCode: OpcodeBootRequest,
180167
HWType: iana.HWTypeEthernet,
@@ -192,7 +179,7 @@ func NewWithContext(ctx context.Context, modifiers ...Modifier) (*DHCPv4, error)
192179
for _, mod := range modifiers {
193180
mod(&d)
194181
}
195-
return &d, nil
182+
return &d
196183
}
197184

198185
// NewDiscoveryForInterface builds a new DHCPv4 Discovery message, with a default

0 commit comments

Comments
 (0)