summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_elapsedtime.go
blob: 1f5fd038fb1070c5ae7f6c09d479b5d4334c0670 (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
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 OPTION_ELAPSED_TIME
}

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

func (op *OptElapsedTime) ElapsedTime() uint16 {
	return op.elapsedTime
}

func (op *OptElapsedTime) SetElapsedTime(elapsedTime uint16) {
	op.elapsedTime = elapsedTime
}

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
}