summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_serverid.go
blob: ffc1b694d9e05e4c49d898cdc620f2a5b986e3d5 (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
package dhcpv6

// This module defines the OptServerId and DUID structures.
// https://www.ietf.org/rfc/rfc3315.txt

import (
	"encoding/binary"
	"fmt"
)

// OptServerId represents a Client ID option
type OptServerId struct {
	Sid Duid
}

func (op *OptServerId) Code() OptionCode {
	return OPTION_SERVERID
}

func (op *OptServerId) ToBytes() []byte {
	buf := make([]byte, 4)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_SERVERID))
	binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
	buf = append(buf, op.Sid.ToBytes()...)
	return buf
}

func (op *OptServerId) Length() int {
	return op.Sid.Length()
}

func (op *OptServerId) String() string {
	return fmt.Sprintf("OptServerId{sid=%v}", op.Sid.String())
}

// ParseOptServerId builds an OptServerId structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptServerId(data []byte) (*OptServerId, error) {
	if len(data) < 2 {
		// at least the DUID type is necessary to continue
		return nil, fmt.Errorf("Invalid OptServerId data: shorter than 2 bytes")
	}
	opt := OptServerId{}
	sid, err := DuidFromBytes(data)
	if err != nil {
		return nil, err
	}
	opt.Sid = *sid
	return &opt, nil
}