blob: 6e09233248e5c547c861a0737e39b35e65f989f6 (
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package dhcpv4
import (
"fmt"
"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 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)
}
|