Skip to content

Commit

Permalink
The network monitor class now uses fixed thread pools to increase
Browse files Browse the repository at this point in the history
discovery performance and avoid an uncontrolled amount of threads to be
simultaneously started.
  • Loading branch information
evilsocket committed Nov 6, 2012
1 parent 6e420e9 commit 3ed7d0d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/it/evilsocket/dsploit/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public void run() {
}
});
}

MainActivity.this.runOnUiThread( new Runnable(){
@Override
public void run()
Expand Down
79 changes: 68 additions & 11 deletions src/it/evilsocket/dsploit/net/NetworkDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -73,13 +76,17 @@ public class NetworkDiscovery extends Thread

private class ArpReader extends Thread
{
private static final int RESOLVER_THREAD_POOL_SIZE = 25;

private ThreadPoolExecutor mExecutor = null;
private boolean mStopped = true;
private HashMap<String,String> mNetBiosMap = null;

public ArpReader() {
super("ArpReader");

mNetBiosMap = new HashMap<String,String>();
mExecutor = ( ThreadPoolExecutor )Executors.newFixedThreadPool( RESOLVER_THREAD_POOL_SIZE );
}

public synchronized void addNetBiosName( String address, String name ) {
Expand Down Expand Up @@ -140,7 +147,8 @@ public void run() {

if( name == null )
{
new NBResolver( address ).start();
mExecutor.execute( new NBResolver( address ) );

if( target.isRouter() == false )
{
// attempt DNS resolution
Expand Down Expand Up @@ -187,6 +195,16 @@ else if( name != null )

public synchronized void exit() {
mStopped = true;
try
{
mExecutor.shutdown();
mExecutor.awaitTermination( 30, TimeUnit.SECONDS );
mExecutor.shutdownNow();
}
catch( Exception e )
{

}
}
}

Expand Down Expand Up @@ -275,9 +293,43 @@ public void run() {

private class UdpProber extends Thread
{
private boolean mStopped = true;
private Network mNetwork = null;
private static final int PROBER_THREAD_POOL_SIZE = 25;

private class SingleProber extends Thread
{
private InetAddress mAddress = null;

public SingleProber( InetAddress address ) {
mAddress = address;
}

@Override
public void run() {
try
{
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket( NETBIOS_REQUEST, NETBIOS_REQUEST.length, mAddress, NETBIOS_UDP_PORT );

socket.setSoTimeout( 200 );
socket.send( packet );

socket.close();
}
catch( Exception e )
{

}
}
}

private ThreadPoolExecutor mExecutor = null;
private boolean mStopped = true;
private Network mNetwork = null;

public UdpProber( ){
mExecutor = ( ThreadPoolExecutor )Executors.newFixedThreadPool( PROBER_THREAD_POOL_SIZE );
}

@Override
public void run() {
Log.d( TAG, "UdpProber started ..." );
Expand Down Expand Up @@ -306,14 +358,9 @@ public void run() {
// rescanning the gateway could cause an issue when the gateway itself has multiple interfaces ( LAN, WAN ... )
if( current.equals( mNetwork.getGatewayAddress() ) == false && current.equals( mNetwork.getLocalAddress() ) == false )
{
InetAddress address = current.toInetAddress();
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket( NETBIOS_REQUEST, NETBIOS_REQUEST.length, address, NETBIOS_UDP_PORT );

socket.setSoTimeout( 200 );
socket.send( packet );

socket.close();
InetAddress address = current.toInetAddress();

mExecutor.execute( new SingleProber( address ) );
}
}

Expand All @@ -328,6 +375,16 @@ public void run() {

public synchronized void exit() {
mStopped = true;
try
{
mExecutor.shutdown();
mExecutor.awaitTermination( 30, TimeUnit.SECONDS );
mExecutor.shutdownNow();
}
catch( Exception e )
{

}
}
}

Expand Down

0 comments on commit 3ed7d0d

Please sign in to comment.