1 18 19 package jcifs.netbios; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.io.BufferedReader ; 27 import java.io.IOException ; 28 import java.util.Hashtable ; 29 import java.net.UnknownHostException ; 30 import jcifs.Config; 31 import jcifs.smb.SmbFileInputStream; 32 import jcifs.util.LogStream; 33 34 public class Lmhosts { 35 36 private static final String FILENAME = Config.getProperty( "jcifs.netbios.lmhosts" ); 37 private static final Hashtable TAB = new Hashtable (); 38 private static long lastModified = 1L; 39 private static int alt; 40 private static LogStream log = LogStream.getInstance(); 41 42 48 49 public synchronized static NbtAddress getByName( String host ) { 50 return getByName( new Name( host, 0x20, null )); 51 } 52 53 synchronized static NbtAddress getByName( Name name ) { 54 NbtAddress result = null; 55 56 try { 57 if( FILENAME != null ) { 58 File f = new File ( FILENAME ); 59 long lm; 60 61 if(( lm = f.lastModified() ) > lastModified ) { 62 lastModified = lm; 63 TAB.clear(); 64 alt = 0; 65 populate( new FileReader ( f )); 66 } 67 result = (NbtAddress)TAB.get( name ); 68 } 69 } catch( FileNotFoundException fnfe ) { 70 if( log.level > 1 ) { 71 log.println( "lmhosts file: " + FILENAME ); 72 fnfe.printStackTrace( log ); 73 } 74 } catch( IOException ioe ) { 75 if( log.level > 0 ) 76 ioe.printStackTrace( log ); 77 } 78 return result; 79 } 80 81 static void populate( Reader r ) throws IOException { 82 String line; 83 BufferedReader br = new BufferedReader ( r ); 84 85 while(( line = br.readLine() ) != null ) { 86 line = line.toUpperCase().trim(); 87 if( line.length() == 0 ) { 88 continue; 89 } else if( line.charAt( 0 ) == '#' ) { 90 if( line.startsWith( "#INCLUDE " )) { 91 line = line.substring( line.indexOf( '\\' )); 92 String url = "smb:" + line.replace( '\\', '/' ); 93 94 if( alt > 0 ) { 95 try { 96 populate( new InputStreamReader ( new SmbFileInputStream( url ))); 97 } catch( IOException ioe ) { 98 log.println( "lmhosts URL: " + url ); 99 ioe.printStackTrace( log ); 100 continue; 101 } 102 103 106 107 alt--; 108 while(( line = br.readLine() ) != null ) { 109 line = line.toUpperCase().trim(); 110 if( line.startsWith( "#END_ALTERNATE" )) { 111 break; 112 } 113 } 114 } else { 115 populate( new InputStreamReader ( new SmbFileInputStream( url ))); 116 } 117 } else if( line.startsWith( "#BEGIN_ALTERNATE" )) { 118 alt++; 119 } else if( line.startsWith( "#END_ALTERNATE" ) && alt > 0 ) { 120 alt--; 121 throw new IOException ( "no lmhosts alternate includes loaded" ); 122 } 123 } else if( Character.isDigit( line.charAt( 0 ))) { 124 char[] data = line.toCharArray(); 125 int ip, i, j; 126 Name name; 127 NbtAddress addr; 128 char c; 129 130 c = '.'; 131 ip = i = 0; 132 for( ; i < data.length && c == '.'; i++ ) { 133 int b = 0x00; 134 135 for( ; i < data.length && ( c = data[i] ) >= 48 && c <= 57; i++ ) { 136 b = b * 10 + c - '0'; 137 } 138 ip = ( ip << 8 ) + b; 139 } 140 while( i < data.length && Character.isWhitespace( data[i] )) { 141 i++; 142 } 143 j = i; 144 while( j < data.length && Character.isWhitespace( data[j] ) == false ) { 145 j++; 146 } 147 148 name = new Name( line.substring( i, j ), 0x20, null ); 149 addr = new NbtAddress( name, ip, false, NbtAddress.B_NODE, 150 false, false, true, true, 151 NbtAddress.UNKNOWN_MAC_ADDRESS ); 152 TAB.put( name, addr ); 153 } 154 } 155 } 156 } 157 | Popular Tags |