summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_server_identifier.go
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2018-12-28 20:19:14 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2019-01-10 23:01:22 +0000
commite87114a6e449d7a2e458b5529923e5668dfa3a11 (patch)
tree7c5ec350c8eb859400a159b881081ffe0dbd4ef0 /dhcpv4/option_server_identifier.go
parent108ed92e1c9901936541020bc3214533acce77bb (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_server_identifier.go')
-rw-r--r--dhcpv4/option_server_identifier.go23
1 files changed, 6 insertions, 17 deletions
diff --git a/dhcpv4/option_server_identifier.go b/dhcpv4/option_server_identifier.go
index 26c21a7..fd0311a 100644
--- a/dhcpv4/option_server_identifier.go
+++ b/dhcpv4/option_server_identifier.go
@@ -3,12 +3,14 @@ package dhcpv4
import (
"fmt"
"net"
+
+ "github.com/u-root/u-root/pkg/uio"
)
+// OptServerIdentifier represents an option encapsulating the server identifier.
+//
// This option implements the server identifier option
// https://tools.ietf.org/html/rfc2132
-
-// OptServerIdentifier represents an option encapsulating the server identifier.
type OptServerIdentifier struct {
ServerID net.IP
}
@@ -16,21 +18,8 @@ type OptServerIdentifier struct {
// ParseOptServerIdentifier returns a new OptServerIdentifier from a byte
// stream, or error if any.
func ParseOptServerIdentifier(data []byte) (*OptServerIdentifier, error) {
- if len(data) < 2 {
- return nil, ErrShortByteStream
- }
- code := OptionCode(data[0])
- if code != OptionServerIdentifier {
- return nil, fmt.Errorf("expected code %v, got %v", OptionServerIdentifier, code)
- }
- length := int(data[1])
- if length != 4 {
- return nil, fmt.Errorf("unexepcted length: expected 4, got %v", length)
- }
- if len(data) < 6 {
- return nil, ErrShortByteStream
- }
- return &OptServerIdentifier{ServerID: net.IP(data[2 : 2+length])}, nil
+ buf := uio.NewBigEndianBuffer(data)
+ return &OptServerIdentifier{ServerID: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError()
}
// Code returns the option code.