summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iaaddress.go
blob: 6f430db303682944ef2742c90048adf2c2b7b5cf (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
package dhcpv6

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

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

// OptIAAddress represents an OptionIAAddr
type OptIAAddress struct {
	IPv6Addr          net.IP
	PreferredLifetime uint32
	ValidLifetime     uint32
	Options           []Option
}

// Code returns the option's code
func (op *OptIAAddress) Code() OptionCode {
	return OptionIAAddr
}

// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptIAAddress) ToBytes() []byte {
	buf := make([]byte, 28)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OptionIAAddr))
	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)
	for _, opt := range op.Options {
		buf = append(buf, opt.ToBytes()...)
	}
	return buf
}

// Length returns the option length
func (op *OptIAAddress) Length() int {
	opLen := 24
	for _, opt := range op.Options {
		opLen += 4 + opt.Length()
	}
	return opLen
}

func (op *OptIAAddress) String() string {
	return fmt.Sprintf("OptIAAddress{ipv6addr=%v, preferredlifetime=%v, validlifetime=%v, options=%v}",
		op.IPv6Addr, op.PreferredLifetime, op.ValidLifetime, op.Options)
}

// ParseOptIAAddress builds 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) {
	var err 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))
	}
	opt.IPv6Addr = net.IP(data[:16])
	opt.PreferredLifetime = binary.BigEndian.Uint32(data[16:20])
	opt.ValidLifetime = binary.BigEndian.Uint32(data[20:24])
	opt.Options, err = OptionsFromBytes(data[24:])
	if err != nil {
		return nil, err
	}
	return &opt, nil
}