Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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 {/*from w ww  .  j ava2s  . c o m*/
        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 List<PathAttribute> decodePathAttributes(final ByteBuf buffer) {

    final List<PathAttribute> attributes = new LinkedList<PathAttribute>();

    while (buffer.isReadable()) {

        buffer.markReaderIndex();//from  w  ww.j a v a2s .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.protocol.open.CapabilityCodec.java

License:Apache License

private static Capability decodeOutboundRouteFilteringCapability(final ByteBuf buffer) {
    final 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   www  .  j a v a  2s . c  o m*/
    cap.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()));

    final 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 (final IllegalArgumentException e) {
        throw new UnspecificOpenPacketException(e);
    }
    return cap;
}

From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java

License:Apache License

private void decodeDataRow(ByteBuf in) {
    QueryCommandBase<?> cmd = (QueryCommandBase<?>) inflight.peek();
    int len = in.readUnsignedShort();
    cmd.decoder.decodeRow(len, in);/*from   w w  w .jav  a2s . co m*/
}

From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java

License:Apache License

private void decodeRowDescription(ByteBuf in) {
    ColumnDesc[] columns = new ColumnDesc[in.readUnsignedShort()];
    for (int c = 0; c < columns.length; ++c) {
        String fieldName = Util.readCStringUTF8(in);
        int tableOID = in.readInt();
        short columnAttributeNumber = in.readShort();
        int typeOID = in.readInt();
        short typeSize = in.readShort();
        int typeModifier = in.readInt();
        int textOrBinary = in.readUnsignedShort(); // Useless for now
        ColumnDesc column = new ColumnDesc(fieldName, tableOID, columnAttributeNumber,
                DataType.valueOf(typeOID), typeSize, typeModifier, DataFormat.valueOf(textOrBinary));
        columns[c] = column;/*from   w w w  .  j  a  va 2  s .c  o m*/
    }
    RowDescription rowDesc = new RowDescription(columns);
    inflight.peek().handleRowDescription(rowDesc);
}

From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java

License:Apache License

private void decodeParameterDescription(ByteBuf in) {
    DataType[] paramDataTypes = new DataType[in.readUnsignedShort()];
    for (int c = 0; c < paramDataTypes.length; ++c) {
        paramDataTypes[c] = DataType.valueOf(in.readInt());
    }//ww  w  .j ava 2  s  .  co m
    inflight.peek().handleParameterDescription(new ParameterDescription(paramDataTypes));
}

From source file:me.bigteddy98.movingmotd.ClientSideConnection.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf clonedBuf = Unpooled.copiedBuffer((ByteBuf) msg);
    ByteBuf originalBuf = (ByteBuf) msg;

    int packetSize = PacketUtils.readVarInt(originalBuf);
    if (originalBuf.readableBytes() < packetSize) {
        System.out.println("Packet was smaller than the lenght.");
    }/*  w  w w.j a  va 2s  .  co m*/
    int id = PacketUtils.readVarInt(originalBuf);
    if (this.stage == Stage.HANDSHAKE) {
        if (id != 0x00) {
            System.out.println("Handshake ID was not equal to 0x00.");
        }
        // followed by varint, string, unsigned short and another varint
        PacketUtils.readVarInt(originalBuf);
        PacketUtils.readString(originalBuf);
        originalBuf.readUnsignedShort();
        int nextStage = PacketUtils.readVarInt(originalBuf);
        this.stage = Stage.fromId(nextStage);
    } else if (this.stage == Stage.LOGIN) {
        // just sent it
    } else if (this.stage == Stage.STATUS) {
        // TODO someone is pinging!

        originalBuf.release();
        return;
    }
    originalBuf.release();
    this.outgoingChannel.writeAndFlush(clonedBuf).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ctx.channel().read();
            } else {
                future.channel().close();
            }
        }
    });
}

From source file:me.melchor9000.net.resolver.DNSMessage.java

License:Open Source License

private int rs(ByteBuf buf) {
    if (buf.readableBytes() < 2)
        throw new DataNotRepresentsObject("Is an incomplete DNS message or isn't it", buf);
    return buf.readUnsignedShort();
}

From source file:me.melchor9000.net.resolver.DNSMX.java

License:Open Source License

@Override
public void fromByteBuf(@NotNull ByteBuf buffer) throws DataNotRepresentsObject {
    if (buffer.readableBytes() < 2)
        throw new DataNotRepresentsObject("DNS RR type MX doesn't contain data", buffer);
    preference = buffer.readUnsignedShort();
    exchange = readName(buffer);//from  www .  j a  v a 2s.co  m
}

From source file:me.melchor9000.net.resolver.DNSQuery.java

License:Open Source License

@Override
public void fromByteBuf(@NotNull ByteBuf buffer) throws DataNotRepresentsObject {
    if (buffer.readableBytes() < 4)
        throw new DataNotRepresentsObject("DNS Query is invalid", buffer);
    name = readName(buffer);/*from w  ww.  j  av  a 2  s .c  o m*/
    type = buffer.readUnsignedShort();
    mclass = buffer.readUnsignedShort();
}