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
00012
00013
00014
00015 public class ServiceNames
00016 {
00017 private Hashtable<String,String> hash;
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 ServiceNames( String filename )
00028 {
00029 this.hash = new Hashtable<String,String> ();
00030
00031 try
00032 {
00033 InputStream is = null;
00034
00035
00036
00037 try {
00038 is = new FileInputStream( filename );
00039 System.out.println( "\nWarning: Loading local services.txt..." );
00040 } catch ( FileNotFoundException e2 ) {
00041
00042 }
00043
00044
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
00060
00061
00062 String[] words = line.trim().split( "\\s{1,}" );
00063
00064
00065
00066 if ( words.length < 2 || words[0].equals( "#" ) ) {
00067 continue;
00068 }
00069
00070
00071
00072
00073
00074
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
00097
00098
00099
00100
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
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 }