[route] Explicitly set SrcLen and DstLen

Signed-off-by: Andrea Barberio <insomniac@slackware.it>
This commit is contained in:
Andrea Barberio 2019-04-10 14:59:34 +01:00
parent 4f20371a97
commit fcb754f111
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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,