File: remoteid.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 (53 lines) | stat: -rw-r--r-- 1,537 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
package dhcp6opts

import (
	"io"

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

// A RemoteIdentifier carries vendor-specific options.
//
// The vendor is indicated in the enterprise-number field.
// The remote-id field may be used to encode, for instance:
// - a "caller ID" telephone number for dial-up connection
// - a "user name" prompted for by a Remote Access Server
// - a remote caller ATM address
// - a "modem ID" of a cable data modem
// - the remote IP address of a point-to-point link
// - a remote X.25 address for X.25 connections
// - an interface or port identifier
type RemoteIdentifier struct {
	// EnterpriseNumber specifies an IANA-assigned vendor Private Enterprise
	// Number.
	EnterpriseNumber uint32

	// The opaque value for the remote-id.
	RemoteID []byte
}

// MarshalBinary allocates a byte slice containing the data
// from a RemoteIdentifier.
func (r *RemoteIdentifier) MarshalBinary() ([]byte, error) {
	// 4 bytes: EnterpriseNumber
	// N bytes: RemoteId
	b := buffer.New(nil)
	b.Write32(r.EnterpriseNumber)
	b.WriteBytes(r.RemoteID)
	return b.Data(), nil
}

// UnmarshalBinary unmarshals a raw byte slice into a RemoteIdentifier.
// If the byte slice does not contain enough data to form a valid
// RemoteIdentifier, io.ErrUnexpectedEOF is returned.
func (r *RemoteIdentifier) UnmarshalBinary(p []byte) error {
	b := buffer.New(p)
	// Too short to be valid RemoteIdentifier
	if b.Len() < 5 {
		return io.ErrUnexpectedEOF
	}

	r.EnterpriseNumber = b.Read32()
	r.RemoteID = b.Remaining()
	return nil
}