[SNMP4J] Code hangs on syncResponse.wait();

Oosten, Harry van harry.van.oosten at logica.com
Tue Aug 5 10:08:20 CEST 2008


Hi,

 

I managed to create an test class that's send a GETBULK request to an
configured switch and router. This goes well as long as I use the right
username password. 

 

Only when I change a username or password the request is ignored by the
device and the code hangs on the syncResponse.wait(); 

 

 

MyCode:

 

public class SNMPManager {

   

    private boolean needConfig = false;

   

    public static void main(String[] args) {

        SNMPManager snmpMan = new SNMPManager();

        System.out.println("Switch = up?
"+snmpMan.isResponding("192.168.16.122", "1AuthUser", "AuthPassword",
null)); //wrong username

        System.out.println(snmpMan.needConfig);

    }

    public boolean isResponding(String ipAddress, String userName,
String authPassword, String privPassword){

    List<String> OIDList = new LinkedList<String>();

    //OIDList.add("");

      PDU pdu;

        try {

            pdu = getPDU(ipAddress, userName, authPassword, privPassword
,OIDList);

            if (pdu != null)  {

                    System.out.println(pdu);

                    return true;      

            } else{

                return false;

            }

        } catch (IOException e) {

            e.printStackTrace();

            needConfig = true;

            return false;

        }

    }

    

    private PDU getPDU(String ipAddress, String userName, String
authPassword, String privPassword ,List<String> OIDList) throws
IOException {

        //Create transport.

        Address targetAddress =
GenericAddress.parse("udp:"+ipAddress+"/161");

        TransportMapping transport = new DefaultUdpTransportMapping();

        Snmp snmp = new Snmp(transport);

        

        USM usm = new USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()),0);

        SecurityModels.getInstance().addSecurityModel(usm);

        transport.listen();

        

        // add user to the USM

        if (privPassword != null)  {

            snmp.getUSM().addUser(new OctetString(userName), 

                                  //new
OctetString("80:00:13:70:c0:a8:01:0d"), 

                                  new UsmUser(new OctetString(userName),


                                              AuthMD5.ID, 

                                              new
OctetString(authPassword),

                                              PrivDES.ID,

                                              new
OctetString(privPassword)

                                              )); //Fill out!  

        } else {

            snmp.getUSM().addUser(new OctetString(userName), 

                               //new
OctetString("80:00:13:70:c0:a8:01:0d"), 

                               new UsmUser(new OctetString(userName), 

                                           AuthMD5.ID, 

                                           new
OctetString(authPassword),

                                           null,

                                           null)); 

        }

        

        //Create target.

        UserTarget target = createTarget(targetAddress, userName);

        

        //Create the PDU

        PDU pdu = createPDU(OIDList);

        

        //Send the PDU

        ResponseEvent responseEvent = snmp.send(pdu, target);

        //Exctract the response PDU (could be null if timed out)

        PDU responsePDU = responseEvent.getResponse();

        closeTransport(transport);

        return responsePDU;

    }

 

    /**

     * Used to create the PDU. 

     * Gets a list of Strings containing the OID's that needs to be
acquired.

     * @param OIDList String List

     * @return PDU

     */

    private PDU createPDU(List<String> OIDList) {

        PDU pdu = new ScopedPDU();

        for (String oid :OIDList ) {

            pdu.add(new VariableBinding(new OID(oid)));

        }

        pdu.setType(PDU.GETBULK);

        return pdu;

    }

 

    private UserTarget createTarget(Address targetAddress, String
userName) {

        UserTarget target = new UserTarget();

        target.setAddress(targetAddress);

        target.setRetries(0);

        target.setTimeout(5000);

        target.setVersion(SnmpConstants.version3);

        target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);

        target.setSecurityName(new OctetString(userName));

        return target;

    }

 

    private void closeTransport(TransportMapping transport) throws
IOException {

            transport.close();

    }

}

 

Where it goes wrong: (snmp.java)

 

public ResponseEvent send(PDU pdu, Target target,

                            TransportMapping transport) throws
IOException {

    if (!pdu.isConfirmedPdu()) {

      sendMessage(pdu, target, transport, null);

      return null;

    }

    if (timer == null) {

      createPendingTimer();

    }

    SyncResponseListener syncResponse = new SyncResponseListener();

    PendingRequest retryRequest = null;

    synchronized (syncResponse) {

      PduHandle handle = null;

      PendingRequest request =

          new PendingRequest(syncResponse, target, pdu, target,
transport);

      handle = sendMessage(pdu, target, transport, request);

      try {

        syncResponse.wait();

        retryRequest = (PendingRequest) pendingRequests.remove(handle);

        if (logger.isDebugEnabled()) {

          logger.debug("Removed pending request with handle: "+handle);

        }

        request.setFinished();

        request.cancel();

      }

      catch (InterruptedException iex) {

        logger.warn(iex);

        // ignore

      }

    }

    if (retryRequest != null) {

      retryRequest.setFinished();

      retryRequest.cancel();

    }

    return syncResponse.response;

  }

 

  private synchronized void createPendingTimer() {

    if (timer == null) {

      timer = SNMP4JSettings.getTimerFactory().createTimer();

    }

  }

 

Hope you can help! 

 

 

Met vriendelijke groet / Kind Regards,

 

ing. Harry van Oosten

 

Software engineer

_________________________________________

 





 



This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.




More information about the SNMP4J mailing list