• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

ServiceNames.java

Go to the documentation of this file.
00001 
00002 import java.io.BufferedReader;
00003 import java.io.FileInputStream;
00004 import java.io.FileNotFoundException;
00005 import java.io.IOException;
00006 import java.io.InputStream;
00007 import java.io.InputStreamReader;
00008 import java.util.Hashtable;
00009 
00010 /**
00011  *  Encapsulates lookup database for TCP and UDP service names.
00012  *  
00013  *  @author Administrator
00014  */
00015 public class ServiceNames 
00016 {
00017     private Hashtable<String,String> hash;
00018     
00019     /**
00020      *  Creates instance from text file containing description of services 
00021      *  where each line describes one service, and is of the form:<pre>
00022      * 
00023      *   service-name  port/protocol  [aliases ...]   [# comment]
00024      *  
00025      *  </pre>The format is compatible with Unix' <code>/etc/services</code>. 
00026      */
00027     ServiceNames( String filename )
00028     {
00029         this.hash = new Hashtable<String,String> ();
00030 
00031         try
00032         {
00033             InputStream is = null;
00034             
00035             /* Read first local file
00036              */
00037             try {
00038                 is = new FileInputStream( filename );
00039                 System.out.println( "\nWarning: Loading local services.txt..." );
00040             } catch ( FileNotFoundException e2 ) {
00041                 /* ignored */
00042             }
00043 
00044             /* then fail back to resource from the JAR
00045              */
00046             if ( is == null ) {
00047                 is = getClass().getResourceAsStream( filename );
00048             }
00049 
00050             if ( is == null ) {
00051                 System.out.println( "\nWarning: Could not find services.txt..." );
00052                 return;
00053             }
00054 
00055             BufferedReader in = new BufferedReader( new InputStreamReader( is ) );
00056             
00057             for( String line = in.readLine (); line != null; line = in.readLine () ) 
00058             {
00059                 /* Split string into words, removing all leading, trailing 
00060                  * and superfluous (betwewn words) white-spaces
00061                  */
00062                 String[] words = line.trim().split( "\\s{1,}" );
00063 
00064                 /* Ignore comments
00065                  */
00066                 if ( words.length < 2 || words[0].equals( "#" ) ) {
00067                     continue;
00068                 }
00069 
00070                 /* Format:  <name>    <port number>/<port type>
00071                  *          words[0]  words[2]
00072                  *
00073                  * Example: nimgtw  48003/udp
00074                  * Example: smtp    25/tcp
00075                  */
00076                 hash.put( words[1], words[0] );
00077             }
00078 
00079             is.close ();
00080         }
00081         catch( FileNotFoundException e )
00082         {
00083             System.err.println(
00084                     "\nFile " + filename + " not found.\n" 
00085                     + e.toString () );
00086         }
00087         catch( IOException e )
00088         {
00089             System.err.println(
00090                     "\nThere was a problem reading the services file.\n" 
00091                     + e.toString () );
00092         }
00093     }
00094     
00095     /**
00096      *  Lookups name for <em>port/protocol</em> from hash table.
00097      *  E.g. Lookup "25/tcp" should return "smtp".
00098      *  
00099      *  @param protocol  protocol name, e.g. "udp" or "tcp"
00100      *  @param port      port number
00101      */
00102     public String lookup( String protocol, int port )
00103     {
00104         if ( hash == null ) {
00105             return null;
00106         }
00107 
00108         String value = hash.get( port + "/" + protocol );
00109         
00110         return value != null ? value.toLowerCase () : null;
00111     }
00112     
00113     /**
00114      *  Verifies exisatnce of the common port names
00115      */
00116     public boolean sanityCheck ()
00117     {
00118         if ( hash == null ) {
00119             return false;
00120         }
00121 
00122         boolean ok = "smtp".equalsIgnoreCase( lookup( "tcp", 25 ) )
00123                   || "http".equalsIgnoreCase( lookup( "tcp", 80 ) )
00124                   || "ssh".equalsIgnoreCase( lookup( "tcp", 22 ) )
00125                   || "ssh".equalsIgnoreCase( lookup( "udp", 22 ) );
00126         
00127         if ( ! ok ) {
00128             System.err.println( "ServiceNames: Sanity check failed." );
00129         }
00130         
00131         return ok;
00132     }
00133 }

Generated on Thu Dec 16 2010 12:29:37 for Port Scanner by  doxygen 1.7.2