From e87114a6e449d7a2e458b5529923e5668dfa3a11 Mon Sep 17 00:00:00 2001 From: Christopher Koch Date: Fri, 28 Dec 2018 20:19:14 -0800 Subject: dhcpv4: simplify option parsing. option's codes and lengths were being parsed twice: once in ParseOption and once in each option type's Parse implementation. Consolidate such that it only happens once. Additionally, only pass data to options that they should parse -- we know the length before the Parse function is called, so the option only gets to see the data it needs to see. Also, use uio.Lexer to simplify parsing code in general. Easier to read and reason about. --- dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) (limited to 'dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go') diff --git a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go index 5b00ded..52b6eab 100644 --- a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go +++ b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go @@ -4,12 +4,13 @@ import ( "fmt" "github.com/insomniacslk/dhcp/dhcpv4" + "github.com/u-root/u-root/pkg/uio" ) +// OptSelectedBootImageID contains the selected boot image ID. +// // Implements the BSDP option selected boot image ID, which tells the server // which boot image has been selected by the client. - -// OptSelectedBootImageID contains the selected boot image ID. type OptSelectedBootImageID struct { ID BootImageID } @@ -17,22 +18,12 @@ type OptSelectedBootImageID struct { // ParseOptSelectedBootImageID constructs an OptSelectedBootImageID struct from a sequence of // bytes and returns it, or an error. func ParseOptSelectedBootImageID(data []byte) (*OptSelectedBootImageID, error) { - if len(data) < 6 { - return nil, dhcpv4.ErrShortByteStream - } - code := dhcpv4.OptionCode(data[0]) - if code != OptionSelectedBootImageID { - return nil, fmt.Errorf("expected option %v, got %v instead", OptionSelectedBootImageID, code) - } - length := int(data[1]) - if length != 4 { - return nil, fmt.Errorf("expected length 4, got %d instead", length) - } - id, err := BootImageIDFromBytes(data[2:6]) - if err != nil { + var o OptSelectedBootImageID + buf := uio.NewBigEndianBuffer(data) + if err := o.ID.Unmarshal(buf); err != nil { return nil, err } - return &OptSelectedBootImageID{*id}, nil + return &o, buf.FinError() } // Code returns the option code. -- cgit v1.2.3