// process request and analyze content with mib file
  public void processPdu(CommandResponderEvent cre) {

    try {
      // create the SnmpMsg received
      MsgSnmp msg = new MsgSnmp();
      msg.setPdu(cre.getPDU());
      // TODO: how to know the version here to set communityTarget or UserTarget
      AbstractTarget target = null;
      if ((cre.getMessageProcessingModel() == MessageProcessingModel.MPv1)
          || (cre.getMessageProcessingModel() == MessageProcessingModel.MPv2c)) {
        target = new CommunityTarget();
        ((CommunityTarget) target)
            .setCommunity(new OctetString(cre.getStateReference().getSecurityName()));
      } else // TODO: manage of snmpV3 not done
      target = new UserTarget();

      if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv1) {
        target.setVersion(SnmpConstants.version1);
      } else if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv2c) {
        target.setVersion(SnmpConstants.version2c);
      } else if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv3) {
        target.setVersion(SnmpConstants.version3);
      }

      target.setAddress(cre.getPeerAddress());
      msg.setTarget((AbstractTarget) target);
      UdpAddress add = (UdpAddress) cre.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
 private void createSession() throws IOException {
   if (snmp == null) {
     Address address = GenericAddress.parse(deviceAddress);
     AbstractTransportMapping transport;
     if (address instanceof TlsAddress) {
       transport = new TLSTM();
     } else if (address instanceof TcpAddress) {
       transport = new DefaultTcpTransportMapping();
     } else {
       transport = new DefaultUdpTransportMapping();
     }
     // Could save some CPU cycles:
     transport.setAsyncMsgProcessingSupported(false);
     snmp = new Snmp(transport);
     snmp.listen();
     if (getSnmpVersion() == SnmpConstants.version3) {
       snmp.getUSM()
           .addUser(
               new OctetString("user"),
               new UsmUser(
                   new OctetString("user"),
                   AuthMD5.ID,
                   new OctetString("user"),
                   AuthMD5.ID,
                   null));
       // create the target
       target = new UserTarget();
       target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
       target.setSecurityName(new OctetString("user"));
     } else {
       // create the target
       target = new CommunityTarget();
       ((CommunityTarget) target).setCommunity(new OctetString(configuration.getReadCommunity()));
     }
     target.setAddress(address);
     target.setVersion(getSnmpVersion());
     target.setRetries(retries);
     target.setTimeout(timeout);
   }
 }