📄 snmpv3message.java
字号:
* @exception SnmpTooBigException If the resulting encoding does not fit * into <CODE>maxDataLength</CODE> bytes. * @exception ArrayIndexOutOfBoundsException If the encoding exceeds * <CODE>maxDataLength</CODE>. */ public void encodeSnmpPdu(SnmpPdu p, int maxDataLength) throws SnmpStatusException, SnmpTooBigException { SnmpScopedPduPacket pdu = (SnmpScopedPduPacket) p; if(isTraceOn()) { trace("encodeSnmpPdu", "Pdu to marshall: \n" + "security parameters : " + pdu.securityParameters + "\n" + "type :" + pdu.type + "\n" + "version :" + pdu.version + "\n" + "requestId :" + pdu.requestId + "\n" + "msgId :" + pdu.msgId + "\n" + "msgMaxSize :" + pdu.msgMaxSize + "\n" + "msgFlags :" + pdu.msgFlags + "\n" + "msgSecurityModel :" + pdu.msgSecurityModel + "\n" + "contextEngineId :" + pdu.contextEngineId + "\n" + "contextName :" + pdu.contextName + "\n"); } version = pdu.version; address = pdu.address; port = pdu.port; msgId = pdu.msgId; msgMaxSize = pdu.msgMaxSize; msgFlags = pdu.msgFlags; msgSecurityModel = pdu.msgSecurityModel; contextEngineId = pdu.contextEngineId; contextName = pdu.contextName; securityParameters = pdu.securityParameters; // // Allocate the array to receive the encoding. // data = new byte[maxDataLength]; // // Encode the pdu // Reminder: BerEncoder does backward encoding ! // try { BerEncoder benc = new BerEncoder(data) ; benc.openSequence() ; encodeVarBindList(benc, pdu.varBindList) ; switch(pdu.type) { case pduGetRequestPdu : case pduGetNextRequestPdu : case pduInformRequestPdu : case pduGetResponsePdu : case pduSetRequestPdu : case pduV2TrapPdu : case pduReportPdu : SnmpPduRequestType reqPdu = (SnmpPduRequestType) pdu; benc.putInteger(reqPdu.getErrorIndex()); benc.putInteger(reqPdu.getErrorStatus()); benc.putInteger(pdu.requestId); break; case pduGetBulkRequestPdu : SnmpPduBulkType bulkPdu = (SnmpPduBulkType) pdu; benc.putInteger(bulkPdu.getMaxRepetitions()); benc.putInteger(bulkPdu.getNonRepeaters()); benc.putInteger(pdu.requestId); break ; default: throw new SnmpStatusException("Invalid pdu type " + String.valueOf(pdu.type)) ; } benc.closeSequence(pdu.type) ; dataLength = benc.trim() ; } catch(ArrayIndexOutOfBoundsException x) { throw new SnmpTooBigException() ; } } /** * 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 (isDebugOn()) { debug("decodeSnmpPdu", 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(isTraceOn()) { trace("decodeSnmpPdu", "Unmarshalled pdu : \n" + "type :" + pdu.type + "\n" + "version :" + pdu.version + "\n" + "requestId :" + pdu.requestId + "\n" + "msgId :" + pdu.msgId + "\n" + "msgMaxSize :" + pdu.msgMaxSize + "\n" + "msgFlags :" + pdu.msgFlags + "\n" + "msgSecurityModel :" + pdu.msgSecurityModel + "\n" + "contextEngineId :" + pdu.contextEngineId + "\n" + "contextName :" + pdu.contextName + "\n"); } return pdu ; } /** * Dumps this message in a string. * * @return The string containing the dump. */ public String printMessage() { StringBuffer sb = new StringBuffer(); sb.append("msgId : " + msgId + "\n"); sb.append("msgMaxSize : " + msgMaxSize + "\n"); sb.append("msgFlags : " + msgFlags + "\n"); sb.append("msgSecurityModel : " + msgSecurityModel + "\n"); if (contextEngineId == null) { sb.append("contextEngineId : null"); } else { sb.append("contextEngineId : {\n"); sb.append(dumpHexBuffer(contextEngineId, 0, contextEngineId.length)); sb.append("\n}\n"); } if (contextName == null) { sb.append("contextName : null"); } else { sb.append("contextName : {\n"); sb.append(dumpHexBuffer(contextName, 0, contextName.length)); sb.append("\n}\n"); } return sb.append(super.printMessage()).toString(); } // TRACES & DEBUG //--------------- boolean isTraceOn() { return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP); } void trace(String clz, String func, String info) { Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info); } void trace(String func, String info) { trace(dbgTag, func, info); } boolean isDebugOn() { return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP); } void debug(String clz, String func, String info) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info); } void debug(String clz, String func, Throwable exception) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception); } void debug(String func, String info) { debug(dbgTag, func, info); } void debug(String func, Throwable exception) { debug(dbgTag, func, exception); } String dbgTag = "SnmpV3Message";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -