NamedPipeServerStream.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // NamedPipeServerStream.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if !BOOTSTRAP_BASIC
  29. using Microsoft.Win32.SafeHandles;
  30. using System;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Security.AccessControl;
  34. using System.Security.Permissions;
  35. using System.Security.Principal;
  36. namespace System.IO.Pipes
  37. {
  38. [MonoTODO ("working only on win32 right now")]
  39. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  40. public sealed class NamedPipeServerStream : PipeStream
  41. {
  42. public const int MaxAllowedServerInstances = -1;
  43. public NamedPipeServerStream (string pipeName)
  44. : this (pipeName, PipeDirection.InOut)
  45. {
  46. }
  47. public NamedPipeServerStream (string pipeName, PipeDirection direction)
  48. : this (pipeName, direction, 1)
  49. {
  50. }
  51. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances)
  52. : this (pipeName, direction, maxNumberOfServerInstances, PipeTransmissionMode.Byte)
  53. {
  54. }
  55. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode)
  56. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, PipeOptions.None)
  57. {
  58. }
  59. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options)
  60. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, DefaultBufferSize, DefaultBufferSize)
  61. {
  62. }
  63. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize)
  64. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, null)
  65. {
  66. }
  67. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity)
  68. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, HandleInheritability.None)
  69. {
  70. }
  71. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability)
  72. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, inheritability, PipeAccessRights.ReadData | PipeAccessRights.WriteData)
  73. {
  74. }
  75. [MonoTODO]
  76. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability, PipeAccessRights additionalAccessRights)
  77. : base (direction, transmissionMode, outBufferSize)
  78. {
  79. if (pipeSecurity != null)
  80. throw ThrowACLException ();
  81. var rights = ToAccessRights (direction) | additionalAccessRights;
  82. // FIXME: reject some rights declarations (for ACL).
  83. if (IsWindows)
  84. impl = new Win32NamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode, rights, options, inBufferSize, outBufferSize, inheritability);
  85. else
  86. impl = new UnixNamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode, rights, options, inBufferSize, outBufferSize, inheritability);
  87. InitializeHandle (impl.Handle, false, (options & PipeOptions.Asynchronous) != PipeOptions.None);
  88. }
  89. public NamedPipeServerStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
  90. : base (direction, DefaultBufferSize)
  91. {
  92. if (IsWindows)
  93. impl = new Win32NamedPipeServer (this, safePipeHandle);
  94. else
  95. impl = new UnixNamedPipeServer (this, safePipeHandle);
  96. IsConnected = isConnected;
  97. InitializeHandle (safePipeHandle, true, isAsync);
  98. }
  99. INamedPipeServer impl;
  100. public void Disconnect ()
  101. {
  102. impl.Disconnect ();
  103. }
  104. [MonoTODO]
  105. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  106. public void RunAsClient (PipeStreamImpersonationWorker impersonationWorker)
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. public void WaitForConnection ()
  111. {
  112. impl.WaitForConnection ();
  113. IsConnected = true;
  114. }
  115. [MonoTODO]
  116. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  117. public string GetImpersonationUserName ()
  118. {
  119. throw new NotImplementedException ();
  120. }
  121. // async operations
  122. Action wait_connect_delegate;
  123. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  124. public IAsyncResult BeginWaitForConnection (AsyncCallback callback, object state)
  125. {
  126. if (wait_connect_delegate == null)
  127. wait_connect_delegate = new Action (WaitForConnection);
  128. return wait_connect_delegate.BeginInvoke (callback, state);
  129. }
  130. public void EndWaitForConnection (IAsyncResult asyncResult)
  131. {
  132. wait_connect_delegate.EndInvoke (asyncResult);
  133. }
  134. }
  135. }
  136. #endif