Newer
Older
bnet "github.com/bio-routing/bio-rd/net"
type NLRI struct {
PathIdentifier uint32
Next *NLRI
}
func decodeNLRIs(buf *bytes.Buffer, length uint16, afi uint16) (*NLRI, error) {
var ret *NLRI
var eol *NLRI
var nlri *NLRI
var err error
var consumed uint8
p := uint16(0)
for p < length {
if err != nil {
return nil, fmt.Errorf("Unable to decode NLRI: %v", err)
}
p += uint16(consumed)
if ret == nil {
ret = nlri
eol = nlri
continue
}
eol.Next = nlri
eol = nlri
}
return ret, nil
}
func decodeNLRI(buf *bytes.Buffer, afi uint16) (*NLRI, uint8, error) {
consumed := uint8(0)
pfxLen, err := buf.ReadByte()
numBytes := uint8(BytesInAddr(pfxLen))
bytes := make([]byte, numBytes)
r, err := buf.Read(bytes)
consumed += uint8(r)
if r < int(numBytes) {
return nil, consumed, fmt.Errorf("expected %d bytes for NLRI, only %d remaining", numBytes, r)
pfx, err := deserializePrefix(bytes, pfxLen, afi)
func (n *NLRI) serialize(buf *bytes.Buffer) uint8 {
buf.WriteByte(n.Prefix.Pfxlen())
b := n.Prefix.Addr().Bytes()
func (n *NLRI) serializeAddPath(buf *bytes.Buffer) uint8 {
return uint8(n.serialize(buf) + 4)
// BytesInAddr gets the amount of bytes needed to encode an NLRI of prefix length pfxlen
func BytesInAddr(pfxlen uint8) uint8 {
return uint8(math.Ceil(float64(pfxlen) / 8))
}