diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-28 20:19:14 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-10 23:01:22 +0000 |
commit | e87114a6e449d7a2e458b5529923e5668dfa3a11 (patch) | |
tree | 7c5ec350c8eb859400a159b881081ffe0dbd4ef0 /dhcpv4/option_ip_address_lease_time.go | |
parent | 108ed92e1c9901936541020bc3214533acce77bb (diff) |
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.
Diffstat (limited to 'dhcpv4/option_ip_address_lease_time.go')
-rw-r--r-- | dhcpv4/option_ip_address_lease_time.go | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go index 7562c58..2f63fb7 100644 --- a/dhcpv4/option_ip_address_lease_time.go +++ b/dhcpv4/option_ip_address_lease_time.go @@ -3,6 +3,8 @@ package dhcpv4 import ( "encoding/binary" "fmt" + + "github.com/u-root/u-root/pkg/uio" ) // This option implements the IP Address Lease Time option @@ -16,20 +18,9 @@ type OptIPAddressLeaseTime struct { // ParseOptIPAddressLeaseTime constructs an OptIPAddressLeaseTime struct from a // sequence of bytes and returns it, or an error. func ParseOptIPAddressLeaseTime(data []byte) (*OptIPAddressLeaseTime, error) { - // Should at least have code, length, and lease time. - if len(data) < 6 { - return nil, ErrShortByteStream - } - code := OptionCode(data[0]) - if code != OptionIPAddressLeaseTime { - return nil, fmt.Errorf("expected option %v, got %v instead", OptionIPAddressLeaseTime, code) - } - length := int(data[1]) - if length != 4 { - return nil, fmt.Errorf("expected length 4, got %v instead", length) - } - leaseTime := binary.BigEndian.Uint32(data[2:6]) - return &OptIPAddressLeaseTime{LeaseTime: leaseTime}, nil + buf := uio.NewBigEndianBuffer(data) + leaseTime := buf.Read32() + return &OptIPAddressLeaseTime{LeaseTime: leaseTime}, buf.FinError() } // Code returns the option code. |