1 17 package org.alfresco.filesys.server.auth.passthru; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.io.PrintStream ; 23 import java.net.Socket ; 24 import java.net.UnknownHostException ; 25 26 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol; 27 import org.alfresco.filesys.smb.NetworkSession; 28 import org.alfresco.filesys.smb.TcpipSMB; 29 import org.alfresco.filesys.util.DataPacker; 30 31 34 public class TcpipSMBNetworkSession implements NetworkSession 35 { 36 37 39 private static int _defTimeout = 30000; 41 43 private Socket m_socket; 44 45 47 private DataInputStream m_in; 48 private DataOutputStream m_out; 49 50 52 private int m_tmo = _defTimeout; 53 54 56 private static boolean m_debug = false; 57 private static PrintStream m_dbg = System.out; 58 59 62 public TcpipSMBNetworkSession() 63 { 64 } 65 66 71 public TcpipSMBNetworkSession(int tmo) 72 { 73 m_tmo = tmo; 74 } 75 76 81 public String getProtocolName() 82 { 83 return "Native SMB (port 445)"; 84 } 85 86 94 public void Open(String toName, String fromName, String toAddr) throws IOException , UnknownHostException 95 { 96 97 99 m_socket = new Socket (toName, TcpipSMB.PORT); 100 101 103 m_socket.setSoTimeout(m_tmo); 104 m_socket.setTcpNoDelay(true); 105 106 108 m_in = new DataInputStream (m_socket.getInputStream()); 109 m_out = new DataOutputStream (m_socket.getOutputStream()); 110 } 111 112 117 public boolean isConnected() 118 { 119 return m_socket != null ? true : false; 120 } 121 122 128 public final boolean hasData() throws IOException 129 { 130 131 133 if (m_socket == null || m_in == null) 134 return false; 135 136 138 return m_in.available() > 0 ? true : false; 139 } 140 141 149 public int Receive(byte[] buf, int tmo) throws IOException 150 { 151 152 154 m_socket.setSoTimeout(tmo); 155 156 158 int rdlen = m_in.read(buf, 0, RFCNetBIOSProtocol.HEADER_LEN); 159 160 162 if (rdlen < RFCNetBIOSProtocol.HEADER_LEN) 163 throw new java.io.IOException ("TCP/IP SMB Short Read"); 164 165 167 int pktlen = DataPacker.getInt(buf, 0); 168 169 171 if (m_debug) 172 m_dbg.println("TcpSMB: Rx " + pktlen + " bytes"); 173 174 177 int totlen = 0; 178 int offset = RFCNetBIOSProtocol.HEADER_LEN; 179 180 while (pktlen > 0) 181 { 182 183 185 rdlen = m_in.read(buf, offset, pktlen); 186 187 189 totlen += rdlen; 190 pktlen -= rdlen; 191 192 195 offset += rdlen; 196 197 } 199 201 return totlen; 202 } 203 204 212 public boolean Send(byte[] data, int siz) throws IOException 213 { 214 215 217 DataPacker.putInt(siz, data, 0); 218 219 221 int len = siz + RFCNetBIOSProtocol.HEADER_LEN; 222 m_out.write(data, 0, len); 223 return true; 224 } 225 226 231 public void Close() throws IOException 232 { 233 234 236 if (m_in != null) 237 { 238 m_in.close(); 239 m_in = null; 240 } 241 242 if (m_out != null) 243 { 244 m_out.close(); 245 m_out = null; 246 } 247 248 250 if (m_socket != null) 251 { 252 m_socket.close(); 253 m_socket = null; 254 } 255 } 256 257 262 public static void setDebug(boolean dbg) 263 { 264 m_debug = dbg; 265 } 266 267 272 public static final int getDefaultTimeout() 273 { 274 return _defTimeout; 275 } 276 277 282 public static final void setDefaultTimeout(int tmo) 283 { 284 _defTimeout = tmo; 285 } 286 } 287 | Popular Tags |