"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "wormscan-1.6.1-src/net/websoup/wormscan/Resolver.java" of archive wormscan-1.6.1-src.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 package net.websoup.wormscan;
    2 
    3 /*
    4 *    This file is part of WormScan.
    5 *
    6 *    WormScan is free software; you can redistribute it and/or
    7 *    modify it under the terms of the GNU General Public License
    8 *    as published by the Free Software Foundation; either version 2
    9 *    of the License, or (at your option) any later version.
   10 *
   11 *    This program is distributed in the hope that it will be useful,
   12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 *    GNU General Public License for more details.
   15 *
   16 *    You should have received a copy of the GNU General Public License
   17 *    along with this program; if not, write to the Free Software
   18 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   19 */
   20 
   21 import java.util.Stack;
   22 
   23 import net.websoup.wormscan.AttackSource;
   24 import net.websoup.wormscan.Program;
   25 
   26 /**
   27  * Gathers AttackSource objects and resolves their hostnames.
   28  * The pool of AttackSource objects is shared between all Resolver threads.
   29  * Resolver threads will wait until AttackSource objects appear in the Stack.
   30  * Threads will exit only when areDone() method is called and no AttackSource objects
   31  * are waiting for resolution.
   32  * Copyright:    Copyright (c) 2001-2004 Andriy Rozeluk <arozeluk@websoup.net>
   33  * @author Andriy Rozeluk
   34  * @version 1.6.1
   35  */
   36 public class Resolver extends Thread {
   37   static int MAX_WAITING = 100;
   38   private static Stack sourceStack;
   39   private static boolean done = false;
   40 
   41   /**
   42    * Initialize our storage for AttackSource objects
   43    */
   44   static {
   45     sourceStack = new Stack();
   46   }
   47 
   48   /**
   49    * Signal that no more AttackSource objects will be added. Finish up!
   50    */
   51   public static void areDone(){
   52     done = true;
   53     synchronized( sourceStack ){
   54       sourceStack.notifyAll();
   55     }
   56   }
   57 
   58   /**
   59    * Add AttackSource object to the Stack for resolution
   60    */
   61   public static void addSource( AttackSource as ){
   62     if ( !Program.RESOLVE_DNS ) return;
   63     for (;;){
   64       synchronized(sourceStack){
   65         if ( MAX_WAITING <= 0 || sourceStack.size() < MAX_WAITING ){
   66           sourceStack.push( as );
   67           if ( Program.OUTPUT_LEVEL > 3 )
   68             System.out.println( "" + sourceStack.size() + " hostnames waiting to be resolved" );
   69           sourceStack.notifyAll();
   70           return;
   71         }
   72         else{
   73           try {
   74             sourceStack.wait();
   75           }
   76           catch( Exception e ){
   77             //ignore
   78           }
   79         }
   80       }
   81     }
   82   }
   83 
   84   /**
   85    * Go through stack, resolve hostnames of AttackSource objects that appear
   86    */
   87   public void run(){
   88     int lastcount = 0;
   89     while(!done || lastcount > 0){
   90       AttackSource as = null;
   91       synchronized( sourceStack ){
   92         lastcount = sourceStack.size();
   93         if ( lastcount > 0 ){
   94           as = (AttackSource)(sourceStack.pop());
   95           sourceStack.notifyAll();
   96         }
   97       }
   98       if ( as != null ){
   99         as.resolveHostnames();
  100       }
  101       else {
  102         synchronized( sourceStack ){
  103           if (!done ){
  104             try {
  105               sourceStack.wait();
  106             }
  107             catch( Exception e ){
  108               //ignore
  109             }
  110           }
  111         }
  112       }
  113     }
  114   }
  115 }