SmbNamedPipe.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Smb
  20. {
  21. /// <summary>
  22. /// This class will allow a Java program to read and write data to Named
  23. /// Pipes and Transact NamedPipes.
  24. /// </summary>
  25. /// <remarks>
  26. /// This class will allow a Java program to read and write data to Named
  27. /// Pipes and Transact NamedPipes.
  28. /// <p>There are three Win32 function calls provided by the Windows SDK
  29. /// that are important in the context of using jCIFS. They are:
  30. /// <ul>
  31. /// <li> <code>CallNamedPipe</code> A message-type pipe call that opens,
  32. /// writes to, reads from, and closes the pipe in a single operation.
  33. /// <li> <code>TransactNamedPipe</code> A message-type pipe call that
  34. /// writes to and reads from an existing pipe descriptor in one operation.
  35. /// <li> <code>CreateFile</code>, <code>ReadFile</code>,
  36. /// <code>WriteFile</code>, and <code>CloseFile</code> A byte-type pipe can
  37. /// be opened, written to, read from and closed using the standard Win32
  38. /// file operations.
  39. /// </ul>
  40. /// <p>The jCIFS API maps all of these operations into the standard Java
  41. /// <code>XxxputStream</code> interface. A special <code>PIPE_TYPE</code>
  42. /// flags is necessary to distinguish which type of Named Pipe behavior
  43. /// is desired.
  44. /// <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
  45. /// <tr bgcolor="#ccccff">
  46. /// <td colspan="2"><b><code>SmbNamedPipe</code> Constructor Examples</b></td>
  47. /// <tr><td width="20%"><b>Code Sample</b></td><td><b>Description</b></td></tr>
  48. /// <tr><td width="20%"><pre>
  49. /// new SmbNamedPipe( "smb://server/IPC$/PIPE/foo",
  50. /// SmbNamedPipe.PIPE_TYPE_RDWR |
  51. /// SmbNamedPipe.PIPE_TYPE_CALL );
  52. /// </pre></td><td>
  53. /// Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>CallNamedPipe</code> interface.
  54. /// </td></tr>
  55. /// <tr><td width="20%"><pre>
  56. /// new SmbNamedPipe( "smb://server/IPC$/foo",
  57. /// SmbNamedPipe.PIPE_TYPE_RDWR |
  58. /// SmbNamedPipe.PIPE_TYPE_TRANSACT );
  59. /// </pre></td><td>
  60. /// Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>TransactNamedPipe</code> interface.
  61. /// </td></tr>
  62. /// <tr><td width="20%"><pre>
  63. /// new SmbNamedPipe( "smb://server/IPC$/foo",
  64. /// SmbNamedPipe.PIPE_TYPE_RDWR );
  65. /// </pre></td><td>
  66. /// Open the Named Pipe foo for reading and writing. The pipe will
  67. /// behave as though the <code>CreateFile</code>, <code>ReadFile</code>,
  68. /// <code>WriteFile</code>, and <code>CloseFile</code> interface was
  69. /// being used.
  70. /// </td></tr>
  71. /// </table>
  72. /// <p>See <a href="../../../pipes.html">Using jCIFS to Connect to Win32
  73. /// Named Pipes</a> for a detailed description of how to use jCIFS with
  74. /// Win32 Named Pipe server processes.
  75. /// </remarks>
  76. public class SmbNamedPipe : SmbFile
  77. {
  78. /// <summary>The pipe should be opened read-only.</summary>
  79. /// <remarks>The pipe should be opened read-only.</remarks>
  80. public const int PipeTypeRdonly = ORdonly;
  81. /// <summary>The pipe should be opened only for writing.</summary>
  82. /// <remarks>The pipe should be opened only for writing.</remarks>
  83. public const int PipeTypeWronly = OWronly;
  84. /// <summary>The pipe should be opened for both reading and writing.</summary>
  85. /// <remarks>The pipe should be opened for both reading and writing.</remarks>
  86. public const int PipeTypeRdwr = ORdwr;
  87. /// <summary>Pipe operations should behave like the <code>CallNamedPipe</code> Win32 Named Pipe function.
  88. /// </summary>
  89. /// <remarks>Pipe operations should behave like the <code>CallNamedPipe</code> Win32 Named Pipe function.
  90. /// </remarks>
  91. public const int PipeTypeCall = unchecked(0x0100);
  92. /// <summary>Pipe operations should behave like the <code>TransactNamedPipe</code> Win32 Named Pipe function.
  93. /// </summary>
  94. /// <remarks>Pipe operations should behave like the <code>TransactNamedPipe</code> Win32 Named Pipe function.
  95. /// </remarks>
  96. public const int PipeTypeTransact = unchecked(0x0200);
  97. public const int PipeTypeDceTransact = unchecked(0x0200) | unchecked(0x0400);
  98. internal InputStream PipeIn;
  99. internal OutputStream PipeOut;
  100. internal int PipeType;
  101. /// <summary>
  102. /// Open the Named Pipe resource specified by the url
  103. /// parameter.
  104. /// </summary>
  105. /// <remarks>
  106. /// Open the Named Pipe resource specified by the url
  107. /// parameter. The pipeType parameter should be at least one of
  108. /// the <code>PIPE_TYPE</code> flags combined with the bitwise OR
  109. /// operator <code>|</code>. See the examples listed above.
  110. /// </remarks>
  111. /// <exception cref="System.UriFormatException"></exception>
  112. /// <exception cref="UnknownHostException"></exception>
  113. public SmbNamedPipe(string url, int pipeType) : base(url)
  114. {
  115. this.PipeType = pipeType;
  116. Type = TypeNamedPipe;
  117. }
  118. /// <exception cref="System.UriFormatException"></exception>
  119. /// <exception cref="UnknownHostException"></exception>
  120. public SmbNamedPipe(string url, int pipeType, NtlmPasswordAuthentication auth) :
  121. base(url, auth)
  122. {
  123. this.PipeType = pipeType;
  124. Type = TypeNamedPipe;
  125. }
  126. /// <exception cref="System.UriFormatException"></exception>
  127. /// <exception cref="UnknownHostException"></exception>
  128. public SmbNamedPipe(Uri url, int pipeType, NtlmPasswordAuthentication auth) : base
  129. (url, auth)
  130. {
  131. this.PipeType = pipeType;
  132. Type = TypeNamedPipe;
  133. }
  134. /// <summary>
  135. /// Return the <code>InputStream</code> used to read information
  136. /// from this pipe instance.
  137. /// </summary>
  138. /// <remarks>
  139. /// Return the <code>InputStream</code> used to read information
  140. /// from this pipe instance. Presumably data would first be written
  141. /// to the <code>OutputStream</code> associated with this Named
  142. /// Pipe instance although this is not a requirement (e.g. a
  143. /// read-only named pipe would write data to this stream on
  144. /// connection). Reading from this stream may block. Therefore it
  145. /// may be necessary that an addition thread be used to read and
  146. /// write to a Named Pipe.
  147. /// </remarks>
  148. /// <exception cref="System.IO.IOException"></exception>
  149. public virtual InputStream GetNamedPipeInputStream()
  150. {
  151. if (PipeIn == null)
  152. {
  153. if ((PipeType & PipeTypeCall) == PipeTypeCall || (PipeType & PipeTypeTransact
  154. ) == PipeTypeTransact)
  155. {
  156. PipeIn = new TransactNamedPipeInputStream(this);
  157. }
  158. else
  159. {
  160. PipeIn = new SmbFileInputStream(this, (PipeType & unchecked((int)(0xFFFF00FF))) |
  161. OExcl);
  162. }
  163. }
  164. return PipeIn;
  165. }
  166. /// <summary>
  167. /// Return the <code>OutputStream</code> used to write
  168. /// information to this pipe instance.
  169. /// </summary>
  170. /// <remarks>
  171. /// Return the <code>OutputStream</code> used to write
  172. /// information to this pipe instance. The act of writing data
  173. /// to this stream will result in response data recieved in the
  174. /// <code>InputStream</code> associated with this Named Pipe
  175. /// instance (unless of course it does not elicite a response or the pipe is write-only).
  176. /// </remarks>
  177. /// <exception cref="System.IO.IOException"></exception>
  178. public virtual OutputStream GetNamedPipeOutputStream()
  179. {
  180. if (PipeOut == null)
  181. {
  182. if ((PipeType & PipeTypeCall) == PipeTypeCall || (PipeType & PipeTypeTransact
  183. ) == PipeTypeTransact)
  184. {
  185. PipeOut = new TransactNamedPipeOutputStream(this);
  186. }
  187. else
  188. {
  189. PipeOut = new SmbFileOutputStream(this, false, (PipeType & unchecked((int)(0xFFFF00FF
  190. ))) | OExcl);
  191. }
  192. }
  193. return PipeOut;
  194. }
  195. }
  196. }