Search in sources :

Example 1 with BerDecoder

use of com.sun.jmx.snmp.BerDecoder in project jdk8u_jdk by JetBrains.

the class SnmpV3Message method decodeSnmpPdu.

/**
     * Gets the PDU encoded in this message.
     * <P>
     * This method decodes the data field and returns the resulting PDU.
     *
     * @return The resulting PDU.
     * @exception SnmpStatusException If the encoding is not valid.
     */
public SnmpPdu decodeSnmpPdu() throws SnmpStatusException {
    SnmpScopedPduPacket pdu = null;
    BerDecoder bdec = new BerDecoder(data);
    try {
        int type = bdec.getTag();
        bdec.openSequence(type);
        switch(type) {
            case pduGetRequestPdu:
            case pduGetNextRequestPdu:
            case pduInformRequestPdu:
            case pduGetResponsePdu:
            case pduSetRequestPdu:
            case pduV2TrapPdu:
            case pduReportPdu:
                SnmpScopedPduRequest reqPdu = new SnmpScopedPduRequest();
                reqPdu.requestId = bdec.fetchInteger();
                reqPdu.setErrorStatus(bdec.fetchInteger());
                reqPdu.setErrorIndex(bdec.fetchInteger());
                pdu = reqPdu;
                break;
            case pduGetBulkRequestPdu:
                SnmpScopedPduBulk bulkPdu = new SnmpScopedPduBulk();
                bulkPdu.requestId = bdec.fetchInteger();
                bulkPdu.setNonRepeaters(bdec.fetchInteger());
                bulkPdu.setMaxRepetitions(bdec.fetchInteger());
                pdu = bulkPdu;
                break;
            default:
                throw new SnmpStatusException(snmpRspWrongEncoding);
        }
        pdu.type = type;
        pdu.varBindList = decodeVarBindList(bdec);
        bdec.closeSequence();
    } catch (BerException e) {
        if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
            SNMP_LOGGER.logp(Level.FINEST, SnmpV3Message.class.getName(), "decodeSnmpPdu", "BerException", e);
        }
        throw new SnmpStatusException(snmpRspWrongEncoding);
    }
    //
    // The easy work.
    //
    pdu.address = address;
    pdu.port = port;
    pdu.msgFlags = msgFlags;
    pdu.version = version;
    pdu.msgId = msgId;
    pdu.msgMaxSize = msgMaxSize;
    pdu.msgSecurityModel = msgSecurityModel;
    pdu.contextEngineId = contextEngineId;
    pdu.contextName = contextName;
    pdu.securityParameters = securityParameters;
    if (SNMP_LOGGER.isLoggable(Level.FINER)) {
        final StringBuilder strb = new StringBuilder().append("Unmarshalled PDU : \n").append("type : ").append(pdu.type).append("\n").append("version : ").append(pdu.version).append("\n").append("requestId : ").append(pdu.requestId).append("\n").append("msgId : ").append(pdu.msgId).append("\n").append("msgMaxSize : ").append(pdu.msgMaxSize).append("\n").append("msgFlags : ").append(pdu.msgFlags).append("\n").append("msgSecurityModel : ").append(pdu.msgSecurityModel).append("\n").append("contextEngineId : ").append(pdu.contextEngineId).append("\n").append("contextName : ").append(pdu.contextName).append("\n");
        SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(), "decodeSnmpPdu", strb.toString());
    }
    return pdu;
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) SnmpScopedPduRequest(com.sun.jmx.snmp.SnmpScopedPduRequest) BerDecoder(com.sun.jmx.snmp.BerDecoder) SnmpScopedPduPacket(com.sun.jmx.snmp.SnmpScopedPduPacket) BerException(com.sun.jmx.snmp.BerException) SnmpScopedPduBulk(com.sun.jmx.snmp.SnmpScopedPduBulk)

Example 2 with BerDecoder

use of com.sun.jmx.snmp.BerDecoder in project jdk8u_jdk by JetBrains.

the class SnmpV3Message method getRequestId.

/**
     * Returns the associated request Id.
     * @param data The flat message.
     * @return The request Id.
     */
public int getRequestId(byte[] data) throws SnmpStatusException {
    BerDecoder bdec = null;
    int msgId = 0;
    try {
        bdec = new BerDecoder(data);
        bdec.openSequence();
        bdec.fetchInteger();
        bdec.openSequence();
        msgId = bdec.fetchInteger();
    } catch (BerException x) {
        throw new SnmpStatusException("Invalid encoding");
    }
    try {
        bdec.closeSequence();
    } catch (BerException x) {
    }
    return msgId;
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) BerDecoder(com.sun.jmx.snmp.BerDecoder) BerException(com.sun.jmx.snmp.BerException)

Example 3 with BerDecoder

use of com.sun.jmx.snmp.BerDecoder in project jdk8u_jdk by JetBrains.

the class SnmpV3Message method decodeMessage.

/**
     * Decodes the specified bytes and initializes this message.
     * For internal use only.
     *
     * @param inputBytes The bytes to be decoded.
     *
     * @exception SnmpStatusException If the specified bytes are not a valid encoding.
     */
public void decodeMessage(byte[] inputBytes, int byteCount) throws SnmpStatusException {
    try {
        BerDecoder bdec = new BerDecoder(inputBytes);
        bdec.openSequence();
        version = bdec.fetchInteger();
        bdec.openSequence();
        msgId = bdec.fetchInteger();
        msgMaxSize = bdec.fetchInteger();
        msgFlags = bdec.fetchOctetString()[0];
        msgSecurityModel = bdec.fetchInteger();
        bdec.closeSequence();
        msgSecurityParameters = bdec.fetchOctetString();
        if ((msgFlags & SnmpDefinitions.privMask) == 0) {
            bdec.openSequence();
            contextEngineId = bdec.fetchOctetString();
            contextName = bdec.fetchOctetString();
            data = bdec.fetchAny();
            dataLength = data.length;
            bdec.closeSequence();
        } else {
            encryptedPdu = bdec.fetchOctetString();
        }
        bdec.closeSequence();
    } catch (BerException x) {
        x.printStackTrace();
        throw new SnmpStatusException("Invalid encoding");
    }
    if (SNMP_LOGGER.isLoggable(Level.FINER)) {
        final StringBuilder strb = new StringBuilder().append("Unmarshalled message : \n").append("version : ").append(version).append("\n").append("msgId : ").append(msgId).append("\n").append("msgMaxSize : ").append(msgMaxSize).append("\n").append("msgFlags : ").append(msgFlags).append("\n").append("msgSecurityModel : ").append(msgSecurityModel).append("\n").append("contextEngineId : ").append(contextEngineId == null ? null : SnmpEngineId.createEngineId(contextEngineId)).append("\n").append("contextName : ").append(contextName).append("\n").append("data : ").append(data).append("\n").append("dat len : ").append((data == null) ? 0 : data.length).append("\n").append("encryptedPdu : ").append(encryptedPdu).append("\n");
        SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(), "decodeMessage", strb.toString());
    }
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) BerDecoder(com.sun.jmx.snmp.BerDecoder) BerException(com.sun.jmx.snmp.BerException)

Aggregations

BerDecoder (com.sun.jmx.snmp.BerDecoder)3 BerException (com.sun.jmx.snmp.BerException)3 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)3 SnmpScopedPduBulk (com.sun.jmx.snmp.SnmpScopedPduBulk)1 SnmpScopedPduPacket (com.sun.jmx.snmp.SnmpScopedPduPacket)1 SnmpScopedPduRequest (com.sun.jmx.snmp.SnmpScopedPduRequest)1