package net.websoup.wormscan; /* * This file is part of WormScan. * * WormScan is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.util.Stack; import net.websoup.wormscan.AttackSource; import net.websoup.wormscan.Program; /** * Gathers AttackSource objects and resolves their hostnames. * The pool of AttackSource objects is shared between all Resolver threads. * Resolver threads will wait until AttackSource objects appear in the Stack. * Threads will exit only when areDone() method is called and no AttackSource objects * are waiting for resolution. * Copyright: Copyright (c) 2001-2004 Andriy Rozeluk * @author Andriy Rozeluk * @version 1.6.1 */ public class Resolver extends Thread { static int MAX_WAITING = 100; private static Stack sourceStack; private static boolean done = false; /** * Initialize our storage for AttackSource objects */ static { sourceStack = new Stack(); } /** * Signal that no more AttackSource objects will be added. Finish up! */ public static void areDone(){ done = true; synchronized( sourceStack ){ sourceStack.notifyAll(); } } /** * Add AttackSource object to the Stack for resolution */ public static void addSource( AttackSource as ){ if ( !Program.RESOLVE_DNS ) return; for (;;){ synchronized(sourceStack){ if ( MAX_WAITING <= 0 || sourceStack.size() < MAX_WAITING ){ sourceStack.push( as ); if ( Program.OUTPUT_LEVEL > 3 ) System.out.println( "" + sourceStack.size() + " hostnames waiting to be resolved" ); sourceStack.notifyAll(); return; } else{ try { sourceStack.wait(); } catch( Exception e ){ //ignore } } } } } /** * Go through stack, resolve hostnames of AttackSource objects that appear */ public void run(){ int lastcount = 0; while(!done || lastcount > 0){ AttackSource as = null; synchronized( sourceStack ){ lastcount = sourceStack.size(); if ( lastcount > 0 ){ as = (AttackSource)(sourceStack.pop()); sourceStack.notifyAll(); } } if ( as != null ){ as.resolveHostnames(); } else { synchronized( sourceStack ){ if (!done ){ try { sourceStack.wait(); } catch( Exception e ){ //ignore } } } } } } }