1 18 19 package jcifs.netbios; 20 21 import java.net.Socket ; 22 import java.net.InetAddress ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.IOException ; 26 import jcifs.Config; 27 import jcifs.util.LogStream; 28 29 34 35 public class NbtSocket extends Socket { 36 37 private static final int SSN_SRVC_PORT = 139; 38 private static final int BUFFER_SIZE = 512; 39 private static final int DEFAULT_SO_TIMEOUT = 5000; 40 41 private static LogStream log = LogStream.getInstance(); 42 43 private NbtAddress address; 44 private Name calledName; 45 private int soTimeout; 46 47 public NbtSocket() { 48 super(); 49 } 50 public NbtSocket( NbtAddress address, int port ) throws IOException { 51 this( address, port, null, 0 ); 52 } 53 public NbtSocket( NbtAddress address, int port, 54 InetAddress localAddr, int localPort ) throws IOException { 55 this( address, null, port, localAddr, localPort ); 56 } 57 public NbtSocket( NbtAddress address, String calledName, int port, 58 InetAddress localAddr, int localPort ) throws IOException { 59 super( address.getInetAddress(), ( port == 0 ? SSN_SRVC_PORT : port ), 60 localAddr, localPort ); 61 this.address = address; 62 if( calledName == null ) { 63 this.calledName = address.hostName; 64 } else { 65 this.calledName = new Name( calledName, 0x20, null ); 66 } 67 soTimeout = Config.getInt( "jcifs.netbios.soTimeout", DEFAULT_SO_TIMEOUT ); 68 connect(); 69 } 70 71 public NbtAddress getNbtAddress() { 72 return address; 73 } 74 public InputStream getInputStream() throws IOException { 75 return new SocketInputStream( super.getInputStream() ); 76 } 77 public OutputStream getOutputStream() throws IOException { 78 return new SocketOutputStream( super.getOutputStream() ); 79 } 80 public int getPort() { 81 return super.getPort(); 82 } 83 public InetAddress getLocalAddress() { 84 return super.getLocalAddress(); 85 } 86 public int getLocalPort() { 87 return super.getLocalPort(); 88 } 89 public String toString() { 90 return "NbtSocket[addr=" + address + 91 ",port=" + super.getPort() + 92 ",localport=" + super.getLocalPort() + "]"; 93 } 94 private void connect() throws IOException { 95 byte[] buffer = new byte[BUFFER_SIZE]; 96 int type; 97 InputStream in; 98 99 try { 100 in = super.getInputStream(); 101 OutputStream out = super.getOutputStream(); 102 103 SessionServicePacket ssp0 = new SessionRequestPacket( calledName, NbtAddress.localhost.hostName ); 104 out.write( buffer, 0, ssp0.writeWireFormat( buffer, 0 )); 105 106 setSoTimeout( soTimeout ); 107 type = ssp0.readPacketType( in, buffer, 0 ); 108 } catch( IOException ioe ) { 109 close(); 110 throw ioe; 111 } 112 113 switch( type ) { 114 case SessionServicePacket.POSITIVE_SESSION_RESPONSE: 115 if( log.level > 2 ) 116 log.println( "session established ok with " + address ); 117 return; 118 case SessionServicePacket.NEGATIVE_SESSION_RESPONSE: 119 int errorCode = (int)( in.read() & 0xFF ); 120 close(); 121 throw new NbtException( NbtException.ERR_SSN_SRVC, errorCode ); 122 case -1: 123 throw new NbtException( NbtException.ERR_SSN_SRVC, NbtException.CONNECTION_REFUSED ); 124 default: 125 close(); 126 throw new NbtException( NbtException.ERR_SSN_SRVC, 0 ); 127 } 128 } 129 public void close() throws IOException { 130 if( log.level > 3 ) 131 log.println( "close: " + this ); 132 super.close(); 133 } 134 } 135 | Popular Tags |