summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iaaddress.go
blob: 7d0f4a6a3a38e241967d6775bf8cc8d59b213a0f (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dhcpv6

// This module defines the OptIAAddress structure.
// https://www.ietf.org/rfc/rfc3633.txt

import (
	"encoding/binary"
	"fmt"
	"net"
)

type OptIAAddress struct {
	ipv6Addr          [16]byte
	preferredLifetime uint32
	validLifetime     uint32
	options           []byte
}

func (op *OptIAAddress) Code() OptionCode {
	return OPTION_IAADDR
}

func (op *OptIAAddress) ToBytes() []byte {
	buf := make([]byte, 28)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_IAADDR))
	binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
	copy(buf[4:20], op.ipv6Addr[:])
	binary.BigEndian.PutUint32(buf[20:24], op.preferredLifetime)
	binary.BigEndian.PutUint32(buf[24:28], op.validLifetime)
	buf = append(buf, op.options...)
	return buf
}

func (op *OptIAAddress) IPv6Addr() []byte {
	return op.ipv6Addr[:]
}

func (op *OptIAAddress) SetIPv6Addr(addr [16]byte) {
	op.ipv6Addr = addr
}

func (op *OptIAAddress) PreferredLifetime() uint32 {
	return op.preferredLifetime
}

func (op *OptIAAddress) SetPreferredLifetime(pl uint32) {
	op.preferredLifetime = pl
}

func (op *OptIAAddress) ValidLifetime() uint32 {
	return op.validLifetime
}

func (op *OptIAAddress) SetValidLifetime(vl uint32) {
	op.validLifetime = vl
}
func (op *OptIAAddress) Options() []byte {
	return op.options
}

func (op *OptIAAddress) SetOptions(options []byte) {
	op.options = options
}

func (op *OptIAAddress) Length() int {
	return 24 + len(op.options)
}

func (op *OptIAAddress) String() string {
	return fmt.Sprintf("OptIAAddress{ipv6addr=%v, preferredlifetime=%v, validlifetime=%v, options=%v}",
		net.IP(op.ipv6Addr[:]), op.preferredLifetime, op.validLifetime, op.options)
}

// build an OptIAAddress structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptIAAddress(data []byte) (*OptIAAddress, error) {
	opt := OptIAAddress{}
	if len(data) < 24 {
		return nil, fmt.Errorf("Invalid IA Address data length. Expected at least 24 bytes, got %v", len(data))
	}
	copy(opt.ipv6Addr[:], data[:16])
	opt.preferredLifetime = binary.BigEndian.Uint32(data[16:20])
	opt.validLifetime = binary.BigEndian.Uint32(data[20:24])
	copy(opt.options, data[24:])
	return &opt, nil
}