home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 July / july_2000.iso / Site Building / port scanner / port.java < prev    next >
Encoding:
Java Source  |  2000-06-07  |  3.6 KB  |  124 lines

  1. /*
  2.  * Java Port Scanner
  3.  *
  4.  * Copyright 2000 Matteo Baccan <mbaccan@planetisa.com>
  5.  * www - http://www.infomedia.it/artic/Baccan
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
  20.  * their web site at http://www.gnu.org/).
  21.  *
  22.  */
  23.  
  24. import java.net.*;
  25. import java.util.*;
  26. import java.io.*;
  27.  
  28. public class port {
  29.    private static Vector aServices = new Vector();
  30.  
  31.    public port(){}
  32.  
  33.    public void setFile( String cFile ){
  34.       try{
  35.          RandomAccessFile RAF;
  36.          RAF = new RandomAccessFile( cFile, "r" );
  37.  
  38.          String cLine = "";
  39.          cLine = RAF.readLine();
  40.          while( cLine!=null ){
  41.             aServices.addElement( new subService( cLine ) );
  42.             cLine = RAF.readLine();
  43.          }
  44.          RAF.close();
  45.       } catch ( Throwable e) {
  46.          System.out.println( "\nError during opening of " +cFile +" : " +e.toString() );
  47.          e.printStackTrace();
  48.       }
  49.    }
  50.    public void setRange( int nPortFrom, int nPortTo ){
  51.       for( int nPort=nPortFrom; nPort<=nPortTo; nPort++ ){
  52.          aServices.addElement( new subService( "+," +nPort +",???,???,," ) );
  53.       }
  54.    }
  55.                /*
  56.    static int toNumber( String cService ){
  57.       int nRet = 0;
  58.       int nPos = 0;
  59.       while( nPos<aServices.size() ){
  60.          if( ((subService)aServices.elementAt(nPos)).equals( cService ) ){
  61.             nRet = aServices[nPos].nPort;
  62.             break;
  63.          }
  64.          nPos++;
  65.       }
  66.       return nRet;
  67.    }
  68.  
  69.    static String toString( int nService ){
  70.       String cRet = new Integer( nService ).toString();
  71.       int nPos = 0;
  72.       while( nPos<aServices.length ){
  73.          if( aServices[nPos].equals( nService ) ){
  74.             cRet = aServices[nPos].cDesc;
  75.             break;
  76.          }
  77.          nPos++;
  78.       }
  79.       return cRet;
  80.    }
  81.                  */
  82.    static int length() {
  83.       return (aServices.size());
  84.    }
  85.  
  86.    static int getPort( int nPos ) {
  87.       return ((subService)aServices.elementAt(nPos)).nPort;
  88.    }
  89.    static String getDesc( int nPos ) {
  90.       return ((subService)aServices.elementAt(nPos)).cShortDesc +" - " +
  91.              ((subService)aServices.elementAt(nPos)).cLongDesc;
  92.    }
  93. }
  94.  
  95. class subService {
  96.    public boolean toScan = false;
  97.    public int     nPort;
  98.    public String  cShortDesc;
  99.    public String  cLongDesc;
  100.  
  101.    public subService( String cService ){
  102.       StringTokenizer token = new StringTokenizer( cService, "," );
  103.  
  104.       if( token.hasMoreTokens() ){
  105.          toScan = token.nextToken().equalsIgnoreCase("+");
  106.       }
  107.       if( token.hasMoreTokens() ){
  108.          nPort = Integer.parseInt( token.nextToken() );
  109.       }
  110.       if( token.hasMoreTokens() ){
  111.          cShortDesc = token.nextToken();
  112.       }
  113.       if( token.hasMoreTokens() ){
  114.          cLongDesc = token.nextToken();
  115.       }
  116.    }
  117.    public boolean equals( String cService ){
  118.       return cLongDesc.equalsIgnoreCase( cService );
  119.    }
  120.    public boolean equals( int nService ){
  121.       return (nPort==nService);
  122.    }
  123. }
  124.