summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ip_address_lease_time.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv4/option_ip_address_lease_time.go')
-rw-r--r--dhcpv4/option_ip_address_lease_time.go54
1 files changed, 35 insertions, 19 deletions
diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go
index 4362419..6e09233 100644
--- a/dhcpv4/option_ip_address_lease_time.go
+++ b/dhcpv4/option_ip_address_lease_time.go
@@ -2,37 +2,53 @@ package dhcpv4
import (
"fmt"
+ "time"
"github.com/u-root/u-root/pkg/uio"
)
-// OptIPAddressLeaseTime implements the IP address lease time option described
-// by RFC 2132, Section 9.2.
-type OptIPAddressLeaseTime struct {
- LeaseTime uint32
-}
+// Duration implements the IP address lease time option described by RFC 2132,
+// Section 9.2.
+type Duration time.Duration
-// ParseOptIPAddressLeaseTime constructs an OptIPAddressLeaseTime struct from a
-// sequence of bytes and returns it, or an error.
-func ParseOptIPAddressLeaseTime(data []byte) (*OptIPAddressLeaseTime, error) {
+// FromBytes parses a duration from a byte stream according to RFC 2132, Section 9.2.
+func (d *Duration) FromBytes(data []byte) error {
buf := uio.NewBigEndianBuffer(data)
- leaseTime := buf.Read32()
- return &OptIPAddressLeaseTime{LeaseTime: leaseTime}, buf.FinError()
-}
-
-// Code returns the option code.
-func (o *OptIPAddressLeaseTime) Code() OptionCode {
- return OptionIPAddressLeaseTime
+ *d = Duration(time.Duration(buf.Read32()) * time.Second)
+ return buf.FinError()
}
// ToBytes returns a serialized stream of bytes for this option.
-func (o *OptIPAddressLeaseTime) ToBytes() []byte {
+func (d Duration) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
- buf.Write32(o.LeaseTime)
+ buf.Write32(uint32(time.Duration(d) / time.Second))
return buf.Data()
}
// String returns a human-readable string for this option.
-func (o *OptIPAddressLeaseTime) String() string {
- return fmt.Sprintf("IP Addresses Lease Time -> %v", o.LeaseTime)
+func (d Duration) String() string {
+ return fmt.Sprintf("%s", time.Duration(d))
+}
+
+// OptIPAddressLeaseTime returns a new IP address lease time option.
+//
+// The IP address lease time option is described by RFC 2132, Section 9.2.
+func OptIPAddressLeaseTime(d time.Duration) Option {
+ return Option{Code: OptionIPAddressLeaseTime, Value: Duration(d)}
+}
+
+// GetIPAddressLeaseTime returns the IP address lease time in o, or the given
+// default duration if not present.
+//
+// The IP address lease time option is described by RFC 2132, Section 9.2.
+func GetIPAddressLeaseTime(o Options, def time.Duration) time.Duration {
+ v := o.Get(OptionIPAddressLeaseTime)
+ if v == nil {
+ return def
+ }
+ var d Duration
+ if err := d.FromBytes(v); err != nil {
+ return def
+ }
+ return time.Duration(d)
}