[SNMP4J] getting snmpv3 variable bindings

Bala Sundaram balasundaram.06 at gmail.com
Mon Apr 13 14:38:14 CEST 2009


Hi,

I'm following Apache MINA to handle the traps.
This is the API(overriding from ProtocolDecoderAdapter) am using it to
decode the snmpv3 trap to get ScopedPDU.
Finally i have to get variable bindings.
But it gives nullpointer exception that also i included down below,
Please let me know if u have a solution for it.
Thanks in advance.
*public void decode(IoSession ioSession, IoBuffer ioBuffer,
ProtocolDecoderOutput decoderOutput) throws Exception
    {
        // TODO Auto-generated method stub
        Integer32 securityModel = new Integer32();
        ByteBuffer pduBuffer = ioBuffer.buf();
        BERInputStream wholeMsg = new BERInputStream(pduBuffer);
        if (!wholeMsg.markSupported()) {
            String   txt = "Message stream must support marks";

            throw new IllegalArgumentException  (txt);
        }

        wholeMsg.mark(16);
        BER.MutableByte type = new BER.MutableByte();
        // decode header but do not check length here, because we do only
decode
        // the first 16 bytes.
        int length = BER.decodeHeader(wholeMsg, type, false);
        if (type.getValue() != BER.SEQUENCE) {
            System.out.println("ASN.1 parse error (message is not a
sequence)");

        }

        long lengthOfLength = wholeMsg.getPosition();
        wholeMsg.reset();
        wholeMsg.mark(length);
        if (wholeMsg.skip(lengthOfLength) != lengthOfLength)
        {
            System.out.println("ASN.1 parse error (unable to parse)");
        }
        Integer32 snmpVersion = new Integer32();
        snmpVersion.decodeBER(wholeMsg);

        if (snmpVersion.getValue() != SnmpConstants.version3)
        {
            // internal error -> should not happen
            throw new RuntimeException(
            "Internal error unexpected snmp version read");
        }
        else
        {
            //Prepare Data elements
            System.out.println("Snmp version : " + snmpVersion.getValue());
            // decode SNMPv3 header
            HeaderData header = new HeaderData();
            header.decodeBER(wholeMsg);

            ScopedPDU scopedPdu = new ScopedPDU();
            MutablePDU pdu = new MutablePDU();
            pdu.setPdu(scopedPdu);
            // Add Security Model
            USM usm = new USM(SecurityProtocols.getInstance(),
                    new OctetString(MPv3.createLocalEngineID()), 0);
            securityModels.addSecurityModel(usm);
            securityModel.setValue(header.getSecurityModel());
            SecurityModel secModel =
securityModels.getSecurityModel(securityModel);

            System.out.println("MODEL " + header.getSecurityModel());

            Integer32 securityLevel = new Integer32();

            // determine security level
            switch (header.getMsgFlags() & 0x03) {
            case 3: {
                securityLevel.setValue(SecurityLevel.AUTH_PRIV);
                System.out.println("Authentication With Priviledge");
                break;
            }
            case 0: {
                securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV);
                System.out.println("No Authentication, No Priviledge");
                break;
            }
            case 1: {
                securityLevel.setValue(SecurityLevel.AUTH_NOPRIV);
                System.out.println("Authentication With No Priviledge");
                break;
            }
            default: {
                securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV);
                System.out.println("RFC3412 7.2.5 - Invalid message (illegal
msgFlags)");
            }
            }

            int secParametersPosition = (int) wholeMsg.getPosition();
            // get security parameters

            SecurityParameters secParameters =
secModel.newSecurityParametersInstance();
            secParameters.decodeBER(wholeMsg);

secParameters.setSecurityParametersPosition(secParametersPosition);

            OctetString securityEngineID = new OctetString();
            // create a new security state reference
            SecurityStateReference secStateReference = secModel
            .newSecurityStateReference();
            // create output stream for scoped PDU
            // may be optimized by an output stream that maps directly into
the
            // original input
            wholeMsg.reset();

            Integer32 maxSizeResponseScopedPDU = new Integer32();
//            // the usm has to recalculate this value
            maxSizeResponseScopedPDU.setValue(header.msgMaxSize
                    .getValue()
                    - MAX_HEADER_LENGTH);
            StatusInformation statusInfo = new StatusInformation();

            BEROutputStream scopedPDU = new BEROutputStream();
            OctetString securityName = new OctetString();
            int status = secModel.processIncomingMsg(snmpVersion
                    .getValue(), header.getMsgMaxSize()
                    - MAX_HEADER_LENGTH, secParameters, secModel,
                    securityLevel.getValue(),
                    wholeMsg,
                    // output parameters
                    securityEngineID, securityName, scopedPDU,
                    maxSizeResponseScopedPDU, secStateReference,
                    statusInfo);
            wholeMsg.close();

            System.out.println("Status : " + status);

            if (status == SnmpConstants.SNMPv3_USM_UNKNOWN_SECURITY_NAME) {
                try {
                    BERInputStream scopedPduStream = new BERInputStream(
                            scopedPDU.rewind());
                    scopedPdu.decodeBER(scopedPduStream);
                } catch (IOException iox) {iox.printStackTrace();}
                if (((scopedPdu.getContextEngineID() == null) || (scopedPdu
                        .getContextEngineID().length() == 0))
                        && ((scopedPdu.getType() != PDU.RESPONSE) &&
(scopedPdu
                                .getType() != PDU.REPORT))) {
                    CounterEvent event = new CounterEvent(this ,
                            SnmpConstants.snmpUnknownPDUHandlers);
                    VariableBinding errorIndication = new VariableBinding(
                            event.getOid(), event.getCurrentValue());
                    statusInfo.setErrorIndication(errorIndication);
                    status = SnmpConstants.SNMP_MP_UNKNOWN_PDU_HANDLERS;
                }
            }

            wholeMsg.close();
        }
    }*

Snmp version : 3
SNMPv3 header decoded: msgId=1817350242, msgMaxSize=65535, msgFlags=03,
secModel=3
MODEL 3
Authentication With Priviledge
Status : 1404
java.lang.NullPointerException (Hexdump: 30 81 AD 02 01 03 30 11 02 04 6C 52
90 62 02 03 00 FF FF 04 01 03 02 01 03 04 33 30 31 04 09 80 00 13 70 01 C0
A8 00 0C 02 01 00 02 01 00 04 06 4D 44 35 44 45 53 04 0C 49 F3 57 B5 C3 DE
23 4B 0A 06 43 9C 04 08 00 00 00 00 2D C7 FC 1B 04 60 21 A4 3B C4 4D 0B 46
F1 63 B7 EE 80 E3 1C F0 D0 7B E5 95 40 CF 23 1F E3 28 46 4E F5 D5 36 12 FA
9C 37 41 20 B2 59 36 BC 5E 31 4A 5D 55 BF 6F B6 AB B8 8E 73 41 26 CA E0 8E
39 30 D6 FE D3 CD F6 CE C9 CD 86 E3 14 F5 77 B9 C2 E5 EF 9A 50 0F 42 20 00
03 C2 A8 5C CF 25 FE 83 66 A8 E8 4F DA B9)
Session Closed




Thank you
Balasundaram L



More information about the SNMP4J mailing list