001: import javax.swing.*;
002: 
003: /**
004:  * Girilecek IP araligina gore, tarama baslatir.
005:  * @author aatay EB
006:  * @version 1.1
007:  */
008: 
009: public class ScanIPRange implements Constants{
010:         private String ip_range_beginning;
011:         private String ip_range_end;
012:         private int loop_count;
013: 
014:         /**
015:          * Default Constructor. Baslangic ve bitis 
016:          * ip'lerine gore tarama islemine hazir
017:          * hale getirilir.
018:          * @param ip_range_beginning
019:          * @param ip_range_end
020:          */
021:         public ScanIPRange(String ip_range_beginning,
022:                            String ip_range_end)
023:         {
024:             this.ip_range_beginning = ip_range_beginning;
025:             this.ip_range_end = ip_range_end;
026:             loop_count = BasicIPTools.get_required_host_count(
027:                 this.ip_range_beginning,this.ip_range_end);
028:             calculate_required_loop();
029:         }
030:         /**
031:          * Kullaniciyla iletisime gecilen ana fonksiyondur.
032:          * @param arg
033:          * @throws InterruptedException
034:          */
035:         public static void main(String arg[]) throws InterruptedException {
036:             String first_ip = JOptionPane.showInputDialog("IP araligin ilk degerini girin:");
037:             String last_ip = JOptionPane.showInputDialog("IP araligin son degerini girin");
038:             //ScanIPRange a = new ScanIPRange("192.168.1.1","192.168.1.255");
039:             //ScanIPRange a = new ScanIPRange(arg[0],arg[1]);
040:             Timer t = new Timer();
041:             t.start();
042:             ScanIPRange a = new ScanIPRange(first_ip,last_ip);
043:             a.start_scan();
044:             t.end();
045:             System.out.println("\n\n**************************");
046:             System.out.println("TOPLAM TARANAN ARALIK: "+first_ip+" - "+last_ip);
047:             System.out.println("Harcanan Zaman:\t" + t.duration()+" (ms)");
048:             System.out.println("**************************");
049:         }
050:         
051:         /**
052:          * PingSweepStart sinifindan nesneler yaratarak
053:          * tarama islemini baslatan ana fonksiyondur.
054:          * @throws InterruptedException
055:          */
056:         public void start_scan() throws InterruptedException {
057:           int i=0;
058:           String start_ip,stop_ip;
059:           start_ip = ip_range_beginning;
060:           stop_ip = BasicIPTools.increase_IP(start_ip,MAX_THREAD_COUNT);
061:           while( i < loop_count ) {
062:               if( !if_exceeds(start_ip,ip_range_end,MAX_THREAD_COUNT) ) {
063:                 PingSweepStart sweep = new PingSweepStart(start_ip,stop_ip);
064:                   sweep.start();
065:                   sweep.join();
066:                   sweep = null;
067:                   //System.out.println("ANA PARCA :"+start_ip+ " - "+stop_ip);
068:                   stop_ip = BasicIPTools.increase_IP(stop_ip);
069:                   start_ip = stop_ip;
070:                   stop_ip = BasicIPTools.increase_IP(stop_ip,MAX_THREAD_COUNT);
071:                 i++;
072:               }
073:               else {
074:                   PingSweepStart sweep = new PingSweepStart(start_ip,ip_range_end);
075:                   sweep.start();
076:                   sweep.join();
077:                   sweep = null;
078:                   //System.out.println("ANA PARCA :"+start_ip+ " - "+ip_range_end);
079:                   break;
080:               }
081:               System.gc();
082:           }
083:       }
084: 
085:      /**
086:       * Eger taranacak alani bir sonraki adimda asiliyorsa
087:       * buna gore sonlandirma islemi yapilmasi gerektigini
088:       * belirtir
089:       * @param start_ip - Baslangic IP
090:       * @param end_ip - Bitis IP
091:       * @param Step - Gidilecek adim sayisi
092:       * @return true ise, asilir. false asilmaz.
093:       */
094:     private boolean if_exceeds(String start_ip, String end_ip,
095:                                int Step)
096:       {
097:           String after_addition =
098:               BasicIPTools.increase_IP(start_ip,Step);
099:           if( after_addition.equals(
100:                   BasicIPTools.find_greater_address(
101:                           after_addition,end_ip)))
102:               return true;
103:           return false;
104:       }
105: 
106:     /**
107:      * Yaratilmasi gereken PingSweepStart nesne
108:      * sayisini bulmaya yarar.
109:      */
110:     private void calculate_required_loop()
111:       {
112:           if( loop_count%MAX_THREAD_COUNT!=0 ) {
113:               loop_count /= MAX_THREAD_COUNT;
114:               loop_count++;
115:           }
116:           else
117:               loop_count /= MAX_THREAD_COUNT;
118:       }
119: }
120: