📄 snmprequesthandler.java
字号:
// if (ipacl != null) { if (pdu.type == SnmpDefinitions.pduSetRequestPdu) { if (!((InetAddressAcl)ipacl). checkWritePermission(pdu.address, community)) { if (isTraceOn()) { trace("checkAcl", "sender is " + pdu.address + " with " + community); trace("checkAcl", "sender has no write permission"); } int err = SnmpSubRequestHandler. mapErrorStatus(SnmpDefinitions. snmpRspAuthorizationError, pdu.version, pdu.type); response = newErrorResponsePdu(pdu, err, 0) ; } else { if (isTraceOn()) { trace("checkAcl", "sender is " + pdu.address + " with " + community); trace("checkAcl", "sender has write permission"); } } } else { if (!((InetAddressAcl)ipacl).checkReadPermission(pdu.address, community)) { if (isTraceOn()) { trace("checkAcl", "sender is " + pdu.address + " with " + community); trace("checkAcl", "sender has no read permission"); } int err = SnmpSubRequestHandler. mapErrorStatus(SnmpDefinitions. snmpRspAuthorizationError, pdu.version, pdu.type); response = newErrorResponsePdu(pdu, err, 0); SnmpAdaptorServer snmpServer = (SnmpAdaptorServer)adaptorServer; snmpServer.updateErrorCounters(SnmpDefinitions. snmpRspNoSuchName); } else { if (isTraceOn()) { trace("checkAcl", "sender is " + pdu.address + " with " + community); trace("checkAcl", "sender has read permission"); } } } } // If the response is not null, this means the pdu is rejected. // So let's update the statistics. // if (response != null) { SnmpAdaptorServer snmpServer = (SnmpAdaptorServer)adaptorServer ; snmpServer.incSnmpInBadCommunityUses(1) ; if (((InetAddressAcl)ipacl).checkCommunity(community) == false) snmpServer.incSnmpInBadCommunityNames(1) ; } return response ; } /** * Make a response pdu with the specified error status and index. * NOTE: the response pdu share its varBindList with the request pdu. */ private SnmpPduRequest newValidResponsePdu(SnmpPduPacket reqPdu, SnmpVarBind[] varBindList) { SnmpPduRequest result = new SnmpPduRequest() ; result.address = reqPdu.address ; result.port = reqPdu.port ; result.version = reqPdu.version ; result.community = reqPdu.community ; result.type = result.pduGetResponsePdu ; result.requestId = reqPdu.requestId ; result.errorStatus = SnmpDefinitions.snmpRspNoError ; result.errorIndex = 0 ; result.varBindList = varBindList ; ((SnmpAdaptorServer)adaptorServer). updateErrorCounters(result.errorStatus) ; return result ; } /** * Make a response pdu with the specified error status and index. * NOTE: the response pdu share its varBindList with the request pdu. */ private SnmpPduRequest newErrorResponsePdu(SnmpPduPacket req,int s,int i) { SnmpPduRequest result = newValidResponsePdu(req, null) ; result.errorStatus = s ; result.errorIndex = i ; result.varBindList = req.varBindList ; ((SnmpAdaptorServer)adaptorServer). updateErrorCounters(result.errorStatus) ; return result ; } private SnmpMessage newTooBigMessage(SnmpMessage reqMsg) throws SnmpTooBigException { SnmpMessage result = null ; SnmpPduPacket reqPdu = null ; try { reqPdu = (SnmpPduPacket)pduFactory.decodeSnmpPdu(reqMsg) ; if (reqPdu != null) { SnmpPduPacket respPdu = newTooBigPdu(reqPdu) ; result = (SnmpMessage)pduFactory. encodeSnmpPdu(respPdu, packet.getData().length) ; } } catch(SnmpStatusException x) { // This should not occur because decodeIncomingRequest has normally // been successfully called before. debug("InternalError: ", x); throw new InternalError() ; } return result ; } private SnmpPduPacket newTooBigPdu(SnmpPduPacket req) { SnmpPduRequest result = newErrorResponsePdu(req, SnmpDefinitions.snmpRspTooBig, 0) ; result.varBindList = null ; return result ; } private SnmpPduPacket reduceResponsePdu(SnmpPduPacket req, SnmpPduPacket resp, int acceptedVbCount) throws SnmpTooBigException { // Reduction can be attempted only on bulk response // if (req.type != req.pduGetBulkRequestPdu) { if (isDebugOn()) { debug("reduceResponsePdu", "cannot remove anything"); } throw new SnmpTooBigException(acceptedVbCount) ; } // We're going to reduce the varbind list. // First determine which items should be removed. // Next duplicate and replace the existing list by the reduced one. // // acceptedVbCount is the number of varbind which have been // successfully encoded before reaching bufferSize: // * when it is >= 2, we split the varbindlist at this // position (-1 to be safe), // * when it is 1, we only put one (big?) item in the varbindlist // * when it is 0 (in fact, acceptedVbCount is not available), // we split the varbindlist by 2. // int vbCount = resp.varBindList.length ; if (acceptedVbCount >= 3) vbCount = Math.min(acceptedVbCount - 1, resp.varBindList.length) ; else if (acceptedVbCount == 1) vbCount = 1 ; else // acceptedCount == 0 ie it is unknown vbCount = resp.varBindList.length / 2 ; if (vbCount < 1) { if (isDebugOn()) { debug("reduceResponsePdu", "cannot remove anything"); } throw new SnmpTooBigException(acceptedVbCount) ; } else { SnmpVarBind[] newVbList = new SnmpVarBind[vbCount] ; for (int i = 0 ; i < vbCount ; i++) { newVbList[i] = resp.varBindList[i] ; } if (isDebugOn()) { debug("reduceResponsePdu", (resp.varBindList.length - newVbList.length) + " items have been removed"); } resp.varBindList = newVbList ; } return resp ; } /** * The method takes the incoming requests and split it into subrequests. */ private void splitRequest(SnmpPduRequest req) { int nbAgents= mibs.size(); SnmpMibAgent agent= (SnmpMibAgent) mibs.firstElement(); if (nbAgents == 1) { // Take all the oids contained in the request and // subs.put(agent, new SnmpSubRequestHandler(agent, req, true)); return; } // For the get next operation we are going to send the varbind list // to all agents // if (req.type == pduGetNextRequestPdu) { for(Enumeration e= mibs.elements(); e.hasMoreElements(); ) { SnmpMibAgent ag= (SnmpMibAgent) e.nextElement(); subs.put(ag, new SnmpSubNextRequestHandler(adaptor, ag, req)); } return; } int nbReqs= req.varBindList.length; SnmpVarBind[] vars= req.varBindList; SnmpSubRequestHandler sub; for(int i=0; i < nbReqs; i++) { agent= root.getAgentMib(vars[i].oid); sub= (SnmpSubRequestHandler) subs.get(agent); if (sub == null) { // We need to create the sub request handler and update // the hashtable // sub= new SnmpSubRequestHandler(agent, req); subs.put(agent, sub); } // Update the translation table within the subrequest // sub.updateRequest(vars[i], i); } } /** * The method takes the incoming get bulk requests and split it into * subrequests. */ private void splitBulkRequest(SnmpPduBulk req, int nonRepeaters, int maxRepetitions, int R) { // Send the getBulk to all agents // for(Enumeration e= mibs.elements(); e.hasMoreElements(); ) { SnmpMibAgent agent = (SnmpMibAgent) e.nextElement(); if(isDebugOn()) trace("splitBulkRequest", "Create a sub with : " + agent + " " + nonRepeaters + " " + maxRepetitions + " " + R); subs.put(agent, new SnmpSubBulkRequestHandler(adaptor, agent, req, nonRepeaters, maxRepetitions, R)); } return; } private SnmpPduPacket mergeResponses(SnmpPduRequest req) { if (req.type == pduGetNextRequestPdu) { return mergeNextResponses(req); } SnmpVarBind[] result= req.varBindList; // Go through the list of subrequests and concatenate. // Hopefully, by now all the sub-requests should be finished // for(Enumeration e= subs.elements(); e.hasMoreElements();) { SnmpSubRequestHandler sub= (SnmpSubRequestHandler) e.nextElement(); sub.updateResult(result); } return newValidResponsePdu(req,result); } private SnmpPduPacket mergeNextResponses(SnmpPduRequest req) { int max= req.varBindList.length; SnmpVarBind[] result= new SnmpVarBind[max]; // Go through the list of subrequests and concatenate. // Hopefully, by now all the sub-requests should be finished // for(Enumeration e= subs.elements(); e.hasMoreElements();) { SnmpSubRequestHandler sub= (SnmpSubRequestHandler) e.nextElement(); sub.updateResult(result); } if (req.version == snmpVersionTwo) { return newValidResponsePdu(req,result); } // In v1 make sure there is no endOfMibView ... // for(int i=0; i < max; i++) { SnmpValue val= result[i].value; if (val == SnmpVarBind.endOfMibView) return newErrorResponsePdu(req, SnmpDefinitions.snmpRspNoSuchName, i+1); } // So far so good ... // return newValidResponsePdu(req,result); } private SnmpVarBind[] mergeBulkResponses(int size) { // Let's allocate the array for storing the result // SnmpVarBind[] result= new SnmpVarBind[size]; for(int i= size-1; i >=0; --i) { result[i]= new SnmpVarBind(); result[i].value= SnmpVarBind.endOfMibView; } // Go through the list of subrequests and concatenate. // Hopefully, by now all the sub-requests should be finished // for(Enumeration e= subs.elements(); e.hasMoreElements();) { SnmpSubRequestHandler sub= (SnmpSubRequestHandler) e.nextElement(); sub.updateResult(result); } return result; } protected boolean isTraceOn() { return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP); } protected void trace(String clz, String func, String info) { Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz,func,info); } protected void trace(String func, String info) { trace(dbgTag, func, info); } protected boolean isDebugOn() { return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); } protected void debug(String clz, String func, String info) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz,func,info); } protected void debug(String clz, String func, Throwable exception) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception); } protected void debug(String func, String info) { debug(dbgTag, func, info); } protected void debug(String func, Throwable exception) { debug(dbgTag, func, exception); } protected String makeDebugTag() { return "SnmpRequestHandler[" + adaptorServer.getProtocol() + ":" + adaptorServer.getPort() + "]"; } Thread createThread(Runnable r) { return null; } static final private String InterruptSysCallMsg = "Interrupted system call"; static final private SnmpStatusException noSuchNameException = new SnmpStatusException(SnmpDefinitions.snmpRspNoSuchName) ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -