summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/options/iaprefix.go
blob: d91df2adc96f579e5bd1e1eb6c23ef35540e19cc (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
87
88
89
90
91
92
93
94
95
96
97
98
package options

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

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

type OptIAPrefix struct {
	preferredLifetime uint32
	validLifetime     uint32
	prefixLength      byte
	ipv6Prefix        [16]byte
	options           []byte
}

func (op *OptIAPrefix) Code() OptionCode {
	return OPTION_IAPREFIX
}

func (op *OptIAPrefix) ToBytes() []byte {
	buf := make([]byte, 25)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_IAPREFIX))
	binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
	binary.BigEndian.PutUint32(buf[4:8], op.preferredLifetime)
	binary.BigEndian.PutUint32(buf[8:12], op.validLifetime)
	buf = append(buf, op.prefixLength)
	buf = append(buf, op.ipv6Prefix[:]...)
	buf = append(buf, op.options...)
	return buf
}

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

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

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

func (op *OptIAPrefix) SetValidLifetime(vl uint32) {
	op.validLifetime = vl
}

func (op *OptIAPrefix) PrefixLength() byte {
	return op.prefixLength
}

func (op *OptIAPrefix) SetPrefixLength(pl byte) {
	op.prefixLength = pl
}

func (op *OptIAPrefix) IPv6Prefix() []byte {
	return op.ipv6Prefix[:]
}

func (op *OptIAPrefix) SetIPv6Prefix(p [16]byte) {
	op.ipv6Prefix = p
}

func (op *OptIAPrefix) Options() []byte {
	return op.options
}

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

func (op *OptIAPrefix) Length() int {
	return 25 + len(op.options)
}

func (op *OptIAPrefix) String() string {
	return fmt.Sprintf("OptIAPrefix{preferredlifetime=%v, validlifetime=%v, prefixlength=%v, ipv6prefix=%v, options=%v}",
		op.preferredLifetime, op.validLifetime, op.prefixLength, net.IP(op.ipv6Prefix[:]), op.options)
}

// build an OptIAPrefix structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptIAPrefix(data []byte) (*OptIAPrefix, error) {
	opt := OptIAPrefix{}
	if len(data) < 12 {
		return nil, fmt.Errorf("Invalid IA for Prefix Delegation data length. Expected at least 12 bytes, got %v", len(data))
	}
	opt.preferredLifetime = binary.BigEndian.Uint32(data[:4])
	opt.validLifetime = binary.BigEndian.Uint32(data[4:8])
	opt.prefixLength = data[9]
	copy(opt.ipv6Prefix[:], data[9:17])
	copy(opt.options, data[17:])
	return &opt, nil
}