TcpipSMBNetworkSession.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.client;
  20. import java.io.*;
  21. import java.net.*;
  22. import org.alfresco.jlan.debug.Debug;
  23. import org.alfresco.jlan.netbios.NetworkSession;
  24. import org.alfresco.jlan.netbios.RFCNetBIOSProtocol;
  25. import org.alfresco.jlan.smb.TcpipSMB;
  26. import org.alfresco.jlan.util.DataPacker;
  27. /**
  28. * Native TCP/IP SMB Network Session Class
  29. *
  30. * <p>Contains the details of a connection to a remote file server that has used the native SMB protocol
  31. * of port 445 to connect to the remote server.
  32. *
  33. * <p>Contains methods for sending/receiving SMB/CIFS requests/responses using native SMB.
  34. *
  35. * @author gkspencer
  36. */
  37. public class TcpipSMBNetworkSession extends NetworkSession {
  38. // Define the protocol name
  39. private static final String ProtocolName = "Native SMB (port 445)";
  40. // Socket used to connect and read/write to remote host
  41. private Socket m_socket;
  42. // Input and output data streams, from the socket network connection
  43. private DataInputStream m_in;
  44. private DataOutputStream m_out;
  45. // Session port
  46. private int m_sessPort = TcpipSMB.PORT;
  47. // Debug enable flag and debug output stream
  48. private static boolean m_debug = false;
  49. /**
  50. * Default constructor
  51. */
  52. public TcpipSMBNetworkSession() {
  53. super(ProtocolName);
  54. }
  55. /**
  56. * Class constructor
  57. *
  58. * @param tmo Socket timeout, in milliseconds
  59. */
  60. public TcpipSMBNetworkSession(int tmo) {
  61. super(ProtocolName);
  62. setTimeout(tmo);
  63. }
  64. /**
  65. * Class constructor
  66. *
  67. * @param tmo Socket timeout, in milliseconds
  68. * @param port Session port to connect to on the server
  69. */
  70. public TcpipSMBNetworkSession(int tmo, int port) {
  71. super(ProtocolName);
  72. setTimeout(tmo);
  73. m_sessPort = port;
  74. }
  75. /**
  76. * Open a connection to a remote host
  77. *
  78. * @param toName Host name/address being called
  79. * @param fromName Local host name/address
  80. * @param toAddr Optional address
  81. * @exception IOException
  82. */
  83. public void Open(String toName, String fromName, String toAddr)
  84. throws IOException, UnknownHostException {
  85. // Create the socket
  86. m_socket = new Socket();
  87. m_socket.connect(new InetSocketAddress(toName, m_sessPort), getTimeout());
  88. // Enable the timeout on the socket, disable the Nagle algorithm
  89. m_socket.setSoTimeout(getTimeout());
  90. m_socket.setTcpNoDelay(true);
  91. // Attach input/output streams to the socket
  92. m_in = new DataInputStream(m_socket.getInputStream());
  93. m_out = new DataOutputStream(m_socket.getOutputStream());
  94. }
  95. /**
  96. * Determine if the session is connected to a remote host
  97. *
  98. * @return boolean
  99. */
  100. public boolean isConnected() {
  101. return m_socket != null ? true : false;
  102. }
  103. /**
  104. * Check if there is data available on this network session
  105. *
  106. * @return boolean
  107. * @exception IOException
  108. */
  109. public final boolean hasData()
  110. throws IOException {
  111. // Check if the connection is active
  112. if ( m_socket == null || m_in == null)
  113. return false;
  114. // Check if there is data available
  115. return m_in.available() > 0 ? true : false;
  116. }
  117. /**
  118. * Receive a data packet from the remote host.
  119. *
  120. * @param buf Byte buffer to receive the data into.
  121. * @return Length of the received data.
  122. * @exception java.io.IOException I/O error occurred.
  123. */
  124. public int Receive(byte[] buf)
  125. throws IOException {
  126. // Read a data packet of data
  127. int rdlen = m_in.read(buf, 0, RFCNetBIOSProtocol.HEADER_LEN);
  128. // Check if a header was received
  129. if ( rdlen < RFCNetBIOSProtocol.HEADER_LEN)
  130. throw new java.io.IOException("TCP/IP SMB Short Read");
  131. // Get the packet data length
  132. int pktlen = DataPacker.getInt(buf, 0);
  133. // Debug mode
  134. if ( m_debug)
  135. Debug.println("TcpSMB: Rx " + pktlen + " bytes");
  136. // Check that the packet size is within the valid range for a CIFS request
  137. if ( pktlen > (buf.length - RFCNetBIOSProtocol.HEADER_LEN) || pktlen > RFCNetBIOSProtocol.MaxPacketSize)
  138. throw new IOException("TCP/IP SMB Long Read");
  139. // Read the data part of the packet into the users buffer, this may take
  140. // several reads
  141. int totlen = 0;
  142. int offset = RFCNetBIOSProtocol.HEADER_LEN;
  143. while (pktlen > 0) {
  144. // Read the data
  145. rdlen = m_in.read(buf, offset, pktlen);
  146. // Update the received length and remaining data length
  147. totlen += rdlen;
  148. pktlen -= rdlen;
  149. // Update the user buffer offset as more reads will be required
  150. // to complete the data read
  151. offset += rdlen;
  152. } // end while reading data
  153. // Return the received data length, not including the header
  154. return totlen;
  155. }
  156. /**
  157. * Send a data packet to the remote host.
  158. *
  159. * @param data Byte array containing the data to be sent.
  160. * @param siz Length of the data to send.
  161. * @return true if the data was sent successfully, else false.
  162. * @exception java.io.IOException I/O error occurred.
  163. */
  164. public boolean Send(byte[] data, int siz)
  165. throws IOException {
  166. // Pack the data length as the first four bytes of the packet
  167. DataPacker.putInt(siz, data, 0);
  168. // Send the packet to the remote host
  169. int len = siz + RFCNetBIOSProtocol.HEADER_LEN;
  170. m_out.write(data, 0, len);
  171. return true;
  172. }
  173. /**
  174. * Close the network session
  175. *
  176. * @exception java.io.IOException I/O error occurred
  177. */
  178. public void Close()
  179. throws IOException {
  180. // Close the input/output streams
  181. if ( m_in != null) {
  182. m_in.close();
  183. m_in = null;
  184. }
  185. if ( m_out != null) {
  186. m_out.close();
  187. m_out = null;
  188. }
  189. // Close the socket
  190. if ( m_socket != null) {
  191. m_socket.close();
  192. m_socket = null;
  193. }
  194. }
  195. /**
  196. * Set the socket timeout
  197. *
  198. * @param tmo int
  199. */
  200. public void setTimeout(int tmo) {
  201. // Call the base class to store the timeout
  202. super.setTimeout(tmo);
  203. // Set the socket timeout, if the socket is valid
  204. if ( m_socket != null) {
  205. try {
  206. m_socket.setSoTimeout(getTimeout());
  207. }
  208. catch (SocketException ex) {
  209. }
  210. }
  211. }
  212. /**
  213. * Enable/disable session debugging output
  214. *
  215. * @param dbg true to enable debugging, else false
  216. */
  217. public static void setDebug(boolean dbg) {
  218. m_debug = dbg;
  219. }
  220. }