/**
  * Closes the socket and stops the listener thread.
  *
  * @throws IOException
  */
 public void close() throws IOException {
   boolean interrupted = false;
   WorkerTask l = listener;
   if (l != null) {
     l.terminate();
     l.interrupt();
     if (socketTimeout > 0) {
       try {
         l.join();
       } catch (InterruptedException ex) {
         interrupted = true;
         logger.warn(ex);
       }
     }
     listener = null;
   }
   DatagramSocket closingSocket = socket;
   if ((closingSocket != null) && (!closingSocket.isClosed())) {
     closingSocket.close();
   }
   socket = null;
   if (interrupted) {
     Thread.currentThread().interrupt();
   }
 }
 /** Closes all open sockets and stops the internal server thread that processes messages. */
 public void close() {
   ServerThread st = server;
   if (st != null) {
     st.close();
     try {
       st.join();
     } catch (InterruptedException ex) {
       logger.warn(ex);
     }
     server = null;
     for (Iterator it = sockets.values().iterator(); it.hasNext(); ) {
       SocketEntry entry = (SocketEntry) it.next();
       try {
         synchronized (entry) {
           entry.getSocket().close();
         }
         logger.debug("Socket to " + entry.getPeerAddress() + " closed");
       } catch (IOException iox) {
         // ingore
         logger.debug(iox);
       }
     }
     if (socketCleaner != null) {
       socketCleaner.cancel();
     }
     socketCleaner = null;
   }
 }
 /**
  * Add the default SecurityProtocols.
  *
  * <p>The names of the SecurityProtocols to add are read from a properties file.
  *
  * @throws InternalError if the properties file cannot be opened/read.
  */
 public synchronized void addDefaultProtocols() {
   if (SNMP4JSettings.isExtensibilityEnabled()) {
     String secProtocols =
         System.getProperty(SECURITY_PROTOCOLS_PROPERTIES, SECURITY_PROTOCOLS_PROPERTIES_DEFAULT);
     InputStream is = SecurityProtocols.class.getResourceAsStream(secProtocols);
     if (is == null) {
       throw new InternalError("Could not read '" + secProtocols + "' from classpath!");
     }
     Properties props = new Properties();
     try {
       props.load(is);
       for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) {
         String className = en.nextElement().toString();
         try {
           Class c = Class.forName(className);
           Object proto = c.newInstance();
           if (proto instanceof AuthenticationProtocol) {
             addAuthenticationProtocol((AuthenticationProtocol) proto);
           } else if (proto instanceof PrivacyProtocol) {
             addPrivacyProtocol((PrivacyProtocol) proto);
           } else {
             logger.error(
                 "Failed to register security protocol because it does "
                     + "not implement required interfaces: "
                     + className);
           }
         } catch (Exception cnfe) {
           logger.error(cnfe);
           throw new InternalError(cnfe.toString());
         }
       }
     } catch (IOException iox) {
       String txt = "Could not read '" + secProtocols + "': " + iox.getMessage();
       logger.error(txt);
       throw new InternalError(txt);
     } finally {
       try {
         is.close();
       } catch (IOException ex) {
         // ignore
         logger.warn(ex);
       }
     }
   } else {
     addAuthenticationProtocol(new AuthMD5());
     addAuthenticationProtocol(new AuthSHA());
     addPrivacyProtocol(new PrivDES());
     addPrivacyProtocol(new PrivAES128());
     addPrivacyProtocol(new PrivAES192());
     addPrivacyProtocol(new PrivAES256());
   }
 }
Exemple #4
0
  /** Default constructor, initializes the salt to a random value. */
  protected Salt() {
    byte[] rnd = new byte[8];

    try {
      SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
      sr.nextBytes(rnd);
    } catch (NoSuchAlgorithmException nsae) {
      logger.warn("Could not use SecureRandom. Using Random instead.");
      Random r = new Random();
      r.nextBytes(rnd);
    }

    salt = rnd[0];

    for (int i = 0; i < 7; i++) {
      salt = (salt * 256) + ((int) rnd[i]) + 128;
    }
    if (logger.isDebugEnabled() == true) {
      logger.debug("Initialized Salt to " + Long.toHexString(salt) + ".");
    }
  }