summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ip_address_lease_time.go
blob: 9bc8156c52e79b94905774190d9307c48055e307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package dhcpv4

import (
	"time"

	"github.com/u-root/u-root/pkg/uio"
)

// Duration implements the IP address lease time option described by RFC 2132,
// Section 9.2.
type Duration time.Duration

// 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)
	*d = Duration(time.Duration(buf.Read32()) * time.Second)
	return buf.FinError()
}

// ToBytes returns a serialized stream of bytes for this option.
func (d Duration) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write32(uint32(time.Duration(d) / time.Second))
	return buf.Data()
}

// String returns a human-readable string for this option.
func (d Duration) String() string {
	return time.Duration(d).String()
}

// 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)}
}