001:
002:
003:
004:
005:
006:
007: public class PingSweepStart extends Thread implements Constants {
008:
009: private String ip_range_beginning;
010: private String ip_range_end;
011: private int thread_count;
012: private PingThread ping_threads[];
013: private StringBuffer total_result = new StringBuffer();
014:
015:
016:
017:
018:
019:
020:
021:
022: public PingSweepStart(String ip_range_beginnig,
023: String ip_range_end) {
024: this.ip_range_beginning =
025: BasicIPTools.find_lesser_address(
026: ip_range_beginnig,ip_range_end);
027: this.ip_range_end =
028: BasicIPTools.find_greater_address(
029: ip_range_beginnig,ip_range_end);
030: thread_count =
031: BasicIPTools.get_required_host_count(
032: this.ip_range_beginning,this.ip_range_end);
033: calculate_required_thread();
034: ping_threads = new PingThread[thread_count];
035: }
036:
037:
038:
039:
040:
041: private void calculate_required_thread()
042: {
043: if( thread_count%PING_PER_THREAD!=0 ) {
044: thread_count /= PING_PER_THREAD;
045: thread_count++;
046: }
047: else
048: thread_count /= PING_PER_THREAD;
049: }
050:
051: public static void main(String arg[]) throws InterruptedException {
052: PingSweepStart a = new PingSweepStart("10.0.0.1","10.0.0.45");
053: a.start();
054: a.join();
055: System.out.println("Bitti");
056: PingSweepStart b = new PingSweepStart("10.0.0.46","10.0.0.75");
057: b.start();
058: b.join();
059:
060: }
061:
062:
063:
064:
065: public void run()
066: {
067: startSweeping();
068: do {
069: try {
070: sleep(1000);
071: } catch (InterruptedException e) {
072: e.printStackTrace();
073: }
074: } while(!check_if_sweep_completed());
075: System.out.print(get_result_of_scan());
076: }
077:
078:
079:
080:
081:
082:
083: public StringBuffer get_result_of_scan()
084: {
085: return total_result;
086: }
087:
088:
089:
090:
091:
092:
093: private boolean check_if_sweep_completed()
094: {
095: for( int i=0;i<thread_count-1;i++ )
096: if( ping_threads[i].isAlive() ) return false;
097: return true;
098: }
099:
100:
101:
102:
103: private void startSweeping() {
104: int i=0;
105: String start_ip,stop_ip;
106: start_ip = ip_range_beginning;
107: stop_ip = BasicIPTools.increase_IP(start_ip,PING_PER_THREAD);
108: while( i < thread_count ) {
109:
110: if( !if_exceeds(start_ip,ip_range_end,PING_PER_THREAD ) ) {
111: ping_threads[i] = new PingThread(start_ip,stop_ip,total_result);
112: ping_threads[i].start();
113: stop_ip = BasicIPTools.increase_IP(stop_ip);
114: start_ip = stop_ip;
115: stop_ip = BasicIPTools.increase_IP(stop_ip,PING_PER_THREAD);
116: i++;
117: }
118: else {
119: ping_threads[i] = new PingThread(start_ip,ip_range_end,total_result);
120:
121: ping_threads[i].start();
122: break;
123: }
124: }
125: }
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136: private boolean if_exceeds(String start_ip, String end_ip,
137: int Step)
138: {
139: String after_addition =
140: BasicIPTools.increase_IP(start_ip,Step);
141: if( after_addition.equals(
142: BasicIPTools.find_greater_address(
143: after_addition,end_ip)))
144: return true;
145: return false;
146: }
147: }