diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-29 14:48:10 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-24 08:05:49 +0000 |
commit | c90ab10024ada840e24bb028a3405961e8e4c26a (patch) | |
tree | 9b8af0c1b80ee6efc112921f9a14b92d6c73f8eb /dhcpv4/option_subnet_mask.go | |
parent | 2be5cae32d33f01ddecf6f167a9c0e5290e6d58f (diff) |
dhcpv4: nicer API for option parsing.
From:
r := d.GetOneOption(OptionRouter).(*OptRouter).Routers
d.UpdateOption(&OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}}})
To:
r := GetRouter(d.Options)
d.UpdateOption(OptRouter(net.IP{192, 168, 0, 1}, ...))
Diffstat (limited to 'dhcpv4/option_subnet_mask.go')
-rw-r--r-- | dhcpv4/option_subnet_mask.go | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/dhcpv4/option_subnet_mask.go b/dhcpv4/option_subnet_mask.go index 19401d8..82b344b 100644 --- a/dhcpv4/option_subnet_mask.go +++ b/dhcpv4/option_subnet_mask.go @@ -1,36 +1,55 @@ package dhcpv4 import ( - "fmt" "net" "github.com/u-root/u-root/pkg/uio" ) -// OptSubnetMask implements the subnet mask option described by RFC 2132, -// Section 3.3. -type OptSubnetMask struct { - SubnetMask net.IPMask +// IPMask represents an option encapsulating the subnet mask. +// +// This option implements the subnet mask option in RFC 2132, Section 3.3. +type IPMask net.IPMask + +// ToBytes returns a serialized stream of bytes for this option. +func (im IPMask) ToBytes() []byte { + if len(im) > net.IPv4len { + return im[:net.IPv4len] + } + return im } -// ParseOptSubnetMask returns a new OptSubnetMask from a byte -// stream, or error if any. -func ParseOptSubnetMask(data []byte) (*OptSubnetMask, error) { - buf := uio.NewBigEndianBuffer(data) - return &OptSubnetMask{SubnetMask: net.IPMask(buf.CopyN(net.IPv4len))}, buf.FinError() +// String returns a human-readable string. +func (im IPMask) String() string { + return net.IPMask(im).String() } -// Code returns the option code. -func (o *OptSubnetMask) Code() OptionCode { - return OptionSubnetMask +// FromBytes parses im from data per RFC 2132. +func (im *IPMask) FromBytes(data []byte) error { + buf := uio.NewBigEndianBuffer(data) + *im = IPMask(buf.CopyN(net.IPv4len)) + return buf.FinError() } -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptSubnetMask) ToBytes() []byte { - return o.SubnetMask[:net.IPv4len] +// GetSubnetMask returns a subnet mask option contained in o, if there is one. +// +// The subnet mask option is described by RFC 2132, Section 3.3. +func GetSubnetMask(o Options) net.IPMask { + v := o.Get(OptionSubnetMask) + if v == nil { + return nil + } + var im IPMask + if err := im.FromBytes(v); err != nil { + return nil + } + return net.IPMask(im) } -// String returns a human-readable string. -func (o *OptSubnetMask) String() string { - return fmt.Sprintf("Subnet Mask -> %v", o.SubnetMask.String()) +// OptSubnetMask returns a new DHCPv4 SubnetMask option per RFC 2132, Section 3.3. +func OptSubnetMask(mask net.IPMask) Option { + return Option{ + Code: OptionSubnetMask, + Value: IPMask(mask), + } } |