📄 snmpcontextv3basis.java
字号:
ret = "userPrivacyPassword is null, but usePrivacy is true"; } else if (userPrivacyPassword.length() == 0) { ret = "userPrivacyPassword is empty, but usePrivacy is true"; } else if (useAuthentication == false) { ret = "useAuthentication is false, but usePrivacy is true"; } } if (useAuthentication == true) { if (userAuthenticationPassword == null) { ret = "userAuthenticationPassword is null, but useAuthentication is true"; } else if (userAuthenticationPassword.length() == 0) { ret = "userAuthenticationPassword is empty, but useAuthentication is true"; } } return ret;}/** * Does the actual encoding. * * @see #encodeDiscoveryPacket * @see #encodePacket */protected byte[] actualEncodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, TimeWindowNode node, Object obj) throws java.io.IOException, EncodingException{ AsnEncoderv3 enc = new AsnEncoderv3(); String msg = checkContextSanity(); if (msg != null) { throw new EncodingException(msg); } int msgId = ((Integer)obj).intValue(); if (AsnObject.debug > 6) { System.out.println(getClass().getName() + ".actualEncodePacket(): msgId=" + msgId + ", Pdu reqId=" + rId); } byte[] packet = enc.EncodeSNMPv3(this, msgId, node, msg_type, rId, errstat, errind, ve); return packet;}/** * Processes an incoming SNMP v3 response. */protected void processIncomingResponse(ByteArrayInputStream in)throws DecodingException, IOException{ AsnDecoderv3 rpdu = new AsnDecoderv3(); // don't have to check for context sanity here: if the request was // fine, so should be the response 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(getClass().getName() + ".processIncomingResponse(): msgId=" + msgId + ", Pdu reqId=" + rid); } Pdu pdu = getPdu(rid); try { AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, bu, false); if (pduSeq != null) { // got a message Integer rid2 = new Integer(pduSeq.getReqId()); if (AsnObject.debug > 6) { System.out.println(getClass().getName() + ".processIncomingResponse():" + " rid2=" + rid2); } Pdu newPdu = null; if (rid2.intValue() != rid.intValue()) { newPdu = getPdu(rid2); if (AsnObject.debug > 3) { System.out.println(getClass().getName() + ".processIncomingResponse(): " + "pduReqId of msgId (" + rid.intValue() + ") != pduReqId of Pdu (" + rid2.intValue() + ")"); } if (newPdu == null) { if (AsnObject.debug > 3) { System.out.println(getClass().getName() + ".processIncomingResponse(): " + "Using pduReqId of msgId (" + rid.intValue() + ")"); } } } if (newPdu != null) { pdu = newPdu; } } else { if (AsnObject.debug > 6) { System.out.println(getClass().getName() + ".processIncomingResponse():" + " pduSeq is null."); } } if (pdu != null) { pdu.fillin(pduSeq); } else { if (AsnObject.debug > 6) { System.out.println(getClass().getName() + ".processIncomingResponse(): 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(getClass().getName() + ".processIncomingResponse(): Pdu of msgId " + msgId + " is already answered"); } rid = new Integer(-1); }}/** * Returns if we send this PDU in authoritative role or not. * The engine who sends a Response, a Trapv2 or a Report is * authoritative. * * @since 4_14 * @return true if authoritative, false if not. */// Note: for when adding INFORM// When sending an INFORM, the receiver is the authoritative engine, so// the INFORM does NOT have to be added to this list!protected boolean isAuthoritative(byte msg_type){ return (msg_type == AsnObject.GET_RSP_MSG || msg_type == AsnObject.TRPV2_REQ_MSG || msg_type == AsnObject.GET_RPRT_MSG);}void discoverIfNeeded()throws java.io.IOException, PduException{ uk.co.westhawk.snmp.beans.UsmDiscoveryBean discBean = null; boolean isNeeded = false; TimeWindow tWindow = TimeWindow.getCurrent(); String engineId = tWindow.getSnmpEngineId(getSendToHostAddress(), hostPort); if (engineId == null) { isNeeded = true; discBean = new uk.co.westhawk.snmp.beans.UsmDiscoveryBean( getSendToHostAddress(), hostPort, bindAddr, typeSocket); } if (isUseAuthentication()) { if (isNeeded) { discBean.setAuthenticationDetails(userName, userAuthenticationPassword, authenticationProtocol); } else if (tWindow.isTimeLineKnown(engineId) == false) { isNeeded = true; discBean = new uk.co.westhawk.snmp.beans.UsmDiscoveryBean( getSendToHostAddress(), hostPort, bindAddr, typeSocket); discBean.setAuthenticationDetails(userName, userAuthenticationPassword, authenticationProtocol); } if (isNeeded && isUsePrivacy()) { discBean.setPrivacyDetails(userPrivacyPassword); } } if (isNeeded) { discBean.startDiscovery(); } // If contextEngineId is null or of length zero, set // it to the snmpEngineId. if (contextEngineId == null || contextEngineId.length == 0) { engineId = tWindow.getSnmpEngineId(getSendToHostAddress(), hostPort); setContextEngineId(SnmpUtilities.toBytes(engineId)); }}/** * Adds the specified request pdu listener to receive PDUs on the * specified listening context that matches this context. * This method will call usmAgent.setSnmpContext(this). * * <p> * Don't use the TCP_SOCKET when listening for request PDUs. It doesn't * provide functionality to send a response back. * </p> * * @see AbstractSnmpContext#addRequestPduListener(RequestPduListener, ListeningContextPool) * * @param l The request PDU listener * @param lcontext The listening context */public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext)throws java.io.IOException{ super.addRequestPduListener(l, lcontext); usmAgent.setSnmpContext(this); TimeWindow tWindow = TimeWindow.getCurrent(); if (usmAgent.getSnmpEngineId() == null) { throw new IOException("UsmAgent " + usmAgent.getClass().getName() + " should provide Engine ID!"); } tWindow.setSnmpEngineId(usmAgent.MYFAKEHOSTNAME, hostPort, usmAgent.getSnmpEngineId()); tWindow.updateTimeWindow(usmAgent.getSnmpEngineId(), usmAgent.getSnmpEngineBoots(), usmAgent.getSnmpEngineTime(), this.isUseAuthentication());}/** * Copies all parameters into another SnmpContextv3. */public Object cloneParameters(SnmpContextv3Face clContext) { clContext.setUserName(new String(userName)); clContext.setUseAuthentication(useAuthentication); if (userAuthenticationPassword != null) { clContext.setUserAuthenticationPassword( new String(userAuthenticationPassword)); } clContext.setAuthenticationProtocol(authenticationProtocol); clContext.setUsePrivacy(usePrivacy); if (userPrivacyPassword != null) { clContext.setUserPrivacyPassword(new String(userPrivacyPassword)); } clContext.setContextName(new String(contextName)); int l = contextEngineId.length; byte[] newContextEngineId = new byte[l]; System.arraycopy(contextEngineId, 0, newContextEngineId, 0, l); clContext.setContextEngineId(newContextEngineId); clContext.setUsmAgent(usmAgent); return clContext;}/** * Returns the hash key. This key is built out of all properties. It * serves as key for a hashtable of (v3) contexts. * * @since 4_14 * @return The hash key */public String getHashKey(){ StringBuffer buffer = new StringBuffer(); buffer.append(hostname); buffer.append("_").append(hostPort); buffer.append("_").append(bindAddr); buffer.append("_").append(typeSocket); buffer.append("_").append(useAuthentication); buffer.append("_").append(authenticationProtocol); buffer.append("_").append(userAuthenticationPassword); buffer.append("_").append(userName); buffer.append("_").append(usePrivacy); buffer.append("_").append(userPrivacyPassword); buffer.append("_").append(SnmpUtilities.toHexString(contextEngineId)); buffer.append("_").append(contextName); buffer.append("_v").append(getVersion()); return buffer.toString();}/** * Returns a string representation of the object. * @return The string */public String toString(){ StringBuffer buffer = new StringBuffer(getClass().getName() + "["); buffer.append("host=").append(hostname); buffer.append(", sendToHost=").append(getSendToHostAddress()); buffer.append(", port=").append(hostPort); buffer.append(", bindAddress=").append(bindAddr); buffer.append(", socketType=").append(typeSocket); buffer.append(", contextEngineId=").append(SnmpUtilities.toHexString(contextEngineId)); buffer.append(", contextName=").append(contextName); buffer.append(", userName=").append(userName); buffer.append(", useAuthentication=").append(useAuthentication); buffer.append(", authenticationProtocol=").append(ProtocolNames[authenticationProtocol]); buffer.append(", userAuthenticationPassword=").append(userAuthenticationPassword); buffer.append(", usePrivacy=").append(usePrivacy); buffer.append(", userPrivacyPassword=").append(userPrivacyPassword); buffer.append(", #trapListeners=").append(trapSupport.getListenerCount()); buffer.append(", #pduListeners=").append(pduSupport.getListenerCount()); buffer.append("]"); return buffer.toString();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -