diff options
author | Chris Koch <chrisko@google.com> | 2019-12-28 08:57:51 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2020-03-05 15:51:55 +0000 |
commit | bb0c09a3da430db48a7bd0749b7d5411649e1026 (patch) | |
tree | 1f51843a9155b6444e999bf141a546bf2eaf51d3 /dhcpv6/option_elapsedtime.go | |
parent | d9b1a20bc08c08acc5e43e818fe1c3b71612f0a7 (diff) |
v6: add ElapsedTime getter
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6/option_elapsedtime.go')
-rw-r--r-- | dhcpv6/option_elapsedtime.go | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/dhcpv6/option_elapsedtime.go b/dhcpv6/option_elapsedtime.go index 194122e..0e2547f 100644 --- a/dhcpv6/option_elapsedtime.go +++ b/dhcpv6/option_elapsedtime.go @@ -2,38 +2,41 @@ package dhcpv6 import ( "fmt" + "time" "github.com/u-root/u-root/pkg/uio" ) -// OptElapsedTime implements the Elapsed Time option. -// -// This module defines the OptElapsedTime structure. -// https://www.ietf.org/rfc/rfc3315.txt -type OptElapsedTime struct { - ElapsedTime uint16 +// OptElapsedTime returns an Elapsed Time option as defined by RFC 3315 Section +// 22.9. +func OptElapsedTime(dur time.Duration) Option { + return &optElapsedTime{ElapsedTime: dur} } -func (op *OptElapsedTime) Code() OptionCode { +type optElapsedTime struct { + ElapsedTime time.Duration +} + +func (*optElapsedTime) Code() OptionCode { return OptionElapsedTime } // ToBytes marshals this option to bytes. -func (op *OptElapsedTime) ToBytes() []byte { +func (op *optElapsedTime) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(op.ElapsedTime) + buf.Write16(uint16(op.ElapsedTime.Round(10*time.Millisecond) / (10 * time.Millisecond))) return buf.Data() } -func (op *OptElapsedTime) String() string { - return fmt.Sprintf("OptElapsedTime{elapsedtime=%v}", op.ElapsedTime) +func (op *optElapsedTime) String() string { + return fmt.Sprintf("ElapsedTime: %s", op.ElapsedTime) } -// build an OptElapsedTime structure from a sequence of bytes. +// build an optElapsedTime structure from a sequence of bytes. // The input data does not include option code and length bytes. -func ParseOptElapsedTime(data []byte) (*OptElapsedTime, error) { - var opt OptElapsedTime +func parseOptElapsedTime(data []byte) (*optElapsedTime, error) { + var opt optElapsedTime buf := uio.NewBigEndianBuffer(data) - opt.ElapsedTime = buf.Read16() + opt.ElapsedTime = time.Duration(buf.Read16()) * 10 * time.Millisecond return &opt, buf.FinError() } |