private boolean processWalk(PDU response, PDU request, OID rootOID) throws SnmpException { if ((response == null) || (response.getErrorStatus() != 0) || (response.getType() == PDU.REPORT)) { return true; } boolean finished = false; OID lastOID = request.get(0).getOid(); for (int i = 0; (!finished) && (i < response.size()); i++) { VariableBinding vb = response.get(i); if ((vb.getOid() == null) || (vb.getOid().size() < rootOID.size()) || (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)) { finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { outputResponse(vb); finished = true; } else if (vb.getOid().compareTo(lastOID) <= 0) { throw new SnmpException( "Variable received is not lexicographic successor of requested one:" + vb.toString() + " <= " + lastOID); } else { outputResponse(vb); lastOID = vb.getOid(); } } if (response.size() == 0) { finished = true; } if (!finished) { VariableBinding next = response.get(response.size() - 1); next.setVariable(new Null()); request.set(0, next); request.setRequestID(new Integer32(0)); } return finished; }
private Request createSnmpRequest(URI url) throws UnknownHostException { String host = url.getHost(); int port = url.getPort(); if (port == -1) { port = SnmpConstants.DEFAULT_COMMAND_RESPONDER_PORT; } String userInfo = url.getUserInfo(); if (userInfo == null) { userInfo = defaultUserInfo; } String path = url.getPath(); String[] segments = path.split("/"); String contextInfo; String contextName = ""; OctetString contextEngineID = null; String oidPart = null; if (segments.length > 1) { contextInfo = segments[0]; oidPart = segments[1]; String[] contextInfos = contextInfo.split(";"); if (contextInfos.length > 1) { contextEngineID = OctetString.fromHexStringPairs(contextInfos[1]); } contextName = contextInfos[0]; } else if (segments.length == 1) { oidPart = segments[0]; } Target t = createTarget(new OctetString(userInfo)); if (host != null) { if (t instanceof CertifiedTarget) { t.setAddress(new TlsAddress(InetAddress.getByName(host), port)); } else { t.setAddress(new UdpAddress(InetAddress.getByName(host), port)); } } else { t = defaultTarget; } PDU pdu = pduFactory.createPDU(t); if (pdu instanceof ScopedPDU) { if (contextEngineID != null) { ((ScopedPDU) pdu).setContextEngineID(contextEngineID); } if (contextName != null) { ((ScopedPDU) pdu).setContextName(new OctetString(contextName)); } } SnmpUriType type = SnmpUriType.GET; if (oidPart != null && oidPart.endsWith(".*")) { type = SnmpUriType.SUBTREE; oidPart = oidPart.substring(0, oidPart.length() - 2); } else if (oidPart != null && oidPart.endsWith("+")) { type = SnmpUriType.NEXT; oidPart = oidPart.substring(0, oidPart.length() - 1); } List<OID> oids; if (oidPart != null && oidPart.contains("(")) { String[] oidStrings = oidPart.split("[\\(,\\),\\,]"); oids = new ArrayList<>(oidStrings.length); for (String oidString : oidStrings) { if (!oidString.isEmpty()) { OID o = new OID(oidString); if (o.isValid()) { oids.add(o); } } } } else if (oidPart != null) { oids = Collections.singletonList(new OID(oidPart)); } else { oids = Collections.emptyList(); } return new Request(t, pdu, oids.toArray(new OID[oids.size()]), type); }