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_bootfile_name.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_bootfile_name.go')
-rw-r--r-- | dhcpv4/option_bootfile_name.go | 22 |
1 files changed, 3 insertions, 19 deletions
diff --git a/dhcpv4/option_bootfile_name.go b/dhcpv4/option_bootfile_name.go index ca9317b..e06e4eb 100644 --- a/dhcpv4/option_bootfile_name.go +++ b/dhcpv4/option_bootfile_name.go @@ -9,7 +9,7 @@ import ( // OptBootfileName implements the BootFile Name option type OptBootfileName struct { - BootfileName []byte + BootfileName string } // Code returns the option code @@ -19,7 +19,7 @@ func (op *OptBootfileName) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptBootfileName) ToBytes() []byte { - return append([]byte{byte(op.Code()), byte(op.Length())}, op.BootfileName...) + return append([]byte{byte(op.Code()), byte(op.Length())}, []byte(op.BootfileName)...) } // Length returns the option length in bytes @@ -34,21 +34,5 @@ func (op *OptBootfileName) String() string { // ParseOptBootfileName returns a new OptBootfile from a byte stream or error if any func ParseOptBootfileName(data []byte) (*OptBootfileName, error) { - if len(data) < 3 { - return nil, ErrShortByteStream - } - code := OptionCode(data[0]) - if code != OptionBootfileName { - return nil, fmt.Errorf("ParseOptBootfileName: invalid code: %v; want %v", code, OptionBootfileName) - } - length := int(data[1]) - if length < 1 { - return nil, fmt.Errorf("Bootfile name has invalid length of %d", length) - } - bootFileNameData := data[2:] - if len(bootFileNameData) < length { - return nil, fmt.Errorf("ParseOptBootfileName: short data: %d bytes; want %d", - len(bootFileNameData), length) - } - return &OptBootfileName{BootfileName: bootFileNameData[:length]}, nil + return &OptBootfileName{BootfileName: string(data)}, nil } |