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 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; } }