File: option_statuscode.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20250417.5f8cf70-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,096 kB
  • sloc: makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,244 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
package dhcpv6

import (
	"fmt"

	"github.com/insomniacslk/dhcp/iana"
	"github.com/u-root/uio/uio"
)

// OptStatusCode represents a DHCPv6 Status Code option
//
// This module defines the OptStatusCode structure.
// https://www.ietf.org/rfc/rfc3315.txt
type OptStatusCode struct {
	StatusCode    iana.StatusCode
	StatusMessage string
}

// Code returns the option code.
func (op *OptStatusCode) Code() OptionCode {
	return OptionStatusCode
}

// ToBytes serializes the option and returns it as a sequence of bytes.
func (op *OptStatusCode) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write16(uint16(op.StatusCode))
	buf.WriteBytes([]byte(op.StatusMessage))
	return buf.Data()
}

// String returns a human-readable option.
func (op *OptStatusCode) String() string {
	return fmt.Sprintf("%s: {Code=%s (%d); Message=%s}",
		op.Code(), op.StatusCode, op.StatusCode, op.StatusMessage)
}

// FromBytes builds an OptStatusCode structure from a sequence of bytes. The
// input data does not include option code and length bytes.
func (op *OptStatusCode) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	op.StatusCode = iana.StatusCode(buf.Read16())
	op.StatusMessage = string(buf.ReadAll())
	return buf.FinError()
}