41 changed files with 3767 additions and 86 deletions
@ -0,0 +1,23 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.InteropServices; |
|||
using System.Text; |
|||
|
|||
namespace Microsoft.Win32 |
|||
{ |
|||
|
|||
[StructLayout(LayoutKind.Sequential)] |
|||
internal class SECURITY_ATTRIBUTES |
|||
{ |
|||
internal int nLength; |
|||
|
|||
internal unsafe byte* pSecurityDescriptor; |
|||
|
|||
internal int bInheritHandle; |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,37 @@ |
|||
#if NET20
|
|||
|
|||
using Apewer.Internals.Interop; |
|||
using Microsoft.Win32; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
|
|||
namespace Microsoft.Win32.SafeHandles |
|||
{ |
|||
|
|||
[SecurityCritical(SecurityCriticalScope.Everything)] |
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] |
|||
internal sealed class SafePipeHandle : SafeHandleZeroOrMinusOneIsInvalid |
|||
{ |
|||
private SafePipeHandle() |
|||
: base(true) |
|||
{ |
|||
} |
|||
|
|||
public SafePipeHandle(IntPtr preexistingHandle, bool ownsHandle) |
|||
: base(ownsHandle) |
|||
{ |
|||
base.SetHandle(preexistingHandle); |
|||
} |
|||
|
|||
protected override bool ReleaseHandle() |
|||
{ |
|||
return Kernel32.CloseHandle(base.handle); |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,148 @@ |
|||
#if NET20
|
|||
|
|||
using Apewer.Internals.Interop; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Runtime.ConstrainedExecution; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Text; |
|||
using System.Threading; |
|||
|
|||
namespace Microsoft.Win32 |
|||
{ |
|||
|
|||
[SuppressUnmanagedCodeSecurity] |
|||
internal static class UnsafeNativeMethods |
|||
{ |
|||
|
|||
#region constants
|
|||
|
|||
internal static readonly IntPtr NULL = IntPtr.Zero; |
|||
|
|||
#endregion
|
|||
|
|||
#region dll methods
|
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal unsafe static extern bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped* overlapped); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped); |
|||
|
|||
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] |
|||
internal static extern SafePipeHandle CreateNamedPipe(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeout, SECURITY_ATTRIBUTES securityAttributes); |
|||
|
|||
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, EntryPoint = "CreateFile", SetLastError = true)] |
|||
internal static extern SafePipeHandle CreateNamedPipeClient(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, SECURITY_ATTRIBUTES lpPipeAttributes, int nSize); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool DisconnectNamedPipe(SafePipeHandle hNamedPipe); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafePipeHandle hSourceHandle, IntPtr hTargetProcessHandle, out SafePipeHandle lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool FlushFileBuffers(SafePipeHandle hNamedPipe); |
|||
|
|||
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto)] |
|||
internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
internal static extern IntPtr GetCurrentProcess(); |
|||
|
|||
[DllImport("kernel32.dll")] |
|||
internal static extern int GetFileType(SafePipeHandle handle); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, IntPtr lpState, out int lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, IntPtr lpUserName, int nMaxUserNameSize); |
|||
|
|||
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, IntPtr lpState, IntPtr lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, StringBuilder lpUserName, int nMaxUserNameSize); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, out int lpState, IntPtr lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, IntPtr lpUserName, int nMaxUserNameSize); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, out int lpFlags, IntPtr lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, IntPtr lpFlags, IntPtr lpOutBufferSize, out int lpInBufferSize, IntPtr lpMaxInstances); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, IntPtr lpFlags, out int lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances); |
|||
|
|||
[DllImport("advapi32.dll", SetLastError = true)] |
|||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
internal unsafe static extern int ReadFile(SafePipeHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
internal unsafe static extern int ReadFile(SafePipeHandle handle, byte* bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped* overlapped); |
|||
|
|||
[DllImport("advapi32.dll", SetLastError = true)] |
|||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal static extern bool RevertToSelf(); |
|||
|
|||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
internal unsafe static extern bool SetNamedPipeHandleState(SafePipeHandle hNamedPipe, int* lpMode, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout); |
|||
|
|||
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] |
|||
[return: MarshalAs(UnmanagedType.Bool)] |
|||
public static extern bool WaitNamedPipe(string name, int timeout); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
internal unsafe static extern int WriteFile(SafePipeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero); |
|||
|
|||
[DllImport("kernel32.dll", SetLastError = true)] |
|||
internal unsafe static extern int WriteFile(SafePipeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped); |
|||
|
|||
#endregion
|
|||
|
|||
#region manaegd methods
|
|||
|
|||
[SecurityCritical] |
|||
internal static string GetMessage(int errorCode) |
|||
{ |
|||
StringBuilder stringBuilder = new StringBuilder(512); |
|||
if (FormatMessage(12800, NULL, errorCode, 0, stringBuilder, stringBuilder.Capacity, NULL) != 0) |
|||
{ |
|||
return stringBuilder.ToString(); |
|||
} |
|||
return "UnknownError_Num " + errorCode; |
|||
} |
|||
|
|||
internal static int MakeHRFromErrorCode(int errorCode) |
|||
{ |
|||
return -2147024896 | errorCode; |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,15 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO |
|||
{ |
|||
|
|||
[Serializable] |
|||
internal enum HandleInheritability |
|||
{ |
|||
None, |
|||
Inheritable |
|||
} |
|||
|
|||
} |
@ -0,0 +1,84 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.IO; |
|||
using System.IO.Pipes; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class AnonymousPipeClientStream : PipeStream |
|||
{ |
|||
|
|||
public override PipeTransmissionMode TransmissionMode |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
return PipeTransmissionMode.Byte; |
|||
} |
|||
} |
|||
|
|||
public override PipeTransmissionMode ReadMode |
|||
{ |
|||
[SecurityCritical] |
|||
set |
|||
{ |
|||
CheckPipePropertyOperations(); |
|||
switch (value) |
|||
{ |
|||
case PipeTransmissionMode.Byte: |
|||
break; |
|||
default: |
|||
throw new ArgumentOutOfRangeException("value", "ArgumentOutOfRange_TransmissionModeByteOrMsg"); |
|||
case PipeTransmissionMode.Message: |
|||
throw new NotSupportedException("NotSupported_AnonymousPipeMessagesNotSupported"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public AnonymousPipeClientStream(string pipeHandleAsString) |
|||
: this(PipeDirection.In, pipeHandleAsString) |
|||
{ |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public AnonymousPipeClientStream(PipeDirection direction, string pipeHandleAsString) |
|||
: this(direction, new SafePipeHandle((IntPtr)long.Parse(pipeHandleAsString), true)) |
|||
{ |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public AnonymousPipeClientStream(PipeDirection direction, SafePipeHandle safePipeHandle) |
|||
: base(direction, 0) |
|||
{ |
|||
if (direction == PipeDirection.InOut) |
|||
{ |
|||
throw new NotSupportedException("NotSupported_AnonymousPipeUnidirectional"); |
|||
} |
|||
if (UnsafeNativeMethods.GetFileType(safePipeHandle) != 3) |
|||
{ |
|||
throw new IOException("IO_IO_InvalidPipeHandle"); |
|||
} |
|||
base.InitializeHandle(safePipeHandle, true, false); |
|||
base.State = PipeState.Connected; |
|||
} |
|||
|
|||
~AnonymousPipeClientStream() |
|||
{ |
|||
Dispose(false); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,204 @@ |
|||
#if NET20
|
|||
|
|||
using Apewer.Internals.Interop; |
|||
using Microsoft.Win32; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.IO; |
|||
using System.IO.Pipes; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class AnonymousPipeServerStream : PipeStream |
|||
{ |
|||
private SafePipeHandle m_clientHandle; |
|||
|
|||
private bool m_clientHandleExposed; |
|||
|
|||
public SafePipeHandle ClientSafePipeHandle |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
m_clientHandleExposed = true; |
|||
return m_clientHandle; |
|||
} |
|||
} |
|||
|
|||
public override PipeTransmissionMode TransmissionMode |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
return PipeTransmissionMode.Byte; |
|||
} |
|||
} |
|||
|
|||
public override PipeTransmissionMode ReadMode |
|||
{ |
|||
[SecurityCritical] |
|||
set |
|||
{ |
|||
CheckPipePropertyOperations(); |
|||
switch (value) |
|||
{ |
|||
case PipeTransmissionMode.Byte: |
|||
break; |
|||
default: |
|||
throw new ArgumentOutOfRangeException("value", System.SR.GetString("ArgumentOutOfRange_TransmissionModeByteOrMsg")); |
|||
case PipeTransmissionMode.Message: |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_AnonymousPipeMessagesNotSupported")); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public AnonymousPipeServerStream() |
|||
: this(PipeDirection.Out, HandleInheritability.None, 0, null) |
|||
{ |
|||
} |
|||
|
|||
public AnonymousPipeServerStream(PipeDirection direction) |
|||
: this(direction, HandleInheritability.None, 0) |
|||
{ |
|||
} |
|||
|
|||
public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability inheritability) |
|||
: this(direction, inheritability, 0) |
|||
{ |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability inheritability, int bufferSize) |
|||
: base(direction, bufferSize) |
|||
{ |
|||
if (direction == PipeDirection.InOut) |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_AnonymousPipeUnidirectional")); |
|||
} |
|||
if (inheritability >= HandleInheritability.None && inheritability <= HandleInheritability.Inheritable) |
|||
{ |
|||
SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(inheritability); |
|||
Create(direction, secAttrs, bufferSize); |
|||
return; |
|||
} |
|||
throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability inheritability, int bufferSize, PipeSecurity pipeSecurity) |
|||
: base(direction, bufferSize) |
|||
{ |
|||
if (direction == PipeDirection.InOut) |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_AnonymousPipeUnidirectional")); |
|||
} |
|||
if (inheritability >= HandleInheritability.None && inheritability <= HandleInheritability.Inheritable) |
|||
{ |
|||
object obj; |
|||
SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(inheritability, pipeSecurity, out obj); |
|||
try |
|||
{ |
|||
Create(direction, secAttrs, bufferSize); |
|||
} |
|||
finally |
|||
{ |
|||
if (obj != null) |
|||
{ |
|||
((GCHandle)obj).Free(); |
|||
} |
|||
} |
|||
return; |
|||
} |
|||
throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); |
|||
} |
|||
|
|||
~AnonymousPipeServerStream() |
|||
{ |
|||
Dispose(false); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public AnonymousPipeServerStream(PipeDirection direction, SafePipeHandle serverSafePipeHandle, SafePipeHandle clientSafePipeHandle) |
|||
: base(direction, 0) |
|||
{ |
|||
if (direction == PipeDirection.InOut) |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_AnonymousPipeUnidirectional")); |
|||
} |
|||
if (UnsafeNativeMethods.GetFileType(serverSafePipeHandle) != 3) |
|||
{ |
|||
throw new IOException(System.SR.GetString("IO_IO_InvalidPipeHandle")); |
|||
} |
|||
if (UnsafeNativeMethods.GetFileType(clientSafePipeHandle) != 3) |
|||
{ |
|||
throw new IOException(System.SR.GetString("IO_IO_InvalidPipeHandle")); |
|||
} |
|||
base.InitializeHandle(serverSafePipeHandle, true, false); |
|||
m_clientHandle = clientSafePipeHandle; |
|||
m_clientHandleExposed = true; |
|||
base.State = PipeState.Connected; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public string GetClientHandleAsString() |
|||
{ |
|||
m_clientHandleExposed = true; |
|||
return m_clientHandle.DangerousGetHandle().ToString(); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public void DisposeLocalCopyOfClientHandle() |
|||
{ |
|||
if (m_clientHandle != null && !m_clientHandle.IsClosed) |
|||
{ |
|||
m_clientHandle.Dispose(); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
protected override void Dispose(bool disposing) |
|||
{ |
|||
try |
|||
{ |
|||
if (!m_clientHandleExposed && m_clientHandle != null && !m_clientHandle.IsClosed) |
|||
{ |
|||
m_clientHandle.Dispose(); |
|||
} |
|||
} |
|||
finally |
|||
{ |
|||
base.Dispose(disposing); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private void Create(PipeDirection direction, SECURITY_ATTRIBUTES secAttrs, int bufferSize) |
|||
{ |
|||
SafePipeHandle safePipeHandle; |
|||
if (!((direction != PipeDirection.In) ? UnsafeNativeMethods.CreatePipe(out m_clientHandle, out safePipeHandle, secAttrs, bufferSize) : UnsafeNativeMethods.CreatePipe(out safePipeHandle, out m_clientHandle, secAttrs, bufferSize))) |
|||
{ |
|||
System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); |
|||
} |
|||
SafePipeHandle handle; |
|||
if (!UnsafeNativeMethods.DuplicateHandle(UnsafeNativeMethods.GetCurrentProcess(), safePipeHandle, UnsafeNativeMethods.GetCurrentProcess(), out handle, 0u, false, 2u)) |
|||
{ |
|||
System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); |
|||
} |
|||
safePipeHandle.Dispose(); |
|||
base.InitializeHandle(handle, false, false); |
|||
base.State = PipeState.Connected; |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,291 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.IO; |
|||
using System.IO.Pipes; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
using System.Security.Principal; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class NamedPipeClientStream : PipeStream |
|||
{ |
|||
private string m_normalizedPipePath; |
|||
|
|||
private TokenImpersonationLevel m_impersonationLevel; |
|||
|
|||
private PipeOptions m_pipeOptions; |
|||
|
|||
private HandleInheritability m_inheritability; |
|||
|
|||
private int m_access; |
|||
|
|||
public int NumberOfServerInstances |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
CheckPipePropertyOperations(); |
|||
int result; |
|||
if (!Microsoft.Win32.UnsafeNativeMethods.GetNamedPipeHandleState(base.InternalHandle, Microsoft.Win32.UnsafeNativeMethods.NULL, out result, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, 0)) |
|||
{ |
|||
base.WinIOError(Marshal.GetLastWin32Error()); |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
public NamedPipeClientStream(string pipeName) |
|||
: this(".", pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.None, HandleInheritability.None) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName) |
|||
: this(serverName, pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.None, HandleInheritability.None) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName, PipeDirection direction) |
|||
: this(serverName, pipeName, direction, PipeOptions.None, TokenImpersonationLevel.None, HandleInheritability.None) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName, PipeDirection direction, PipeOptions options) |
|||
: this(serverName, pipeName, direction, options, TokenImpersonationLevel.None, HandleInheritability.None) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel) |
|||
: this(serverName, pipeName, direction, options, impersonationLevel, HandleInheritability.None) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability) |
|||
: base(direction, 0) |
|||
{ |
|||
if (pipeName == null) |
|||
{ |
|||
throw new ArgumentNullException("pipeName"); |
|||
} |
|||
if (serverName == null) |
|||
{ |
|||
throw new ArgumentNullException("serverName", System.SR.GetString("ArgumentNull_ServerName")); |
|||
} |
|||
if (pipeName.Length == 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NeedNonemptyPipeName")); |
|||
} |
|||
if (serverName.Length == 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_EmptyServerName")); |
|||
} |
|||
if ((options & (PipeOptions)1073741823) != 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("options", System.SR.GetString("ArgumentOutOfRange_OptionsInvalid")); |
|||
} |
|||
if (impersonationLevel >= TokenImpersonationLevel.None && impersonationLevel <= TokenImpersonationLevel.Delegation) |
|||
{ |
|||
if (inheritability >= HandleInheritability.None && inheritability <= HandleInheritability.Inheritable) |
|||
{ |
|||
m_normalizedPipePath = Path.GetFullPath("\\\\" + serverName + "\\pipe\\" + pipeName); |
|||
if (string.Compare(m_normalizedPipePath, "\\\\.\\pipe\\anonymous", StringComparison.OrdinalIgnoreCase) == 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("pipeName", System.SR.GetString("ArgumentOutOfRange_AnonymousReserved")); |
|||
} |
|||
m_inheritability = inheritability; |
|||
m_impersonationLevel = impersonationLevel; |
|||
m_pipeOptions = options; |
|||
if ((PipeDirection.In & direction) != 0) |
|||
{ |
|||
m_access |= -2147483648; |
|||
} |
|||
if ((PipeDirection.Out & direction) != 0) |
|||
{ |
|||
m_access |= 1073741824; |
|||
} |
|||
return; |
|||
} |
|||
throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); |
|||
} |
|||
throw new ArgumentOutOfRangeException("impersonationLevel", System.SR.GetString("ArgumentOutOfRange_ImpersonationInvalid")); |
|||
} |
|||
|
|||
public NamedPipeClientStream(string serverName, string pipeName, PipeAccessRights desiredAccessRights, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability) |
|||
: base(DirectionFromRights(desiredAccessRights), 0) |
|||
{ |
|||
if (pipeName == null) |
|||
{ |
|||
throw new ArgumentNullException("pipeName"); |
|||
} |
|||
if (serverName == null) |
|||
{ |
|||
throw new ArgumentNullException("serverName", System.SR.GetString("ArgumentNull_ServerName")); |
|||
} |
|||
if (pipeName.Length == 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NeedNonemptyPipeName")); |
|||
} |
|||
if (serverName.Length == 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_EmptyServerName")); |
|||
} |
|||
if ((options & (PipeOptions)1073741823) != 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("options", System.SR.GetString("ArgumentOutOfRange_OptionsInvalid")); |
|||
} |
|||
if (impersonationLevel >= TokenImpersonationLevel.None && impersonationLevel <= TokenImpersonationLevel.Delegation) |
|||
{ |
|||
if (inheritability >= HandleInheritability.None && inheritability <= HandleInheritability.Inheritable) |
|||
{ |
|||
if ((desiredAccessRights & ~(PipeAccessRights.ReadData | PipeAccessRights.WriteData | PipeAccessRights.ReadAttributes | PipeAccessRights.WriteAttributes | PipeAccessRights.ReadExtendedAttributes | PipeAccessRights.WriteExtendedAttributes | PipeAccessRights.CreateNewInstance | PipeAccessRights.Delete | PipeAccessRights.ReadPermissions | PipeAccessRights.ChangePermissions | PipeAccessRights.TakeOwnership | PipeAccessRights.Synchronize | PipeAccessRights.AccessSystemSecurity)) != 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("desiredAccessRights", System.SR.GetString("ArgumentOutOfRange_InvalidPipeAccessRights")); |
|||
} |
|||
m_normalizedPipePath = Path.GetFullPath("\\\\" + serverName + "\\pipe\\" + pipeName); |
|||
if (string.Compare(m_normalizedPipePath, "\\\\.\\pipe\\anonymous", StringComparison.OrdinalIgnoreCase) == 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("pipeName", System.SR.GetString("ArgumentOutOfRange_AnonymousReserved")); |
|||
} |
|||
m_inheritability = inheritability; |
|||
m_impersonationLevel = impersonationLevel; |
|||
m_pipeOptions = options; |
|||
m_access = (int)desiredAccessRights; |
|||
return; |
|||
} |
|||
throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); |
|||
} |
|||
throw new ArgumentOutOfRangeException("impersonationLevel", System.SR.GetString("ArgumentOutOfRange_ImpersonationInvalid")); |
|||
} |
|||
|
|||
private static PipeDirection DirectionFromRights(PipeAccessRights rights) |
|||
{ |
|||
PipeDirection pipeDirection = (PipeDirection)0; |
|||
if ((rights & PipeAccessRights.ReadData) != 0) |
|||
{ |
|||
pipeDirection |= PipeDirection.In; |
|||
} |
|||
if ((rights & PipeAccessRights.WriteData) != 0) |
|||
{ |
|||
pipeDirection |= PipeDirection.Out; |
|||
} |
|||
return pipeDirection; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public NamedPipeClientStream(PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle) |
|||
: base(direction, 0) |
|||
{ |
|||
if (Microsoft.Win32.UnsafeNativeMethods.GetFileType(safePipeHandle) != 3) |
|||
{ |
|||
throw new IOException(System.SR.GetString("IO_IO_InvalidPipeHandle")); |
|||
} |
|||
base.InitializeHandle(safePipeHandle, true, isAsync); |
|||
if (isConnected) |
|||
{ |
|||
base.State = PipeState.Connected; |
|||
} |
|||
} |
|||
|
|||
~NamedPipeClientStream() |
|||
{ |
|||
Dispose(false); |
|||
} |
|||
|
|||
public void Connect() |
|||
{ |
|||
Connect(-1); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public void Connect(int timeout) |
|||
{ |
|||
CheckConnectOperationsClient(); |
|||
if (timeout < 0 && timeout != -1) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("timeout", System.SR.GetString("ArgumentOutOfRange_InvalidTimeout")); |
|||
} |
|||
SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(m_inheritability); |
|||
int num = (int)m_pipeOptions; |
|||
if (m_impersonationLevel != 0) |
|||
{ |
|||
num |= 0x100000; |
|||
num |= (int)(m_impersonationLevel - 1) << 16; |
|||
} |
|||
int tickCount = Environment.TickCount; |
|||
int num2 = 0; |
|||
do |
|||
{ |
|||
if (!Microsoft.Win32.UnsafeNativeMethods.WaitNamedPipe(m_normalizedPipePath, timeout - num2)) |
|||
{ |
|||
int lastWin32Error = Marshal.GetLastWin32Error(); |
|||
switch (lastWin32Error) |
|||
{ |
|||
case 2: |
|||
continue; |
|||
case 0: |
|||
goto end_IL_005c; |
|||
} |
|||
System.IO.__Error.WinIOError(lastWin32Error, string.Empty); |
|||
} |
|||
SafePipeHandle safePipeHandle = Microsoft.Win32.UnsafeNativeMethods.CreateNamedPipeClient(m_normalizedPipePath, m_access, FileShare.None, secAttrs, FileMode.Open, num, Microsoft.Win32.UnsafeNativeMethods.NULL); |
|||
if (safePipeHandle.IsInvalid) |
|||
{ |
|||
int lastWin32Error2 = Marshal.GetLastWin32Error(); |
|||
if (lastWin32Error2 != 231) |
|||
{ |
|||
System.IO.__Error.WinIOError(lastWin32Error2, string.Empty); |
|||
goto IL_00cc; |
|||
} |
|||
continue; |
|||
} |
|||
goto IL_00cc; |
|||
IL_00cc: |
|||
base.InitializeHandle(safePipeHandle, false, (m_pipeOptions & PipeOptions.Asynchronous) != PipeOptions.None); |
|||
base.State = PipeState.Connected; |
|||
return; |
|||
continue; |
|||
end_IL_005c: |
|||
break; |
|||
} |
|||
while (timeout == -1 || (num2 = Environment.TickCount - tickCount) < timeout); |
|||
throw new TimeoutException(); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
protected internal override void CheckPipePropertyOperations() |
|||
{ |
|||
base.CheckPipePropertyOperations(); |
|||
if (base.State == PipeState.WaitingToConnect) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeNotYetConnected")); |
|||
} |
|||
if (base.State != PipeState.Broken) |
|||
{ |
|||
return; |
|||
} |
|||
throw new IOException(System.SR.GetString("IO_IO_PipeBroken")); |
|||
} |
|||
|
|||
private void CheckConnectOperationsClient() |
|||
{ |
|||
if (base.State == PipeState.Connected) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeAlreadyConnected")); |
|||
} |
|||
if (base.State == PipeState.Closed) |
|||
{ |
|||
System.IO.__Error.PipeNotOpen(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,459 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32; |
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.IO; |
|||
using System.IO.Pipes; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.ConstrainedExecution; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
using System.Text; |
|||
using System.Threading; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class NamedPipeServerStream : PipeStream |
|||
{ |
|||
internal class ExecuteHelper |
|||
{ |
|||
internal PipeStreamImpersonationWorker m_userCode; |
|||
|
|||
internal SafePipeHandle m_handle; |
|||
|
|||
internal bool m_mustRevert; |
|||
|
|||
internal int m_impersonateErrorCode; |
|||
|
|||
internal int m_revertImpersonateErrorCode; |
|||
|
|||
[SecurityCritical] |
|||
internal ExecuteHelper(PipeStreamImpersonationWorker userCode, SafePipeHandle handle) |
|||
{ |
|||
m_userCode = userCode; |
|||
m_handle = handle; |
|||
} |
|||
} |
|||
|
|||
public const int MaxAllowedServerInstances = -1; |
|||
|
|||
private static int s_maxUsernameLength; |
|||
|
|||
private static readonly IOCompletionCallback WaitForConnectionCallback; |
|||
|
|||
private static RuntimeHelpers.TryCode tryCode; |
|||
|
|||
private static RuntimeHelpers.CleanupCode cleanupCode; |
|||
|
|||
[SecurityCritical] |
|||
static unsafe NamedPipeServerStream() |
|||
{ |
|||
s_maxUsernameLength = 20; |
|||
WaitForConnectionCallback = AsyncWaitForConnectionCallback; |
|||
tryCode = ImpersonateAndTryCode; |
|||
cleanupCode = RevertImpersonationOnBackout; |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName) |
|||
: this(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction) |
|||
: this(pipeName, direction, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, transmissionMode, PipeOptions.None, 0, 0, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, 0, 0, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, null, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, HandleInheritability.None, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability) |
|||
: this(pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, inheritability, (PipeAccessRights)0) |
|||
{ |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public NamedPipeServerStream(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability, PipeAccessRights additionalAccessRights) |
|||
: base(direction, transmissionMode, outBufferSize) |
|||
{ |
|||
if (pipeName == null) |
|||
{ |
|||
throw new ArgumentNullException("pipeName"); |
|||
} |
|||
if (pipeName.Length == 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NeedNonemptyPipeName")); |
|||
} |
|||
if ((options & (PipeOptions)1073741823) != 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("options", System.SR.GetString("ArgumentOutOfRange_OptionsInvalid")); |
|||
} |
|||
if (inBufferSize < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("inBufferSize", System.SR.GetString("ArgumentOutOfRange_NeedNonNegNum")); |
|||
} |
|||
if ((maxNumberOfServerInstances < 1 || maxNumberOfServerInstances > 254) && maxNumberOfServerInstances != -1) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", System.SR.GetString("ArgumentOutOfRange_MaxNumServerInstances")); |
|||
} |
|||
if (inheritability >= HandleInheritability.None && inheritability <= HandleInheritability.Inheritable) |
|||
{ |
|||
if ((additionalAccessRights & ~(PipeAccessRights.ChangePermissions | PipeAccessRights.TakeOwnership | PipeAccessRights.AccessSystemSecurity)) != 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("additionalAccessRights", System.SR.GetString("ArgumentOutOfRange_AdditionalAccessLimited")); |
|||
} |
|||
if (Environment.OSVersion.Platform == PlatformID.Win32Windows) |
|||
{ |
|||
throw new PlatformNotSupportedException(System.SR.GetString("PlatformNotSupported_NamedPipeServers")); |
|||
} |
|||
string fullPath = Path.GetFullPath("\\\\.\\pipe\\" + pipeName); |
|||
if (string.Compare(fullPath, "\\\\.\\pipe\\anonymous", StringComparison.OrdinalIgnoreCase) == 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("pipeName", System.SR.GetString("ArgumentOutOfRange_AnonymousReserved")); |
|||
} |
|||
object obj = null; |
|||
SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(inheritability, pipeSecurity, out obj); |
|||
try |
|||
{ |
|||
Create(fullPath, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, additionalAccessRights, secAttrs); |
|||
} |
|||
finally |
|||
{ |
|||
if (obj != null) |
|||
{ |
|||
((GCHandle)obj).Free(); |
|||
} |
|||
} |
|||
return; |
|||
} |
|||
throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
|||
public NamedPipeServerStream(PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle) |
|||
: base(direction, PipeTransmissionMode.Byte, 0) |
|||
{ |
|||
if (UnsafeNativeMethods.GetFileType(safePipeHandle) != 3) |
|||
{ |
|||
throw new IOException(System.SR.GetString("Pipe_InvalidHandle")); |
|||
} |
|||
base.InitializeHandle(safePipeHandle, true, isAsync); |
|||
if (isConnected) |
|||
{ |
|||
base.State = PipeState.Connected; |
|||
} |
|||
} |
|||
|
|||
~NamedPipeServerStream() |
|||
{ |
|||
Dispose(false); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private void Create(string fullPipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeAccessRights rights, SECURITY_ATTRIBUTES secAttrs) |
|||
{ |
|||
int openMode = (int)direction | ((maxNumberOfServerInstances == 1) ? 524288 : 0) | (int)options | (int)rights; |
|||
int pipeMode = (int)transmissionMode << 2 | (int)transmissionMode << 1; |
|||
if (maxNumberOfServerInstances == -1) |
|||
{ |
|||
maxNumberOfServerInstances = 255; |
|||
} |
|||
SafePipeHandle safePipeHandle = UnsafeNativeMethods.CreateNamedPipe(fullPipeName, openMode, pipeMode, maxNumberOfServerInstances, outBufferSize, inBufferSize, 0, secAttrs); |
|||
if (safePipeHandle.IsInvalid) |
|||
{ |
|||
System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); |
|||
} |
|||
base.InitializeHandle(safePipeHandle, false, (options & PipeOptions.Asynchronous) != PipeOptions.None); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public void WaitForConnection() |
|||
{ |
|||
CheckConnectOperationsServer(); |
|||
if (base.IsAsync) |
|||
{ |
|||
IAsyncResult asyncResult = BeginWaitForConnection(null, null); |
|||
EndWaitForConnection(asyncResult); |
|||
} |
|||
else |
|||
{ |
|||
if (!UnsafeNativeMethods.ConnectNamedPipe(base.InternalHandle, UnsafeNativeMethods.NULL)) |
|||
{ |
|||
int lastWin32Error = Marshal.GetLastWin32Error(); |
|||
if (lastWin32Error != 535) |
|||
{ |
|||
System.IO.__Error.WinIOError(lastWin32Error, string.Empty); |
|||
} |
|||
if (lastWin32Error == 535 && base.State == PipeState.Connected) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeAlreadyConnected")); |
|||
} |
|||
} |
|||
base.State = PipeState.Connected; |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)] |
|||
public unsafe IAsyncResult BeginWaitForConnection(AsyncCallback callback, object state) |
|||
{ |
|||
CheckConnectOperationsServer(); |
|||
if (!base.IsAsync) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeNotAsync")); |
|||
} |
|||
PipeAsyncResult pipeAsyncResult = new PipeAsyncResult(); |
|||
pipeAsyncResult._handle = base.InternalHandle; |
|||
pipeAsyncResult._userCallback = callback; |
|||
pipeAsyncResult._userStateObject = state; |
|||
ManualResetEvent manualResetEvent = pipeAsyncResult._waitHandle = new ManualResetEvent(false); |
|||
Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, pipeAsyncResult); |
|||
NativeOverlapped* ptr = pipeAsyncResult._overlapped = overlapped.Pack(WaitForConnectionCallback, null); |
|||
if (!UnsafeNativeMethods.ConnectNamedPipe(base.InternalHandle, ptr)) |
|||
{ |
|||
int lastWin32Error = Marshal.GetLastWin32Error(); |
|||
switch (lastWin32Error) |
|||
{ |
|||
case 535: |
|||
ptr->InternalLow = IntPtr.Zero; |
|||
if (base.State == PipeState.Connected) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeAlreadyConnected")); |
|||
} |
|||
pipeAsyncResult.CallUserCallback(); |
|||
break; |
|||
default: |
|||
System.IO.__Error.WinIOError(lastWin32Error, string.Empty); |
|||
break; |
|||
case 997: |
|||
break; |
|||
} |
|||
} |
|||
return pipeAsyncResult; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public unsafe void EndWaitForConnection(IAsyncResult asyncResult) |
|||
{ |
|||
CheckConnectOperationsServer(); |
|||
if (asyncResult == null) |
|||
{ |
|||
throw new ArgumentNullException("asyncResult"); |
|||
} |
|||
if (!base.IsAsync) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeNotAsync")); |
|||
} |
|||
PipeAsyncResult pipeAsyncResult = asyncResult as PipeAsyncResult; |
|||
if (pipeAsyncResult == null) |
|||
{ |
|||
System.IO.__Error.WrongAsyncResult(); |
|||
} |
|||
if (1 == Interlocked.CompareExchange(ref pipeAsyncResult._EndXxxCalled, 1, 0)) |
|||
{ |
|||
System.IO.__Error.EndWaitForConnectionCalledTwice(); |
|||
} |
|||
WaitHandle waitHandle = pipeAsyncResult._waitHandle; |
|||
if (waitHandle != null) |
|||
{ |
|||
try |
|||
{ |
|||
waitHandle.WaitOne(); |
|||
} |
|||
finally |
|||
{ |
|||
waitHandle.Close(); |
|||
} |
|||
} |
|||
NativeOverlapped* overlapped = pipeAsyncResult._overlapped; |
|||
if (overlapped != null) |
|||
{ |
|||
Overlapped.Free(overlapped); |
|||
} |
|||
if (pipeAsyncResult._errorCode != 0) |
|||
{ |
|||
System.IO.__Error.WinIOError(pipeAsyncResult._errorCode, string.Empty); |
|||
} |
|||
base.State = PipeState.Connected; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
public void Disconnect() |
|||
{ |
|||
CheckDisconnectOperations(); |
|||
if (!UnsafeNativeMethods.DisconnectNamedPipe(base.InternalHandle)) |
|||
{ |
|||
System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); |
|||
} |
|||
base.State = PipeState.Disconnected; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)] |
|||
public void RunAsClient(PipeStreamImpersonationWorker impersonationWorker) |
|||
{ |
|||
base.CheckWriteOperations(); |
|||
ExecuteHelper executeHelper = new ExecuteHelper(impersonationWorker, base.InternalHandle); |
|||
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, executeHelper); |
|||
if (executeHelper.m_impersonateErrorCode != 0) |
|||
{ |
|||
base.WinIOError(executeHelper.m_impersonateErrorCode); |
|||
} |
|||
else if (executeHelper.m_revertImpersonateErrorCode != 0) |
|||
{ |
|||
base.WinIOError(executeHelper.m_revertImpersonateErrorCode); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private static void ImpersonateAndTryCode(object helper) |
|||
{ |
|||
ExecuteHelper executeHelper = (ExecuteHelper)helper; |
|||
RuntimeHelpers.PrepareConstrainedRegions(); |
|||
try |
|||
{ |
|||
} |
|||
finally |
|||
{ |
|||
if (UnsafeNativeMethods.ImpersonateNamedPipeClient(executeHelper.m_handle)) |
|||
{ |
|||
executeHelper.m_mustRevert = true; |
|||
} |
|||
else |
|||
{ |
|||
executeHelper.m_impersonateErrorCode = Marshal.GetLastWin32Error(); |
|||
} |
|||
} |
|||
if (executeHelper.m_mustRevert) |
|||
{ |
|||
executeHelper.m_userCode(); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[PrePrepareMethod] |
|||
private static void RevertImpersonationOnBackout(object helper, bool exceptionThrown) |
|||
{ |
|||
ExecuteHelper executeHelper = (ExecuteHelper)helper; |
|||
if (executeHelper.m_mustRevert && !UnsafeNativeMethods.RevertToSelf()) |
|||
{ |
|||
executeHelper.m_revertImpersonateErrorCode = Marshal.GetLastWin32Error(); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)] |
|||
public string GetImpersonationUserName() |
|||
{ |
|||
base.CheckWriteOperations(); |
|||
StringBuilder stringBuilder = new StringBuilder(s_maxUsernameLength); |
|||
if (!UnsafeNativeMethods.GetNamedPipeHandleState(base.InternalHandle, UnsafeNativeMethods.NULL, UnsafeNativeMethods.NULL, UnsafeNativeMethods.NULL, UnsafeNativeMethods.NULL, stringBuilder, s_maxUsernameLength)) |
|||
{ |
|||
base.WinIOError(Marshal.GetLastWin32Error()); |
|||
} |
|||
return stringBuilder.ToString(); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private unsafe static void AsyncWaitForConnectionCallback(uint errorCode, uint numBytes, NativeOverlapped* pOverlapped) |
|||
{ |
|||
Overlapped overlapped = Overlapped.Unpack(pOverlapped); |
|||
PipeAsyncResult pipeAsyncResult = (PipeAsyncResult)overlapped.AsyncResult; |
|||
if (errorCode == 535) |
|||
{ |
|||
errorCode = 0u; |
|||
} |
|||
pipeAsyncResult._errorCode = (int)errorCode; |
|||
pipeAsyncResult._completedSynchronously = false; |
|||
pipeAsyncResult._isComplete = true; |
|||
ManualResetEvent waitHandle = pipeAsyncResult._waitHandle; |
|||
if (waitHandle != null && !waitHandle.Set()) |
|||
{ |
|||
System.IO.__Error.WinIOError(); |
|||
} |
|||
AsyncCallback userCallback = pipeAsyncResult._userCallback; |
|||
if (userCallback != null) |
|||
{ |
|||
userCallback(pipeAsyncResult); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private void CheckConnectOperationsServer() |
|||
{ |
|||
if (base.InternalHandle == null) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeHandleNotSet")); |
|||
} |
|||
if (base.State == PipeState.Closed) |
|||
{ |
|||
System.IO.__Error.PipeNotOpen(); |
|||
} |
|||
if (base.InternalHandle.IsClosed) |
|||
{ |
|||
System.IO.__Error.PipeNotOpen(); |
|||
} |
|||
if (base.State != PipeState.Broken) |
|||
{ |
|||
return; |
|||
} |
|||
throw new IOException(System.SR.GetString("IO_IO_PipeBroken")); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
private void CheckDisconnectOperations() |
|||
{ |
|||
if (base.State == PipeState.WaitingToConnect) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeNotYetConnected")); |
|||
} |
|||
if (base.State == PipeState.Disconnected) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeAlreadyDisconnected")); |
|||
} |
|||
if (base.InternalHandle == null) |
|||
{ |
|||
throw new InvalidOperationException(System.SR.GetString("InvalidOperation_PipeHandleNotSet")); |
|||
} |
|||
if (base.State == PipeState.Closed) |
|||
{ |
|||
System.IO.__Error.PipeNotOpen(); |
|||
} |
|||
if (base.InternalHandle.IsClosed) |
|||
{ |
|||
System.IO.__Error.PipeNotOpen(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,34 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[Flags] |
|||
internal enum PipeAccessRights |
|||
{ |
|||
ReadData = 0x1, |
|||
WriteData = 0x2, |
|||
ReadAttributes = 0x80, |
|||
WriteAttributes = 0x100, |
|||
ReadExtendedAttributes = 0x8, |
|||
WriteExtendedAttributes = 0x10, |
|||
CreateNewInstance = 0x4, |
|||
Delete = 0x10000, |
|||
ReadPermissions = 0x20000, |
|||
ChangePermissions = 0x40000, |
|||
TakeOwnership = 0x80000, |
|||
Synchronize = 0x100000, |
|||
FullControl = 0x1F019F, |
|||
Read = 0x20089, |
|||
Write = 0x112, |
|||
ReadWrite = 0x2019B, |
|||
AccessSystemSecurity = 0x1000000 |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,67 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.IO.Pipes; |
|||
using System.Security.AccessControl; |
|||
using System.Security.Permissions; |
|||
using System.Security.Principal; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class PipeAccessRule : AccessRule |
|||
{ |
|||
public PipeAccessRights PipeAccessRights |
|||
{ |
|||
get |
|||
{ |
|||
return RightsFromAccessMask(base.AccessMask); |
|||
} |
|||
} |
|||
|
|||
public PipeAccessRule(string identity, PipeAccessRights rights, AccessControlType type) |
|||
: this(new NTAccount(identity), AccessMaskFromRights(rights, type), false, type) |
|||
{ |
|||
} |
|||
|
|||
public PipeAccessRule(IdentityReference identity, PipeAccessRights rights, AccessControlType type) |
|||
: this(identity, AccessMaskFromRights(rights, type), false, type) |
|||
{ |
|||
} |
|||
|
|||
internal PipeAccessRule(IdentityReference identity, int accessMask, bool isInherited, AccessControlType type) |
|||
: base(identity, accessMask, isInherited, InheritanceFlags.None, PropagationFlags.None, type) |
|||
{ |
|||
} |
|||
|
|||
internal static int AccessMaskFromRights(PipeAccessRights rights, AccessControlType controlType) |
|||
{ |
|||
if (rights >= (PipeAccessRights)0 && rights <= (PipeAccessRights.ReadData | PipeAccessRights.WriteData | PipeAccessRights.ReadAttributes | PipeAccessRights.WriteAttributes | PipeAccessRights.ReadExtendedAttributes | PipeAccessRights.WriteExtendedAttributes | PipeAccessRights.CreateNewInstance | PipeAccessRights.Delete | PipeAccessRights.ReadPermissions | PipeAccessRights.ChangePermissions | PipeAccessRights.TakeOwnership | PipeAccessRights.Synchronize | PipeAccessRights.AccessSystemSecurity)) |
|||
{ |
|||
switch (controlType) |
|||
{ |
|||
case AccessControlType.Allow: |
|||
rights |= PipeAccessRights.Synchronize; |
|||
break; |
|||
case AccessControlType.Deny: |
|||
if (rights != PipeAccessRights.FullControl) |
|||
{ |
|||
rights &= ~PipeAccessRights.Synchronize; |
|||
} |
|||
break; |
|||
} |
|||
return (int)rights; |
|||
} |
|||
throw new ArgumentOutOfRangeException("rights", System.SR.GetString("ArgumentOutOfRange_NeedValidPipeAccessRights")); |
|||
} |
|||
|
|||
internal static PipeAccessRights RightsFromAccessMask(int accessMask) |
|||
{ |
|||
return (PipeAccessRights)accessMask; |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,110 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
using System.Threading; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] |
|||
[PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")] |
|||
internal sealed class PipeAsyncResult : IAsyncResult |
|||
{ |
|||
internal AsyncCallback _userCallback; |
|||
|
|||
internal object _userStateObject; |
|||
|
|||
internal ManualResetEvent _waitHandle; |
|||
|
|||
internal SafePipeHandle _handle; |
|||
|
|||
internal unsafe NativeOverlapped* _overlapped; |
|||
|
|||
internal int _EndXxxCalled; |
|||
|
|||
internal int _errorCode; |
|||
|
|||
internal bool _isComplete; |
|||
|
|||
internal bool _completedSynchronously; |
|||
|
|||
public object AsyncState |
|||
{ |
|||
get |
|||
{ |
|||
return _userStateObject; |
|||
} |
|||
} |
|||
|
|||
public bool IsCompleted |
|||
{ |
|||
get |
|||
{ |
|||
return _isComplete; |
|||
} |
|||
} |
|||
|
|||
public unsafe WaitHandle AsyncWaitHandle |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
if (_waitHandle == null) |
|||
{ |
|||
ManualResetEvent manualResetEvent = new ManualResetEvent(false); |
|||
if (_overlapped != null && _overlapped->EventHandle != IntPtr.Zero) |
|||
{ |
|||
manualResetEvent.SafeWaitHandle = new SafeWaitHandle(_overlapped->EventHandle, true); |
|||
} |
|||
if (_isComplete) |
|||
{ |
|||
manualResetEvent.Set(); |
|||
} |
|||
_waitHandle = manualResetEvent; |
|||
} |
|||
return _waitHandle; |
|||
} |
|||
} |
|||
|
|||
public bool CompletedSynchronously |
|||
{ |
|||
get |
|||
{ |
|||
return _completedSynchronously; |
|||
} |
|||
} |
|||
|
|||
private void CallUserCallbackWorker(object callbackState) |
|||
{ |
|||
_isComplete = true; |
|||
if (_waitHandle != null) |
|||
{ |
|||
_waitHandle.Set(); |
|||
} |
|||
_userCallback(this); |
|||
} |
|||
|
|||
internal void CallUserCallback() |
|||
{ |
|||
if (_userCallback != null) |
|||
{ |
|||
_completedSynchronously = false; |
|||
ThreadPool.QueueUserWorkItem(CallUserCallbackWorker); |
|||
} |
|||
else |
|||
{ |
|||
_isComplete = true; |
|||
if (_waitHandle != null) |
|||
{ |
|||
_waitHandle.Set(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,50 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.IO.Pipes; |
|||
using System.Security.AccessControl; |
|||
using System.Security.Permissions; |
|||
using System.Security.Principal; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal sealed class PipeAuditRule : AuditRule |
|||
{ |
|||
public PipeAccessRights PipeAccessRights |
|||
{ |
|||
get |
|||
{ |
|||
return PipeAccessRule.RightsFromAccessMask(base.AccessMask); |
|||
} |
|||
} |
|||
|
|||
public PipeAuditRule(IdentityReference identity, PipeAccessRights rights, AuditFlags flags) |
|||
: this(identity, AccessMaskFromRights(rights), false, flags) |
|||
{ |
|||
} |
|||
|
|||
public PipeAuditRule(string identity, PipeAccessRights rights, AuditFlags flags) |
|||
: this(new NTAccount(identity), AccessMaskFromRights(rights), false, flags) |
|||
{ |
|||
} |
|||
|
|||
internal PipeAuditRule(IdentityReference identity, int accessMask, bool isInherited, AuditFlags flags) |
|||
: base(identity, accessMask, isInherited, InheritanceFlags.None, PropagationFlags.None, flags) |
|||
{ |
|||
} |
|||
|
|||
private static int AccessMaskFromRights(PipeAccessRights rights) |
|||
{ |
|||
if (rights >= (PipeAccessRights)0 && rights <= (PipeAccessRights.ReadData | PipeAccessRights.WriteData | PipeAccessRights.ReadAttributes | PipeAccessRights.WriteAttributes | PipeAccessRights.ReadExtendedAttributes | PipeAccessRights.WriteExtendedAttributes | PipeAccessRights.CreateNewInstance | PipeAccessRights.Delete | PipeAccessRights.ReadPermissions | PipeAccessRights.ChangePermissions | PipeAccessRights.TakeOwnership | PipeAccessRights.Synchronize | PipeAccessRights.AccessSystemSecurity)) |
|||
{ |
|||
return (int)rights; |
|||
} |
|||
throw new ArgumentOutOfRangeException("rights", System.SR.GetString("ArgumentOutOfRange_NeedValidPipeAccessRights")); |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,20 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[Serializable] |
|||
internal enum PipeDirection |
|||
{ |
|||
In = 1, |
|||
Out, |
|||
InOut |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,21 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[Serializable] |
|||
[Flags] |
|||
internal enum PipeOptions |
|||
{ |
|||
None = 0x0, |
|||
WriteThrough = int.MinValue, |
|||
Asynchronous = 0x40000000 |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,243 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.IO.Pipes; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.AccessControl; |
|||
using System.Security.Permissions; |
|||
using System.Security.Principal; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)] |
|||
internal class PipeSecurity : NativeObjectSecurity |
|||
{ |
|||
public override Type AccessRightType |
|||
{ |
|||
get |
|||
{ |
|||
return typeof(PipeAccessRights); |
|||
} |
|||
} |
|||
|
|||
public override Type AccessRuleType |
|||
{ |
|||
get |
|||
{ |
|||
return typeof(PipeAccessRule); |
|||
} |
|||
} |
|||
|
|||
public override Type AuditRuleType |
|||
{ |
|||
get |
|||
{ |
|||
return typeof(PipeAuditRule); |
|||
} |
|||
} |
|||
|
|||
public PipeSecurity() |
|||
: base(false, ResourceType.KernelObject) |
|||
{ |
|||
} |
|||
|
|||
internal PipeSecurity(SafePipeHandle safeHandle, AccessControlSections includeSections) |
|||
: base(false, ResourceType.KernelObject, safeHandle, includeSections) |
|||
{ |
|||
} |
|||
|
|||
public void AddAccessRule(PipeAccessRule rule) |
|||
{ |
|||
if (rule == null) |
|||
{ |
|||
throw new ArgumentNullException("rule"); |
|||
} |
|||
base.AddAccessRule(rule); |
|||
} |
|||
|
|||
public void SetAccessRule(PipeAccessRule rule) |
|||
{ |
|||
if (rule == null) |
|||
{ |
|||
throw new ArgumentNullException("rule"); |
|||
} |
|||
base.SetAccessRule(rule); |
|||
} |
|||
|
|||
public void ResetAccessRule(PipeAccessRule rule) |
|||
{ |
|||
if (rule == null) |
|||
{ |
|||
throw new ArgumentNullException("rule"); |
|||
} |
|||
base.ResetAccessRule(rule); |
|||
} |
|||
|
|||
public bool RemoveAccessRule(PipeAccessRule rule) |
|||
{ |
|||
if (rule == null) |
|||
{ |
|||
throw new ArgumentNullException("rule"); |
|||
} |
|||
AuthorizationRuleCollection accessRules = base.GetAccessRules(true, true, rule.IdentityReference.GetType()); |
|||
for (int i = 0; i < accessRules.Count; i++) |
|||
{ |
|||
PipeAccessRule pipeAccessRule = accessRules[i] as PipeAccessRule; |
|||
if (pipeAccessRule != null && pipeAccessRule.PipeAccessRights == rule.PipeAccessRights && pipeAccessRule.IdentityReference == rule.IdentityReference && pipeAccessRule.AccessControlType == rule.AccessControlType) |
|||
{ |
|||
return base.RemoveAccessRule(rule); |
|||
} |
|||
} |
|||
if (rule.PipeAccessRights != PipeAccessRights.FullControl) |
|||
{ |
|||
return base.RemoveAccessRule(new PipeAccessRule(rule.IdentityReference, PipeAccessRule.AccessMaskFromRights(rule.PipeAccessRights, AccessControlType.Deny), false, rule.AccessControlType)); |
|||
} |
|||
return base.RemoveAccessRule(rule); |
|||
} |
|||
|
|||
public void RemoveAccessRuleSpecific(PipeAccessRule rule) |
|||
{ |
|||
if (rule == null) |
|||
{ |
|||
throw new ArgumentNullException("rule"); |
|||
} |
|||
AuthorizationRuleCollection accessRules = base.GetAccessRules(true, true, rule.IdentityReference.GetType()); |
|||
for (int i = 0; i < accessRules.Count; i++) |
|||
{ |
|||
PipeAccessRule pipeAccessRule = accessRules[i] as PipeAccessRule; |
|||
if (pipeAccessRule != null && pipeAccessRule.PipeAccessRights == rule.PipeAccessRights && pipeAccessRule.IdentityReference == rule.IdentityReference && pipeAccessRule.AccessControlType == rule.AccessControlType) |
|||
{ |
|||
base.RemoveAccessRuleSpecific(rule); |
|||
return; |
|||
} |
|||
} |
|||
if (rule.PipeAccessRights != PipeAccessRights.FullControl) |
|||
{ |
|||
base.RemoveAccessRuleSpecific(new PipeAccessRule(rule.IdentityReference, PipeAccessRule.AccessMaskFromRights(rule.PipeAccessRights, AccessControlType.Deny), false, rule.AccessControlType)); |
|||
} |
|||
else |
|||
{ |
|||
base.RemoveAccessRuleSpecific(rule); |
|||
} |
|||
} |
|||
|
|||
public void AddAuditRule(PipeAuditRule rule) |
|||
{ |
|||
base.AddAuditRule(rule); |
|||
} |
|||
|
|||
public void SetAuditRule(PipeAuditRule rule) |
|||
{ |
|||
base.SetAuditRule(rule); |
|||
} |
|||
|
|||
public bool RemoveAuditRule(PipeAuditRule rule) |
|||
{ |
|||
return base.RemoveAuditRule(rule); |
|||
} |
|||
|
|||
public void RemoveAuditRuleAll(PipeAuditRule rule) |
|||
{ |
|||
base.RemoveAuditRuleAll(rule); |
|||
} |
|||
|
|||
public void RemoveAuditRuleSpecific(PipeAuditRule rule) |
|||
{ |
|||
base.RemoveAuditRuleSpecific(rule); |
|||
} |
|||
|
|||
public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type) |
|||
{ |
|||
if (inheritanceFlags != 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NonContainerInvalidAnyFlag"), "inheritanceFlags"); |
|||
} |
|||
if (propagationFlags != 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NonContainerInvalidAnyFlag"), "propagationFlags"); |
|||
} |
|||
return new PipeAccessRule(identityReference, accessMask, isInherited, type); |
|||
} |
|||
|
|||
public sealed override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags) |
|||
{ |
|||
if (inheritanceFlags != 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NonContainerInvalidAnyFlag"), "inheritanceFlags"); |
|||
} |
|||
if (propagationFlags != 0) |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_NonContainerInvalidAnyFlag"), "propagationFlags"); |
|||
} |
|||
return new PipeAuditRule(identityReference, accessMask, isInherited, flags); |
|||
} |
|||
|
|||
private AccessControlSections GetAccessControlSectionsFromChanges() |
|||
{ |
|||
AccessControlSections accessControlSections = AccessControlSections.None; |
|||
if (base.AccessRulesModified) |
|||
{ |
|||
accessControlSections = AccessControlSections.Access; |
|||
} |
|||
if (base.AuditRulesModified) |
|||
{ |
|||
accessControlSections |= AccessControlSections.Audit; |
|||
} |
|||
if (base.OwnerModified) |
|||
{ |
|||
accessControlSections |= AccessControlSections.Owner; |
|||
} |
|||
if (base.GroupModified) |
|||
{ |
|||
accessControlSections |= AccessControlSections.Group; |
|||
} |
|||
return accessControlSections; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] |
|||
protected internal void Persist(SafeHandle handle) |
|||
{ |
|||
base.WriteLock(); |
|||
try |
|||
{ |
|||
AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges(); |
|||
base.Persist(handle, accessControlSectionsFromChanges); |
|||
bool flag2 = base.AccessRulesModified = false; |
|||
bool flag4 = base.AuditRulesModified = flag2; |
|||
bool ownerModified = base.GroupModified = flag4; |
|||
base.OwnerModified = ownerModified; |
|||
} |
|||
finally |
|||
{ |
|||
base.WriteUnlock(); |
|||
} |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
[SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] |
|||
protected internal void Persist(string name) |
|||
{ |
|||
base.WriteLock(); |
|||
try |
|||
{ |
|||
AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges(); |
|||
base.Persist(name, accessControlSectionsFromChanges); |
|||
bool flag2 = base.AccessRulesModified = false; |
|||
bool flag4 = base.AuditRulesModified = flag2; |
|||
bool ownerModified = base.GroupModified = flag4; |
|||
base.OwnerModified = ownerModified; |
|||
} |
|||
finally |
|||
{ |
|||
base.WriteUnlock(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,22 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[Serializable] |
|||
internal enum PipeState |
|||
{ |
|||
WaitingToConnect, |
|||
Connected, |
|||
Broken, |
|||
Disconnected, |
|||
Closed |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
File diff suppressed because it is too large
@ -0,0 +1,116 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32.SafeHandles; |
|||
using System; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
using System.Threading; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] |
|||
[PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")] |
|||
internal sealed class PipeStreamAsyncResult : IAsyncResult |
|||
{ |
|||
internal AsyncCallback _userCallback; |
|||
|
|||
internal object _userStateObject; |
|||
|
|||
internal ManualResetEvent _waitHandle; |
|||
|
|||
internal SafePipeHandle _handle; |
|||
|
|||
internal unsafe NativeOverlapped* _overlapped; |
|||
|
|||
internal int _EndXxxCalled; |
|||
|
|||
internal int _numBytes; |
|||
|
|||
internal int _errorCode; |
|||
|
|||
internal bool _isMessageComplete; |
|||
|
|||
internal bool _isWrite; |
|||
|
|||
internal bool _isComplete; |
|||
|
|||
internal bool _completedSynchronously; |
|||
|
|||
public object AsyncState |
|||
{ |
|||
get |
|||
{ |
|||
return _userStateObject; |
|||
} |
|||
} |
|||
|
|||
public bool IsCompleted |
|||
{ |
|||
get |
|||
{ |
|||
return _isComplete; |
|||
} |
|||
} |
|||
|
|||
public unsafe WaitHandle AsyncWaitHandle |
|||
{ |
|||
[SecurityCritical] |
|||
get |
|||
{ |
|||
if (_waitHandle == null) |
|||
{ |
|||
ManualResetEvent manualResetEvent = new ManualResetEvent(false); |
|||
if (_overlapped != null && _overlapped->EventHandle != IntPtr.Zero) |
|||
{ |
|||
manualResetEvent.SafeWaitHandle = new SafeWaitHandle(_overlapped->EventHandle, true); |
|||
} |
|||
if (_isComplete) |
|||
{ |
|||
manualResetEvent.Set(); |
|||
} |
|||
_waitHandle = manualResetEvent; |
|||
} |
|||
return _waitHandle; |
|||
} |
|||
} |
|||
|
|||
public bool CompletedSynchronously |
|||
{ |
|||
get |
|||
{ |
|||
return _completedSynchronously; |
|||
} |
|||
} |
|||
|
|||
private void CallUserCallbackWorker(object callbackState) |
|||
{ |
|||
_isComplete = true; |
|||
if (_waitHandle != null) |
|||
{ |
|||
_waitHandle.Set(); |
|||
} |
|||
_userCallback(this); |
|||
} |
|||
|
|||
internal void CallUserCallback() |
|||
{ |
|||
if (_userCallback != null) |
|||
{ |
|||
_completedSynchronously = false; |
|||
ThreadPool.QueueUserWorkItem(CallUserCallbackWorker); |
|||
} |
|||
else |
|||
{ |
|||
_isComplete = true; |
|||
if (_waitHandle != null) |
|||
{ |
|||
_waitHandle.Set(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,10 @@ |
|||
#if NET20
|
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
internal delegate void PipeStreamImpersonationWorker(); |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,20 @@ |
|||
#if NET20
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System.IO.Pipes |
|||
{ |
|||
|
|||
[Serializable] |
|||
internal enum PipeTransmissionMode |
|||
{ |
|||
Byte, |
|||
Message |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
|||
|
@ -0,0 +1,184 @@ |
|||
#if NET20
|
|||
|
|||
using Microsoft.Win32; |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
using System.Security.Permissions; |
|||
|
|||
namespace System.IO |
|||
{ |
|||
|
|||
internal static class __Error |
|||
{ |
|||
|
|||
internal static void EndOfFile() |
|||
{ |
|||
throw new EndOfStreamException(System.SR.GetString("IO_EOF_ReadBeyondEOF")); |
|||
} |
|||
|
|||
internal static void FileNotOpen() |
|||
{ |
|||
throw new ObjectDisposedException(null, System.SR.GetString("ObjectDisposed_FileClosed")); |
|||
} |
|||
|
|||
internal static void PipeNotOpen() |
|||
{ |
|||
throw new ObjectDisposedException(null, System.SR.GetString("ObjectDisposed_PipeClosed")); |
|||
} |
|||
|
|||
internal static void ReadNotSupported() |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_UnreadableStream")); |
|||
} |
|||
|
|||
internal static void SeekNotSupported() |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_UnseekableStream")); |
|||
} |
|||
|
|||
internal static void WrongAsyncResult() |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("Argument_WrongAsyncResult")); |
|||
} |
|||
|
|||
internal static void EndReadCalledTwice() |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("InvalidOperation_EndReadCalledMultiple")); |
|||
} |
|||
|
|||
internal static void EndWriteCalledTwice() |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("InvalidOperation_EndWriteCalledMultiple")); |
|||
} |
|||
|
|||
internal static void EndWaitForConnectionCalledTwice() |
|||
{ |
|||
throw new ArgumentException(System.SR.GetString("InvalidOperation_EndWaitForConnectionCalledMultiple")); |
|||
} |
|||
|
|||
internal static string GetDisplayablePath(string path, bool isInvalidPath) |
|||
{ |
|||
if (string.IsNullOrEmpty(path)) |
|||
{ |
|||
return path; |
|||
} |
|||
bool flag = false; |
|||
if (path.Length < 2) |
|||
{ |
|||
return path; |
|||
} |
|||
if (path[0] == Path.DirectorySeparatorChar && path[1] == Path.DirectorySeparatorChar) |
|||
{ |
|||
flag = true; |
|||
} |
|||
else if (path[1] == Path.VolumeSeparatorChar) |
|||
{ |
|||
flag = true; |
|||
} |
|||
if (!flag && !isInvalidPath) |
|||
{ |
|||
return path; |
|||
} |
|||
bool flag2 = false; |
|||
try |
|||
{ |
|||
if (!isInvalidPath) |
|||
{ |
|||
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[1] |
|||
{ |
|||
path |
|||
}).Demand(); |
|||
flag2 = true; |
|||
} |
|||
} |
|||
catch (SecurityException) |
|||
{ |
|||
} |
|||
catch (ArgumentException) |
|||
{ |
|||
} |
|||
catch (NotSupportedException) |
|||
{ |
|||
} |
|||
if (!flag2) |
|||
{ |
|||
path = ((path[path.Length - 1] != Path.DirectorySeparatorChar) ? Path.GetFileName(path) : System.SR.GetString("IO_IO_NoPermissionToDirectoryName")); |
|||
} |
|||
return path; |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
internal static void WinIOError() |
|||
{ |
|||
int lastWin32Error = Marshal.GetLastWin32Error(); |
|||
WinIOError(lastWin32Error, string.Empty); |
|||
} |
|||
|
|||
[SecurityCritical] |
|||
internal static void WinIOError(int errorCode, string maybeFullPath) |
|||
{ |
|||
bool isInvalidPath = errorCode == 123 || errorCode == 161; |
|||
string displayablePath = GetDisplayablePath(maybeFullPath, isInvalidPath); |
|||
switch (errorCode) |
|||
{ |
|||
case 2: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
throw new FileNotFoundException(System.SR.GetString("IO_FileNotFound")); |
|||
} |
|||
throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, System.SR.GetString("IO_FileNotFound_FileName"), displayablePath), displayablePath); |
|||
case 3: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
throw new DirectoryNotFoundException(System.SR.GetString("IO_PathNotFound_NoPathName")); |
|||
} |
|||
throw new DirectoryNotFoundException(string.Format(CultureInfo.CurrentCulture, System.SR.GetString("IO_PathNotFound_Path"), displayablePath)); |
|||
case 5: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
throw new UnauthorizedAccessException(System.SR.GetString("UnauthorizedAccess_IODenied_NoPathName")); |
|||
} |
|||
throw new UnauthorizedAccessException(string.Format(CultureInfo.CurrentCulture, System.SR.GetString("UnauthorizedAccess_IODenied_Path"), displayablePath)); |
|||
case 183: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
break; |
|||
} |
|||
throw new IOException(System.SR.GetString("IO_IO_AlreadyExists_Name", displayablePath), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
case 206: |
|||
throw new PathTooLongException(System.SR.GetString("IO_PathTooLong")); |
|||
case 15: |
|||
throw new DriveNotFoundException(string.Format(CultureInfo.CurrentCulture, System.SR.GetString("IO_DriveNotFound_Drive"), displayablePath)); |
|||
case 87: |
|||
throw new IOException(Microsoft.Win32.UnsafeNativeMethods.GetMessage(errorCode), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
case 32: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
throw new IOException(System.SR.GetString("IO_IO_SharingViolation_NoFileName"), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
} |
|||
throw new IOException(System.SR.GetString("IO_IO_SharingViolation_File", displayablePath), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
case 80: |
|||
if (displayablePath.Length == 0) |
|||
{ |
|||
break; |
|||
} |
|||
throw new IOException(string.Format(CultureInfo.CurrentCulture, System.SR.GetString("IO_IO_FileExists_Name"), displayablePath), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
case 995: |
|||
throw new OperationCanceledException(); |
|||
} |
|||
throw new IOException(Microsoft.Win32.UnsafeNativeMethods.GetMessage(errorCode), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode)); |
|||
} |
|||
|
|||
internal static void WriteNotSupported() |
|||
{ |
|||
throw new NotSupportedException(System.SR.GetString("NotSupported_UnwritableStream")); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace System |
|||
{ |
|||
|
|||
class SR |
|||
{ |
|||
|
|||
public static string GetString(string name) => name ?? ""; |
|||
|
|||
public static string GetString(string name, params object[] args) => name ?? ""; |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,21 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.InteropServices; |
|||
using System.Text; |
|||
|
|||
namespace Apewer.Internals.Interop |
|||
{ |
|||
|
|||
/// <summary>Mouse Hook Struct</summary>
|
|||
[StructLayout(LayoutKind.Sequential)] |
|||
internal class MSLLHOOKSTRUCT |
|||
{ |
|||
|
|||
public Point pt; |
|||
public int hwnd; |
|||
public int wHitTestCode; |
|||
public int dwExtraInfo; |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using System.Net.Sockets; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
namespace Apewer.Internals.Interop |
|||
{ |
|||
|
|||
internal class WS2_32 |
|||
{ |
|||
|
|||
[DllImport("ws2_32.dll", SetLastError = true)] |
|||
internal static extern IntPtr gethostbyaddr([In] ref int addr, [In] int len, [In] ProtocolFamily type); |
|||
|
|||
[DllImport("ws2_32.dll", BestFitMapping = false, CharSet = CharSet.Ansi, SetLastError = true, ThrowOnUnmappableChar = true)] |
|||
internal static extern IntPtr gethostbyname([In] string host); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.InteropServices; |
|||
using System.Text; |
|||
|
|||
namespace Apewer.Internals.Interop |
|||
{ |
|||
|
|||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
|||
internal struct hostent |
|||
{ |
|||
public IntPtr h_name; |
|||
|
|||
public IntPtr h_aliases; |
|||
|
|||
public short h_addrtype; |
|||
|
|||
public short h_length; |
|||
|
|||
public IntPtr h_addr_list; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,31 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Apewer.Models |
|||
{ |
|||
|
|||
/// <summary></summary>
|
|||
public enum MouseButtons |
|||
{ |
|||
|
|||
/// <summary></summary>
|
|||
None = 0x0, |
|||
|
|||
/// <summary></summary>
|
|||
Left = 0x100000, |
|||
|
|||
/// <summary></summary>
|
|||
Right = 0x200000, |
|||
|
|||
/// <summary></summary>
|
|||
Middle = 0x400000, |
|||
|
|||
/// <summary></summary>
|
|||
XButton1 = 0x800000, |
|||
|
|||
/// <summary></summary>
|
|||
XButton2 = 0x1000000 |
|||
} |
|||
|
|||
} |
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Apewer.Models |
|||
{ |
|||
|
|||
/// <summary></summary>
|
|||
public struct MouseEvent |
|||
{ |
|||
|
|||
/// <summary></summary>
|
|||
public int Hook; |
|||
|
|||
/// <summary></summary>
|
|||
public int X; |
|||
|
|||
/// <summary></summary>
|
|||
public int Y; |
|||
|
|||
/// <summary></summary>
|
|||
public long Button; |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue