summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iapd.go
blob: fbf40b4f90976199f243db441cf8eb6085eed934 (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
99
100
101
package dhcpv6

import (
	"fmt"
	"time"

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

// PDOptions are options used with the IAPD (prefix delegation) option.
//
// RFC 3633 describes that IA_PD-options may contain the IAPrefix option and
// the StatusCode option.
type PDOptions struct {
	Options
}

// Prefixes are the prefixes associated with this delegation.
func (po PDOptions) Prefixes() []*OptIAPrefix {
	opts := po.Options.Get(OptionIAPrefix)
	if len(opts) == 0 {
		return nil
	}
	pre := make([]*OptIAPrefix, 0, len(opts))
	for _, o := range opts {
		if iap, ok := o.(*OptIAPrefix); ok {
			pre = append(pre, iap)
		}
	}
	return pre
}

// Status returns the status code associated with this option.
func (po PDOptions) Status() *OptStatusCode {
	opt := po.Options.GetOne(OptionStatusCode)
	if opt == nil {
		return nil
	}
	sc, ok := opt.(*OptStatusCode)
	if !ok {
		return nil
	}
	return sc
}

// OptIAPD implements the identity association for prefix
// delegation option defined by RFC 3633, Section 9.
type OptIAPD struct {
	IaId    [4]byte
	T1      time.Duration
	T2      time.Duration
	Options PDOptions
}

// Code returns the option code
func (op *OptIAPD) Code() OptionCode {
	return OptionIAPD
}

// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptIAPD) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.WriteBytes(op.IaId[:])

	t1 := Duration{op.T1}
	t1.Marshal(buf)
	t2 := Duration{op.T2}
	t2.Marshal(buf)

	buf.WriteBytes(op.Options.ToBytes())
	return buf.Data()
}

// String returns a string representation of the OptIAPD data
func (op *OptIAPD) String() string {
	return fmt.Sprintf("%s: {IAID=%#x T1=%v T2=%v Options=%v}",
		op.Code(), op.IaId, op.T1, op.T2, op.Options)
}

// LongString returns a multi-line string representation of the OptIAPD data
func (op *OptIAPD) LongString(indentSpace int) string {
	return fmt.Sprintf("%s: IAID=%#x T1=%v T2=%v Options=%v", op.Code(), op.IaId, op.T1, op.T2, op.Options.LongString(indentSpace))
}

// FromBytes builds an OptIAPD structure from a sequence of bytes. The input
// data does not include option code and length bytes.
func (op *OptIAPD) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	buf.ReadBytes(op.IaId[:])

	var t1, t2 Duration
	t1.Unmarshal(buf)
	t2.Unmarshal(buf)
	op.T1 = t1.Duration
	op.T2 = t2.Duration

	if err := op.Options.FromBytes(buf.ReadAll()); err != nil {
		return err
	}
	return buf.FinError()
}