Ejemplo n.º 1
0
        public NspiRpcClientConnection GetNspiRpcClientConnection()
        {
            string text = base.ServerSettings.PreferredGlobalCatalog(base.SessionSettings.GetAccountOrResourceForestFqdn());
            string domainController;

            if (!string.IsNullOrEmpty(text))
            {
                domainController = text;
            }
            else
            {
                PooledLdapConnection pooledLdapConnection = null;
                try
                {
                    pooledLdapConnection = ConnectionPoolManager.GetConnection(ConnectionType.GlobalCatalog, base.SessionSettings.GetAccountOrResourceForestFqdn());
                    domainController     = pooledLdapConnection.ServerName;
                }
                finally
                {
                    if (pooledLdapConnection != null)
                    {
                        pooledLdapConnection.ReturnToPool();
                    }
                }
            }
            return(NspiRpcClientConnection.GetNspiRpcClientConnection(domainController));
        }
        public static NspiRpcClientConnection GetNspiRpcClientConnection(string domainController)
        {
            NspiRpcClientConnection nspiRpcClientConnection = new NspiRpcClientConnection();
            int hashCode = nspiRpcClientConnection.GetHashCode();
            int num      = 0;

            while (!nspiRpcClientConnection.bound)
            {
                try
                {
                    NspiRpcClientConnection.TraceDebug(hashCode, "Creating NspiRpcClient and attempting bind to domain controller {0}", new object[]
                    {
                        domainController
                    });
                    nspiRpcClientConnection.nspiRpcClient = new NspiRpcClient(domainController, "ncacn_ip_tcp", null);
                    using (SafeRpcMemoryHandle safeRpcMemoryHandle = new SafeRpcMemoryHandle(36))
                    {
                        using (SafeRpcMemoryHandle safeRpcMemoryHandle2 = new SafeRpcMemoryHandle(16))
                        {
                            IntPtr ptr = safeRpcMemoryHandle.DangerousGetHandle();
                            Marshal.WriteInt32(ptr, 0, 0);
                            Marshal.WriteInt32(ptr, 4, 0);
                            Marshal.WriteInt32(ptr, 8, 0);
                            Marshal.WriteInt32(ptr, 12, 0);
                            Marshal.WriteInt32(ptr, 16, 0);
                            Marshal.WriteInt32(ptr, 20, 0);
                            Marshal.WriteInt32(ptr, 24, 1252);
                            Marshal.WriteInt32(ptr, 28, 1033);
                            Marshal.WriteInt32(ptr, 32, 1033);
                            int num2 = nspiRpcClientConnection.nspiRpcClient.Bind(0, safeRpcMemoryHandle.DangerousGetHandle(), safeRpcMemoryHandle2.DangerousGetHandle());
                            if (num2 != 0)
                            {
                                NspiRpcClientConnection.TraceError(hashCode, "Bind returned non-zero SCODE {0}", new object[]
                                {
                                    num2
                                });
                                throw new NspiFailureException(num2);
                            }
                            NspiRpcClientConnection.TraceDebug(hashCode, "Bind to domain controller {0} succeeded", new object[]
                            {
                                domainController
                            });
                            nspiRpcClientConnection.bound = true;
                            NspiRpcClientConnection.PerfCounterInstance.NumberOfOpenConnections.Increment();
                        }
                    }
                }
                catch (RpcException ex)
                {
                    num++;
                    if (ex.ErrorCode != 1753 && ex.ErrorCode != 1727)
                    {
                        NspiRpcClientConnection.TraceError(hashCode, "Caught RpcException \"{0}\" with error code {1}.  We will not retry the bind.", new object[]
                        {
                            ex.Message,
                            ex.ErrorCode
                        });
                        throw new ADOperationException(DirectoryStrings.NspiRpcError(ex.Message), ex);
                    }
                    if (num >= 3)
                    {
                        NspiRpcClientConnection.TraceError(hashCode, "Caught RpcException \"{0}\" with error code {1}.  Out of retries; giving up.", new object[]
                        {
                            ex.Message,
                            ex.ErrorCode
                        });
                        throw new ADTransientException(DirectoryStrings.NspiRpcError(ex.Message), ex);
                    }
                    NspiRpcClientConnection.TraceWarning(hashCode, "Caught RpcException \"{0}\" with error code {1}.  We will retry the bind; this is retry {2}.", new object[]
                    {
                        ex.Message,
                        ex.ErrorCode,
                        num
                    });
                    Thread.Sleep(1000);
                }
                finally
                {
                    if (nspiRpcClientConnection != null && !nspiRpcClientConnection.bound)
                    {
                        NspiRpcClientConnection.TraceDebug(hashCode, "Disposing the NspiRpcClient because we did not successfully bind", new object[0]);
                        nspiRpcClientConnection.nspiRpcClient.Dispose();
                        nspiRpcClientConnection.nspiRpcClient = null;
                    }
                }
            }
            return(nspiRpcClientConnection);
        }
 public virtual void Dispose()
 {
     NspiRpcClientConnection.TraceDebug(this.GetHashCode(), "Disposing by calling Dispose()", new object[0]);
     this.Dispose(true);
     GC.SuppressFinalize(this);
 }