summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp/bsdp_option_misc.go
blob: 3848a6c3f87e81b2b3a5f48207e03429a1118ab5 (plain)
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
64
65
66
package bsdp

import (
	"fmt"
	"net"

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

// OptReplyPort returns a new BSDP reply port option.
//
// Implements the BSDP option reply port. This is used when BSDP responses
// should be sent to a reply port other than the DHCP default. The macOS GUI
// "Startup Disk Select" sends this option since it's operating in an
// unprivileged context.
func OptReplyPort(port uint16) dhcpv4.Option {
	return dhcpv4.Option{Code: OptionReplyPort, Value: dhcpv4.Uint16(port)}
}

// OptServerPriority returns a new BSDP server priority option.
func OptServerPriority(prio uint16) dhcpv4.Option {
	return dhcpv4.Option{Code: OptionServerPriority, Value: dhcpv4.Uint16(prio)}
}

// OptMachineName returns a BSDP Machine Name option.
func OptMachineName(name string) dhcpv4.Option {
	return dhcpv4.Option{Code: OptionMachineName, Value: dhcpv4.String(name)}
}

// Version is the BSDP protocol version. Can be one of 1.0 or 1.1.
type Version [2]byte

// Specific versions.
var (
	Version1_0 = Version{1, 0}
	Version1_1 = Version{1, 1}
)

// ToBytes returns a serialized stream of bytes for this option.
func (o Version) ToBytes() []byte {
	return o[:]
}

// String returns a human-readable string for this option.
func (o Version) String() string {
	return fmt.Sprintf("%d.%d", o[0], o[1])
}

// FromBytes constructs a Version struct from a sequence of
// bytes and returns it, or an error.
func (o *Version) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	buf.ReadBytes(o[:])
	return buf.FinError()
}

// OptVersion returns a new BSDP version option.
func OptVersion(version Version) dhcpv4.Option {
	return dhcpv4.Option{Code: OptionVersion, Value: version}
}

// OptServerIdentifier returns a new BSDP Server Identifier option.
func OptServerIdentifier(ip net.IP) dhcpv4.Option {
	return dhcpv4.Option{Code: OptionServerIdentifier, Value: dhcpv4.IP(ip)}
}