/** * Create a new PDU of the specified type from the supplied BER encoding. * * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc. */ protected SNMPv1TrapPDU(byte[] enc) throws SNMPBadValueException { tag = SNMPBERCodec.SNMPTRAP; extractFromBEREncoding(enc); // validate the message: make sure we have the appropriate pieces Vector contents = (Vector) (this.getValue()); if (contents.size() != 6) { throw new SNMPBadValueException("Bad Trap PDU"); } if (!(contents.elementAt(0) instanceof SNMPObjectIdentifier)) { throw new SNMPBadValueException("Bad Trap PDU: bad enterprise OID"); } if (!(contents.elementAt(1) instanceof SNMPIPAddress)) { throw new SNMPBadValueException("Bad Trap PDU: bad agent address"); } if (!(contents.elementAt(2) instanceof SNMPInteger)) { throw new SNMPBadValueException("Bad Trap PDU: bad generic trap code"); } if (!(contents.elementAt(3) instanceof SNMPInteger)) { throw new SNMPBadValueException("Bad Trap PDU: bad specific trap code"); } if (!(contents.elementAt(4) instanceof SNMPTimeTicks)) { throw new SNMPBadValueException("Bad Trap PDU: bad timestamp"); } if (!(contents.elementAt(5) instanceof SNMPSequence)) { throw new SNMPBadValueException("Bad Trap PDU: bad variable binding list"); } // now validate the variable binding list: should be list of sequences which // are (OID, value) pairs SNMPSequence varBindList = this.getVarBindList(); for (int i = 0; i < varBindList.size(); i++) { SNMPObject element = varBindList.getSNMPObjectAt(i); // must be a two-element sequence if (!(element instanceof SNMPSequence)) { throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i); } // variable binding sequence must have 2 elements, first of which must be an object identifier SNMPSequence varBind = (SNMPSequence) element; if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier)) { throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i); } } }
/** * Create a new PDU of the specified type from the supplied BER encoding. * * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc. */ protected SNMPv2TrapPDU(byte[] enc) throws SNMPBadValueException { super(enc, SNMPBERCodec.SNMPv2TRAP); // validate the message: make sure the first two components of the varBindList // are the appropriate variable pairs SNMPSequence varBindList = this.getVarBindList(); if (varBindList.size() < 2) { throw new SNMPBadValueException("Bad v2 Trap PDU: missing snmpTrapOID or sysUptime"); } // validate that the first variable binding is the sysUptime SNMPSequence variablePair = (SNMPSequence) varBindList.getSNMPObjectAt(0); SNMPObjectIdentifier oid = (SNMPObjectIdentifier) variablePair.getSNMPObjectAt(0); SNMPObject value = variablePair.getSNMPObjectAt(1); SNMPObjectIdentifier sysUptimeOID = new SNMPObjectIdentifier("1.3.6.1.2.1.1.3.0"); if (!(value instanceof SNMPTimeTicks) || !oid.equals(sysUptimeOID)) { throw new SNMPBadValueException("Bad v2 Trap PDU: bad sysUptime in variable binding list"); } // validate that the second variable binding is the snmpTrapOID variablePair = (SNMPSequence) varBindList.getSNMPObjectAt(1); oid = (SNMPObjectIdentifier) variablePair.getSNMPObjectAt(0); value = variablePair.getSNMPObjectAt(1); SNMPObjectIdentifier snmpTrapOIDOID = new SNMPObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"); if (!(value instanceof SNMPObjectIdentifier) || !oid.equals(snmpTrapOIDOID)) { throw new SNMPBadValueException("Bad v2 Trap PDU: bad snmpTrapOID in variable binding list"); } }
/** * A utility method that extracts the sysUptime from the variable bind list (it's the first of the * variable pairs). */ public SNMPTimeTicks getSysUptime() { SNMPSequence contents = this.getVarBindList(); SNMPSequence variablePair = (SNMPSequence) contents.getSNMPObjectAt(0); return (SNMPTimeTicks) variablePair.getSNMPObjectAt(1); }
/** * A utility method that extracts the snmpTrapOID from the variable bind list (it's the second of * the variable pairs). */ public SNMPObjectIdentifier getSNMPTrapOID() { SNMPSequence contents = this.getVarBindList(); SNMPSequence variablePair = (SNMPSequence) contents.getSNMPObjectAt(1); return (SNMPObjectIdentifier) variablePair.getSNMPObjectAt(1); }