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

Improve support for renewal and rebinding options #536

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions dhcpv4/option_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func OptIPAddressLeaseTime(d time.Duration) Option {
return Option{Code: OptionIPAddressLeaseTime, Value: Duration(d)}
}

// The IP address renew time option as described by RFC 2132, Section 9.11.
func OptRenewTimeValue(d time.Duration) Option {
return Option{Code: OptionRenewTimeValue, Value: Duration(d)}
}

// The IP address rebinding time option as described by RFC 2132, Section 9.12.
func OptRebindingTimeValue(d time.Duration) Option {
return Option{Code: OptionRebindingTimeValue, Value: Duration(d)}
}

// The IPv6-Only Preferred option is described by RFC 8925, Section 3.1
func OptIPv6OnlyPreferred(d time.Duration) Option {
return Option{Code: OptionIPv6OnlyPreferred, Value: Duration(d)}
Expand Down
54 changes: 54 additions & 0 deletions dhcpv4/option_duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ func TestGetIPAddressLeaseTime(t *testing.T) {
require.Equal(t, time.Duration(10), m.IPAddressLeaseTime(10))
}

func TestOptRenewTimeValue(t *testing.T) {
o := OptRenewTimeValue(43200 * time.Second)
require.Equal(t, OptionRenewTimeValue, o.Code, "Code")
require.Equal(t, []byte{0, 0, 168, 192}, o.Value.ToBytes(), "ToBytes")
require.Equal(t, "Renew Time Value: 12h0m0s", o.String(), "String")
}

func TestGetRenewTimeValue(t *testing.T) {
m, _ := New(WithGeneric(OptionRenewTimeValue, []byte{0, 0, 168, 192}))
leaseTime := m.IPAddressRenewalTime(0)
require.Equal(t, 43200*time.Second, leaseTime)

// Too short.
m, _ = New(WithGeneric(OptionRenewTimeValue, []byte{168, 192}))
leaseTime = m.IPAddressRenewalTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Too long.
m, _ = New(WithGeneric(OptionRenewTimeValue, []byte{1, 1, 1, 1, 1}))
leaseTime = m.IPAddressRenewalTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Empty.
m, _ = New()
require.Equal(t, time.Duration(10), m.IPAddressRenewalTime(10))
}

func TestOptRebindingTimeValue(t *testing.T) {
o := OptRebindingTimeValue(43200 * time.Second)
require.Equal(t, OptionRebindingTimeValue, o.Code, "Code")
require.Equal(t, []byte{0, 0, 168, 192}, o.Value.ToBytes(), "ToBytes")
require.Equal(t, "Rebinding Time Value: 12h0m0s", o.String(), "String")
}

func TestGetRebindingTimeValue(t *testing.T) {
m, _ := New(WithGeneric(OptionRebindingTimeValue, []byte{0, 0, 168, 192}))
leaseTime := m.IPAddressRebindingTime(0)
require.Equal(t, 43200*time.Second, leaseTime)

// Too short.
m, _ = New(WithGeneric(OptionRebindingTimeValue, []byte{168, 192}))
leaseTime = m.IPAddressRebindingTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Too long.
m, _ = New(WithGeneric(OptionRebindingTimeValue, []byte{1, 1, 1, 1, 1}))
leaseTime = m.IPAddressRebindingTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Empty.
m, _ = New()
require.Equal(t, time.Duration(10), m.IPAddressRebindingTime(10))
}

func TestOptIPv6OnlyPreferred(t *testing.T) {
o := OptIPv6OnlyPreferred(43200 * time.Second)
require.Equal(t, OptionIPv6OnlyPreferred, o.Code, "Code")
Expand Down
2 changes: 1 addition & 1 deletion dhcpv4/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func getOption(code OptionCode, data []byte, vendorDecoder OptionDecoder) fmt.St
case OptionDNSDomainSearchList:
d = &rfc1035label.Labels{}

case OptionIPAddressLeaseTime, OptionIPv6OnlyPreferred:
case OptionIPAddressLeaseTime, OptionRenewTimeValue, OptionRebindingTimeValue, OptionIPv6OnlyPreferred:
var dur Duration
d = &dur

Expand Down
Loading