diff options
Diffstat (limited to 'src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java')
-rw-r--r-- | src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java new file mode 100644 index 0000000..346cb0e --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java @@ -0,0 +1,59 @@ +package com.lumaserv.bgp.protocol.attribute; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; + +import lombok.Getter; +import lombok.Setter; + +import com.lumaserv.bgp.protocol.AFI; +import com.lumaserv.bgp.protocol.IPPrefix; +import com.lumaserv.bgp.protocol.SAFI; + +@Getter +@Setter +public class MPReachableNLRIAttribute implements PathAttribute { + + AFI afi; + SAFI safi; + byte[] nextHop; + Collection<IPPrefix> nlriPrefixes; + + public MPReachableNLRIAttribute(byte typeCode, byte[] data) { + afi = AFI.fromInteger((data[0] << 8) | data[1]); + safi = SAFI.fromInteger(data[2]); + int nhLen = data[3]; + nextHop = new byte[nhLen]; + System.arraycopy(data, 4, nextHop, 0, nextHop.length); + int nlriLen = data.length - 5 - nhLen; + nlriPrefixes = new ArrayList<>(); + + int offset = 4 + nhLen + 1; + int offsetOffset = offset; + while ((offset - offsetOffset) < nlriLen) { + IPPrefix prefix = new IPPrefix() + .setLength((int)(data[offset]&0xff)) // FIXME IPv4/IPv6 etc + .setAddress(new byte[16]); + offset++; + int addressLen = (int) Math.ceil(prefix.getLength() / 8d); + System.arraycopy(data, offset, prefix.getAddress(), 0, addressLen); + offset += addressLen; + nlriPrefixes.add(prefix); + } + } + + public byte getTypeCode() { + return 14; + } + + public void build(ByteBuffer b) { + // FIXME + } + + public String toString() { + return "AFI:" + afi + " SAFI:" + safi + " NLRIs:" + nlriPrefixes; + } +} |