internal static extern int DsGetDcNext([In] SafeDsGetDcContextHandle getDcContextHandle, [In][Out] ref IntPtr sockAddressCount, out IntPtr sockAdresses, out SafeDnsHostNameHandle dnsHostName);
Exemplo n.º 2
0
        private StringCollection FindAllDirectoryServers(string forestFqdn, bool requireGCs, string siteName = null)
        {
            Hashtable hashtable = new Hashtable();
            SafeDsGetDcContextHandle safeDsGetDcContextHandle = null;
            SafeDnsHostNameHandle    safeDnsHostNameHandle    = null;
            IntPtr zero  = IntPtr.Zero;
            IntPtr zero2 = IntPtr.Zero;

            NativeMethods.DsGetDcOpenFlags dsGetDcOpenFlags = NativeMethods.DsGetDcOpenFlags.ForceRediscovery;
            if (requireGCs)
            {
                dsGetDcOpenFlags |= NativeMethods.DsGetDcOpenFlags.GCRequired;
            }
            try
            {
                int num = NativeMethods.DsGetDcOpen(forestFqdn, 0, siteName, IntPtr.Zero, null, (int)dsGetDcOpenFlags, out safeDsGetDcContextHandle);
                ExTraceGlobals.FaultInjectionTracer.TraceTest(3854970173U);
                if (num != 0)
                {
                    throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                }
                num = NativeMethods.DsGetDcNext(safeDsGetDcContextHandle, ref zero, out zero2, out safeDnsHostNameHandle);
                if (num != 0 && num != 1101 && num != 9003 && num != 259)
                {
                    throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                }
                while (num != 259)
                {
                    if (num != 1101 && num != 9003)
                    {
                        try
                        {
                            string text = Marshal.PtrToStringUni(safeDnsHostNameHandle.DangerousGetHandle());
                            string key  = text.ToLower(CultureInfo.InvariantCulture);
                            if (!hashtable.ContainsKey(key))
                            {
                                hashtable.Add(key, null);
                            }
                        }
                        finally
                        {
                            if (safeDnsHostNameHandle != null)
                            {
                                safeDnsHostNameHandle.Dispose();
                                safeDnsHostNameHandle = null;
                            }
                        }
                    }
                    num = NativeMethods.DsGetDcNext(safeDsGetDcContextHandle, ref zero, out zero2, out safeDnsHostNameHandle);
                    if (num != 0 && num != 1101 && num != 9003 && num != 259)
                    {
                        throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                    }
                }
            }
            finally
            {
                if (safeDsGetDcContextHandle != null)
                {
                    safeDsGetDcContextHandle.Dispose();
                }
                if (safeDnsHostNameHandle != null)
                {
                    safeDnsHostNameHandle.Dispose();
                }
            }
            StringCollection stringCollection = new StringCollection();

            foreach (object obj in hashtable.Keys)
            {
                string value = (string)obj;
                stringCollection.Add(value);
            }
            return(stringCollection);
        }