001/*
002 *  $URL: svn://svn.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/portscanner/PortScanner.java $
003 *  $Author: tgutwin $
004 *  $Revision: 1224 $
005 *  $Date: 2018-03-02 22:18:54 -0800 (Fri, 02 Mar 2018) $
006 */
007/*
008 *
009 *  Written by Tom Gutwin - WebARTS Design.
010 *  Copyright (C) 2012 WebARTS Design, North Vancouver Canada
011 *  http://www.webarts.bc.ca
012 *
013 *  This program is free software; you can redistribute it and/or modify
014 *  it under the terms of the GNU General Public License as published by
015 *  the Free Software Foundation; either version 2 of the License, or
016 *  (at your option) any later version.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without_ even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027
028package ca.bc.webarts.tools.portscanner;
029
030import java.net.InetSocketAddress ;
031import java.net.InetAddress;
032import java.net.Socket;
033import java.net.UnknownHostException;
034import java.util.Observable;
035import java.io.IOException;
036
037/**
038 * An {@link Observable} threaded class that scans a specified range of IP ports for access and notifies the Observer when access is available.
039 * The user can specify the target IP addres and the port range. Once you start this thread register it with an Observer.<br />
040 * see {@link PortScannerApp} for a basic implementation.
041 **/
042 public class PortScanner extends Observable implements Runnable
043{
044  /** The class field for the IP address to target this scan. **/
045  private String target;
046  /**the starting port to start the scan [default=0]. **/
047  private int fromPort = 0;   //default start port number
048  /** sets the last port in the scan range [default=65535]. **/
049  private int toPort = 65535 ; //default end port number
050
051  /** Class var indicating when this Observable class thread has completed **/
052  public boolean completed = false;
053
054
055  /** sets the starting port to start the scan. **/
056  public void setFromPort(int fromPort)
057  {
058      this.fromPort = fromPort;
059  }
060
061
062  /** sets the last port in the scan range. **/
063  public void setToPort(int toPort)
064  {
065      this.toPort = toPort;
066  }
067
068
069  /** sets IP address to scan. NO error checking is done on the passed in target.
070   *
071   * @param target is the String representation of the IP address to scan.**/
072  public void setTarget(String target)
073  {
074      this.target = target;
075  }
076
077
078  /** Thread Implementation. **/
079  public void run()
080  {
081    InetSocketAddress inetAddr = null;
082    InetAddress addr = null;
083    //String hostName = inetAddress.getHostName();
084    for (int port = fromPort; port <= toPort; port++)
085    {
086        try
087        {
088            addr = InetAddress.getByName(target);
089            inetAddr = new InetSocketAddress( addr, port);
090            Socket socket = new Socket();
091            socket.setSoTimeout(10);
092            socket.connect(inetAddr);
093            socket.close();
094            setChanged();
095            notifyObservers(inetAddr);
096            //notifyObservers(new Integer(port));
097        }
098       catch (UnknownHostException e)
099       {
100           e.printStackTrace();
101       }
102        catch (java.net.SocketException sockE)
103        {
104            // ignore... means it did not see an open port
105            //System.out.print(".");
106        }
107        catch (IOException e)
108        {
109            // ignore... means it did not see an open port
110            //System.out.print("-");
111        }
112    }
113    setChanged();
114    notifyObservers(""+fromPort + "-" + toPort);
115     //System.out.println("\n Scan ports  "+ fromPort + "-" + toPort +"  Complete.");
116    completed = true;
117  }
118}