public final Pdu read() throws IOException { Pdu pdu = new Pdu(); pdu.id = readUInt2(); int length = readUInt3(); pdu.version = readUInt2(); pdu.payload = cipher.encrypt(readBytes(length)); return pdu; }
/** * Implementing the Observer interface. Receiving the response from the Pdu. * * @param obs the UpSincePdu variable * @param ov the date * @see uk.co.westhawk.snmp.pdu.UpSincePdu */ public void update(Observable obs, Object ov) { Pdu pdu = (Pdu) obs; if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { Date dres = (Date) ov; if (dres != null) { // TODO: invokeLater v.setText(dres.toString()); } } else { // TODO: invokeLater v.setText(pdu.getErrorStatusString()); } }
/** * Adds an pdu. This method adds the Pdu and checks if discovery is needed depending on the * parameter <code>checkDiscovery</code>. If discovery is needed this method will block until it * have done so. Discovery is only needed if the stack is non authoritative. * * @param pdu the pdu * @param checkDiscovery check if discovery is needed * @return pdu is succesful added * @see AbstractSnmpContext#addPdu(Pdu) * @see #addDiscoveryPdu(DiscoveryPdu) * @see #addPdu(Pdu) */ protected boolean addPdu(Pdu pdu, boolean checkDiscovery) throws java.io.IOException, PduException { boolean added = super.addPdu(pdu); if (checkDiscovery == true && isAuthoritative(pdu.getMsgType()) == false) { discoverIfNeeded(); } return added; }
private static Boolean hasTpField(Pdu pdu, String fieldName) { try { Method m = pdu.getClass().getMethod("has" + fieldName); return (Boolean) m.invoke(pdu); } catch (Exception e) { } return null; }
// ================================================== // ENCODING/DECODING utility methods // ================================================== private static Integer getTpField(Pdu pdu, String fieldName) { try { Method m = pdu.getClass().getMethod("get" + fieldName); return (Integer) m.invoke(pdu); } catch (Exception e) { } return null; }
public static String decodeDataCodingScheme(Pdu pdu) { StringBuffer sb = new StringBuffer(); switch (PduUtils.extractDcsEncoding(pdu.getDataCodingScheme())) { case PduUtils.DCS_ENCODING_7BIT: sb.append("7-bit GSM Alphabet"); break; case PduUtils.DCS_ENCODING_8BIT: sb.append("8-bit encoding"); break; case PduUtils.DCS_ENCODING_UCS2: sb.append("UCS2 encoding"); break; } // are flash messages are only applicable to general coding group? if ((pdu.getDataCodingScheme() & ~PduUtils.DCS_CODING_GROUP_GENERAL) == 0) { if (PduUtils.extractDcsFlash(pdu.getDataCodingScheme()) == PduUtils.DCS_MESSAGE_CLASS_FLASH) { sb.append(", (Flash Message)"); } } return sb.toString(); }
/** Processes an incoming SNMP v3 message. */ protected void ProcessIncomingMessage(AsnDecoder rpdu, ByteArrayInputStream in) throws DecodingException, IOException { byte[] bu = null; // need to duplicate the message for V3 to rewrite int nb = in.available(); bu = new byte[nb]; in.read(bu); in = new ByteArrayInputStream(bu); AsnSequence asnTopSeq = rpdu.DecodeSNMPv3(in); int msgId = rpdu.getMsgId(asnTopSeq); Integer rid = (Integer) msgIdHash.get(new Integer(msgId)); if (rid != null) { if (AsnObject.debug > 6) { System.out.println( "SnmpContextv3.ProcessIncomingMessage(): msgId=" + msgId + ", Pdu reqId=" + rid); } Pdu pdu = getPdu(rid); try { AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, bu); if (pduSeq != null) { // got a message Integer rid2 = new Integer(pduSeq.getReqId()); if (AsnObject.debug > 6) { System.out.println("SnmpContextv3.ProcessIncomingMessage():" + " rid2=" + rid2); } Pdu newPdu = null; if (rid2.intValue() != rid.intValue()) { newPdu = getPdu(rid2); if (AsnObject.debug > 3) { System.out.println( "ProcessIncomingMessage(): " + "pduReqId of msgId (" + rid.intValue() + ") != pduReqId of Pdu (" + rid2.intValue() + ")"); } if (newPdu == null) { if (AsnObject.debug > 3) { System.out.println( "ProcessIncomingMessage(): " + "Using pduReqId of msgId (" + rid.intValue() + ")"); } } } if (newPdu != null) { pdu = newPdu; } } else { if (AsnObject.debug > 6) { System.out.println("SnmpContextv3.ProcessIncomingMessage():" + " pduSeq is null."); } } if (pdu != null) { pdu.fillin(pduSeq); } else { if (AsnObject.debug > 6) { System.out.println("ProcessIncomingMessage(): No Pdu with reqid " + rid.intValue()); } } } catch (DecodingException exc) { if (pdu != null) { pdu.setErrorStatus(AsnObject.SNMP_ERR_DECODING_EXC, exc); pdu.fillin(null); } else { throw exc; } } } else { if (AsnObject.debug > 3) { System.out.println("Pdu of msgId " + msgId + " is already answered"); } rid = new Integer(-1); } }
public static String decodeFirstOctet(Pdu pdu) { // Use reflection to check for method signatures hasTpXXX() and getTpXXX() // that are specific to a particular type in actual object StringBuffer sb = new StringBuffer(); sb.append("First Octet: " + PduUtils.byteToPdu(pdu.getFirstOctet())); sb.append(" ["); // TP-MTI switch (pdu.getTpMti()) { case PduUtils.TP_MTI_SMS_DELIVER: sb.append("TP-MTI: (SMS-DELIVER)"); break; case PduUtils.TP_MTI_SMS_STATUS_REPORT: sb.append("TP-MTI: (SMS-STATUS REPORT)"); break; case PduUtils.TP_MTI_SMS_SUBMIT: sb.append("TP-MTI: (SMS-SUBMIT)"); break; default: throw new RuntimeException("Invalid message type"); } // TP-MMS if (hasTpField(pdu, "TpMms") != null) { if (hasTpField(pdu, "TpMms")) { sb.append(", TP-MMS: (Has more messages)"); } else { sb.append(", TP-MMS: (has no messages)"); } } // TP-RD if (hasTpField(pdu, "TpRd") != null) { if (hasTpField(pdu, "TpRd")) { sb.append(", TP-RD: (Reject duplicates)"); } else { sb.append(", TP-RD: (allow duplicates)"); } } // TP-VPF if ((hasTpField(pdu, "TpVpf") != null) && (hasTpField(pdu, "TpVpf") != false)) { switch (getTpField(pdu, "TpVpf")) { case PduUtils.TP_VPF_INTEGER: sb.append(", TP-VPF: (validity format, integer"); break; case PduUtils.TP_VPF_TIMESTAMP: sb.append(", TP-VPF: (validity format, timestamp"); break; case PduUtils.TP_VPF_NONE: sb.append(", TP-VPF: (validity format, none)"); break; } } // TP-SRI if (hasTpField(pdu, "TpSri") != null) { if (hasTpField(pdu, "TpSri")) { sb.append(", TP-SRI: (Requests Status Report)"); } else { sb.append(", TP-SRI: (No Status Report)"); } } // TP-SRR if (hasTpField(pdu, "TpSrr") != null) { if (hasTpField(pdu, "TpSrr")) { sb.append(", TP-SRR: (Requests Status Report)"); } else { sb.append(", TP-SRR: (No Status Report)"); } } // TP-UDHI if (pdu.hasTpUdhi()) { sb.append(", TP-UDHI: (has UDH)"); } else { sb.append(", TP-UDHI: (no UDH)"); } sb.append("]"); sb.append("\n"); return sb.toString(); }