summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iaprefix.go
blob: 7260d2c8e38537ad73c6702bb25bf61b78f49708 (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
package dhcpv6

import (
	"fmt"
	"net"

	"github.com/u-root/u-root/pkg/uio"
)

// OptIAPrefix implements the IAPrefix option.
//
// This module defines the OptIAPrefix structure.
// https://www.ietf.org/rfc/rfc3633.txt
type OptIAPrefix struct {
	PreferredLifetime uint32
	ValidLifetime     uint32
	prefixLength      byte
	ipv6Prefix        net.IP
	Options           Options
}

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

// ToBytes marshals this option according to RFC 3633, Section 10.
func (op *OptIAPrefix) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write32(op.PreferredLifetime)
	buf.Write32(op.ValidLifetime)
	buf.Write8(op.prefixLength)
	buf.WriteBytes(op.ipv6Prefix.To16())
	buf.WriteBytes(op.Options.ToBytes())
	return buf.Data()
}

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

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

// IPv6Prefix returns the ipv6Prefix
func (op *OptIAPrefix) IPv6Prefix() net.IP {
	return op.ipv6Prefix
}

// SetIPv6Prefix sets the ipv6Prefix
func (op *OptIAPrefix) SetIPv6Prefix(p net.IP) {
	op.ipv6Prefix = p
}

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(), op.IPv6Prefix(), op.Options)
}

// GetOneOption will get an option of the give type from the Options field, if
// it is present. It will return `nil` otherwise
func (op *OptIAPrefix) GetOneOption(code OptionCode) Option {
	return op.Options.GetOne(code)
}

// DelOption will remove all the options that match a Option code.
func (op *OptIAPrefix) DelOption(code OptionCode) {
	op.Options.Del(code)
}

// ParseOptIAPrefix 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) {
	buf := uio.NewBigEndianBuffer(data)
	var opt OptIAPrefix
	opt.PreferredLifetime = buf.Read32()
	opt.ValidLifetime = buf.Read32()
	opt.prefixLength = buf.Read8()
	opt.ipv6Prefix = net.IP(buf.CopyN(net.IPv6len))
	if err := opt.Options.FromBytes(buf.ReadAll()); err != nil {
		return nil, err
	}
	return &opt, buf.FinError()
}