Example usage for io.netty.buffer ByteBuf readUnsignedByte

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

Introduction

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

Prototype

public abstract short readUnsignedByte();

Source Link

Document

Gets an unsigned byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

public static Capability decodeCapability(ByteBuf buffer) {
    Capability cap = null;//from   w  ww. ja  v a2s  . c o  m

    try {
        buffer.markReaderIndex();

        int type = buffer.readUnsignedByte();

        switch (type) {
        case BGPv4Constants.BGP_CAPABILITY_TYPE_MULTIPROTOCOL:
            cap = decodeMultiProtocolCapability(buffer);
            break;
        case BGPv4Constants.BGP_CAPABILITY_TYPE_ROUTE_REFRESH:
            cap = decodeRouteRefreshCapability(buffer);
            break;
        case BGPv4Constants.BGP_CAPABILITY_TYPE_AS4_NUMBERS:
            cap = decodeAutonomousSystem4Capability(buffer);
            break;
        case BGPv4Constants.BGP_CAPABILITY_TYPE_OUTBOUND_ROUTE_FILTERING:
            cap = decodeOutboundRouteFilteringCapability(buffer);
            break;
        default:
            cap = decodeUnknownCapability(type, buffer);
            break;
        }
    } catch (CapabilityException e) {
        buffer.resetReaderIndex();

        int type = buffer.readUnsignedByte();
        int capLength = buffer.readUnsignedByte();

        byte[] capPacket = new byte[capLength + 2];

        buffer.readBytes(capPacket, 2, capLength);
        capPacket[0] = (byte) type;
        capPacket[1] = (byte) capLength;

        e.setCapability(capPacket);
        throw e;
    }

    return cap;
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static Capability decodeUnknownCapability(int type, ByteBuf buffer) {
    UnknownCapability cap = new UnknownCapability();

    cap.setCapabilityType(type);//from   w  w  w  .  j  ava2 s.c  om
    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength > 0) {
        byte[] value = new byte[parameterLength];

        buffer.readBytes(value);
        cap.setValue(value);
    }

    return cap;
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static Capability decodeOutboundRouteFilteringCapability(ByteBuf buffer) {
    OutboundRouteFilteringCapability cap = new OutboundRouteFilteringCapability();

    assertMinimalLength(buffer, 5); // 2 octest AFI + 1 octet reserved + 1 octet SAFI + 1 octet number of (ORF type, Send/Receive) tuples

    cap.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort()));
    buffer.readByte();//from   w  ww  . j  a v  a2 s.  c om
    cap.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()));

    int orfs = buffer.readUnsignedByte();

    if (buffer.readableBytes() != 2 * orfs)
        throw new UnspecificOpenPacketException(
                "Expected " + (2 * orfs) + " octets parameter, got " + buffer.readableBytes() + " octets");

    try {
        cap.getFilters().put(ORFType.fromCode(buffer.readUnsignedByte()),
                ORFSendReceive.fromCode(buffer.readUnsignedByte()));
    } catch (IllegalArgumentException e) {
        throw new UnspecificOpenPacketException(e);
    }
    return cap;
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static final void assertEmptyParameter(ByteBuf buffer) {
    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength != 0)
        throw new UnspecificOpenPacketException(
                "Expected zero-length parameter, got " + parameterLength + " octets");
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static final void assertFixedLength(ByteBuf buffer, int length) {
    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength != length)
        throw new UnspecificOpenPacketException(
                "Expected " + length + " octets parameter, got " + parameterLength + " octets");
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static final void assertMinimalLength(ByteBuf buffer, int length) {
    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength < length)
        throw new UnspecificOpenPacketException(
                "Expected " + length + " octets parameter, got " + parameterLength + " octets");
}

From source file:org.bgp4j.netty.protocol.open.OpenPacketDecoder.java

License:Apache License

/**
 * decode the OPEN network packet. The passed channel buffer MUST point to the first packet octet AFTER the packet type and the buffer 
 * must be at least 9 octets large at this point.
 * //from   www .jav a  2 s.  c om
 * @param buffer the buffer containing the data. 
 * @return
 */
public OpenPacket decodeOpenPacket(ByteBuf buffer) {
    OpenPacket packet = new OpenPacket();

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

    packet.setProtocolVersion(buffer.readUnsignedByte());
    if (packet.getProtocolVersion() != BGPv4Constants.BGP_VERSION)
        throw new UnsupportedVersionNumberException(BGPv4Constants.BGP_VERSION);
    packet.setAutonomousSystem(buffer.readUnsignedShort());
    packet.setHoldTime(buffer.readUnsignedShort());
    packet.setBgpIdentifier(buffer.readUnsignedInt());
    if ((packet.getBgpIdentifier() & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_MASK)
        throw new BadBgpIdentifierException();

    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength > 0) {
        while (buffer.isReadable()) {
            int parameterType = buffer.readUnsignedByte();
            int paramLength = buffer.readUnsignedByte();

            ByteBuf valueBuffer = buffer.readBytes(paramLength);

            switch (parameterType) {
            case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_AUTH:
                break;
            case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_CAPABILITY:
                packet.getCapabilities().addAll(CapabilityCodec.decodeCapabilities(valueBuffer));
                break;
            default:
                throw new UnsupportedOptionalParameterException();
            }
        }
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.refresh.RouteRefreshPacketDecoder.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  a2s.  c om*/
 * @param buffer the buffer containing the data. 
 * @return the decoded packet or null on decoding problems. Neither RFC2918 nor RFC5291 nor RFC4271 describe an error
 * handling procedure, so best advise is to ignore invalid packets for now.
 */
public BGPv4Packet decodeRouteRefreshPacket(ByteBuf buffer) {
    RouteRefreshPacket packet = null;

    try {
        AddressFamily af = AddressFamily.fromCode(buffer.readUnsignedShort());

        buffer.readByte(); // swallow reserved octet

        SubsequentAddressFamily saf = SubsequentAddressFamily.fromCode(buffer.readUnsignedByte());

        packet = new RouteRefreshPacket(af, saf);

        if (buffer.isReadable()) {
            // we have outbound router filter rules here
            OutboundRouteFilter orf = new OutboundRouteFilter(af, saf);

            orf.setRefreshType(ORFRefreshType.fromCode(buffer.readUnsignedByte()));

            while (buffer.isReadable()) {
                ORFType orfType = ORFType.fromCode(buffer.readUnsignedByte());
                ByteBuf entriesBuffer = buffer.readSlice(buffer.readUnsignedShort());

                buffer.readBytes(entriesBuffer);
                orf.addAllORFEntries(decodeORFEntries(entriesBuffer, orfType));
            }

            packet.setOutboundRouteFilter(orf);
        }
    } catch (Exception e) {
        log.error("cannot decode ROUTE_REFRESH packet, suppressing it from further processing", e);

        packet = null;
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.refresh.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  w  w.ja v  a  2s .  c  om*/
            throw new IllegalArgumentException("cannot decode OutboudRouteFilter entries of type " + orfType);
        }

    }

    return entries;
}

From source file:org.bgp4j.netty.protocol.refresh.RouteRefreshPacketDecoder.java

License:Apache License

private ORFEntry decodeAddressPrefixBasedORFEntry(ByteBuf buffer, ORFAction action, ORFMatch match) {
    AddressPrefixBasedORFEntry entry = new AddressPrefixBasedORFEntry(action, match);

    if (action != ORFAction.REMOVE_ALL) {
        entry.setSequence((int) buffer.readUnsignedInt());
        entry.setMinLength(buffer.readUnsignedByte());
        entry.setMaxLength(buffer.readUnsignedByte());
        entry.setPrefix(NLRICodec.decodeNLRI(buffer));
    }//from ww  w.j  a v  a 2 s  .  c  o m

    return entry;
}