NamedPipeServerStream.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. [MonoTODO]
  43. public const int MaxAllowedServerInstances = 1;
  44. public NamedPipeServerStream (string pipeName)
  45. : this (pipeName, PipeDirection.InOut)
  46. {
  47. }
  48. public NamedPipeServerStream (string pipeName, PipeDirection direction)
  49. : this (pipeName, direction, 1)
  50. {
  51. }
  52. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances)
  53. : this (pipeName, direction, maxNumberOfServerInstances, PipeTransmissionMode.Byte)
  54. {
  55. }
  56. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode)
  57. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, PipeOptions.None)
  58. {
  59. }
  60. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options)
  61. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, DefaultBufferSize, DefaultBufferSize)
  62. {
  63. }
  64. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize)
  65. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, null)
  66. {
  67. }
  68. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity)
  69. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, HandleInheritability.None)
  70. {
  71. }
  72. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability)
  73. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, inheritability, PipeAccessRights.ReadData | PipeAccessRights.WriteData)
  74. {
  75. }
  76. [MonoTODO]
  77. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability, PipeAccessRights additionalAccessRights)
  78. : base (direction, transmissionMode, outBufferSize)
  79. {
  80. if (pipeSecurity != null)
  81. throw ThrowACLException ();
  82. var rights = ToAccessRights (direction) | additionalAccessRights;
  83. // FIXME: reject some rights declarations (for ACL).
  84. if (IsWindows)
  85. impl = new Win32NamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode, rights, options, inBufferSize, outBufferSize, inheritability);
  86. else
  87. impl = new UnixNamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode, rights, options, inBufferSize, outBufferSize, inheritability);
  88. InitializeHandle (impl.Handle, false, (options & PipeOptions.Asynchronous) != PipeOptions.None);
  89. }
  90. public NamedPipeServerStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
  91. : base (direction, DefaultBufferSize)
  92. {
  93. if (IsWindows)
  94. impl = new Win32NamedPipeServer (this, safePipeHandle);
  95. else
  96. impl = new UnixNamedPipeServer (this, safePipeHandle);
  97. IsConnected = isConnected;
  98. InitializeHandle (safePipeHandle, true, isAsync);
  99. }
  100. INamedPipeServer impl;
  101. public void Disconnect ()
  102. {
  103. impl.Disconnect ();
  104. }
  105. [MonoTODO]
  106. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  107. public void RunAsClient (PipeStreamImpersonationWorker impersonationWorker)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. public void WaitForConnection ()
  112. {
  113. impl.WaitForConnection ();
  114. IsConnected = true;
  115. }
  116. [MonoTODO]
  117. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  118. public string GetImpersonationUserName ()
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. // async operations
  123. Action wait_connect_delegate;
  124. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  125. public IAsyncResult BeginWaitForConnection (AsyncCallback callback, object state)
  126. {
  127. if (wait_connect_delegate == null)
  128. wait_connect_delegate = new Action (WaitForConnection);
  129. return wait_connect_delegate.BeginInvoke (callback, state);
  130. }
  131. public void EndWaitForConnection (IAsyncResult asyncResult)
  132. {
  133. wait_connect_delegate.EndInvoke (asyncResult);
  134. }
  135. }
  136. }
  137. #endif