diff options
author | Sean Karlage <skarlage@fb.com> | 2018-08-11 14:30:48 -0700 |
---|---|---|
committer | Sean Karlage <skarlage@fb.com> | 2018-08-11 14:32:26 -0700 |
commit | 8ea2525c898436a2a935580de67727bbe7035c85 (patch) | |
tree | 69d35d17c238feabb07ef07a907aae5520104911 /dhcpv4/option_subnet_mask.go | |
parent | 2b05c7d03724d31529886d98f738499ac06ead7e (diff) | |
parent | a6212f1f72e94821a29894fb66656a981bd035d0 (diff) |
Merge branch 'master' into dhcpv4-moar-tests
Diffstat (limited to 'dhcpv4/option_subnet_mask.go')
-rw-r--r-- | dhcpv4/option_subnet_mask.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/dhcpv4/option_subnet_mask.go b/dhcpv4/option_subnet_mask.go new file mode 100644 index 0000000..f1ff4a4 --- /dev/null +++ b/dhcpv4/option_subnet_mask.go @@ -0,0 +1,56 @@ +package dhcpv4 + +import ( + "fmt" + "net" +) + +// This option implements the subnet mask option +// https://tools.ietf.org/html/rfc2132 + +// OptSubnetMask represents an option encapsulating the subnet mask. +type OptSubnetMask struct { + SubnetMask net.IPMask +} + +// ParseOptSubnetMask returns a new OptSubnetMask from a byte +// stream, or error if any. +func ParseOptSubnetMask(data []byte) (*OptSubnetMask, error) { + if len(data) < 2 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionSubnetMask { + return nil, fmt.Errorf("expected code %v, got %v", OptionSubnetMask, 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 &OptSubnetMask{SubnetMask: net.IPMask(data[2 : 2+length])}, nil +} + +// Code returns the option code. +func (o *OptSubnetMask) Code() OptionCode { + return OptionSubnetMask +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptSubnetMask) ToBytes() []byte { + ret := []byte{byte(o.Code()), byte(o.Length())} + return append(ret, o.SubnetMask[:4]...) +} + +// String returns a human-readable string. +func (o *OptSubnetMask) String() string { + return fmt.Sprintf("Subnet Mask -> %v", o.SubnetMask.String()) +} + +// Length returns the length of the data portion (excluding option code an byte +// length). +func (o *OptSubnetMask) Length() int { + return 4 +} |