summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_elapsedtime.go
blob: 194122ee018f92d1e26db137b68900d979d4df2f (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
package dhcpv6

import (
	"fmt"

	"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
}

func (op *OptElapsedTime) Code() OptionCode {
	return OptionElapsedTime
}

// ToBytes marshals this option to bytes.
func (op *OptElapsedTime) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write16(op.ElapsedTime)
	return buf.Data()
}

func (op *OptElapsedTime) String() string {
	return fmt.Sprintf("OptElapsedTime{elapsedtime=%v}", op.ElapsedTime)
}

// 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
	buf := uio.NewBigEndianBuffer(data)
	opt.ElapsedTime = buf.Read16()
	return &opt, buf.FinError()
}