forked from insomniacslk/dhcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsdp_option_message_type.go
57 lines (48 loc) · 1.36 KB
/
bsdp_option_message_type.go
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
package bsdp
import (
"fmt"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/u-root/u-root/pkg/uio"
)
// MessageType represents the different BSDP message types.
//
// Implements the BSDP option message type. Can be one of LIST, SELECT, or
// FAILED.
type MessageType byte
// BSDP Message types - e.g. LIST, SELECT, FAILED
const (
MessageTypeNone MessageType = 0
MessageTypeList MessageType = 1
MessageTypeSelect MessageType = 2
MessageTypeFailed MessageType = 3
)
// ToBytes returns a serialized stream of bytes for this option.
func (m MessageType) ToBytes() []byte {
return []byte{byte(m)}
}
// String returns a human-friendly representation of MessageType.
func (m MessageType) String() string {
if s, ok := messageTypeToString[m]; ok {
return s
}
return fmt.Sprintf("unknown (%d)", m)
}
// messageTypeToString maps each BSDP message type to a human-readable string.
var messageTypeToString = map[MessageType]string{
MessageTypeList: "LIST",
MessageTypeSelect: "SELECT",
MessageTypeFailed: "FAILED",
}
// FromBytes reads data into m.
func (m *MessageType) FromBytes(data []byte) error {
buf := uio.NewBigEndianBuffer(data)
*m = MessageType(buf.Read8())
return buf.FinError()
}
// OptMessageType returns a new BSDP Message Type option.
func OptMessageType(mt MessageType) dhcpv4.Option {
return dhcpv4.Option{
Code: OptionMessageType,
Value: mt,
}
}