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