Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientAcknowledgeHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case Acknowledge:
            onAcknowledge(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default://from   w ww  .j av a 2  s .c o m
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientAsymmetricHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case OpenSecureChannel:
            onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default://from   ww w .  j ava  2s. c  om
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientMessageHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (buffer.readableBytes() >= HEADER_LENGTH) {
        buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

        if (buffer.readableBytes() >= getMessageLength(buffer)) {
            decodeMessage(ctx, buffer);//from  w  ww  .  j  av a2s . c o  m
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientSymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case SecureMessage:
            onSecureMessage(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default://  www  .  j  av  a 2 s. c  om
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java

License:Apache License

private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers)
        throws UaException {
    CompositeByteBuf composite = BufferUtil.compositeBuffer();

    int signatureSize = delegate.getSignatureSize(channel);
    int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel);

    boolean encrypted = delegate.isEncryptionEnabled(channel);
    boolean signed = delegate.isSigningEnabled(channel);

    for (ByteBuf chunkBuffer : chunkBuffers) {
        char chunkType = (char) chunkBuffer.getByte(3);

        chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE);

        delegate.readSecurityHeader(channel, chunkBuffer);

        if (encrypted) {
            decryptChunk(delegate, channel, chunkBuffer);
        }//from www. j  av  a  2 s  . co  m

        int encryptedStart = chunkBuffer.readerIndex();
        chunkBuffer.readerIndex(0);

        if (signed) {
            delegate.verifyChunk(channel, chunkBuffer);
        }

        int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0;
        int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize;

        chunkBuffer.readerIndex(encryptedStart);

        SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer);
        long sequenceNumber = sequenceHeader.getSequenceNumber();
        lastRequestId = sequenceHeader.getRequestId();

        if (lastSequenceNumber == -1) {
            lastSequenceNumber = sequenceNumber;
        } else {
            if (lastSequenceNumber + 1 != sequenceNumber) {
                String message = String.format("expected sequence number %s but received %s",
                        lastSequenceNumber + 1, sequenceNumber);

                logger.error(message);
                logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex()));

                throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message);
            }

            lastSequenceNumber = sequenceNumber;
        }

        ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex());

        if (chunkType == 'A') {
            ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer);

            throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason());
        }

        composite.addComponent(bodyBuffer);
        composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes());
    }

    return composite.order(ByteOrder.LITTLE_ENDIAN);
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java

License:Apache License

private void decryptChunk(Delegate delegate, SecureChannel channel, ByteBuf chunkBuffer) throws UaException {
    int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel);
    int blockCount = chunkBuffer.readableBytes() / cipherTextBlockSize;

    int plainTextBufferSize = cipherTextBlockSize * blockCount;

    ByteBuf plainTextBuffer = BufferUtil.buffer(plainTextBufferSize);

    ByteBuffer plainTextNioBuffer = plainTextBuffer.writerIndex(plainTextBufferSize).nioBuffer();

    ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer();

    try {/*from   ww  w.  ja  v  a2s  .  c o  m*/
        Cipher cipher = delegate.getCipher(channel);

        assert (chunkBuffer.readableBytes() % cipherTextBlockSize == 0);

        if (delegate instanceof AsymmetricDelegate) {
            for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) {
                chunkNioBuffer.limit(chunkNioBuffer.position() + cipherTextBlockSize);

                cipher.doFinal(chunkNioBuffer, plainTextNioBuffer);
            }
        } else {
            cipher.doFinal(chunkNioBuffer, plainTextNioBuffer);
        }
    } catch (GeneralSecurityException e) {
        throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e);
    }

    /* Write plainTextBuffer back into the chunk buffer we decrypted from. */
    plainTextNioBuffer.flip(); // limit = pos, pos = 0

    chunkBuffer.writerIndex(chunkBuffer.readerIndex());
    chunkBuffer.writeBytes(plainTextNioBuffer);

    plainTextBuffer.release();
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java

License:Apache License

private int getPaddingSize(int cipherTextBlockSize, int signatureSize, ByteBuf buffer) {
    int lastPaddingByteOffset = buffer.readableBytes() - signatureSize - 1;

    return cipherTextBlockSize <= 256 ? buffer.getUnsignedByte(lastPaddingByteOffset) + 1
            : buffer.getUnsignedShort(lastPaddingByteOffset - 1) + 2;
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkEncoder.java

License:Apache License

private List<ByteBuf> encode(Delegate delegate, SecureChannel channel, MessageType messageType,
        ByteBuf messageBuffer, long requestId) throws UaException {

    List<ByteBuf> chunks = new ArrayList<>();

    boolean encrypted = delegate.isEncryptionEnabled(channel);

    int securityHeaderSize = delegate.getSecurityHeaderSize(channel);
    int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel);
    int plainTextBlockSize = delegate.getPlainTextBlockSize(channel);
    int signatureSize = delegate.getSignatureSize(channel);

    int maxChunkSize = parameters.getLocalSendBufferSize();
    int headerSizes = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize;
    int paddingOverhead = encrypted ? (cipherTextBlockSize > 256 ? 2 : 1) : 0;

    int maxBlockCount = (maxChunkSize - headerSizes - signatureSize - paddingOverhead) / cipherTextBlockSize;
    int maxBodySize = (plainTextBlockSize * maxBlockCount - SequenceHeader.SEQUENCE_HEADER_SIZE);

    while (messageBuffer.readableBytes() > 0) {
        int bodySize = Math.min(messageBuffer.readableBytes(), maxBodySize);

        int paddingSize = encrypted
                ? plainTextBlockSize//from w ww .  ja v  a2 s . com
                        - (SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingOverhead)
                                % plainTextBlockSize
                : 0;

        int plainTextContentSize = SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingSize
                + paddingOverhead;

        assert (plainTextContentSize % plainTextBlockSize == 0);

        int chunkSize = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize
                + (plainTextContentSize / plainTextBlockSize) * cipherTextBlockSize;

        ByteBuf chunkBuffer = BufferUtil.buffer(chunkSize);

        /* Message Header */
        SecureMessageHeader messageHeader = new SecureMessageHeader(messageType,
                messageBuffer.readableBytes() > bodySize ? 'C' : 'F', chunkSize, channel.getChannelId());

        SecureMessageHeader.encode(messageHeader, chunkBuffer);

        /* Security Header */
        delegate.encodeSecurityHeader(channel, chunkBuffer);

        /* Sequence Header */
        SequenceHeader sequenceHeader = new SequenceHeader(sequenceNumber.getAndIncrement(), requestId);

        SequenceHeader.encode(sequenceHeader, chunkBuffer);

        /* Message Body */
        chunkBuffer.writeBytes(messageBuffer, bodySize);

        /* Padding and Signature */
        if (encrypted) {
            writePadding(cipherTextBlockSize, paddingSize, chunkBuffer);
        }

        if (delegate.isSigningEnabled(channel)) {
            ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(0, chunkBuffer.writerIndex());

            byte[] signature = delegate.signChunk(channel, chunkNioBuffer);

            chunkBuffer.writeBytes(signature);
        }

        /* Encryption */
        if (encrypted) {
            chunkBuffer.readerIndex(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize);

            assert (chunkBuffer.readableBytes() % plainTextBlockSize == 0);

            try {
                int blockCount = chunkBuffer.readableBytes() / plainTextBlockSize;

                ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(chunkBuffer.readerIndex(),
                        blockCount * cipherTextBlockSize);
                ByteBuf copyBuffer = chunkBuffer.copy();
                ByteBuffer plainTextNioBuffer = copyBuffer.nioBuffer();

                Cipher cipher = delegate.getAndInitializeCipher(channel);

                if (delegate instanceof AsymmetricDelegate) {
                    for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) {
                        int position = blockNumber * plainTextBlockSize;
                        int limit = (blockNumber + 1) * plainTextBlockSize;
                        plainTextNioBuffer.position(position).limit(limit);

                        int bytesWritten = cipher.doFinal(plainTextNioBuffer, chunkNioBuffer);

                        assert (bytesWritten == cipherTextBlockSize);
                    }
                } else {
                    cipher.doFinal(plainTextNioBuffer, chunkNioBuffer);
                }

                copyBuffer.release();
            } catch (GeneralSecurityException e) {
                throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e);
            }
        }

        chunkBuffer.readerIndex(0).writerIndex(chunkSize);

        chunks.add(chunkBuffer);
    }

    lastRequestId = requestId;

    return chunks;
}

From source file:com.digitalpetri.opcua.stack.core.serialization.OpcUaDataTypeEncoding.java

License:Apache License

@Override
public ByteString encodeToByteString(Object object, NodeId encodingTypeId) {
    EncoderDelegate<Object> delegate = DelegateRegistry.getEncoder(encodingTypeId);

    ByteBuf buffer = allocator.buffer().order(ByteOrder.LITTLE_ENDIAN);

    BinaryEncoder encoder = new BinaryEncoder();
    encoder.setBuffer(buffer);//w  w w  .j a va  2 s . c om

    delegate.encode(object, encoder);

    byte[] bs = new byte[buffer.readableBytes()];
    buffer.readBytes(bs);
    buffer.release();

    return ByteString.of(bs);
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerAsymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case OpenSecureChannel:
            onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
            break;

        case CloseSecureChannel:
            logger.debug("Received CloseSecureChannelRequest");
            if (secureChannel != null) {
                server.closeSecureChannel(secureChannel);
            }/*from w  w w. jav a2s .c om*/
            buffer.skipBytes(messageLength);
            break;

        default:
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}