File: authentication.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 (63 lines) | stat: -rw-r--r-- 1,765 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
56
57
58
59
60
61
62
63
package dhcp6opts

import (
	"io"

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

// The Authentication option carries authentication information to
// authenticate the identity and contents of DHCP messages. The use of
// the Authentication option is described in section 21.
type Authentication struct {
	// The authentication protocol used in this authentication option
	Protocol byte

	// The algorithm used in the authentication protocol
	Algorithm byte

	// The replay detection method used in this authentication option
	RDM byte

	// The replay detection information for the RDM
	ReplayDetection uint64

	// The authentication information, as specified by the protocol and
	// algorithm used in this authentication option.
	AuthenticationInformation []byte
}

// MarshalBinary allocates a byte slice containing the data from a Authentication.
func (a *Authentication) MarshalBinary() ([]byte, error) {
	// 1 byte:  Protocol
	// 1 byte:  Algorithm
	// 1 byte:  RDM
	// 8 bytes: ReplayDetection
	// N bytes: AuthenticationInformation (can have 0 len byte)
	b := buffer.New(nil)
	b.Write8(a.Protocol)
	b.Write8(a.Algorithm)
	b.Write8(a.RDM)
	b.Write64(a.ReplayDetection)
	b.WriteBytes(a.AuthenticationInformation)

	return b.Data(), nil
}

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

	a.Protocol = b.Read8()
	a.Algorithm = b.Read8()
	a.RDM = b.Read8()
	a.ReplayDetection = b.Read64()
	a.AuthenticationInformation = b.Remaining()
	return nil
}