diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-29 14:48:10 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-24 08:05:49 +0000 |
commit | c90ab10024ada840e24bb028a3405961e8e4c26a (patch) | |
tree | 9b8af0c1b80ee6efc112921f9a14b92d6c73f8eb /dhcpv4/bsdp/boot_image.go | |
parent | 2be5cae32d33f01ddecf6f167a9c0e5290e6d58f (diff) |
dhcpv4: nicer API for option parsing.
From:
r := d.GetOneOption(OptionRouter).(*OptRouter).Routers
d.UpdateOption(&OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}}})
To:
r := GetRouter(d.Options)
d.UpdateOption(OptRouter(net.IP{192, 168, 0, 1}, ...))
Diffstat (limited to 'dhcpv4/bsdp/boot_image.go')
-rw-r--r-- | dhcpv4/bsdp/boot_image.go | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/dhcpv4/bsdp/boot_image.go b/dhcpv4/bsdp/boot_image.go index 954dcb6..58b5167 100644 --- a/dhcpv4/bsdp/boot_image.go +++ b/dhcpv4/bsdp/boot_image.go @@ -3,6 +3,7 @@ package bsdp import ( "fmt" + "github.com/insomniacslk/dhcp/dhcpv4" "github.com/u-root/u-root/pkg/uio" ) @@ -18,9 +19,9 @@ const ( // 4 - 127 are reserved for future use. ) -// BootImageTypeToString maps the different BootImageTypes to human-readable +// bootImageTypeToString maps the different BootImageTypes to human-readable // representations. -var BootImageTypeToString = map[BootImageType]string{ +var bootImageTypeToString = map[BootImageType]string{ BootImageTypeMacOS9: "macOS 9", BootImageTypeMacOSX: "macOS", BootImageTypeMacOSXServer: "macOS Server", @@ -35,6 +36,16 @@ type BootImageID struct { Index uint16 } +// ToBytes implements dhcpv4.OptionValue. +func (b BootImageID) ToBytes() []byte { + return uio.ToBigEndian(b) +} + +// FromBytes reads data into b. +func (b *BootImageID) FromBytes(data []byte) error { + return uio.FromBigEndian(b, data) +} + // Marshal writes the binary representation to buf. func (b BootImageID) Marshal(buf *uio.Lexer) { var byte0 byte @@ -55,7 +66,7 @@ func (b BootImageID) String() string { } else { s += " uninstallable" } - t, ok := BootImageTypeToString[b.ImageType] + t, ok := bootImageTypeToString[b.ImageType] if !ok { t = "unknown" } @@ -99,3 +110,37 @@ func (b *BootImage) Unmarshal(buf *uio.Lexer) error { b.Name = string(buf.Consume(int(nameLength))) return buf.Error() } + +func getBootImageID(code dhcpv4.OptionCode, o dhcpv4.Options) *BootImageID { + v := o.Get(code) + if v == nil { + return nil + } + var b BootImageID + if err := uio.FromBigEndian(&b, v); err != nil { + return nil + } + return &b +} + +// OptDefaultBootImageID returns a new default boot image ID option as per +// BSDP. +func OptDefaultBootImageID(b BootImageID) dhcpv4.Option { + return dhcpv4.Option{Code: OptionDefaultBootImageID, Value: b} +} + +// GetDefaultBootImageID returns the default boot image ID contained in o. +func GetDefaultBootImageID(o dhcpv4.Options) *BootImageID { + return getBootImageID(OptionDefaultBootImageID, o) +} + +// OptSelectedBootImageID returns a new selected boot image ID option as per +// BSDP. +func OptSelectedBootImageID(b BootImageID) dhcpv4.Option { + return dhcpv4.Option{Code: OptionSelectedBootImageID, Value: b} +} + +// GetSelectedBootImageID returns the selected boot image ID contained in o. +func GetSelectedBootImageID(o dhcpv4.Options) *BootImageID { + return getBootImageID(OptionSelectedBootImageID, o) +} |