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

// This module defines the OptElapsedTime structure.
// https://www.ietf.org/rfc/rfc3315.txt

import (
	"encoding/binary"
	"fmt"
)

type OptElapsedTime struct {
	ElapsedTime uint16
}

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

func (op *OptElapsedTime) ToBytes() []byte {
	buf := make([]byte, 6)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OptionElapsedTime))
	binary.BigEndian.PutUint16(buf[2:4], 2)
	binary.BigEndian.PutUint16(buf[4:6], uint16(op.ElapsedTime))
	return buf
}

func (op *OptElapsedTime) Length() int {
	return 2
}

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) {
	opt := OptElapsedTime{}
	if len(data) != 2 {
		return nil, fmt.Errorf("Invalid elapsed time data length. Expected 2 bytes, got %v", len(data))
	}
	opt.ElapsedTime = binary.BigEndian.Uint16(data)
	return &opt, nil
}