Public Member Functions | Package Functions | Private Attributes

ServiceNames Class Reference

Encapsulates lookup database for TCP and UDP service names. More...

Collaboration diagram for ServiceNames:
Collaboration graph
[legend]

List of all members.

Public Member Functions

String lookup (String protocol, int port)
 Lookups name for port/protocol from hash table.
boolean sanityCheck ()
 Verifies exisatnce of the common port names.

Package Functions

 ServiceNames (String filename)
 Creates instance from text file containing description of services where each line describes one service, and is of the form:

Private Attributes

Hashtable< String, String > hash

Detailed Description

Encapsulates lookup database for TCP and UDP service names.

Author:
Administrator

Definition at line 15 of file ServiceNames.java.


Constructor & Destructor Documentation

ServiceNames.ServiceNames ( String  filename ) [package]

Creates instance from text file containing description of services where each line describes one service, and is of the form:

   service-name  port/protocol  [aliases ...]   [# comment]
  

The format is compatible with Unix' /etc/services.

Definition at line 27 of file ServiceNames.java.

References hash.

    {
        this.hash = new Hashtable<String,String> ();

        try
        {
            InputStream is = null;
            
            /* Read first local file
             */
            try {
                is = new FileInputStream( filename );
                System.out.println( "\nWarning: Loading local services.txt..." );
            } catch ( FileNotFoundException e2 ) {
                /* ignored */
            }

            /* then fail back to resource from the JAR
             */
            if ( is == null ) {
                is = getClass().getResourceAsStream( filename );
            }

            if ( is == null ) {
                System.out.println( "\nWarning: Could not find services.txt..." );
                return;
            }

            BufferedReader in = new BufferedReader( new InputStreamReader( is ) );
            
            for( String line = in.readLine (); line != null; line = in.readLine () ) 
            {
                /* Split string into words, removing all leading, trailing 
                 * and superfluous (betwewn words) white-spaces
                 */
                String[] words = line.trim().split( "\\s{1,}" );

                /* Ignore comments
                 */
                if ( words.length < 2 || words[0].equals( "#" ) ) {
                    continue;
                }

                /* Format:  <name>    <port number>/<port type>
                 *          words[0]  words[2]
                 *
                 * Example: nimgtw  48003/udp
                 * Example: smtp    25/tcp
                 */
                hash.put( words[1], words[0] );
            }

            is.close ();
        }
        catch( FileNotFoundException e )
        {
            System.err.println(
                    "\nFile " + filename + " not found.\n" 
                    + e.toString () );
        }
        catch( IOException e )
        {
            System.err.println(
                    "\nThere was a problem reading the services file.\n" 
                    + e.toString () );
        }
    }

Member Function Documentation

String ServiceNames.lookup ( String  protocol,
int  port 
)

Lookups name for port/protocol from hash table.

E.g. Lookup "25/tcp" should return "smtp".

Parameters:
protocolprotocol name, e.g. "udp" or "tcp"
portport number

Definition at line 102 of file ServiceNames.java.

References hash.

Referenced by PortScanner.onPortConnected(), and sanityCheck().

    {
        if ( hash == null ) {
            return null;
        }

        String value = hash.get( port + "/" + protocol );
        
        return value != null ? value.toLowerCase () : null;
    }
boolean ServiceNames.sanityCheck (  )

Verifies exisatnce of the common port names.

Definition at line 116 of file ServiceNames.java.

References hash, and lookup().

    {
        if ( hash == null ) {
            return false;
        }

        boolean ok = "smtp".equalsIgnoreCase( lookup( "tcp", 25 ) )
                  || "http".equalsIgnoreCase( lookup( "tcp", 80 ) )
                  || "ssh".equalsIgnoreCase( lookup( "tcp", 22 ) )
                  || "ssh".equalsIgnoreCase( lookup( "udp", 22 ) );
        
        if ( ! ok ) {
            System.err.println( "ServiceNames: Sanity check failed." );
        }
        
        return ok;
    }

Member Data Documentation

Hashtable<String,String> ServiceNames.hash [private]

Definition at line 17 of file ServiceNames.java.

Referenced by lookup(), sanityCheck(), and ServiceNames().


The documentation for this class was generated from the following file: