diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-03-04 21:24:31 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2020-03-04 21:31:14 +0000 |
commit | 9e4750895995d4d3f4dd36d4fff26d7b3927a34b (patch) | |
tree | b143dcb213f9e32bc3bbffc1bc53a94c591b140c | |
parent | 81b9770086ea37a41d98163431b0c53ec70c823c (diff) |
dhcpv6: allow using OptIAPrefix without initializing
Set the IPv6 prefix field to zero if the prefix is invalid
or not initialized.
Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
-rw-r--r-- | dhcpv6/option_iaprefix.go | 7 | ||||
-rw-r--r-- | dhcpv6/option_iaprefix_test.go | 15 |
2 files changed, 21 insertions, 1 deletions
diff --git a/dhcpv6/option_iaprefix.go b/dhcpv6/option_iaprefix.go index abd7a34..c66b242 100644 --- a/dhcpv6/option_iaprefix.go +++ b/dhcpv6/option_iaprefix.go @@ -34,7 +34,12 @@ func (op *OptIAPrefix) ToBytes() []byte { t2.Marshal(buf) buf.Write8(op.prefixLength) - buf.WriteBytes(op.ipv6Prefix.To16()) + prefix := op.ipv6Prefix.To16() + if prefix != nil { + buf.WriteBytes(prefix) + } else { + buf.WriteBytes(make([]byte, net.IPv6len)) + } buf.WriteBytes(op.Options.ToBytes()) return buf.Data() } diff --git a/dhcpv6/option_iaprefix_test.go b/dhcpv6/option_iaprefix_test.go index 7a24d2b..a1c140e 100644 --- a/dhcpv6/option_iaprefix_test.go +++ b/dhcpv6/option_iaprefix_test.go @@ -56,6 +56,21 @@ func TestOptIAPrefixToBytes(t *testing.T) { } } +func TestOptIAPrefixToBytesDefault(t *testing.T) { + buf := []byte{ + 0, 0, 0, 0, // preferredLifetime + 0, 0, 0, 0, // validLifetime + 0, // prefixLength + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ipv6Prefix + } + opt := OptIAPrefix{ + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, buf) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", buf, toBytes) + } +} + func TestOptIAPrefixParseInvalidTooShort(t *testing.T) { buf := []byte{ 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime |