Example usage for io.netty.buffer ByteBuf isReadable

List of usage examples for io.netty.buffer ByteBuf isReadable

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isReadable.

Prototype

public abstract boolean isReadable();

Source Link

Document

Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0 .

Usage

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

/**
 * decode the UPDATE network packet. The passed channel buffer MUST point to the first packet octet AFTER the type octet.
 * //  w  ww . j  a v a2 s .  c om
 * @param buffer the buffer containing the data. 
 * @return
 */
public BGPv4Packet decodeUpdatePacket(ByteBuf buffer) {
    UpdatePacket packet = new UpdatePacket();

    ProtocolPacketUtils.verifyPacketSize(buffer, BGPv4Constants.BGP_PACKET_MIN_SIZE_UPDATE, -1);

    if (buffer.readableBytes() < 2)
        throw new MalformedAttributeListException();

    // handle withdrawn routes
    int withdrawnOctets = buffer.readUnsignedShort();

    // sanity checking
    if (withdrawnOctets > buffer.readableBytes())
        throw new MalformedAttributeListException();

    ByteBuf withdrawnBuffer = null;

    if (withdrawnOctets > 0) {
        withdrawnBuffer = buffer.readSlice(withdrawnOctets);
    }

    // sanity checking
    if (buffer.readableBytes() < 2)
        throw new MalformedAttributeListException();

    // handle path attributes
    int pathAttributeOctets = buffer.readUnsignedShort();

    // sanity checking
    if (pathAttributeOctets > buffer.readableBytes())
        throw new MalformedAttributeListException();

    ByteBuf pathAttributesBuffer = null;

    if (pathAttributeOctets > 0) {
        pathAttributesBuffer = buffer.readSlice(pathAttributeOctets);
    }

    if (withdrawnBuffer != null) {
        try {
            packet.getWithdrawnRoutes().addAll(decodeWithdrawnRoutes(withdrawnBuffer));
        } catch (IndexOutOfBoundsException e) {
            throw new MalformedAttributeListException();
        }
    }

    if (pathAttributesBuffer != null) {
        try {
            packet.getPathAttributes().addAll(decodePathAttributes(pathAttributesBuffer));
        } catch (IndexOutOfBoundsException ex) {
            throw new MalformedAttributeListException();
        }
    }

    // handle network layer reachability information
    if (buffer.readableBytes() > 0) {
        try {
            while (buffer.isReadable()) {
                packet.getNlris().add(NLRICodec.decodeNLRI(buffer));
            }
        } catch (IndexOutOfBoundsException e) {
            throw new InvalidNetworkFieldException();
        } catch (IllegalArgumentException e) {
            throw new InvalidNetworkFieldException();
        }
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.update.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.
 * /*from  w  ww .jav a2 s  .  c  om*/
 * @param buffer the buffer containing the data. 
 * @return
 */
public NotificationPacket decodeUpdateNotification(ByteBuf buffer, 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:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private ASPathAttribute decodeASPathAttribute(ByteBuf buffer, ASType asType) {
    ASPathAttribute attr = new ASPathAttribute(asType);

    while (buffer.isReadable()) {
        if (buffer.readableBytes() < 2)
            throw new MalformedASPathAttributeException();

        int segmentType = buffer.readUnsignedByte();
        int pathLength = buffer.readUnsignedByte();
        int pathOctetLength = (pathLength * (asType == ASType.AS_NUMBER_4OCTETS ? 4 : 2));

        if (buffer.readableBytes() < pathOctetLength)
            throw new MalformedASPathAttributeException();

        PathSegment segment = new PathSegment(asType);

        try {/*from www  .java  2s  .c  o m*/
            segment.setPathSegmentType(PathSegmentTypeCodec.fromCode(segmentType));
        } catch (IllegalArgumentException e) {
            log.error("cannot convert AS_PATH type", 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:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private CommunityPathAttribute decodeCommunityPathAttribute(ByteBuf buffer) {
    CommunityPathAttribute attr = new CommunityPathAttribute();

    if (buffer.readableBytes() < 4 || (buffer.readableBytes() % 4 != 0))
        throw new OptionalAttributeErrorException();

    attr.setCommunity((int) buffer.readUnsignedInt());
    while (buffer.isReadable()) {
        CommunityMember member = new CommunityMember();

        member.setAsNumber(buffer.readUnsignedShort());
        member.setMemberFlags(buffer.readUnsignedShort());

        attr.getMembers().add(member);/*from   www .  jav a  2s  . c  om*/
    }

    return attr;
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private MultiProtocolReachableNLRI decodeMpReachNlriPathAttribute(ByteBuf buffer) {
    MultiProtocolReachableNLRI attr = new MultiProtocolReachableNLRI();

    try {/*from   ww w. j av a2s . co  m*/
        attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort()));
        attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()));

        int nextHopLength = buffer.readUnsignedByte();

        if (nextHopLength > 0) {
            byte[] nextHop = new byte[nextHopLength];

            buffer.readBytes(nextHop);
            attr.setNextHopAddress(nextHop);
        }

        buffer.readByte(); // reserved

        while (buffer.isReadable()) {
            attr.getNlris().add(NLRICodec.decodeNLRI(buffer));
        }
    } catch (RuntimeException e) {
        log.error("failed to decode MP_REACH_NLRI path attribute", e);

        throw new OptionalAttributeErrorException();
    }

    return attr;
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private MultiProtocolUnreachableNLRI decodeMpUnreachNlriPathAttribute(ByteBuf buffer) {
    MultiProtocolUnreachableNLRI attr = new MultiProtocolUnreachableNLRI();

    try {//from  ww  w. j a v a 2s .c om
        attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort()));
        attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()));

        while (buffer.isReadable()) {
            attr.getNlris().add(NLRICodec.decodeNLRI(buffer));
        }
    } catch (RuntimeException e) {
        log.error("failed to decode MP_UNREACH_NLRI path attribute", e);

        throw new OptionalAttributeErrorException();
    }

    return attr;
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private ClusterListPathAttribute decodeClusterListPathAttribute(ByteBuf buffer) {
    ClusterListPathAttribute attr = new ClusterListPathAttribute();

    try {/*from  w w  w  . j a  v  a 2  s  .c  o m*/
        while (buffer.isReadable()) {
            attr.getClusterIds().add((int) buffer.readUnsignedInt());
        }
    } catch (RuntimeException e) {
        log.error("failed to decode ORIGINATOR_ID attribute", e);

        throw new OptionalAttributeErrorException();
    }
    return attr;
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private List<PathAttribute> decodePathAttributes(ByteBuf buffer) {
    List<PathAttribute> attributes = new LinkedList<PathAttribute>();

    while (buffer.isReadable()) {
        buffer.markReaderIndex();/*from   w  w w . j  a v  a2  s  .  c  o  m*/

        try {
            int flagsType = buffer.readUnsignedShort();
            boolean optional = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_OPTIONAL_BIT) != 0);
            boolean transitive = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TRANSITIVE_BIT) != 0);
            boolean partial = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_PARTIAL_BIT) != 0);
            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();

            ByteBuf valueBuffer = buffer.readSlice(valueLength);

            PathAttribute attr = null;

            switch (typeCode) {
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AGGREGATOR:
                attr = decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_AGGREGATOR:
                attr = decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_PATH:
                attr = decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS_PATH:
                attr = decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ATOMIC_AGGREGATE:
                attr = decodeAtomicAggregatePathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_COMMUNITIES:
                attr = decodeCommunityPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_LOCAL_PREF:
                attr = decodeLocalPrefPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MULTI_EXIT_DISC:
                attr = decodeMultiExitDiscPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_NEXT_HOP:
                attr = decodeNextHopPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGIN:
                attr = decodeOriginPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_REACH_NLRI:
                attr = decodeMpReachNlriPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_UNREACH_NLRI:
                attr = decodeMpUnreachNlriPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGINATOR_ID:
                attr = decodeOriginatorIDPathAttribute(valueBuffer);
                break;
            case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_CLUSTER_LIST:
                attr = decodeClusterListPathAttribute(valueBuffer);
                break;
            default: {
                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 (AttributeException ex) {
            int endReadIndex = buffer.readerIndex();

            buffer.resetReaderIndex();

            int attributeLength = endReadIndex - buffer.readerIndex();
            byte[] packet = new byte[attributeLength];

            buffer.readBytes(packet);
            ex.setOffendingAttribute(packet);

            throw ex;
        } catch (IndexOutOfBoundsException ex) {
            int endReadIndex = buffer.readerIndex();

            buffer.resetReaderIndex();

            int attributeLength = endReadIndex - buffer.readerIndex();
            byte[] packet = new byte[attributeLength];

            buffer.readBytes(packet);

            throw new AttributeLengthException(packet);
        }

    }

    return attributes;
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java

License:Apache License

private List<NetworkLayerReachabilityInformation> decodeWithdrawnRoutes(ByteBuf buffer) {
    List<NetworkLayerReachabilityInformation> routes = new LinkedList<NetworkLayerReachabilityInformation>();

    while (buffer.isReadable()) {
        routes.add(NLRICodec.decodeNLRI(buffer));
    }/*from   w w w .  j ava 2 s .c  om*/
    return routes;
}

From source file:org.bridje.http.impl.HttpBridletRequestImpl.java

License:Apache License

protected void setContent(ByteBuf content) {
    if (content.isReadable()) {
        if (this.buffer != null) {
            this.buffer.release();
        }/* ww  w .  j ava 2  s  . co  m*/
        this.buffer = content;
        this.buffer.retain();
    }
}