/** * This method creates a new Vector which does not contain the first * element up to the specified limit. * * @param original The original vector. * @param limit The limit. */ private Vector<SnmpVarBind> splitFrom(Vector<SnmpVarBind> original, int limit) { int max= original.size(); Vector<SnmpVarBind> result= new Vector<>(max - limit); int i= limit; // Ok the loop looks a bit strange. But in order to improve the // perf, we try to avoid reference to the limit variable from // within the loop ... // for(Enumeration<SnmpVarBind> e= original.elements(); e.hasMoreElements(); --i) { SnmpVarBind var= e.nextElement(); if (i >0) continue; result.addElement(new SnmpVarBind(var.oid, var.value)); } return result; }
/** * This is a factory method for creating new SnmpMibRequest objects. * @param engine The local engine. * @param reqPdu The received pdu. * @param vblist The vector of SnmpVarBind objects in which the * MIB concerned by this request is involved. * @param version The protocol version of the SNMP request. * @param userData User allocated contextual data. * * @return A new SnmpMibRequest object. * * @since 1.5 **/ public static SnmpMibRequest newMibRequest(SnmpEngine engine, SnmpPdu reqPdu, Vector<SnmpVarBind> vblist, int version, Object userData, String principal, int securityLevel, int securityModel, byte[] contextName, byte[] accessContextName) { return new SnmpMibRequestImpl(engine, reqPdu, vblist, version, userData, principal, securityLevel, securityModel, contextName, accessContextName); }
@Override public void registerCheckException(SnmpVarBind var, SnmpStatusException exception) throws SnmpStatusException { // The index in the exception must correspond to // the SNMP index ... // // We throw the exception in order to abort the SET operation // in an atomic way. final int errorCode = exception.getStatus(); final int mappedErrorCode = mapSetException(errorCode, version); if (errorCode != mappedErrorCode) throw new SnmpStatusException(mappedErrorCode, getVarIndex(var)+1); else throw new SnmpStatusException(exception, getVarIndex(var)+1); }
private void init(SnmpAdaptorServer server, SnmpPdu req, int nonRepeat, int maxRepeat, int R) { this.server = server; this.nonRepeat= nonRepeat; this.maxRepeat= maxRepeat; this.globalR= R; final int max= translation.length; final SnmpVarBind[] list= req.varBindList; final NonSyncVector<SnmpVarBind> nonSyncVarBind = ((NonSyncVector<SnmpVarBind>)varBind); for(int i=0; i < max; i++) { translation[i]= i; // we need to allocate a new SnmpVarBind. Otherwise the first // sub request will modify the list... // final SnmpVarBind newVarBind = new SnmpVarBind(list[i].oid, list[i].value); nonSyncVarBind.addNonSyncElement(newVarBind); } }
@Override public void registerSetException(SnmpVarBind var, SnmpStatusException exception) throws SnmpStatusException { // The index in the exception must correspond to // the SNMP index ... // if (version == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(exception, getVarIndex(var)+1); // Although the first pass of check() did not fail, // the set() phase could not be carried out correctly. // Since we don't know how to make an "undo", and some // assignation may already have been performed, we're going // to throw an snmpRspUndoFailed. // throw new SnmpStatusException(SnmpDefinitions.snmpRspUndoFailed, getVarIndex(var)+1); }
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<SnmpSubRequestHandler> e= subs.elements(); e.hasMoreElements();) { SnmpSubRequestHandler sub= e.nextElement(); sub.updateResult(result); } 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 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 = SnmpPduRequest.pduGetResponsePdu ; result.requestId = reqPdu.requestId ; result.errorStatus = SnmpDefinitions.snmpRspNoError ; result.errorIndex = 0 ; result.varBindList = varBindList ; ((SnmpAdaptorServer)adaptorServer). updateErrorCounters(result.errorStatus) ; return result ; }
/** * @param engine The local engine. * @param reqPdu The received pdu. * @param vblist The vector of SnmpVarBind objects in which the * MIB concerned by this request is involved. * @param protocolVersion The protocol version of the SNMP request. * @param userData User allocated contextual data. This object must * be allocated on a per SNMP request basis through the * SnmpUserDataFactory registered with the SnmpAdaptorServer, * and is handed back to the user through SnmpMibRequest objects. */ public SnmpMibRequestImpl(SnmpEngine engine, SnmpPdu reqPdu, Vector<SnmpVarBind> vblist, int protocolVersion, Object userData, String principal, int securityLevel, int securityModel, byte[] contextName, byte[] accessContextName) { varbinds = vblist; version = protocolVersion; data = userData; this.reqPdu = reqPdu; this.engine = engine; this.principal = principal; this.securityLevel = securityLevel; this.securityModel = securityModel; this.contextName = contextName; this.accessContextName = accessContextName; }
/** * Processes a <CODE>get</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests. * * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved. * * @exception SnmpStatusException An error occurred during the operation. */ @Override public void get(SnmpMibRequest inRequest) throws SnmpStatusException { SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpErrorHandlerAgent.class.getName(), "get", "Get in Exception"); if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(SnmpStatusException.noSuchName); Enumeration<SnmpVarBind> l = inRequest.getElements(); while(l.hasMoreElements()) { SnmpVarBind varbind = l.nextElement(); varbind.setNoSuchObject(); } }
/** * Processes a <CODE>getNext</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests.. * * @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved. * * @exception SnmpStatusException An error occurred during the operation. */ @Override public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException { SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpErrorHandlerAgent.class.getName(), "getNext", "GetNext in Exception"); if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(SnmpStatusException.noSuchName); Enumeration<SnmpVarBind> l = inRequest.getElements(); while(l.hasMoreElements()) { SnmpVarBind varbind = l.nextElement(); varbind.setEndOfMibView(); } }
/** * Processes a <CODE>getBulk</CODE> operation. It will throw an exception if the request is a V1 one or it will set exceptions within the list for V2 ones. * * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved. * * @exception SnmpStatusException An error occurred during the operation. */ @Override public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat) throws SnmpStatusException { SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpErrorHandlerAgent.class.getName(), "getBulk", "GetBulk in Exception"); if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0); Enumeration<SnmpVarBind> l = inRequest.getElements(); while(l.hasMoreElements()) { SnmpVarBind varbind = l.nextElement(); varbind.setEndOfMibView(); } }
private void init(SnmpPdu req, SnmpAdaptorServer server) { this.server = server; // The translation table is easy in this case ... // final int max= translation.length; final SnmpVarBind[] list= req.varBindList; final NonSyncVector<SnmpVarBind> nonSyncVarBind = ((NonSyncVector<SnmpVarBind>)varBind); for(int i=0; i < max; i++) { translation[i]= i; // we need to allocate a new SnmpVarBind. Otherwise the first // sub request will modify the list... // final SnmpVarBind newVarBind = new SnmpVarBind(list[i].oid, list[i].value); nonSyncVarBind.addNonSyncElement(newVarBind); } }
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); }
/** * SNMP V1/V2 The constructor initialize the subrequest with the whole varbind list contained * in the original request. */ @SuppressWarnings("unchecked") // cast to NonSyncVector<SnmpVarBind> protected SnmpSubRequestHandler(SnmpMibAgent agent, SnmpPdu req, boolean nouse) { this(agent,req); // The translation table is easy in this case ... // int max= translation.length; SnmpVarBind[] list= req.varBindList; for(int i=0; i < max; i++) { translation[i]= i; ((NonSyncVector<SnmpVarBind>)varBind).addNonSyncElement(list[i]); } }
/** * The method updates a given var bind list with the result of a * previsouly invoked operation. * Prior to calling the method, one must make sure that the operation was * successful. As such the method getErrorIndex or getErrorStatus should be * called. */ protected void updateResult(SnmpVarBind[] result) { if (result == null) return; final int max=varBind.size(); final int len=result.length; for(int i= 0; i< max ; i++) { // bugId 4641694: must check position in order to avoid // ArrayIndexOutOfBoundException final int pos=translation[i]; if (pos < len) { result[pos] = (SnmpVarBind)((NonSyncVector)varBind).elementAtNonSync(i); } else { if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) { SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpSubRequestHandler.class.getName(), "updateResult","Position `"+pos+"' is out of bound..."); } } } }
/** * SNMP V1/V2 . To be called with updateRequest. */ protected SnmpSubRequestHandler(SnmpMibAgent agent, SnmpPdu req) { if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) { SNMP_ADAPTOR_LOGGER.logp(Level.FINER, SnmpSubRequestHandler.class.getName(), "constructor", "creating instance for request " + String.valueOf(req.requestId)); } version= req.version; type= req.type; this.agent= agent; // We get a ref on the pdu in order to pass it to SnmpMibRequest. reqPdu = req; //Pre-allocate room for storing varbindlist and translation table. // int length= req.varBindList.length; translation= new int[length]; varBind= new NonSyncVector<SnmpVarBind>(length); }
/** * This is a factory method for creating new SnmpMibRequest objects. * @param reqPdu The received PDU. * @param vblist The vector of SnmpVarBind objects in which the * MIB concerned by this request is involved. * @param version The protocol version of the SNMP request. * @param userData User allocated contextual data. * * @return A new SnmpMibRequest object. * * @since 1.5 **/ public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu, Vector<SnmpVarBind> vblist, int version, Object userData) { return new SnmpMibRequestImpl(null, reqPdu, vblist, version, userData, null, SnmpDefinitions.noAuthNoPriv, getSecurityModel(version), null,null); }
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<SnmpSubRequestHandler> e= subs.elements(); e.hasMoreElements();) { SnmpSubRequestHandler sub= 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 void concatVector(SnmpMibRequest req, Vector<SnmpVarBind> source) { for(Enumeration<SnmpVarBind> e= source.elements(); e.hasMoreElements(); ) { SnmpVarBind var= e.nextElement(); // We need to duplicate the SnmpVarBind otherwise it is going // to be overloaded by the next get Next ... req.addVarBind(new SnmpVarBind(var.oid, var.value)); } }
@Override void findHandlingNode(SnmpVarBind varbind, long[] oid, int depth, SnmpRequestTree handlers) throws SnmpStatusException { final int length = oid.length; SnmpMibNode node = null; if (handlers == null) throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr); if (depth > length) { // Nothing is left... the oid is not valid throw new SnmpStatusException(SnmpStatusException.noSuchObject); } else if (depth == length) { // The oid is not complete... throw new SnmpStatusException(SnmpStatusException.noSuchInstance); } else { // Some children variable or subobject is being querried // getChild() will raise an exception if no child is found. // final SnmpMibNode child= getChild(oid[depth]); // XXXX zzzz : what about null children? // (variables for nested groups) // if child==null, then we're dealing with a variable or // a table: we register this node. // This behaviour should be overriden in subclasses, // in particular in group meta classes: the group // meta classes that hold tables should take care // of forwarding this call to all the tables involved. // if (child == null) handlers.add(this,depth,varbind); else child.findHandlingNode(varbind,oid,depth+1,handlers); } }
@Override public void registerGetException(SnmpVarBind var, SnmpStatusException exception) throws SnmpStatusException { // The index in the exception must correspond to // the SNMP index ... // if (version == SnmpDefinitions.snmpVersionOne) throw new SnmpStatusException(exception, getVarIndex(var)+1); if (var == null) throw exception; // If we're doing a getnext ==> endOfMibView if (getnextflag) { var.value = SnmpVarBind.endOfMibView; return; } final int errorCode = mapGetException(exception.getStatus(), version); // Now take care of V2 errorCodes that can be stored // in the varbind itself: if (errorCode == SnmpStatusException.noSuchObject) // noSuchObject => noSuchObject var.value= SnmpVarBind.noSuchObject; else if (errorCode == SnmpStatusException.noSuchInstance) // noSuchInstance => noSuchInstance var.value= SnmpVarBind.noSuchInstance; else throw new SnmpStatusException(errorCode, getVarIndex(var)+1); }
SnmpMibRequest createMibRequest(Vector<SnmpVarBind> vblist, int protocolVersion, Object userData) { // This is an optimization: // The SnmpMibRequest created in the check() phase is // reused in the set() phase. // if (type == pduSetRequestPdu && mibRequest != null) return mibRequest; //This is a request comming from an SnmpV3AdaptorServer. //Full power. SnmpMibRequest result = null; if(incRequest != null) { result = SnmpMibAgent.newMibRequest(engine, reqPdu, vblist, protocolVersion, userData, incRequest.getPrincipal(), incRequest.getSecurityLevel(), incRequest.getSecurityModel(), incRequest.getContextName(), incRequest.getAccessContext()); } else { result = SnmpMibAgent.newMibRequest(reqPdu, vblist, protocolVersion, userData); } // If we're doing the check() phase, we store the SnmpMibRequest // so that we can reuse it in the set() phase. // if (type == pduWalkRequest) mibRequest = result; return result; }
public Vector<SnmpVarBind> getEntrySubList(int pos) { if (entrylists == null) return null; // if (pos == -1 || pos >= entrylists.size() ) return null; if (pos == -1 || pos >= entrycount ) return null; // return (Vector) entrylists.get(pos); return entrylists[pos]; }
static void checkRowStatusFail(SnmpMibSubRequest req, int errorStatus) throws SnmpStatusException { final SnmpVarBind statusvb = req.getRowStatusVarBind(); final SnmpStatusException x = new SnmpStatusException(errorStatus); req.registerCheckException(statusvb,x); }
static void setRowStatusFail(SnmpMibSubRequest req, int errorStatus) throws SnmpStatusException { final SnmpVarBind statusvb = req.getRowStatusVarBind(); final SnmpStatusException x = new SnmpStatusException(errorStatus); req.registerSetException(statusvb,x); }
public SnmpVarBind getRowStatusVarBind(int pos) { if (entryoids == null) return null; // if (pos == -1 || pos >= entryoids.size() ) return false; if (pos == -1 || pos >= entrycount ) return null; // return ((Boolean)isentrynew.get(pos)).booleanValue(); return rowstatus[pos]; }
@Override public void addVarBind(SnmpVarBind varbind) { // XXX not sure we must also add the varbind in the global // request? or whether we should raise an exception: // in principle, this method should not be called! varbinds.addElement(varbind); global.addVarBind(varbind); }