summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2020-03-10 06:30:45 +0000
committerChris K <c@chrisko.ch>2020-03-10 14:41:26 -0700
commit15ad2c44a4ebc572866a5666c8875c3b192a4975 (patch)
tree3b243db20a497041dbbca83e6e1f625e229e9f81
parentaf38d14404756c4167372e214816af59b41d343b (diff)
dhcpv6 fqdn: fix domain encoding
Signed-off-by: Pablo Mazzini <pmazzini@gmail.com>
-rw-r--r--dhcpv6/modifiers.go8
-rw-r--r--dhcpv6/modifiers_test.go2
-rw-r--r--dhcpv6/option_fqdn.go11
-rw-r--r--dhcpv6/option_fqdn_test.go17
4 files changed, 25 insertions, 13 deletions
diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go
index dbf5d82..95f49d1 100644
--- a/dhcpv6/modifiers.go
+++ b/dhcpv6/modifiers.go
@@ -32,8 +32,12 @@ func WithNetboot(d DHCPv6) {
// WithFQDN adds a fully qualified domain name option to the packet
func WithFQDN(flags uint8, domainname string) Modifier {
return func(d DHCPv6) {
- ofqdn := OptFQDN{Flags: flags, DomainName: domainname}
- d.AddOption(&ofqdn)
+ d.UpdateOption(&OptFQDN{
+ Flags: flags,
+ DomainName: &rfc1035label.Labels{
+ Labels: []string{domainname},
+ },
+ })
}
}
diff --git a/dhcpv6/modifiers_test.go b/dhcpv6/modifiers_test.go
index bd328a3..8d06e75 100644
--- a/dhcpv6/modifiers_test.go
+++ b/dhcpv6/modifiers_test.go
@@ -90,7 +90,7 @@ func TestWithFQDN(t *testing.T) {
ofqdn := d.Options.FQDN()
require.Equal(t, OptionFQDN, ofqdn.Code())
require.Equal(t, uint8(4), ofqdn.Flags)
- require.Equal(t, "cnos.localhost", ofqdn.DomainName)
+ require.Equal(t, "cnos.localhost", ofqdn.DomainName.Labels[0])
}
func TestWithDHCP4oDHCP6Server(t *testing.T) {
diff --git a/dhcpv6/option_fqdn.go b/dhcpv6/option_fqdn.go
index 82d1254..8bac95d 100644
--- a/dhcpv6/option_fqdn.go
+++ b/dhcpv6/option_fqdn.go
@@ -3,6 +3,7 @@ package dhcpv6
import (
"fmt"
+ "github.com/insomniacslk/dhcp/rfc1035label"
"github.com/u-root/u-root/pkg/uio"
)
@@ -11,7 +12,7 @@ import (
// https://tools.ietf.org/html/rfc4704
type OptFQDN struct {
Flags uint8
- DomainName string
+ DomainName *rfc1035label.Labels
}
// Code returns the option code.
@@ -23,7 +24,7 @@ func (op *OptFQDN) Code() OptionCode {
func (op *OptFQDN) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write8(op.Flags)
- buf.WriteBytes([]byte(op.DomainName))
+ buf.WriteBytes(op.DomainName.ToBytes())
return buf.Data()
}
@@ -34,8 +35,12 @@ func (op *OptFQDN) String() string {
// ParseOptFQDN deserializes from bytes to build a OptFQDN structure.
func ParseOptFQDN(data []byte) (*OptFQDN, error) {
var opt OptFQDN
+ var err error
buf := uio.NewBigEndianBuffer(data)
opt.Flags = buf.Read8()
- opt.DomainName = string(buf.ReadAll())
+ opt.DomainName, err = rfc1035label.FromBytes(buf.ReadAll())
+ if err != nil {
+ return nil, err
+ }
return &opt, buf.FinError()
}
diff --git a/dhcpv6/option_fqdn_test.go b/dhcpv6/option_fqdn_test.go
index 870ed9d..f698c93 100644
--- a/dhcpv6/option_fqdn_test.go
+++ b/dhcpv6/option_fqdn_test.go
@@ -4,33 +4,36 @@ import (
"bytes"
"testing"
+ "github.com/insomniacslk/dhcp/rfc1035label"
"github.com/stretchr/testify/require"
)
func TestParseOptFQDN(t *testing.T) {
data := []byte{
0, // Flags
- 'c', 'n', 'o', 's', '.', 'l', 'o', 'c', 'a', 'l',
- 'h', 'o', 's', 't',
+ 4, 'c', 'n', 'o', 's', 9, 'l', 'o', 'c', 'a', 'l',
+ 'h', 'o', 's', 't', 0,
}
opt, err := ParseOptFQDN(data)
require.NoError(t, err)
require.Equal(t, OptionFQDN, opt.Code())
require.Equal(t, uint8(0), opt.Flags)
- require.Equal(t, "cnos.localhost", opt.DomainName)
- require.Equal(t, "OptFQDN{flags=0, domainname=cnos.localhost}", opt.String())
+ require.Equal(t, "cnos.localhost", opt.DomainName.Labels[0])
+ require.Equal(t, "OptFQDN{flags=0, domainname=[cnos.localhost]}", opt.String())
}
func TestOptFQDNToBytes(t *testing.T) {
opt := OptFQDN{
Flags: 0,
- DomainName: "cnos.localhost",
+ DomainName: &rfc1035label.Labels{
+ Labels: []string{"cnos.localhost"},
+ },
}
want := []byte{
0, // Flags
- 'c', 'n', 'o', 's', '.', 'l', 'o', 'c', 'a', 'l',
- 'h', 'o', 's', 't',
+ 4, 'c', 'n', 'o', 's', 9, 'l', 'o', 'c', 'a', 'l',
+ 'h', 'o', 's', 't', 0,
}
b := opt.ToBytes()
if !bytes.Equal(b, want) {