"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "wormscan-1.6.1-src/net/websoup/wormscan/Sorter.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.Arrays;
   22 import java.util.Comparator;
   23 import java.util.Date;
   24 import java.util.Collection;
   25 
   26 import net.websoup.wormscan.AttackSource;
   27 
   28 /**
   29  * Sort Attacks by specified parameters
   30  * Copyright:    Copyright (c) 2001-2004 Andriy Rozeluk <arozeluk@websoup.net>
   31  * @author Andriy Rozeluk
   32  * @version 1.6.1
   33  */
   34 public class Sorter extends Thread {
   35   private boolean finished = false;
   36   private Object[] results;
   37   private Collection source;
   38   private Comparator compare;
   39   private String sortMethodName;
   40 
   41   /**
   42    * Sort by IP address of attacking host
   43    */
   44   public static final Comparator SORT_IP = new Comparator() {
   45     public int compare( Object a, Object b ){
   46       if ( a instanceof Attack && b instanceof Attack ){
   47         long ip1 = ((Attack)a).getSourceOfAttack().getIntegerIP();
   48         long ip2 = ((Attack)b).getSourceOfAttack().getIntegerIP();
   49 
   50         if ( ip1 < 0 && ip2 > 0 ){
   51          return 1;
   52         }
   53         else if ( ip1 > 0 && ip2 < 0 ){
   54          return -1;
   55         }
   56         else if ( ip1 < 0 && ip2 < 0 ){
   57          boolean unres1 = ((Attack)a).getSourceOfAttack().isHostnameUnresolved();
   58          boolean unres2 = ((Attack)b).getSourceOfAttack().isHostnameUnresolved();
   59 
   60          if ( unres1 && unres2 ){
   61            return SORT_DATE.compare( a, b );
   62          }
   63          else {
   64            return SORT_HOSTNAME.compare( a, b );
   65          }
   66         }
   67         else {
   68           long c = ip1 - ip2;
   69           if ( c == 0 ){
   70             return SORT_DATE.compare(a, b);
   71           }
   72           else if( c > 0 ){
   73            return 1;
   74           }
   75           else{
   76            return -1;
   77           }
   78         }
   79       }
   80       else {
   81         return 0;
   82       }
   83     }
   84   };
   85 
   86   /**
   87    * Sort by Date of attack
   88    */
   89   public static final Comparator SORT_DATE = new Comparator() {
   90     public int compare( Object a, Object b ){
   91       if ( a instanceof Attack && b instanceof Attack ){
   92         Date ip1 = ((Attack)a).getDateOfAttack();
   93         Date ip2 = ((Attack)b).getDateOfAttack();
   94 
   95         int c = ip1.compareTo(ip2);
   96         if ( c == 0 ){
   97           return a.hashCode() - b.hashCode();
   98         }
   99         return c;
  100       }
  101       else {
  102         return 0;
  103       }
  104     }
  105   };
  106 
  107   /**
  108    * Sort by name of worm
  109    */
  110   public static final Comparator SORT_WORM = new Comparator() {
  111     public int compare( Object a, Object b ){
  112       if ( a instanceof Attack && b instanceof Attack ){
  113         String ip1 = ((Attack)a).getAttackingWorm().getName();
  114         String ip2 = ((Attack)b).getAttackingWorm().getName();
  115         int c = ip1.compareTo(ip2);
  116         if ( c == 0 ){
  117           return SORT_NUMATTACKS.compare( a, b );
  118         }
  119         return c;
  120       }
  121       else {
  122         return 0;
  123       }
  124     }
  125   };
  126 
  127   /**
  128    * Sort by number of attacks by host
  129    */
  130   public static final Comparator SORT_NUMATTACKS = new Comparator() {
  131     public int compare( Object a, Object b ){
  132       if ( a instanceof Attack && b instanceof Attack ){
  133         int ip1 = ((Attack)a).getSourceOfAttack().getAttackCount();
  134         int ip2 = ((Attack)b).getSourceOfAttack().getAttackCount();
  135 
  136         int c = ip1 - ip2;
  137         if ( c == 0 ){
  138           return SORT_IP.compare(a, b);
  139         }
  140         return c;
  141       }
  142       else {
  143         return 0;
  144       }
  145     }
  146   };
  147 
  148   /**
  149    * Sort by hostname of attacker
  150    */
  151   public static final Comparator SORT_HOSTNAME = new Comparator() {
  152 
  153     public int compare( Object a, Object b ){
  154       if ( a instanceof Attack && b instanceof Attack ){
  155         AttackSource source1 = ((Attack)a).getSourceOfAttack();
  156         AttackSource source2 = ((Attack)b).getSourceOfAttack();
  157 
  158         boolean unres1 = source1.isHostnameUnresolved();
  159         boolean unres2 = source2.isHostnameUnresolved();
  160 
  161         if ( unres1 || unres2 ){
  162           if ( unres1 && source1.getIntegerIP() > 0 && unres2 && source2.getIntegerIP() > 0 ){
  163             return SORT_IP.compare( a, b );
  164           }
  165           else if ( unres1 && source1.getIntegerIP() > 0 ){
  166             return 1;
  167           }
  168           else if ( unres2 && source2.getIntegerIP() > 0 ){
  169             return -1;
  170           }
  171         }
  172 
  173         String[] ip1 = source1.getReversedHostnames();
  174         String[] ip2 = source2.getReversedHostnames();
  175 
  176         if ( ip1 != null && ip1.length > 0 && ip2 != null && ip2.length > 0 ){
  177           int c = ip1[0].compareTo( ip2[0] );
  178           if ( c == 0 ) return SORT_DATE.compare( a, b );
  179           return c;
  180         }
  181         else if ( (ip1 == null || ip1.length <= 0) && (ip2 != null && ip2.length > 0) ){
  182           return -1;
  183         }
  184         else if ( (ip2 == null || ip2.length <= 0) && (ip1 != null && ip1.length > 0) ){
  185           return 1;
  186         }
  187         else {
  188           return SORT_DATE.compare( a, b );
  189         }
  190       }
  191       else {
  192         return 0;
  193       }
  194     }
  195   };
  196 
  197   /**
  198    * Initialize a sort thread for specified order
  199    */
  200   public Sorter( Collection c, Comparator sortOrder, String sortMethodName ){
  201     source = c;
  202     compare = sortOrder;
  203     this.sortMethodName = sortMethodName;
  204   }
  205 
  206   /**
  207    * Begin sorting
  208    */
  209   public void run(){
  210     Object[] working = source.toArray();
  211     Arrays.sort( working, compare );
  212 
  213     results = working;
  214     finished = true;
  215   }
  216 
  217   /**
  218    * Utility method to reverse an array. This is the easy way to get reversed
  219    * sorts.
  220    */
  221   public static Object[] reverseArray( Object[] array ){
  222     int len = array.length;
  223     for ( int i = 0; i < (len >> 1); i++ ){
  224       Object temp = array[i];
  225       array[i] = array[ len - i - 1 ];
  226       array[ len - i - 1 ] = temp;
  227     }
  228     return array;
  229   }
  230 
  231   /**
  232    * Return the sorted array of objects, or null if we're not done sorting yet.
  233    */
  234   public Object[] getResults(){
  235     if ( !finished ) return null;
  236     return results;
  237   }
  238 
  239   /**
  240    * Is sorting complete yet? Maybe it's easier to just check if the Thread's
  241    * stopped executing.
  242    */
  243   public boolean isFinished(){
  244     return finished;
  245   }
  246 
  247   public String getSortMethodName(){
  248     return sortMethodName;
  249   }
  250 }