From c90ab10024ada840e24bb028a3405961e8e4c26a Mon Sep 17 00:00:00 2001 From: Christopher Koch Date: Sat, 29 Dec 2018 14:48:10 -0800 Subject: 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}, ...)) --- dhcpv4/bsdp/boot_image.go | 51 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'dhcpv4/bsdp/boot_image.go') 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) +} -- cgit v1.2.3