summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2020-03-09 19:48:51 +0000
committerGitHub <noreply@github.com>2020-03-09 19:48:51 +0000
commit030b5b237deb0bb0c4d821dde661af87e9f0263c (patch)
tree2657c611cc944321182aa37db73bc2430b9d402e
parentbd34b7c6963c8c124b45759423d41987d428668e (diff)
parent27111faab6c0ca7834f2078eb9a14615c8f4fa59 (diff)
Merge pull request #365 from hugelgupf/write16
dhcpv6: always write 16-byte IPs
-rw-r--r--dhcpv6/dhcpv6relay.go13
-rw-r--r--dhcpv6/dhcpv6relay_test.go11
-rw-r--r--dhcpv6/option_iaaddress.go2
-rw-r--r--dhcpv6/option_iaaddress_test.go10
-rw-r--r--dhcpv6/option_iaprefix.go7
5 files changed, 34 insertions, 9 deletions
diff --git a/dhcpv6/dhcpv6relay.go b/dhcpv6/dhcpv6relay.go
index 02ed9bb..209cc71 100644
--- a/dhcpv6/dhcpv6relay.go
+++ b/dhcpv6/dhcpv6relay.go
@@ -20,6 +20,15 @@ type RelayMessage struct {
Options Options
}
+func write16(b *uio.Lexer, ip net.IP) {
+ if ip == nil || ip.To16() == nil {
+ var zeros [net.IPv6len]byte
+ b.WriteBytes(zeros[:])
+ } else {
+ b.WriteBytes(ip.To16())
+ }
+}
+
// Type is this relay message's types.
func (r *RelayMessage) Type() MessageType {
return r.MessageType
@@ -58,8 +67,8 @@ func (r *RelayMessage) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(make([]byte, 0, RelayHeaderSize))
buf.Write8(byte(r.MessageType))
buf.Write8(r.HopCount)
- buf.WriteBytes(r.LinkAddr.To16())
- buf.WriteBytes(r.PeerAddr.To16())
+ write16(buf, r.LinkAddr)
+ write16(buf, r.PeerAddr)
buf.WriteBytes(r.Options.ToBytes())
return buf.Data()
}
diff --git a/dhcpv6/dhcpv6relay_test.go b/dhcpv6/dhcpv6relay_test.go
index a7918ab..c383487 100644
--- a/dhcpv6/dhcpv6relay_test.go
+++ b/dhcpv6/dhcpv6relay_test.go
@@ -36,6 +36,17 @@ func TestRelayMessage(t *testing.T) {
}
}
+func TestRelayMessageToBytesDefault(t *testing.T) {
+ want := []byte{
+ 12, // MessageTypeRelayForward
+ 0, // hop count
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // link addr
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // peer addr
+ }
+ r := RelayMessage{MessageType: MessageTypeRelayForward}
+ require.Equal(t, r.ToBytes(), want)
+}
+
func TestRelayMessageToBytes(t *testing.T) {
expected := []byte{
12, // MessageTypeRelayForward
diff --git a/dhcpv6/option_iaaddress.go b/dhcpv6/option_iaaddress.go
index 9752ee7..bb92c7a 100644
--- a/dhcpv6/option_iaaddress.go
+++ b/dhcpv6/option_iaaddress.go
@@ -47,7 +47,7 @@ func (op *OptIAAddress) Code() OptionCode {
// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptIAAddress) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
- buf.WriteBytes(op.IPv6Addr.To16())
+ write16(buf, op.IPv6Addr)
t1 := Duration{op.PreferredLifetime}
t1.Marshal(buf)
diff --git a/dhcpv6/option_iaaddress_test.go b/dhcpv6/option_iaaddress_test.go
index 900973c..26f1732 100644
--- a/dhcpv6/option_iaaddress_test.go
+++ b/dhcpv6/option_iaaddress_test.go
@@ -43,6 +43,16 @@ func TestOptIAAddressParseInvalidBrokenOptions(t *testing.T) {
require.Error(t, err)
}
+func TestOptIAAddressToBytesDefault(t *testing.T) {
+ want := []byte{
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // IP
+ 0, 0, 0, 0, // preferred lifetime
+ 0, 0, 0, 0, // valid lifetime
+ }
+ opt := OptIAAddress{}
+ require.Equal(t, opt.ToBytes(), want)
+}
+
func TestOptIAAddressToBytes(t *testing.T) {
ipBytes := []byte{0x24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
expected := append(ipBytes, []byte{
diff --git a/dhcpv6/option_iaprefix.go b/dhcpv6/option_iaprefix.go
index c66b242..bb907b2 100644
--- a/dhcpv6/option_iaprefix.go
+++ b/dhcpv6/option_iaprefix.go
@@ -34,12 +34,7 @@ func (op *OptIAPrefix) ToBytes() []byte {
t2.Marshal(buf)
buf.Write8(op.prefixLength)
- prefix := op.ipv6Prefix.To16()
- if prefix != nil {
- buf.WriteBytes(prefix)
- } else {
- buf.WriteBytes(make([]byte, net.IPv6len))
- }
+ write16(buf, op.ipv6Prefix)
buf.WriteBytes(op.Options.ToBytes())
return buf.Data()
}