public void onResponse(ResponseEvent re) {
    // Always cancel async request when response has been received
    // otherwise a memory leak is created! Not canceling a request
    // immediately can be useful when sending a request to a broadcast
    // address.
    try {
      Object source = re.getSource();

      // test to ignore REPORTS from DISCOVERY messages in SNMPv3
      if (!(source instanceof Snmp)) return;

      ((Snmp) source).cancel(re.getRequest(), this);

      // create the SnmpMsg received
      MsgSnmp msg = new MsgSnmp();
      msg.setPdu(re.getResponse());

      // TODO: how to know the version here to set communityTarget or UserTarget
      Target target = new CommunityTarget();
      //           ((CommunityTarget)target).setCommunity(new
      // OctetString(re.getStateReference().getSecurityName()));
      target.setAddress(re.getPeerAddress());
      msg.setTarget((AbstractTarget) target);
      UdpAddress add = (UdpAddress) re.getPeerAddress();
      msg.setRemotePort(add.getPort());
      msg.setRemoteHost(add.getInetAddress().getHostAddress());
      msg.setListenpoint(listenpoint);

      StackFactory.getStack(StackFactory.PROTOCOL_SNMP).receiveMessage(msg);
    } catch (Exception ex) {
    }
  }
Example #2
0
  public Target getTarget(String host, int port, int retries, int timeout)
      throws UnknownHostException {
    Target target = getTarget();

    Address address = new UdpAddress(InetAddress.getByName(host), port);
    target.setAddress(address);
    target.setRetries(retries);
    target.setTimeout(timeout);

    return target;
  }
Example #3
0
 private Target makeSnmpTarget(HttpServletRequest request) throws UnknownHostException {
   String hostname = request.getParameter("host");
   String community = request.getParameter("discoverSnmpCommunity");
   if (community == null) {
     community = "public";
   }
   int port = jrds.Util.parseStringNumber(request.getParameter("discoverSnmpPort"), 161);
   IpAddress addr = new UdpAddress(InetAddress.getByName(hostname), port);
   Target hosttarget = new CommunityTarget(addr, new OctetString(community));
   hosttarget.setVersion(SnmpConstants.version2c);
   return hosttarget;
 }
Example #4
0
 @Override
 public void addConnection(JrdsElement hostElement, HttpServletRequest request) {
   JrdsElement snmpElem = hostElement.addElement("connection", "type=jrds.snmp.SnmpConnection");
   if (hosttarget instanceof CommunityTarget) {
     CommunityTarget ct = (CommunityTarget) hosttarget;
     snmpElem.addElement("attr", "name=community").setTextContent(ct.getCommunity().toString());
   }
   snmpElem
       .addElement("attr", "name=version")
       .setTextContent(Integer.toString(1 + hosttarget.getVersion()));
 }
Example #5
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);
 }