Skip to content
Snippets Groups Projects
Commit 5ecbc83c authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

added MP_UNREACH_NLRI

parent 378fbb29
No related branches found
No related tags found
No related merge requests found
package packet
import (
"math"
bnet "github.com/bio-routing/bio-rd/net"
)
func serializePrefix(pfx bnet.Prefix) []byte {
if pfx.Pfxlen() == 0 {
return []byte{}
}
numBytes := uint8(math.Ceil(float64(pfx.Pfxlen()) / float64(8)))
b := make([]byte, numBytes+1)
b[0] = pfx.Pfxlen()
copy(b[1:numBytes+1], pfx.Addr().Bytes()[0:numBytes])
return b
}
...@@ -2,14 +2,13 @@ package packet ...@@ -2,14 +2,13 @@ package packet
import ( import (
"bytes" "bytes"
"math"
"github.com/taktv6/tflow2/convert" "github.com/taktv6/tflow2/convert"
bnet "github.com/bio-routing/bio-rd/net" bnet "github.com/bio-routing/bio-rd/net"
) )
// MultiProtocolReachNLRI represents Network Layer Reachability Information for one prefix of an IP address family (rfc4760) // MultiProtocolReachNLRI represents network layer reachability information for one prefix of an IP address family (rfc4760)
type MultiProtocolReachNLRI struct { type MultiProtocolReachNLRI struct {
AFI uint16 AFI uint16
SAFI uint8 SAFI uint8
...@@ -26,23 +25,9 @@ func (n *MultiProtocolReachNLRI) serialize(buf *bytes.Buffer) uint8 { ...@@ -26,23 +25,9 @@ func (n *MultiProtocolReachNLRI) serialize(buf *bytes.Buffer) uint8 {
tempBuf.WriteByte(uint8(len(nextHop))) // NextHop length tempBuf.WriteByte(uint8(len(nextHop))) // NextHop length
tempBuf.Write(nextHop) tempBuf.Write(nextHop)
tempBuf.WriteByte(0) // RESERVED tempBuf.WriteByte(0) // RESERVED
tempBuf.Write(n.serializePrefix()) tempBuf.Write(serializePrefix(n.Prefix))
buf.Write(tempBuf.Bytes()) buf.Write(tempBuf.Bytes())
return uint8(tempBuf.Len()) return uint8(tempBuf.Len())
} }
func (n *MultiProtocolReachNLRI) serializePrefix() []byte {
if n.Prefix.Pfxlen() == 0 {
return []byte{}
}
numBytes := uint8(math.Ceil(float64(n.Prefix.Pfxlen()) / float64(8)))
b := make([]byte, numBytes+1)
b[0] = n.Prefix.Pfxlen()
copy(b[1:numBytes+1], n.Prefix.Addr().Bytes()[0:numBytes])
return b
}
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSerializeMultiProtocolNLRI(t *testing.T) { func TestSerializeMultiProtocolReachNLRI(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
nlri MultiProtocolReachNLRI nlri MultiProtocolReachNLRI
......
package packet
import (
"bytes"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/taktv6/tflow2/convert"
)
// MultiProtocolUnreachNLRI represents network layer withdraw information for one prefix of an IP address family (rfc4760)
type MultiProtocolUnreachNLRI struct {
AFI uint16
SAFI uint8
Prefix bnet.Prefix
}
func (n *MultiProtocolUnreachNLRI) serialize(buf *bytes.Buffer) uint8 {
tempBuf := bytes.NewBuffer(nil)
tempBuf.Write(convert.Uint16Byte(n.AFI))
tempBuf.WriteByte(n.SAFI)
tempBuf.Write(serializePrefix(n.Prefix))
buf.Write(tempBuf.Bytes())
return uint8(tempBuf.Len())
}
package packet
import (
"bytes"
"testing"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert"
)
func TestSerializeMultiProtocolUnreachNLRI(t *testing.T) {
tests := []struct {
name string
nlri MultiProtocolUnreachNLRI
expected []byte
}{
{
name: "Simple IPv6 prefix",
nlri: MultiProtocolUnreachNLRI{
AFI: IPv6AFI,
SAFI: UnicastSAFI,
Prefix: bnet.NewPfx(bnet.IPv6FromBlocks(0x2620, 0x110, 0x9000, 0, 0, 0, 0, 0), 44),
},
expected: []byte{
0x00, 0x02, // AFI
0x01, // SAFI
0x2c, 0x26, 0x20, 0x01, 0x10, 0x90, 0x00, // Prefix
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := &bytes.Buffer{}
test.nlri.serialize(buf)
assert.Equal(t, test.expected, buf.Bytes())
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment