Exemplo n.º 1
0
 private Target createTarget(OctetString userInfo) {
   if ((userInfo == null) || (userInfo.length() == 0)) {
     return defaultTarget;
   }
   if (version == SnmpConstants.version3) {
     if ((securityModel == SecurityModel.SECURITY_MODEL_USM) && (usm != null)) {
       UsmUserEntry user = usm.getUser(null, userInfo);
       UserTarget target = new UserTarget();
       if (user != null) {
         if (user.getAuthenticationKey() != null) {
           if (user.getPrivacyKey() != null) {
             target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
           } else {
             target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
           }
         } else {
           target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
         }
       }
       target.setVersion(version);
       target.setSecurityName(userInfo);
       target.setSecurityModel(securityModel);
       target.setTimeout(timeout);
       target.setRetries(retries);
       return target;
     } else if (securityModel == SecurityModel.SECURITY_MODEL_TSM) {
       CertifiedTarget target = new CertifiedTarget(userInfo);
       target.setVersion(version);
       target.setSecurityModel(securityModel);
       target.setTimeout(timeout);
       target.setRetries(retries);
       return target;
     }
   } else {
     CommunityTarget target = new CommunityTarget();
     target.setCommunity(userInfo);
     target.setVersion(version);
     target.setSecurityModel(securityModel);
     target.setTimeout(timeout);
     target.setRetries(retries);
     return target;
   }
   return null;
 }
Exemplo n.º 2
0
 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);
 }