From fcb754f11193bbcd0604de2d6a557547715359dc Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Wed, 10 Apr 2019 14:59:34 +0100 Subject: [PATCH] [route] Explicitly set SrcLen and DstLen Signed-off-by: Andrea Barberio --- route.go | 20 ++++++++++++++++++++ route_test.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/route.go b/route.go index 0391f99..6475b36 100644 --- a/route.go +++ b/route.go @@ -37,6 +37,26 @@ const routeMessageLength = 12 func (m *RouteMessage) MarshalBinary() ([]byte, error) { b := make([]byte, routeMessageLength) + // ensure that DstLength and SrcLength are correct + if m.Attributes.Dst != nil { + if m.Attributes.Dst.To4() != nil { + m.DstLength = 32 + } else if m.Attributes.Dst.To16() != nil { + m.DstLength = 128 + } else { + return nil, errInvalidRouteMessageAttr + } + } + if m.Attributes.Src != nil { + if m.Attributes.Src.To4() != nil { + m.SrcLength = 32 + } else if m.Attributes.Src.To16() != nil { + m.SrcLength = 128 + } else { + return nil, errInvalidRouteMessageAttr + } + } + b[0] = m.Family b[1] = m.DstLength b[2] = m.SrcLength diff --git a/route_test.go b/route_test.go index 16a7f4c..1a50f4d 100644 --- a/route_test.go +++ b/route_test.go @@ -46,7 +46,7 @@ func TestRouteMessageMarshalBinary(t *testing.T) { }, }, b: []byte{ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x08, 0x00, 0x04, 0x00,