List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:io.netlibs.bgp.netty.codec.RouteRefreshPacketDecoder.java
License:Apache License
private List<ORFEntry> decodeORFEntries(ByteBuf buffer, ORFType orfType) { List<ORFEntry> entries = new LinkedList<ORFEntry>(); while (buffer.isReadable()) { int actionMatch = buffer.readUnsignedByte(); ORFAction action = ORFAction.fromCode((actionMatch >> 6) & 0x03); ORFMatch match = ORFMatch.fromCode((actionMatch >> 5) & 0x01); switch (orfType) { case ADDRESS_PREFIX_BASED: entries.add(decodeAddressPrefixBasedORFEntry(buffer, action, match)); break; default://from w ww.j a v a2s .com throw new IllegalArgumentException("cannot decode OutboudRouteFilter entries of type " + orfType); } } return entries; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
/** * decode a NOTIFICATION packet that corresponds to UPDATE apckets. The passed channel buffer MUST point to the first packet octet AFTER * the terror sub code.// w w w .j av a 2 s . co m * * @param buffer * the buffer containing the data. * @return */ public NotificationPacket decodeUpdateNotification(final ByteBuf buffer, final int errorSubcode) { UpdateNotificationPacket packet = null; byte[] offendingAttribute = null; if (buffer.isReadable()) { offendingAttribute = new byte[buffer.readableBytes()]; buffer.readBytes(offendingAttribute); } switch (errorSubcode) { case UpdateNotificationPacket.SUBCODE_MALFORMED_ATTRIBUTE_LIST: packet = new MalformedAttributeListNotificationPacket(); break; case UpdateNotificationPacket.SUBCODE_UNRECOGNIZED_WELL_KNOWN_ATTRIBUTE: packet = new UnrecognizedWellKnownAttributeNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_MISSING_WELL_KNOWN_ATTRIBUTE: packet = new MissingWellKnownAttributeNotificationPacket(0); break; case UpdateNotificationPacket.SUBCODE_ATTRIBUTE_FLAGS_ERROR: packet = new AttributeFlagsNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_ATTRIBUTE_LENGTH_ERROR: packet = new AttributeLengthNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_INVALID_ORIGIN_ATTRIBUTE: packet = new InvalidOriginNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_INVALID_NEXT_HOP_ATTRIBUTE: packet = new InvalidNextHopNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_OPTIONAL_ATTRIBUTE_ERROR: packet = new OptionalAttributeErrorNotificationPacket(offendingAttribute); break; case UpdateNotificationPacket.SUBCODE_INVALID_NETWORK_FIELD: packet = new InvalidNetworkFieldNotificationPacket(); break; case UpdateNotificationPacket.SUBCODE_MALFORMED_AS_PATH: packet = new MalformedASPathAttributeNotificationPacket(offendingAttribute); break; } return packet; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private ASPathAttribute decodeASPathAttribute(final ByteBuf buffer, final ASType asType) { final ASPathAttribute attr = new ASPathAttribute(asType); while (buffer.isReadable()) { if (buffer.readableBytes() < 2) { throw new MalformedASPathAttributeException(); }/* w ww . j a v a2 s. c om*/ final int segmentType = buffer.readUnsignedByte(); final int pathLength = buffer.readUnsignedByte(); final int pathOctetLength = (pathLength * (asType == ASType.AS_NUMBER_4OCTETS ? 4 : 2)); if (buffer.readableBytes() < pathOctetLength) { throw new MalformedASPathAttributeException(); } final PathSegment segment = new PathSegment(asType); try { segment.setPathSegmentType(PathSegmentTypeCodec.fromCode(segmentType)); } catch (final IllegalArgumentException e) { throw new MalformedASPathAttributeException(); } for (int i = 0; i < pathLength; i++) { if (asType == ASType.AS_NUMBER_4OCTETS) { segment.getAses().add((int) buffer.readUnsignedInt()); } else { segment.getAses().add(buffer.readUnsignedShort()); } } attr.getPathSegments().add(segment); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private CommunityPathAttribute decodeCommunityPathAttribute(final ByteBuf buffer) { final CommunityPathAttribute attr = new CommunityPathAttribute(); if ((buffer.readableBytes() < 4) || ((buffer.readableBytes() % 4) != 0)) { throw new OptionalAttributeErrorException(); }//w w w. ja v a 2 s .c o m while (buffer.isReadable()) { final CommunityMember member = new CommunityMember(); member.setAsNumber(buffer.readUnsignedShort()); member.setValue(buffer.readUnsignedShort()); attr.getMembers().add(member); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private ExtendedCommunityPathAttribute decodeExtendedCommunityPathAttribute(final ByteBuf buffer) { final ExtendedCommunityPathAttribute attr = new ExtendedCommunityPathAttribute(); if ((buffer.readableBytes() < 8) || ((buffer.readableBytes() % 8) != 0)) { throw new OptionalAttributeErrorException(); }/*from w ww.j av a2s. c o m*/ while (buffer.isReadable()) { AbstractExtendedCommunityInterface extcomm; // we need to check whether this is a transitive or non-transitive value // and then determine whether we need to red the lower type byte byte higherType = buffer.readByte(); byte higherTypeCompare = (byte) (higherType & ~(1 << 6)); if (((higherType >> 6) & 1) == 0) { // bit 7 is not set in the byte, therefore this is a transitive type // clear bit 8, not interested in this value TransitiveExtendedCommunityType transCommType = TransitiveExtendedCommunityType .fromCode((byte) (higherType & (~(3 << 6)))); switch (transCommType) { case TWO_OCTET_AS_SPECIFIC: TransitiveTwoOctetASSpecificExtCommSubTypes twoOctASNLowerType = TransitiveTwoOctetASSpecificExtCommSubTypes .fromCode(buffer.readByte()); switch (twoOctASNLowerType) { case ROUTE_TARGET: extcomm = new TransitiveTwoByteASNFourByteAdministratorRT((int) buffer.readShort(), (long) buffer.readInt()); break; default: // all non-RT types are currently unimplemented extcomm = new UnknownTransitiveTwoByteASNSpecificExtendedCommunity(transCommType, twoOctASNLowerType, buffer.readBytes(6).array()); } break; case TWO_OCTET_IPv4_ADDRESS_SPECIFIC: TransitiveIPv4AddressSpecificExtCommSubTypes ipv4LowerType = TransitiveIPv4AddressSpecificExtCommSubTypes .fromCode(buffer.readByte()); switch (ipv4LowerType) { case ROUTE_TARGET: try { extcomm = new TransitiveIPv4AddressTwoByteAdministratorRT( (Inet4Address) InetAddresses .fromLittleEndianByteArray(buffer.readBytes(4).array()), (int) buffer.readShort()); } catch (UnknownHostException e) { ByteBuf data = Unpooled.buffer(); data.getByte(ipv4LowerType.toCode()); data.readBytes(buffer.readBytes(6)); extcomm = new UnknownTransitiveExtendedCommunity(transCommType, data.array()); } break; default: // all non-RT types are currently unimplemented extcomm = new UnknownTransitiveIPv4AddressSpecificExtendedCommunity(transCommType, ipv4LowerType, buffer.readBytes(6).array()); break; } break; default: // by default, just create an unknown type, reading the subsequent // 7 bytes (we have already read byte 1) extcomm = new UnknownTransitiveExtendedCommunity(transCommType, buffer.readBytes(7).array()); } } else { // bit 7 is set, these are non-transitive NonTransitiveExtendedCommunityType nonTransCommType = NonTransitiveExtendedCommunityType .fromCode((byte) (higherType & (~(3 << 6)))); // all non-transitive types are currently unimplemented extcomm = new UnknownNonTransitiveExtendedCommunity(nonTransCommType, buffer.readBytes(7).array()); } attr.getMembers().add(extcomm); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private MultiProtocolReachableNLRI decodeMpReachNlriPathAttribute(final ByteBuf buffer) { final MultiProtocolReachableNLRI attr = new MultiProtocolReachableNLRI(); try {/*w w w . j a v a2 s.co m*/ attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort())); attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte())); final int nextHopLength = buffer.readUnsignedByte(); if (nextHopLength > 0) { final byte[] nextHop = new byte[nextHopLength]; buffer.readBytes(nextHop); attr.setNextHopAddress(nextHop); } buffer.readByte(); // reserved while (buffer.isReadable()) { attr.getNlris().add(NLRICodec.decodeNLRI(buffer)); } } catch (final RuntimeException e) { throw new OptionalAttributeErrorException(); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private MultiProtocolUnreachableNLRI decodeMpUnreachNlriPathAttribute(final ByteBuf buffer) { final MultiProtocolUnreachableNLRI attr = new MultiProtocolUnreachableNLRI(); try {// w w w .ja va 2 s .com attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort())); attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte())); while (buffer.isReadable()) { attr.getNlris().add(NLRICodec.decodeNLRI(buffer)); } } catch (final RuntimeException e) { throw new OptionalAttributeErrorException(); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private ClusterListPathAttribute decodeClusterListPathAttribute(final ByteBuf buffer) { final ClusterListPathAttribute attr = new ClusterListPathAttribute(); try {//from www. j a v a 2s .co m while (buffer.isReadable()) { attr.getClusterIds().add((int) buffer.readUnsignedInt()); } } catch (final RuntimeException e) { throw new OptionalAttributeErrorException(); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private List<PathAttribute> decodePathAttributes(final ByteBuf buffer) { final List<PathAttribute> attributes = new LinkedList<PathAttribute>(); while (buffer.isReadable()) { buffer.markReaderIndex();/* w w w . j a va 2 s . co m*/ try { final int flagsType = buffer.readUnsignedShort(); final boolean optional = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_OPTIONAL_BIT) != 0); final boolean transitive = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TRANSITIVE_BIT) != 0); final boolean partial = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_PARTIAL_BIT) != 0); final int typeCode = (flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MASK); int valueLength = 0; if ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_EXTENDED_LENGTH_BIT) != 0) { valueLength = buffer.readUnsignedShort(); } else { valueLength = buffer.readUnsignedByte(); } // final ByteBuf valueBuffer = Unpooled.buffer(valueLength); // buffer.readBytes(valueBuffer); final ByteBuf valueBuffer = buffer.readBytes(valueLength); PathAttribute attr = null; switch (typeCode) { case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AGGREGATOR: attr = this.decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_AGGREGATOR: attr = this.decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_PATH: attr = this.decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS_PATH: attr = this.decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ATOMIC_AGGREGATE: attr = this.decodeAtomicAggregatePathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_COMMUNITIES: attr = this.decodeCommunityPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_LOCAL_PREF: attr = this.decodeLocalPrefPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MULTI_EXIT_DISC: attr = this.decodeMultiExitDiscPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_NEXT_HOP: attr = this.decodeNextHopPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGIN: attr = this.decodeOriginPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_REACH_NLRI: attr = this.decodeMpReachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_UNREACH_NLRI: attr = this.decodeMpUnreachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGINATOR_ID: attr = this.decodeOriginatorIDPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_CLUSTER_LIST: attr = this.decodeClusterListPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_EXTENDED_COMMUNITIES: attr = this.decodeExtendedCommunityPathAttribute(valueBuffer); break; default: { final byte[] value = new byte[valueBuffer.readableBytes()]; valueBuffer.readBytes(value); attr = new UnknownPathAttribute(typeCode, value); } break; } attr.setOptional(optional); attr.setTransitive(transitive); attr.setPartial(partial); attributes.add(attr); } catch (final AttributeException ex) { final int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); final int attributeLength = endReadIndex - buffer.readerIndex(); final byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); ex.setOffendingAttribute(packet); throw ex; } catch (final IndexOutOfBoundsException ex) { // this is almost certinally an internal error with parsing .... final int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); final int attributeLength = endReadIndex - buffer.readerIndex(); final byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); throw new AttributeLengthException(ex, packet); } } return attributes; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private List<NetworkLayerReachabilityInformation> decodeWithdrawnRoutes(final ByteBuf buffer) { final List<NetworkLayerReachabilityInformation> routes = new LinkedList<NetworkLayerReachabilityInformation>(); while (buffer.isReadable()) { routes.add(NLRICodec.decodeNLRI(buffer)); }/*w ww . jav a2 s . c o m*/ return routes; }