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 /iana/archtype.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 'iana/archtype.go')
-rw-r--r-- | iana/archtype.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/iana/archtype.go b/iana/archtype.go index 510c6fc..255687c 100644 --- a/iana/archtype.go +++ b/iana/archtype.go @@ -1,5 +1,12 @@ package iana +import ( + "fmt" + "strings" + + "github.com/u-root/u-root/pkg/uio" +) + // Arch encodes an architecture type per RFC 4578, Section 2.1. type Arch uint16 @@ -38,3 +45,40 @@ func (a Arch) String() string { } return "unknown" } + +// Archs represents multiple Arch values. +type Archs []Arch + +// ToBytes returns the serialized option defined by RFC 4578 (DHCPv4) and RFC +// 5970 (DHCPv6) as the Client System Architecture Option. +func (a Archs) ToBytes() []byte { + buf := uio.NewBigEndianBuffer(nil) + for _, at := range a { + buf.Write16(uint16(at)) + } + return buf.Data() +} + +// String returns the list of archs in a human-readable manner. +func (a Archs) String() string { + s := make([]string, 0, len(a)) + for _, arch := range a { + s = append(s, arch.String()) + } + return strings.Join(s, ", ") +} + +// FromBytes parses a DHCP list of architecture types as defined by RFC 4578 +// and RFC 5970. +func (a *Archs) FromBytes(data []byte) error { + buf := uio.NewBigEndianBuffer(data) + if buf.Len() == 0 { + return fmt.Errorf("must have at least one archtype if option is present") + } + + *a = make([]Arch, 0, buf.Len()/2) + for buf.Has(2) { + *a = append(*a, Arch(buf.Read16())) + } + return buf.FinError() +} |