Example usage for io.netty.buffer ByteBuf getUnsignedShort

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

Introduction

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

Prototype

public abstract int getUnsignedShort(int index);

Source Link

Document

Gets an unsigned 16-bit short integer at the specified absolute index in this buffer.

Usage

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

/**
 * Parses a collectd packet "part"./* w  w  w  . j a v  a 2s  .com*/
 *
 * @param startOffset beginning offset for this part
 * @param buf         buffered packet
 * @param fields      field map for the output record
 * @return offset after consuming part
 */
private int parsePart(int startOffset, ByteBuf buf, Map<String, Field> fields) throws OnRecordErrorException {
    int offset = startOffset;
    int type = buf.getUnsignedShort(offset); // 0-1
    offset += 2;
    final int length = buf.getUnsignedShort(offset); // 2-3
    offset += 2;

    switch (type) {
    case HOST:
    case PLUGIN:
    case PLUGIN_INSTANCE:
    case TYPE:
    case TYPE_INSTANCE:
    case MESSAGE:
        pruneFields(type);
        fields.put(PART_TYPES.get(type), Field.create(parseString(offset, length, buf)));
        offset += length - 4;
        break;
    case TIME_HIRES:
    case INTERVAL_HIRES:
        if (type != INTERVAL_HIRES || !excludeInterval) {
            long value = parseNumeric(offset, buf);
            if (convertTime) {
                value *= (Math.pow(2, -30) * 1000);
                type = type == TIME_HIRES ? TIME : INTERVAL;
            }
            fields.put(PART_TYPES.get(type), Field.create(value));
        }
        offset += 8;
        break;
    case TIME:
    case INTERVAL:
    case SEVERITY:
        if (type != INTERVAL || !excludeInterval) {
            fields.put(PART_TYPES.get(type), Field.create(parseNumeric(offset, buf)));
        }
        offset += 8;
        break;
    case VALUES:
        offset = parseValues(offset, buf);
        startNewRecord();
        break;
    case SIGNATURE:
        if (!verifySignature(offset, length, buf)) {
            throw new OnRecordErrorException(Errors.COLLECTD_02);
        }
        offset += length - 4;
        break;
    case ENCRYPTION:
        String user = parseUser(offset, buf);
        offset += (2 + user.length());
        byte[] iv = parseIv(offset, buf);
        offset += 16;
        decrypt(offset, length, buf, user, iv);
        // Skip the checksum and continue processing.
        offset += 20;
        break;
    default:
        // Don't recognize this part type, so skip it
        LOG.warn("Unrecognized part type: {}", type);
        offset += length - 4;
        break;
    }

    return offset;
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

/**
 * Parses the value part of the packet where metrics are located
 *
 * @param startOffset beginning offset for this part
 * @param buf         buffered packet/*w  w  w . ja v a2s.  com*/
 * @return offset after consuming part
 */
private int parseValues(int startOffset, ByteBuf buf) throws OnRecordErrorException {
    int offset = startOffset;
    // N Values
    // For each Value:
    // 1 byte data type code
    int numValues = buf.getUnsignedShort(offset); // 4-5
    offset += 2;

    List<Byte> types = new ArrayList<>(numValues);

    while (numValues-- > 0) {
        types.add(buf.getByte(offset));
        offset += 1;
    }

    for (int i = 0; i < types.size(); i++) {
        Byte type = types.get(i);
        String label = getValueLabel(i, type);
        switch (type) {
        case COUNTER:
            fields.put(label, Field.create(buf.getUnsignedInt(offset)));
            offset += 8;
            break;
        case GAUGE:
            fields.put(label, Field.create(buf.order(ByteOrder.LITTLE_ENDIAN).getDouble(offset)));
            offset += 8;
            break;
        case DERIVE:
            fields.put(label, Field.create(buf.getLong(offset)));
            offset += 8;
            break;
        case ABSOLUTE:
            fields.put(label, Field.create(buf.getUnsignedInt(offset)));
            offset += 8;
            break;
        default:
            // error
            throw new OnRecordErrorException(Errors.COLLECTD_01, type);
        }
    }
    return offset;
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private String parseUser(int offset, ByteBuf buf) {
    int userLength = buf.getUnsignedShort(offset);
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 2, userBytes, 0, userLength);
    return new String(userBytes);
}

From source file:com.streamsets.pipeline.lib.parser.netflow.NetflowParser.java

License:Apache License

@Override
public List<Record> parse(ByteBuf buf, InetSocketAddress recipient, InetSocketAddress sender)
        throws OnRecordErrorException {
    int packetLength = buf.readableBytes();
    if (packetLength < 4) {
        throw new OnRecordErrorException(Errors.NETFLOW_01,
                Utils.format("Packet must be at least 4 bytes, was: {}", packetLength));
    }// ww w.j av a  2 s .com
    String readerId = String.valueOf(recipient);
    int version = buf.getUnsignedShort(0); // 0-1
    switch (version) {
    case 5:
        return parseV5(version, packetLength, buf, readerId, sender);
    default:
        throw new OnRecordErrorException(Errors.NETFLOW_00, version);
    }
}

From source file:com.streamsets.pipeline.lib.parser.netflow.NetflowParser.java

License:Apache License

private List<Record> parseV5(int version, int packetLength, ByteBuf buf, String readerId,
        InetSocketAddress sender) throws OnRecordErrorException {
    int count = buf.getUnsignedShort(2); // 2-3
    if (count <= 0) {
        throw new OnRecordErrorException(Errors.NETFLOW_01, Utils.format("Count is invalid: {}", count));
    } else if (packetLength < V5_HEADER_SIZE + count * V5_FLOW_SIZE) {
        String msg = Utils.format("Readable bytes {} too small for count {} (max {})", packetLength, count,
                (V5_HEADER_SIZE + count * V5_FLOW_SIZE));
        throw new OnRecordErrorException(Errors.NETFLOW_01, msg);
    }/*from   w  ww.j ava2  s  . co m*/
    List<Record> result = new ArrayList<>(count);
    long uptime = buf.getUnsignedInt(4); // 4-7
    long seconds = buf.getUnsignedInt(8); // 8-11
    long millis = buf.getUnsignedInt(8) / 1000; // 12-15
    long timestamp = (seconds * 1000L) + millis; // java timestamp, which is milliseconds
    UUID packetId = UUIDs.startOfJavaTimestamp(timestamp);
    long flowSequence = buf.getUnsignedInt(16); // 16-19
    short engineType = buf.getUnsignedByte(20); // 20
    short engineId = buf.getUnsignedByte(21); // 21
    // the first 2 bits are the sampling mode, the remaining 14 the interval
    int sampling = buf.getUnsignedShort(22); // 22-23
    int samplingInterval = sampling & 0x3FFF;
    int samplingMode = sampling >> 14;
    Map<String, Field> headers = new HashMap<>();
    headers.put(VERSION, Field.create(version));
    headers.put(PACKETID, Field.create(packetId.toString()));
    headers.put(SENDER, Field.create((sender == null) ? "unknown" : sender.getAddress().toString()));
    headers.put(LENGTH, Field.create(packetLength));
    headers.put(UPTIME, Field.create(uptime));
    headers.put(TIMESTAMP, Field.create(timestamp));
    headers.put(FLOWSEQ, Field.create(flowSequence));
    headers.put(ENGINEID, Field.create(engineId));
    headers.put(ENGINETYPE, Field.create(engineType));
    headers.put(SAMPLINGINT, Field.create(samplingInterval));
    headers.put(SAMPLINGMODE, Field.create(samplingMode));
    headers.put(READERID, Field.create(readerId));
    for (int i = 0; i < count; i++) {
        ByteBuf flowBuf = buf.slice(V5_HEADER_SIZE + (i * V5_FLOW_SIZE), V5_FLOW_SIZE);
        Map<String, Field> fields = new HashMap<>();
        fields.putAll(headers);
        long pkts = flowBuf.getUnsignedInt(16);
        long bytes = flowBuf.getUnsignedInt(20);
        fields.put(ID, Field.create(UUIDs.timeBased().toString()));
        int srcaddr = (int) flowBuf.getUnsignedInt(0);
        int dstaddr = (int) flowBuf.getUnsignedInt(4);
        int nexthop = (int) flowBuf.getUnsignedInt(8);
        fields.put(SRCADDR, Field.create(srcaddr));
        fields.put(DSTADDR, Field.create(dstaddr));
        fields.put(NEXTHOP, Field.create(nexthop));
        fields.put(SRCADDR_S, Field.create(ipToString(srcaddr)));
        fields.put(DSTADDR_S, Field.create(ipToString(dstaddr)));
        fields.put(NEXTHOP_S, Field.create(ipToString(nexthop)));
        fields.put(SRCPORT, Field.create(flowBuf.getUnsignedShort(32)));
        fields.put(DSTPORT, Field.create(flowBuf.getUnsignedShort(34)));
        fields.put(SRCAS, Field.create(flowBuf.getUnsignedShort(40)));
        fields.put(DSTAS, Field.create(flowBuf.getUnsignedShort(42)));
        fields.put(PACKETS, Field.create(pkts));
        fields.put(DOCTECTS, Field.create(bytes));
        fields.put(PROTO, Field.create(flowBuf.getUnsignedByte(38)));
        fields.put(TOS, Field.create(flowBuf.getUnsignedByte(39)));
        fields.put(TCPFLAGS, Field.create(flowBuf.getUnsignedByte(37)));
        long first = flowBuf.getUnsignedInt(24);
        if (first > 0) {
            fields.put(FIRST, Field.create(timestamp - uptime - first));
        } else {
            fields.put(FIRST, Field.create(0L));
        }
        long last = flowBuf.getUnsignedInt(28);
        if (last > 0) {
            fields.put(LAST, Field.create(timestamp - uptime - last));
        } else {
            fields.put(LAST, Field.create(0L));
        }
        fields.put(SNMPINPUT, Field.create(flowBuf.getUnsignedShort(12)));
        fields.put(SNMPOUTPUT, Field.create(flowBuf.getUnsignedShort(14)));
        fields.put(SRCMASK, Field.create(flowBuf.getUnsignedByte(44)));
        fields.put(DSTMASK, Field.create(flowBuf.getUnsignedByte(45)));
        Record record = context.createRecord(readerId + "::" + recordId++);
        record.set(Field.create(fields));
        result.add(record);

    }
    return result;
}

From source file:com.streamsets.pipeline.lib.parser.udp.collectd.CollectdParser.java

License:Apache License

private String parseUser(int offset, ByteBuf buf) {
    int userLength = buf.getUnsignedShort(offset);
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 2, userBytes, 0, userLength);
    return new String(userBytes, StandardCharsets.UTF_8);
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
    int packetLength = 0;

    // SSLv3 or TLS - Check ContentType
    boolean tls;/*from   w w w . ja v  a 2s.  c om*/
    switch (buffer.getUnsignedByte(offset)) {
    case 20: // change_cipher_spec
    case 21: // alert
    case 22: // handshake
    case 23: // application_data
        tls = true;
        break;
    default:
        // SSLv2 or bad data
        tls = false;
    }

    if (tls) {
        // SSLv3 or TLS - Check ProtocolVersion
        int majorVersion = buffer.getUnsignedByte(offset + 1);
        if (majorVersion == 3) {
            // SSLv3 or TLS
            packetLength = buffer.getUnsignedShort(offset + 3) + 5;
            if (packetLength <= 5) {
                // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                tls = false;
            }
        } else {
            // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
            tls = false;
        }
    }

    if (!tls) {
        // SSLv2 or bad data - Check the version
        boolean sslv2 = true;
        int headerLength = (buffer.getUnsignedByte(offset) & 0x80) != 0 ? 2 : 3;
        int majorVersion = buffer.getUnsignedByte(offset + headerLength + 1);
        if (majorVersion == 2 || majorVersion == 3) {
            // SSLv2
            if (headerLength == 2) {
                packetLength = (buffer.getShort(offset) & 0x7FFF) + 2;
            } else {
                packetLength = (buffer.getShort(offset) & 0x3FFF) + 3;
            }
            if (packetLength <= headerLength) {
                sslv2 = false;
            }
        } else {
            sslv2 = false;
        }

        if (!sslv2) {
            return -1;
        }
    }
    return packetLength;
}

From source file:io.airlift.drift.transport.netty.server.OptionalSslHandler.java

License:Apache License

private static boolean isTls(ByteBuf buffer, int offset) {
    // SSLv3 or TLS - Check ContentType
    int contentType = buffer.getUnsignedByte(offset);
    if (contentType != SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC && contentType != SSL_CONTENT_TYPE_ALERT
            && contentType != SSL_CONTENT_TYPE_HANDSHAKE && contentType != SSL_CONTENT_TYPE_APPLICATION_DATA) {
        return false;
    }/*w ww  .  j av a  2  s .  c  o  m*/

    // SSLv3 or TLS - Check ProtocolVersion
    int majorVersion = buffer.getUnsignedByte(offset + 1);
    if (majorVersion != 3) {
        return false;
    }

    // SSLv3 or TLS  - Check packet length is positive
    if (buffer.getUnsignedShort(offset + 3) <= 0) {
        return false;
    }

    return true;
}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterResponseDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 6) {
        return;/*ww  w . j  a  v  a2s. c  o m*/
    }
    int adupLength = in.getUnsignedShort(in.readerIndex() + 4);
    int wholeLength = adupLength + 6 + (withCheckSum ? 2 : 0);
    if (wholeLength > in.readableBytes()) {
        return;
    }
    if (withCheckSum) {
        checkSum(in, wholeLength);
    }
    int transectionId = in.readUnsignedShort();
    ModbusRequest request = transectingRequestRetriever.apply(transectionId);
    if (null == request) {
        in.readerIndex(in.readerIndex() + wholeLength - 2);
        out.add(new MissMatchResponse(transectionId));
        return;
    }
    in.readerIndex(in.readerIndex() + 4);
    int unitId = in.readUnsignedByte();
    if (unitId != request.getUnitId()) {
        throw new DecoderException();
    }
    int functionCode = in.readUnsignedByte();
    if ((functionCode & 0x7F) != request.getFunction().getCode()) {
        throw new DecoderException();
    }

    ModbusResponse response;

    if ((functionCode & 0x80) == 0) {
        switch (functionCode) {
        case 0x01: {
            ReadBooleanRegistersResponse bResponse = new ReadBooleanRegistersResponse();
            bResponse.setRegisterType(ModbusRegisterType.COIL);
            response = bResponse;
        }
            break;
        case 0x02: {
            ReadBooleanRegistersResponse bResponse = new ReadBooleanRegistersResponse();
            bResponse.setRegisterType(ModbusRegisterType.DISCRETE_INPUT);
            response = bResponse;
        }
            break;
        case 0x03: {
            ReadWordRegistersResponse rResponse = new ReadWordRegistersResponse();
            rResponse.setRegisterType(ModbusRegisterType.HOLDING);
            response = rResponse;
            break;
        }
        case 0x04: {
            ReadWordRegistersResponse rResponse = new ReadWordRegistersResponse();
            rResponse.setRegisterType(ModbusRegisterType.INPUT);
            response = rResponse;
        }
            break;
        case 0x05:
            response = new WriteCoilResponse();
            break;
        case 0x06:
            response = new WriteHoldingResponse();
            break;
        default:
            throw new UnsupportedFunctionCodeException();
        }
        request.getFunction().decodeResponseData(in, response);
    } else {
        ExceptionResponse exResponse = new ExceptionResponse();
        exResponse.setFunctionCode(functionCode);
        exResponse.setExceptionCode(in.readUnsignedByte());
        response = exResponse;
    }
    response.setTransectionId(transectionId);
    response.setUnitId(unitId);

    out.add(response);

    if (withCheckSum) {
        in.readerIndex(in.readerIndex() + 2);
    }

}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterResponseDecoder.java

License:Open Source License

private void checkSum(ByteBuf in, int wholeLength) {
    int crcGet = in.getUnsignedShort(in.readerIndex() + wholeLength - 2);
    int crcCalc = Utils.crc(in, in.readerIndex(), wholeLength - 2);
    if (crcCalc != crcGet) {
        throw new DecoderException("wrong crc");
    }/*from   w  w w .  j ava2s . c o m*/

}