Skip to content

Commit

Permalink
Merge pull request #536 from michaelharo/time
Browse files Browse the repository at this point in the history
Improve support for renewal and rebinding options
  • Loading branch information
pmazzini authored Jun 28, 2024
2 parents 51bc613 + aa6e637 commit bf3278a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
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

0 comments on commit bf3278a

Please sign in to comment.