File: vendoropts.go

package info (click to toggle)
golang-github-mdlayher-dhcp6 0.0~git20190311.2a67805-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 400 kB
  • sloc: makefile: 3
file content (55 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download
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
package dhcp6opts

import (
	"io"

	"github.com/mdlayher/dhcp6"
	"github.com/mdlayher/dhcp6/internal/buffer"
)

// A VendorOpts is used by clients and servers to exchange
// VendorOpts information.
type VendorOpts struct {
	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// An opaque object of option-len octets,
	// interpreted by vendor-specific code on the
	// clients and servers
	Options dhcp6.Options
}

// MarshalBinary allocates a byte slice containing the data from a VendorOpts.
func (v *VendorOpts) MarshalBinary() ([]byte, error) {
	// 4 bytes: EnterpriseNumber
	// N bytes: options slice byte count
	b := buffer.New(nil)
	b.Write32(v.EnterpriseNumber)
	opts, err := v.Options.MarshalBinary()
	if err != nil {
		return nil, err
	}
	b.WriteBytes(opts)

	return b.Data(), nil
}

// UnmarshalBinary unmarshals a raw byte slice into a VendorOpts.
// If the byte slice does not contain enough data to form a valid
// VendorOpts, io.ErrUnexpectedEOF is returned.
// If option-data are invalid, then ErrInvalidPacket is returned.
func (v *VendorOpts) UnmarshalBinary(p []byte) error {
	b := buffer.New(p)
	// Too short to be valid VendorOpts
	if b.Len() < 4 {
		return io.ErrUnexpectedEOF
	}

	v.EnterpriseNumber = b.Read32()
	if err := (&v.Options).UnmarshalBinary(b.Remaining()); err != nil {
		// Invalid options means an invalid RelayMessage
		return dhcp6.ErrInvalidPacket
	}
	return nil
}