Esempio n. 1
0
 internal static string GetPipeName(Uri uri)
 {
     string[] strArray   = new string[] { "+", uri.Host, "*" };
     bool[]   flagArray2 = new bool[2];
     flagArray2[0] = true;
     bool[] flagArray = flagArray2;
     for (int i = 0; i < strArray.Length; i++)
     {
         for (int j = 0; j < flagArray.Length; j++)
         {
             for (string str = PipeUri.GetPath(uri); str.Length > 0; str = PipeUri.GetParentPath(str))
             {
                 string sharedMemoryName = PipeUri.BuildSharedMemoryName(strArray[i], str, flagArray[j]);
                 try
                 {
                     PipeSharedMemory memory = PipeSharedMemory.Open(sharedMemoryName, uri);
                     if (memory != null)
                     {
                         try
                         {
                             string pipeName = memory.PipeName;
                             if (pipeName != null)
                             {
                                 return(pipeName);
                             }
                         }
                         finally
                         {
                             memory.Dispose();
                         }
                     }
                 }
                 catch (AddressAccessDeniedException exception)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { uri.AbsoluteUri }), exception));
                 }
             }
         }
     }
     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { uri.AbsoluteUri }), new PipeException(System.ServiceModel.SR.GetString("PipeEndpointNotFound", new object[] { uri.AbsoluteUri }))));
 }
 public void Listen()
 {
     lock (this.ThisLock)
     {
         if (!this.isListening)
         {
             string sharedMemoryName = PipeUri.BuildSharedMemoryName(this.pipeUri, this.hostNameComparisonMode, true);
             if (!PipeSharedMemory.TryCreate(this.allowedSids, this.pipeUri, sharedMemoryName, out this.sharedMemory))
             {
                 PipeSharedMemory result = null;
                 Uri    uri  = new Uri(this.pipeUri, Guid.NewGuid().ToString());
                 string str2 = PipeUri.BuildSharedMemoryName(uri, this.hostNameComparisonMode, true);
                 if (PipeSharedMemory.TryCreate(this.allowedSids, uri, str2, out result))
                 {
                     result.Dispose();
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(PipeSharedMemory.CreatePipeNameInUseException(5, this.pipeUri));
                 }
                 sharedMemoryName  = PipeUri.BuildSharedMemoryName(this.pipeUri, this.hostNameComparisonMode, false);
                 this.sharedMemory = PipeSharedMemory.Create(this.allowedSids, this.pipeUri, sharedMemoryName);
             }
             this.isListening = true;
         }
     }
 }
 public static unsafe bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result)
 {
     byte[] buffer;
     SafeFileMappingHandle handle;
     int num;
     bool flag2;
     Guid guid = Guid.NewGuid();
     string pipeName = BuildPipeName(guid);
     try
     {
         buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648);
     }
     catch (Win32Exception exception)
     {
         Exception innerException = new PipeException(exception.Message, exception);
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException));
     }
     result = null;
     fixed (byte* numRef = buffer)
     {
         UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES {
             lpSecurityDescriptor = numRef
         };
         handle = UnsafeNativeMethods.CreateFileMapping((IntPtr) (-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), sharedMemoryName);
         num = Marshal.GetLastWin32Error();
     }
     if (handle.IsInvalid)
     {
         handle.SetHandleAsInvalid();
         if (num == 5)
         {
             return false;
         }
         Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeNameCantBeReserved", new object[] { pipeUri.AbsoluteUri, PipeError.GetErrorString(num) }), num);
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3));
     }
     if (num == 0xb7)
     {
         handle.Close();
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(num, pipeUri));
     }
     PipeSharedMemory memory = new PipeSharedMemory(handle, pipeUri, pipeName);
     bool flag = true;
     try
     {
         memory.InitializeContents(guid);
         flag = false;
         result = memory;
         flag2 = true;
     }
     finally
     {
         if (flag)
         {
             memory.Dispose();
         }
     }
     return flag2;
 }
 public void Listen()
 {
     lock (this.ThisLock)
     {
         if (!this.isListening)
         {
             string sharedMemoryName = PipeUri.BuildSharedMemoryName(this.pipeUri, this.hostNameComparisonMode, true);
             if (!PipeSharedMemory.TryCreate(this.allowedSids, this.pipeUri, sharedMemoryName, out this.sharedMemory))
             {
                 PipeSharedMemory result = null;
                 Uri uri = new Uri(this.pipeUri, Guid.NewGuid().ToString());
                 string str2 = PipeUri.BuildSharedMemoryName(uri, this.hostNameComparisonMode, true);
                 if (PipeSharedMemory.TryCreate(this.allowedSids, uri, str2, out result))
                 {
                     result.Dispose();
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(PipeSharedMemory.CreatePipeNameInUseException(5, this.pipeUri));
                 }
                 sharedMemoryName = PipeUri.BuildSharedMemoryName(this.pipeUri, this.hostNameComparisonMode, false);
                 this.sharedMemory = PipeSharedMemory.Create(this.allowedSids, this.pipeUri, sharedMemoryName);
             }
             this.isListening = true;
         }
     }
 }
Esempio n. 5
0
        public unsafe static bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result)
        {
            Guid pipeGuid = Guid.NewGuid();
            string pipeName = BuildPipeName(pipeGuid.ToString());
            byte[] binarySecurityDescriptor;
            try
            {
                binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ);
            }
            catch (Win32Exception e)
            {
                // While Win32exceptions are not expected, if they do occur we need to obey the pipe/communication exception model.
                Exception innerException = new PipeException(e.Message, e);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException));
            }

            SafeFileMappingHandle fileMapping;
            int error;
            result = null;
            fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor)
            {
                UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES();
                securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor;

                fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes,
                    UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), sharedMemoryName);
                error = Marshal.GetLastWin32Error();
            }

            if (fileMapping.IsInvalid)
            {
                fileMapping.SetHandleAsInvalid();
                if (error == UnsafeNativeMethods.ERROR_ACCESS_DENIED)
                {
                    return false;
                }
                else
                {
                    Exception innerException = new PipeException(SR.GetString(SR.PipeNameCantBeReserved,
                        pipeUri.AbsoluteUri, PipeError.GetErrorString(error)), error);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(innerException.Message, innerException));
                }
            }

            // now we have a valid file mapping handle
            if (error == UnsafeNativeMethods.ERROR_ALREADY_EXISTS)
            {
                fileMapping.Close();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(error, pipeUri));
            }
            PipeSharedMemory pipeSharedMemory = new PipeSharedMemory(fileMapping, pipeUri, pipeName);
            bool disposeSharedMemory = true;
            try
            {
                pipeSharedMemory.InitializeContents(pipeGuid);
                disposeSharedMemory = false;
                result = pipeSharedMemory;

                if (TD.PipeSharedMemoryCreatedIsEnabled())
                {
                    TD.PipeSharedMemoryCreated(sharedMemoryName);
                }
                return true;
            }
            finally
            {
                if (disposeSharedMemory)
                {
                    pipeSharedMemory.Dispose();
                }
            }
        }
Esempio n. 6
0
        public void Listen()
        {
            lock (ThisLock)
            {
                if (!isListening)
                {
                    string sharedMemoryName = PipeUri.BuildSharedMemoryName(pipeUri, hostNameComparisonMode, true);
                    if (!PipeSharedMemory.TryCreate(allowedSids, pipeUri, sharedMemoryName, out this.sharedMemory))
                    {
                        PipeSharedMemory tempSharedMemory = null;

                        // first see if we're in RANU by creating a unique Uri in the global namespace
                        Uri tempUri = new Uri(pipeUri, Guid.NewGuid().ToString());
                        string tempSharedMemoryName = PipeUri.BuildSharedMemoryName(tempUri, hostNameComparisonMode, true);
                        if (PipeSharedMemory.TryCreate(allowedSids, tempUri, tempSharedMemoryName, out tempSharedMemory))
                        {
                            // we're not RANU, throw PipeNameInUse
                            tempSharedMemory.Dispose();
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                PipeSharedMemory.CreatePipeNameInUseException(UnsafeNativeMethods.ERROR_ACCESS_DENIED, pipeUri));
                        }
                        else
                        {
                            // try the session namespace since we're RANU
                            sharedMemoryName = PipeUri.BuildSharedMemoryName(pipeUri, hostNameComparisonMode, false);
                            this.sharedMemory = PipeSharedMemory.Create(allowedSids, pipeUri, sharedMemoryName);
                        }
                    }

                    isListening = true;
                }
            }
        }
Esempio n. 7
0
        public static unsafe bool TryCreate(List <SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result)
        {
            byte[] buffer;
            SafeFileMappingHandle handle;
            int    num;
            bool   flag2;
            Guid   guid     = Guid.NewGuid();
            string pipeName = BuildPipeName(guid);

            try
            {
                buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648);
            }
            catch (Win32Exception exception)
            {
                Exception innerException = new PipeException(exception.Message, exception);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException));
            }
            result = null;
            fixed(byte *numRef = buffer)
            {
                UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES {
                    lpSecurityDescriptor = numRef
                };
                handle = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), sharedMemoryName);
                num    = Marshal.GetLastWin32Error();
            }

            if (handle.IsInvalid)
            {
                handle.SetHandleAsInvalid();
                if (num == 5)
                {
                    return(false);
                }
                Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeNameCantBeReserved", new object[] { pipeUri.AbsoluteUri, PipeError.GetErrorString(num) }), num);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3));
            }
            if (num == 0xb7)
            {
                handle.Close();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(num, pipeUri));
            }
            PipeSharedMemory memory = new PipeSharedMemory(handle, pipeUri, pipeName);
            bool             flag   = true;

            try
            {
                memory.InitializeContents(guid);
                flag   = false;
                result = memory;
                flag2  = true;
            }
            finally
            {
                if (flag)
                {
                    memory.Dispose();
                }
            }
            return(flag2);
        }