diff options
Diffstat (limited to 'dhcpv4/option_generic.go')
-rw-r--r-- | dhcpv4/option_generic.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/dhcpv4/option_generic.go b/dhcpv4/option_generic.go index 0fb57cd..0cecfd6 100644 --- a/dhcpv4/option_generic.go +++ b/dhcpv4/option_generic.go @@ -1,6 +1,7 @@ package dhcpv4 import ( + "errors" "fmt" ) @@ -12,6 +13,27 @@ type OptionGeneric struct { Data []byte } +// ParseOptionGeneric parses a bytestream and creates a new OptionGeneric from +// it, or an error. +func ParseOptionGeneric(data []byte) (*OptionGeneric, error) { + if len(data) == 0 { + return nil, errors.New("invalid zero-length bytestream") + } + var ( + length int + optionData []byte + ) + code := OptionCode(data[0]) + if code != OptionPad && code != OptionEnd { + length = int(data[1]) + if len(data) < length+2 { + return nil, fmt.Errorf("invalid data length: declared %v, actual %v", length, len(data)) + } + optionData = data[2 : length+2] + } + return &OptionGeneric{OptionCode: code, Data: optionData}, nil +} + // Code returns the generic option code. func (o OptionGeneric) Code() OptionCode { return o.OptionCode |