summaryrefslogtreecommitdiffhomepage
path: root/iana
diff options
context:
space:
mode:
Diffstat (limited to 'iana')
-rw-r--r--iana/archtype.go44
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()
+}