📄 snmprequesthandler.java
字号:
case SnmpPduPacket.pduGetBulkRequestPdu: respPdu = makeGetBulkResponsePdu((SnmpPduBulk)reqPdu, userData) ; break ; } } else { // reqPdu is rejected by ACLs // respPdu contains the error response to be sent. // We send this response only if authResEnabled is true. if (!snmpServer.getAuthRespEnabled()) { // No response should be sent respPdu = null ; } if (snmpServer.getAuthTrapEnabled()) { // A trap must be sent try { snmpServer.snmpV1Trap(SnmpPduTrap. trapAuthenticationFailure, 0, new SnmpVarBindList()) ; } catch(Exception x) { if (isDebugOn()) { debug("makeResponsePdu", "failure when sending authentication trap"); debug("makeResponsePdu", x); } } } } } return respPdu ; } // // Generates a response packet, filling the values in the // varbindlist with one of endOfMibView, noSuchObject, noSuchInstance // according to the value of <code>status</code> // // @param statusTag should be one of: // <li>SnmpDataTypeEnums.errEndOfMibViewTag</li> // <li>SnmpDataTypeEnums.errNoSuchObjectTag</li> // <li>SnmpDataTypeEnums.errNoSuchInstanceTag</li> // SnmpPduPacket makeErrorVarbindPdu(SnmpPduPacket req, int statusTag) { final SnmpVarBind[] vblist = req.varBindList; final int length = vblist.length; switch (statusTag) { case SnmpDataTypeEnums.errEndOfMibViewTag: for (int i=0 ; i<length ; i++) vblist[i].value = SnmpVarBind.endOfMibView; break; case SnmpDataTypeEnums.errNoSuchObjectTag: for (int i=0 ; i<length ; i++) vblist[i].value = SnmpVarBind.noSuchObject; break; case SnmpDataTypeEnums.errNoSuchInstanceTag: for (int i=0 ; i<length ; i++) vblist[i].value = SnmpVarBind.noSuchInstance; break; default: return newErrorResponsePdu(req,snmpRspGenErr,1); } return newValidResponsePdu(req,vblist); } // Generates an appropriate response when no mib is registered in // the adaptor. // // <li>If the version is V1:</li> // <ul><li>Generates a NoSuchName error V1 response PDU</li></ul> // <li>If the version is V2:</li> // <ul><li>If the request is a GET, fills the varbind list with // NoSuchObject's</li> // <li>If the request is a GET-NEXT/GET-BULK, fills the varbind // list with EndOfMibView's</li> // <li>If the request is a SET, generates a NoAccess error V2 // response PDU</li> // </ul> // // SnmpPduPacket makeNoMibErrorPdu(SnmpPduRequest req, Object userData) { // There is no agent registered // if (req.version == SnmpDefinitions.snmpVersionOne) { // Version 1: => NoSuchName return newErrorResponsePdu(req,snmpRspNoSuchName,1); } else if (req.version == SnmpDefinitions.snmpVersionTwo) { // Version 2: => depends on PDU type switch (req.type) { case pduSetRequestPdu : case pduWalkRequest : // SET request => NoAccess return newErrorResponsePdu(req,snmpRspNoAccess,1); case pduGetRequestPdu : // GET request => NoSuchObject return makeErrorVarbindPdu(req,SnmpDataTypeEnums. errNoSuchObjectTag); case pduGetNextRequestPdu : case pduGetBulkRequestPdu : // GET-NEXT or GET-BULK => EndOfMibView return makeErrorVarbindPdu(req,SnmpDataTypeEnums. errEndOfMibViewTag); default: } } // Something wrong here: => snmpRspGenErr return newErrorResponsePdu(req,snmpRspGenErr,1); } /** * Here we make the response pdu from a get/set request pdu. * At this level, the result is never null. */ private SnmpPduPacket makeGetSetResponsePdu(SnmpPduRequest req, Object userData) { // Create the trhead group specific for handling sub-requests // associated to the current request. Use the invoke id // // Nice idea to use a thread group on a request basis. // However the impact on performance is terrible ! // theGroup= new ThreadGroup(thread.getThreadGroup(), // "request " + String.valueOf(req.requestId)); // Let's build the varBindList for the response pdu // if (req.varBindList == null) { // Good ! Let's make a full response pdu. // return newValidResponsePdu(req, null) ; } // First we need to split the request into subrequests // splitRequest(req); int nbSubRequest= subs.size(); if (nbSubRequest == 1) return turboProcessingGetSet(req,userData); // Execute all the subrequests resulting from the split of the // varbind list. // SnmpPduPacket result= executeSubRequest(req,userData); if (result != null) // It means that an error occured. The error is already // formatted by the executeSubRequest // method. return result; // So far so good. So we need to concatenate all the answers. // if (isTraceOn()) { trace("makeGetSetResponsePdu", "Build the unified response for request " + req.requestId); } return mergeResponses(req); } /** * The method runs all the sub-requests associated to the current * instance of SnmpRequestHandler. */ private SnmpPduPacket executeSubRequest(SnmpPduPacket req, Object userData) { int errorStatus = SnmpDefinitions.snmpRspNoError ; int nbSubRequest= subs.size(); int i=0; // If it's a set request, we must first check any varBind // if (req.type == pduSetRequestPdu) { i=0; for(Enumeration e= subs.elements(); e.hasMoreElements() ; i++) { // Indicate to the sub request that a check must be invoked ... // OK we should have defined out own tag for that ! // SnmpSubRequestHandler sub= (SnmpSubRequestHandler) e.nextElement(); sub.setUserData(userData); sub.type= pduWalkRequest; sub.run(); sub.type= pduSetRequestPdu; if (sub.getErrorStatus() != SnmpDefinitions.snmpRspNoError) { // No point to go any further. // if (isDebugOn()) { debug("executeSubRequest", "an error occurs"); } return newErrorResponsePdu(req, errorStatus, sub.getErrorIndex() + 1) ; } } }// end processing check operation for a set PDU. // Let's start the sub-requests. // i=0; for(Enumeration e= subs.elements(); e.hasMoreElements() ;i++) { SnmpSubRequestHandler sub= (SnmpSubRequestHandler) e.nextElement(); /* NPCTE fix for bugId 4492741, esc 0, 16-August 2001 */ sub.setUserData(userData); /* end of NPCTE fix for bugId 4492741 */ sub.run(); if (sub.getErrorStatus() != SnmpDefinitions.snmpRspNoError) { // No point to go any further. // if (isDebugOn()) { debug("executeSubRequest", "an error occurs"); } return newErrorResponsePdu(req, errorStatus, sub.getErrorIndex() + 1) ; } } // everything is ok // return null; } /** * Optimize when there is only one sub request */ private SnmpPduPacket turboProcessingGetSet(SnmpPduRequest req, Object userData) { int errorStatus = SnmpDefinitions.snmpRspNoError ; SnmpSubRequestHandler sub= (SnmpSubRequestHandler) subs.elements().nextElement(); sub.setUserData(userData); // Indicate to the sub request that a check must be invoked ... // OK we should have defined out own tag for that ! // if (req.type == SnmpDefinitions.pduSetRequestPdu) { sub.type= pduWalkRequest; sub.run(); sub.type= pduSetRequestPdu; // Check the error status. // errorStatus= sub.getErrorStatus(); if (errorStatus != SnmpDefinitions.snmpRspNoError) { // No point to go any further. // return newErrorResponsePdu(req, errorStatus, sub.getErrorIndex() + 1) ; } } // process the operation // sub.run(); errorStatus= sub.getErrorStatus(); if (errorStatus != SnmpDefinitions.snmpRspNoError) { // No point to go any further. // if (isDebugOn()) { debug("turboProcessingGetSet", "an error occurs"); } int realIndex= sub.getErrorIndex() + 1; return newErrorResponsePdu(req, errorStatus, realIndex) ; } // So far so good. So we need to concatenate all the answers. // if (isTraceOn()) { trace("turboProcessingGetSet", "build the unified response for request " + req.requestId); } return mergeResponses(req); } /** * Here we make the response pdu for a bulk request. * At this level, the result is never null. */ private SnmpPduPacket makeGetBulkResponsePdu(SnmpPduBulk req, Object userData) { SnmpVarBind[] respVarBindList = null ; // RFC 1905, Section 4.2.3, p14 int L = req.varBindList.length ; int N = Math.max(Math.min(req.nonRepeaters, L), 0) ; int M = Math.max(req.maxRepetitions, 0) ; int R = L - N ; if (req.varBindList == null) { // Good ! Let's make a full response pdu. // return newValidResponsePdu(req, null) ; } // Split the request into subrequests. // splitBulkRequest(req, N, M, R); SnmpPduPacket result= executeSubRequest(req,userData); if (result != null) return result; respVarBindList= mergeBulkResponses(N + (M * R)); // Now we remove useless trailing endOfMibView. // int m2 ; // respVarBindList[m2] item and next are going to be removed int t = respVarBindList.length ; while ((t > N) && (respVarBindList[t-1]. value.equals(SnmpVarBind.endOfMibView))) { t-- ; } if (t == N) m2 = N + R ; else m2 = N + ((t -1 -N) / R + 2) * R ; // Trivial, of course... if (m2 < respVarBindList.length) { SnmpVarBind[] truncatedList = new SnmpVarBind[m2] ; for (int i = 0 ; i < m2 ; i++) { truncatedList[i] = respVarBindList[i] ; } respVarBindList = truncatedList ; } // Good ! Let's make a full response pdu. // return newValidResponsePdu(req, respVarBindList) ; } /** * Check the type of the pdu: only the get/set/bulk request * are accepted. */ private boolean checkPduType(SnmpPduPacket pdu) { boolean result = true ; switch(pdu.type) { case SnmpDefinitions.pduGetRequestPdu: case SnmpDefinitions.pduGetNextRequestPdu: case SnmpDefinitions.pduSetRequestPdu: case SnmpDefinitions.pduGetBulkRequestPdu: result = true ; break; default: if (isDebugOn()) { debug("checkPduType", "cannot respond to this kind of PDU"); } result = false ; break; } return result ; } /** * Check if the specified pdu is conform to the ACL. * This method returns null if the pdu is ok. If not, it returns * the response pdu to be replied. */ private SnmpPduPacket checkAcl(SnmpPduPacket pdu) { SnmpPduPacket response = null ; String community = new String(pdu.community) ; // We check the pdu type and create an error response if // the check failed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -