diff options
author | Sean Karlage <skarlage@get9.io> | 2018-03-16 08:42:29 -0700 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-03-16 15:42:29 +0000 |
commit | 79b8450e99efee338596f15fc1f8f88c3e42edc0 (patch) | |
tree | 9e62bedb49ca5b6b8a61188f71a9cdfd248edafd /dhcpv4/option_requested_ip_address.go | |
parent | 1d020f7d3aba3dfe1c142c5f73d3b3b3992ad2ad (diff) |
Add more specific dhcpv4 options (#17)
Added several DHCPv4 options
Diffstat (limited to 'dhcpv4/option_requested_ip_address.go')
-rw-r--r-- | dhcpv4/option_requested_ip_address.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/dhcpv4/option_requested_ip_address.go b/dhcpv4/option_requested_ip_address.go new file mode 100644 index 0000000..8662263 --- /dev/null +++ b/dhcpv4/option_requested_ip_address.go @@ -0,0 +1,57 @@ +package dhcpv4 + +import ( + "fmt" + "net" +) + +// This option implements the requested IP address option +// https://tools.ietf.org/html/rfc2132 + +// OptRequestedIPAddress represents an option encapsulating the server +// identifier. +type OptRequestedIPAddress struct { + RequestedAddr net.IP +} + +// ParseOptRequestedIPAddress returns a new OptServerIdentifier from a byte +// stream, or error if any. +func ParseOptRequestedIPAddress(data []byte) (*OptRequestedIPAddress, error) { + if len(data) < 2 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionRequestedIPAddress { + return nil, fmt.Errorf("expected code %v, got %v", OptionRequestedIPAddress, 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 &OptRequestedIPAddress{RequestedAddr: net.IP(data[2 : 2+length])}, nil +} + +// Code returns the option code. +func (o *OptRequestedIPAddress) Code() OptionCode { + return OptionRequestedIPAddress +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptRequestedIPAddress) ToBytes() []byte { + ret := []byte{byte(o.Code()), byte(o.Length())} + return append(ret, o.RequestedAddr.To4()...) +} + +// String returns a human-readable string. +func (o *OptRequestedIPAddress) String() string { + return fmt.Sprintf("Requested IP Address -> %v", o.RequestedAddr.String()) +} + +// Length returns the length of the data portion (excluding option code an byte +// length). +func (o *OptRequestedIPAddress) Length() int { + return len(o.RequestedAddr.To4()) +} |