diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2019-03-13 22:34:21 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-13 22:34:21 +0000 |
commit | 1c542dbd7c4522d965f58dfff8090184d38fca2d (patch) | |
tree | c3a1809d5b842220a403069d5cef1c1f0bb16997 /dhcpv4/option_strings.go | |
parent | 509f500742c67a3fbfa009df77986fb0977ad5f0 (diff) |
[dhcpv4] simplify userclass handling (#249)
Diffstat (limited to 'dhcpv4/option_strings.go')
-rw-r--r-- | dhcpv4/option_strings.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/dhcpv4/option_strings.go b/dhcpv4/option_strings.go new file mode 100644 index 0000000..dafa9f2 --- /dev/null +++ b/dhcpv4/option_strings.go @@ -0,0 +1,55 @@ +package dhcpv4 + +import ( + "fmt" + "strings" + + "github.com/u-root/u-root/pkg/uio" +) + +// Strings represents an option encapsulating a list of strings in IPv4 DHCP as +// specified in RFC 3004 +// +// Strings implements the OptionValue type. +type Strings []string + +// FromBytes parses Strings from a DHCP packet as specified by RFC 3004. +func (o *Strings) FromBytes(data []byte) error { + buf := uio.NewBigEndianBuffer(data) + if buf.Len() == 0 { + return fmt.Errorf("Strings DHCP option must always list at least one String") + } + + *o = make(Strings, 0) + for buf.Has(1) { + ucLen := buf.Read8() + if ucLen == 0 { + return fmt.Errorf("DHCP Strings must have length greater than 0") + } + *o = append(*o, string(buf.CopyN(int(ucLen)))) + } + return buf.FinError() +} + +// ToBytes marshals Strings to a DHCP packet as specified by RFC 3004. +func (o Strings) ToBytes() []byte { + buf := uio.NewBigEndianBuffer(nil) + for _, uc := range o { + buf.Write8(uint8(len(uc))) + buf.WriteBytes([]byte(uc)) + } + return buf.Data() +} + +// String returns a human-readable representation of a list of Strings. +func (o Strings) String() string { + return strings.Join(o, ", ") +} + +// OptRFC3004UserClass returns a new user class option according to RFC 3004. +func OptRFC3004UserClass(v []string) Option { + return Option{ + Code: OptionUserClassInformation, + Value: Strings(v), + } +} |