📄 snmpcontextv3basis.java
字号:
* * @param newUserPrivacyPassword The user privacy password */public void setUserPrivacyPassword(String newUserPrivacyPassword){ if (newUserPrivacyPassword != null && newUserPrivacyPassword.equals(userPrivacyPassword) == false) { userPrivacyPassword = newUserPrivacyPassword; userPrivKeyMD5 = null; userPrivKeySHA1 = null; }}/** * Sets the contextEngineID. * See <a href="http://www.ietf.org/rfc/rfc3411.txt">RFC 3411</a>. * * A contextEngineID uniquely * identifies an SNMP entity that may realize an instance of a context * with a particular contextName. * * <p> * Note, when the stack is an authoritative engine, this parameter should * equal the UsmAgent.getSnmpEngineId(). See the StackUsage * documentation for an explanation. * </p> * * <p> * If the contextEngineID is of length zero, the encoder will use the (discovered) * snmpEngineId. * </p> * * @see UsmAgent#getSnmpEngineId() * @param newContextEngineId The contextEngineID */public void setContextEngineId(byte [] newContextEngineId)throws IllegalArgumentException{ if (newContextEngineId != null) { contextEngineId = newContextEngineId; } else { throw new IllegalArgumentException("contextEngineId is null"); }}/** * Returns the contextEngineID. * * @return The contextEngineID */public byte [] getContextEngineId(){ return contextEngineId;}/** * Sets the contextName. * See <a href="http://www.ietf.org/rfc/rfc3411.txt">RFC 3411</a>. * * A contextName is used to name a context. Each contextName MUST be * unique within an SNMP entity. * By default this is "" (the empty String). * * @param newContextName The contextName * @see #Default_ContextName */public void setContextName(String newContextName){ contextName = newContextName;}/** * Returns the contextName. * * @return The contextName */public String getContextName(){ return contextName;}/** * Adds a discovery pdu. This method adds the PDU (without checking if * discovery is needed). * * @param pdu the discovery pdu * @return pdu is succesful added * @see AbstractSnmpContext#addPdu(Pdu) * @see #addPdu(Pdu) */public boolean addDiscoveryPdu(DiscoveryPdu pdu)throws java.io.IOException, PduException{ // since this is a DiscoveryPdu we do not check for discovery :-) return this.addPdu(pdu, false);}/** * Adds a PDU. This method adds the PDU and blocks until it has all the * discovery parameters it needs. * * @param pdu the PDU * @return pdu is succesful added * @see AbstractSnmpContext#addPdu(Pdu) * @see #addDiscoveryPdu(DiscoveryPdu) */public boolean addPdu(Pdu pdu)throws java.io.IOException, PduException{ return this.addPdu(pdu, true);}/** * Creates the USM agent. * @see DefaultUsmAgent * @see #isAuthoritative */protected UsmAgent createUsmAgent(){ return new DefaultUsmAgent();}/** * Sets the UsmAgent, needed when this stack is used as authoritative * SNMP engine. This interface provides authentiation details, like its * clock and its Engine ID. * * @see DefaultUsmAgent * @param agent The USM authoritative interface * @since 4_14 */public void setUsmAgent(UsmAgent agent){ usmAgent = agent;}/** * Returns the UsmAgent. * @see #setUsmAgent * @since 4_14 */public UsmAgent getUsmAgent(){ return usmAgent;}/** * Adds a 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 has done so. * Discovery is only needed if the stack is non authoritative. * * <p> * This method stores the SNMPv3 msgId and PDU * request id in a Hashtable. * Since the encoding only happens once and every retry sends the same * encoded packet, only one msgId is used. * </p> * * @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{ // TODO, when sending response or report, the msgId should be set! Integer msgId = pdu.snmpv3MsgId; if (msgId == null) { msgId = new Integer(next_id++); } else if (pdu.isExpectingResponse() == true) { // generate a new msgId, even if this is already set. The user // could be adding the same PDU more than once to the // context. msgId = new Integer(next_id++); } pdu.snmpv3MsgId = msgId; msgIdHash.put(msgId, new Integer(pdu.req_id)); if (AsnObject.debug > 6) { System.out.println(getClass().getName() + ".addPdu(): msgId=" + msgId.toString() + ", Pdu reqId=" + pdu.req_id); } if (checkDiscovery == true && isAuthoritative(pdu.getMsgType()) == false) { discoverIfNeeded(pdu); } boolean added = super.addPdu(pdu); return added;}/** * Removes a PDU. This removes the PDU from the AbstractSnmpContext and * clears the link with the SNMPv3 msgId. * * @param rid the PDU request id * @return whether the PDU has been successfully removed * @see AbstractSnmpContext#removePdu(int) */public synchronized boolean removePdu(int rid){ boolean removed = super.removePdu(rid); if (removed) { Enumeration keys = msgIdHash.keys(); Integer msgIdI = null; boolean found = false; while (keys.hasMoreElements() && found == false) { msgIdI = (Integer) keys.nextElement(); Integer pduIdI = (Integer) msgIdHash.get(msgIdI); found = (pduIdI.intValue() == rid); } if (found) { msgIdHash.remove(msgIdI); } } return removed;}/** * Encodes a discovery PDU packet. This methods encodes without checking * if the discovery parameters are all known. */public byte[] encodeDiscoveryPacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws java.io.IOException, EncodingException{ String engineId = ""; TimeWindow tWindow = TimeWindow.getCurrent(); if (tWindow.isSnmpEngineIdKnown(getSendToHostAddress(), hostPort) == true) { engineId = tWindow.getSnmpEngineId(getSendToHostAddress(), hostPort); } TimeWindowNode node = new TimeWindowNode(engineId, 0, 0); return actualEncodePacket(msg_type, rId, errstat, errind, ve, node, obj);}/** * Encodes a PDU. This is for internal use only and should * NOT be called by the developer. * This is called by the the PDU itself and is added to the interface to * cover the different kind of Contexts. * * <p> * When the stack is * <ul> * <li> * authoritative, the timeline details are retrieved from the UsmAgent. * </li> * <li> * non authoritative, this methods first checks if all the discovery * parameters are known; * <ul> * <li> * If so, it encodes and returns the bytes. * </li> * <li> * If not, it will throw an EncodingException. * </li> * </ul> * </li> * </ul> * </p> * * @see #isAuthoritative(byte) * @param msg_type The message type * @param rId The message id * @param errstat The error status * @param errind The error index * @param ve The varbind list * @param obj Additional object (only used in SNMPv3) * @return The encoded packet */public byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws java.io.IOException, EncodingException{ TimeWindowNode node = null; if (isDestroyed == true) { throw new EncodingException("Context can no longer be used, since it is already destroyed"); } else { TimeWindow tWindow = TimeWindow.getCurrent(); if (isAuthoritative(msg_type) == true) { usmAgent.setSnmpContext(this); if (usmAgent.getSnmpEngineId() == null) { throw new EncodingException("UsmAgent " + usmAgent.getClass().getName() + " should provide Engine ID!"); } tWindow.updateTimeWindow(usmAgent.getSnmpEngineId(), usmAgent.getSnmpEngineBoots(), usmAgent.getSnmpEngineTime(), this.isUseAuthentication()); node = tWindow.getTimeLine(usmAgent.getSnmpEngineId()); } else { if (tWindow.isSnmpEngineIdKnown(getSendToHostAddress(), hostPort) == false) { throw new EncodingException("Engine ID of host " + getSendToHostAddress() + ", port " + hostPort + " is unknown (rId=" + rId + "). Perform discovery."); } String engineId = tWindow.getSnmpEngineId(getSendToHostAddress(), hostPort); node = new TimeWindowNode(engineId, 0, 0); if (isUseAuthentication()) { if (tWindow.isTimeLineKnown(engineId) == true) { node = tWindow.getTimeLine(engineId); } else { throw new EncodingException("Time Line of Engine ID of host " + getSendToHostAddress() + ", port " + hostPort + " is unknown. " + "Perform discovery."); } } } } return actualEncodePacket(msg_type, rId, errstat, errind, ve, node, obj);}/** * Checks the sanity of the context and returns an error message when it * is not correct. */protected String checkContextSanity(){ String ret = null; if (usePrivacy == true) { if (userPrivacyPassword == null) { ret = "userPrivacyPassword is null, but usePrivacy is true"; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -