public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability inheritability, int bufferSize, PipeSecurity pipeSecurity) : base(direction, bufferSize) { object obj2; if (direction == PipeDirection.InOut) { throw new NotSupportedException(System.SR.GetString("NotSupported_AnonymousPipeUnidirectional")); } if ((inheritability < HandleInheritability.None) || (inheritability > HandleInheritability.Inheritable)) { throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); } Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(inheritability, pipeSecurity, out obj2); try { this.Create(direction, secAttrs, bufferSize); } finally { if (obj2 != null) { ((GCHandle)obj2).Free(); } } }
private static unsafe Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, out object pinningHandle) { pinningHandle = null; Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if (((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) || (memoryMappedFileSecurity != null)) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; if ((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) { structure.bInheritHandle = 1; } if (memoryMappedFileSecurity == null) { return(structure); } byte[] securityDescriptorBinaryForm = memoryMappedFileSecurity.GetSecurityDescriptorBinaryForm(); pinningHandle = GCHandle.Alloc(securityDescriptorBinaryForm, GCHandleType.Pinned); fixed(byte *numRef = securityDescriptorBinaryForm) { structure.pSecurityDescriptor = numRef; } } return(structure); }
private void Create(PipeDirection direction, Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs, int bufferSize) { bool flag; SafePipeHandle handle; SafePipeHandle handle2; if (direction == PipeDirection.In) { flag = Microsoft.Win32.UnsafeNativeMethods.CreatePipe(out handle, out this.m_clientHandle, secAttrs, bufferSize); } else { flag = Microsoft.Win32.UnsafeNativeMethods.CreatePipe(out this.m_clientHandle, out handle, secAttrs, bufferSize); } if (!flag) { System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); } if (!Microsoft.Win32.UnsafeNativeMethods.DuplicateHandle(Microsoft.Win32.UnsafeNativeMethods.GetCurrentProcess(), handle, Microsoft.Win32.UnsafeNativeMethods.GetCurrentProcess(), out handle2, 0, false, 2)) { System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); } handle.Dispose(); base.InitializeHandle(handle2, false, false); base.State = PipeState.Connected; }
internal LogStream(string path, int bufferSize, LogRetentionOption retention, long maxFileSize, int maxNumOfFiles) { string fullPath = Path.GetFullPath(path); this._fileName = fullPath; if (fullPath.StartsWith(@"\\.\", StringComparison.Ordinal)) { throw new NotSupportedException(System.SR.GetString("NotSupported_IONonFileDevices")); } Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(FileShare.Read); int num = 0x100000; this._canWrite = true; this._pathSav = fullPath; this._fAccessSav = 0x40000000; this._shareSav = FileShare.Read; this._secAttrsSav = secAttrs; this._secAccessSav = FileIOPermissionAccess.Write; this._modeSav = (retention != LogRetentionOption.SingleFileUnboundedSize) ? FileMode.Create : FileMode.OpenOrCreate; this._flagsAndAttributesSav = num; this._seekToEndSav = retention == LogRetentionOption.SingleFileUnboundedSize; base.bufferSize = bufferSize; this._retention = retention; this._maxFileSize = maxFileSize; this._maxNumberOfFiles = maxNumOfFiles; this._Init(fullPath, this._fAccessSav, this._shareSav, this._secAttrsSav, this._secAccessSav, this._modeSav, this._flagsAndAttributesSav, this._seekToEndSav); }
private static Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle CreateCore(SafeFileHandle fileHandle, string mapName, HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle handle = null; object obj2; Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES lpAttributes = GetSecAttrs(inheritability, memoryMappedFileSecurity, out obj2); int dwMaximumSizeLow = (int)(((ulong)capacity) & 0xffffffffL); int dwMaximumSizeHigh = (int)(capacity >> 0x20); try { handle = Microsoft.Win32.UnsafeNativeMethods.CreateFileMapping(fileHandle, lpAttributes, GetPageAccess(access) | options, dwMaximumSizeHigh, dwMaximumSizeLow, mapName); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid && (errorCode == 0xb7)) { handle.Dispose(); System.IO.__Error.WinIOError(errorCode, string.Empty); return(handle); } if (handle.IsInvalid) { System.IO.__Error.WinIOError(errorCode, string.Empty); } } finally { if (obj2 != null) { ((GCHandle)obj2).Free(); } } return(handle); }
internal LogStream(string path, int bufferSize, LogRetentionOption retention, long maxFileSize, int maxNumOfFiles) { string fullPath = Path.GetFullPath(path); this._fileName = fullPath; if (fullPath.StartsWith(@"\\.\", StringComparison.Ordinal)) { throw new NotSupportedException(System.SR.GetString("NotSupported_IONonFileDevices")); } Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(FileShare.Read); int num = 0x100000; this._canWrite = true; this._pathSav = fullPath; this._fAccessSav = 0x40000000; this._shareSav = FileShare.Read; this._secAttrsSav = secAttrs; this._secAccessSav = FileIOPermissionAccess.Write; this._modeSav = (retention != LogRetentionOption.SingleFileUnboundedSize) ? FileMode.Create : FileMode.OpenOrCreate; this._flagsAndAttributesSav = num; this._seekToEndSav = retention == LogRetentionOption.SingleFileUnboundedSize; base.bufferSize = bufferSize; this._retention = retention; this._maxFileSize = maxFileSize; this._maxNumberOfFiles = maxNumOfFiles; this._Init(fullPath, this._fAccessSav, this._shareSav, this._secAttrsSav, this._secAccessSav, this._modeSav, this._flagsAndAttributesSav, this._seekToEndSav); }
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.Asynchronous | PipeOptions.WriteThrough)) != PipeOptions.None) { 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 > 0xfe)) && (maxNumberOfServerInstances != -1)) { throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", System.SR.GetString("ArgumentOutOfRange_MaxNumServerInstances")); } if ((inheritability < HandleInheritability.None) || (inheritability > HandleInheritability.Inheritable)) { throw new ArgumentOutOfRangeException("inheritability", System.SR.GetString("ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable")); } if ((additionalAccessRights & ~(PipeAccessRights.AccessSystemSecurity | PipeAccessRights.TakeOwnership | PipeAccessRights.ChangePermissions)) != 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 pinningHandle = null; Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(inheritability, pipeSecurity, out pinningHandle); try { this.Create(fullPath, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, additionalAccessRights, secAttrs); } finally { if (pinningHandle != null) { ((GCHandle)pinningHandle).Free(); } } }
public void Connect(int timeout) { this.CheckConnectOperationsClient(); if ((timeout < 0) && (timeout != -1)) { throw new ArgumentOutOfRangeException("timeout", System.SR.GetString("ArgumentOutOfRange_InvalidTimeout")); } Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = PipeStream.GetSecAttrs(this.m_inheritability); int pipeOptions = (int)this.m_pipeOptions; if (this.m_impersonationLevel != TokenImpersonationLevel.None) { pipeOptions |= 0x100000; pipeOptions |= ((int)(this.m_impersonationLevel - 1)) << 0x10; } int tickCount = Environment.TickCount; int num3 = 0; Label_005C: if (!Microsoft.Win32.UnsafeNativeMethods.WaitNamedPipe(this.m_normalizedPipePath, timeout - num3)) { int errorCode = Marshal.GetLastWin32Error(); switch (errorCode) { case 2: goto Label_00EF; case 0: goto Label_0105; } System.IO.__Error.WinIOError(errorCode, string.Empty); } SafePipeHandle handle = Microsoft.Win32.UnsafeNativeMethods.CreateNamedPipeClient(this.m_normalizedPipePath, this.m_access, FileShare.None, secAttrs, FileMode.Open, pipeOptions, Microsoft.Win32.UnsafeNativeMethods.NULL); if (handle.IsInvalid) { int num5 = Marshal.GetLastWin32Error(); if (num5 == 0xe7) { goto Label_00EF; } System.IO.__Error.WinIOError(num5, string.Empty); } base.InitializeHandle(handle, false, (this.m_pipeOptions & PipeOptions.Asynchronous) != PipeOptions.None); base.State = PipeState.Connected; return; Label_00EF: if ((timeout == -1) || ((num3 = Environment.TickCount - tickCount) < timeout)) { goto Label_005C; } Label_0105: throw new TimeoutException(); }
private static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(FileShare share) { Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if ((share & FileShare.Inheritable) != FileShare.None) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure), bInheritHandle = 1 }; } return(structure); }
internal static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability) { Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if ((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure), bInheritHandle = 1 }; } return(structure); }
internal void _Init(string path, int fAccess, FileShare share, Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs, FileIOPermissionAccess secAccess, FileMode mode, int flagsAndAttributes, bool seekToEnd) { string fullPath = Path.GetFullPath(path); this._fileName = fullPath; new FileIOPermission(secAccess, new string[] { fullPath }).Demand(); int newMode = Microsoft.Win32.UnsafeNativeMethods.SetErrorMode(1); try { this._handle = Microsoft.Win32.UnsafeNativeMethods.SafeCreateFile(fullPath, fAccess, share, secAttrs, mode, flagsAndAttributes, Microsoft.Win32.UnsafeNativeMethods.NULL); int errorCode = Marshal.GetLastWin32Error(); if (this._handle.IsInvalid) { bool flag = false; try { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[] { this._fileName }).Demand(); flag = true; } catch (SecurityException) { } if (flag) { System.IO.__Error.WinIOError(errorCode, this._fileName); } else { System.IO.__Error.WinIOError(errorCode, Path.GetFileName(this._fileName)); } } } finally { Microsoft.Win32.UnsafeNativeMethods.SetErrorMode(newMode); } base.pos = 0L; if (seekToEnd) { this.SeekCore(0L, SeekOrigin.End); } }
private void Create(string fullPipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeAccessRights rights, Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs) { int openMode = (int)(((direction | ((maxNumberOfServerInstances == 1) ? ((PipeDirection)0x80000) : ((PipeDirection)0))) | ((PipeDirection)((int)options))) | ((PipeDirection)((int)rights))); int pipeMode = (((int)transmissionMode) << 2) | (((int)transmissionMode) << 1); if (maxNumberOfServerInstances == -1) { maxNumberOfServerInstances = 0xff; } SafePipeHandle handle = Microsoft.Win32.UnsafeNativeMethods.CreateNamedPipe(fullPipeName, openMode, pipeMode, maxNumberOfServerInstances, outBufferSize, inBufferSize, 0, secAttrs); if (handle.IsInvalid) { System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); } base.InitializeHandle(handle, false, (options & PipeOptions.Asynchronous) != PipeOptions.None); }
private static Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle CreateOrOpenCore(SafeFileHandle fileHandle, string mapName, HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle handle = null; object obj2; Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES lpAttributes = GetSecAttrs(inheritability, memoryMappedFileSecurity, out obj2); int dwMaximumSizeLow = (int)(((ulong)capacity) & 0xffffffffL); int dwMaximumSizeHigh = (int)(capacity >> 0x20); try { int num3 = 14; int millisecondsTimeout = 0; while (num3 > 0) { handle = Microsoft.Win32.UnsafeNativeMethods.CreateFileMapping(fileHandle, lpAttributes, GetPageAccess(access) | options, dwMaximumSizeHigh, dwMaximumSizeLow, mapName); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { break; } if (errorCode != 5) { System.IO.__Error.WinIOError(errorCode, string.Empty); } handle.SetHandleAsInvalid(); handle = Microsoft.Win32.UnsafeNativeMethods.OpenFileMapping(GetFileMapAccess(access), (inheritability & HandleInheritability.Inheritable) != HandleInheritability.None, mapName); int num6 = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { break; } if (num6 != 2) { System.IO.__Error.WinIOError(num6, string.Empty); } num3--; if (millisecondsTimeout == 0) { millisecondsTimeout = 10; } else { Thread.Sleep(millisecondsTimeout); millisecondsTimeout *= 2; } } if ((handle == null) || handle.IsInvalid) { throw new InvalidOperationException(System.SR.GetString("InvalidOperation_CantCreateFileMapping")); } return(handle); } finally { if (obj2 != null) { ((GCHandle)obj2).Free(); } } return(handle); }
private static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(FileShare share) { Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if ((share & FileShare.Inheritable) != FileShare.None) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure), bInheritHandle = 1 }; } return structure; }
internal static unsafe Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability, PipeSecurity pipeSecurity, out object pinningHandle) { pinningHandle = null; Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if (((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) || (pipeSecurity != null)) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; if ((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) { structure.bInheritHandle = 1; } if (pipeSecurity == null) { return structure; } byte[] securityDescriptorBinaryForm = pipeSecurity.GetSecurityDescriptorBinaryForm(); pinningHandle = GCHandle.Alloc(securityDescriptorBinaryForm, GCHandleType.Pinned); fixed (byte* numRef = securityDescriptorBinaryForm) { structure.pSecurityDescriptor = numRef; } } return structure; }
internal static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability) { Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES structure = null; if ((inheritability & HandleInheritability.Inheritable) != HandleInheritability.None) { structure = new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure), bInheritHandle = 1 }; } return structure; }