private static Hashtable DnsQueryWrapper(string domainName, string siteName, long dcFlags)
 {
     Hashtable hashtable = new Hashtable();
     string recordName = "_ldap._tcp.";
     int errorCode = 0;
     int options = 0;
     IntPtr zero = IntPtr.Zero;
     if ((siteName != null) && (siteName.Length != 0))
     {
         recordName = recordName + siteName + "._sites.";
     }
     if ((dcFlags & 0x40L) != 0L)
     {
         recordName = recordName + "gc._msdcs.";
     }
     else if ((dcFlags & 0x1000L) != 0L)
     {
         recordName = recordName + "dc._msdcs.";
     }
     recordName = recordName + domainName;
     if ((dcFlags & 1L) != 0L)
     {
         options |= 8;
     }
     errorCode = System.DirectoryServices.ActiveDirectory.NativeMethods.DnsQuery(recordName, 0x21, options, IntPtr.Zero, out zero, IntPtr.Zero);
     if (errorCode == 0)
     {
         try
         {
             PartialDnsRecord record;
             for (IntPtr ptr2 = zero; ptr2 != IntPtr.Zero; ptr2 = record.next)
             {
                 record = new PartialDnsRecord();
                 Marshal.PtrToStructure(ptr2, record);
                 if (record.type == 0x21)
                 {
                     DnsRecord structure = new DnsRecord();
                     Marshal.PtrToStructure(ptr2, structure);
                     string key = structure.data.targetName.ToLower(CultureInfo.InvariantCulture);
                     if (!hashtable.Contains(key))
                     {
                         hashtable.Add(key, null);
                     }
                 }
             }
             return hashtable;
         }
         finally
         {
             if (zero != IntPtr.Zero)
             {
                 System.DirectoryServices.ActiveDirectory.NativeMethods.DnsRecordListFree(zero, true);
             }
         }
     }
     if (errorCode != 0)
     {
         throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
     }
     return hashtable;
 }
Ejemplo n.º 2
0
        private static Hashtable DnsQueryWrapper(string domainName, string siteName, long dcFlags)
        {
            Hashtable domainControllers = new Hashtable();
            string    recordName        = "_ldap._tcp.";
            int       result            = 0;
            int       options           = 0;
            IntPtr    dnsResults        = IntPtr.Zero;

            // construct the record name
            if ((siteName != null) && (!(siteName.Length == 0)))
            {
                // only looking for domain controllers / global catalogs within a
                // particular site
                recordName = recordName + siteName + "._sites.";
            }

            // check if gc or dc
            if (((long)dcFlags & (long)(PrivateLocatorFlags.GCRequired)) != 0)
            {
                // global catalog
                recordName += "gc._msdcs.";
            }
            else if (((long)dcFlags & (long)(PrivateLocatorFlags.DSWriteableRequired)) != 0)
            {
                // domain controller
                recordName += "dc._msdcs.";
            }

            // now add the domainName
            recordName = recordName + domainName;

            // set the BYPASS CACHE option is specified
            if (((long)dcFlags & (long)LocatorOptions.ForceRediscovery) != 0)
            {
                options |= NativeMethods.DnsQueryBypassCache;
            }

            // Call DnsQuery
            result = NativeMethods.DnsQuery(recordName, NativeMethods.DnsSrvData, options, IntPtr.Zero, out dnsResults, IntPtr.Zero);
            if (result == 0)
            {
                try
                {
                    IntPtr currentDnsRecord = dnsResults;

                    while (currentDnsRecord != IntPtr.Zero)
                    {
                        // partial marshalling of dns record data
                        PartialDnsRecord partialDnsRecord = new PartialDnsRecord();
                        Marshal.PtrToStructure(currentDnsRecord, partialDnsRecord);

                        //check if the record is of type DNS_SRV_DATA
                        if (partialDnsRecord.type == NativeMethods.DnsSrvData)
                        {
                            // remarshal to get the srv record data
                            DnsRecord dnsRecord = new DnsRecord();
                            Marshal.PtrToStructure(currentDnsRecord, dnsRecord);
                            string targetName = dnsRecord.data.targetName;
                            string key        = targetName.ToLowerInvariant();

                            if (!domainControllers.Contains(key))
                            {
                                domainControllers.Add(key, null);
                            }
                        }
                        // move to next record
                        currentDnsRecord = partialDnsRecord.next;
                    }
                }
                finally
                {
                    // release the dns results buffer
                    if (dnsResults != IntPtr.Zero)
                    {
                        NativeMethods.DnsRecordListFree(dnsResults, true);
                    }
                }
            }
            else if (result != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(result);
            }

            return(domainControllers);
        }
Ejemplo n.º 3
0
        private static Hashtable DnsQueryWrapper(string domainName, string siteName, long dcFlags)
        {
            PartialDnsRecord partialDnsRecord = null;
            Hashtable        hashtables       = new Hashtable();
            string           str = "_ldap._tcp.";
            int    num           = 0;
            IntPtr zero          = IntPtr.Zero;

            if (siteName != null && siteName.Length != 0)
            {
                str = string.Concat(str, siteName, "._sites.");
            }
            if ((dcFlags & (long)64) == (long)0)
            {
                if ((dcFlags & (long)0x1000) != (long)0)
                {
                    str = string.Concat(str, "dc._msdcs.");
                }
            }
            else
            {
                str = string.Concat(str, "gc._msdcs.");
            }
            str = string.Concat(str, domainName);
            if ((dcFlags & (long)1) != (long)0)
            {
                num = num | 8;
            }
            int num1 = NativeMethods.DnsQuery(str, 33, num, IntPtr.Zero, out zero, IntPtr.Zero);

            if (num1 != 0)
            {
                if (num1 != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(num1);
                }
            }
            else
            {
                try
                {
                    for (IntPtr i = zero; i != IntPtr.Zero; i = partialDnsRecord.next)
                    {
                        partialDnsRecord = new PartialDnsRecord();
                        Marshal.PtrToStructure(i, partialDnsRecord);
                        if (partialDnsRecord.type == 33)
                        {
                            DnsRecord dnsRecord = new DnsRecord();
                            Marshal.PtrToStructure(i, dnsRecord);
                            string str1  = dnsRecord.data.targetName;
                            string lower = str1.ToLower(CultureInfo.InvariantCulture);
                            if (!hashtables.Contains(lower))
                            {
                                hashtables.Add(lower, null);
                            }
                        }
                    }
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        NativeMethods.DnsRecordListFree(zero, true);
                    }
                }
            }
            return(hashtables);
        }
Ejemplo n.º 4
0
		private static Hashtable DnsQueryWrapper(string domainName, string siteName, long dcFlags)
		{
			PartialDnsRecord partialDnsRecord = null;
			Hashtable hashtables = new Hashtable();
			string str = "_ldap._tcp.";
			int num = 0;
			IntPtr zero = IntPtr.Zero;
			if (siteName != null && siteName.Length != 0)
			{
				str = string.Concat(str, siteName, "._sites.");
			}
			if ((dcFlags & (long)64) == (long)0)
			{
				if ((dcFlags & (long)0x1000) != (long)0)
				{
					str = string.Concat(str, "dc._msdcs.");
				}
			}
			else
			{
				str = string.Concat(str, "gc._msdcs.");
			}
			str = string.Concat(str, domainName);
			if ((dcFlags & (long)1) != (long)0)
			{
				num = num | 8;
			}
			int num1 = NativeMethods.DnsQuery(str, 33, num, IntPtr.Zero, out zero, IntPtr.Zero);
			if (num1 != 0)
			{
				if (num1 != 0)
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(num1);
				}
			}
			else
			{
				try
				{
					for (IntPtr i = zero; i != IntPtr.Zero; i = partialDnsRecord.next)
					{
						partialDnsRecord = new PartialDnsRecord();
						Marshal.PtrToStructure(i, partialDnsRecord);
						if (partialDnsRecord.type == 33)
						{
							DnsRecord dnsRecord = new DnsRecord();
							Marshal.PtrToStructure(i, dnsRecord);
							string str1 = dnsRecord.data.targetName;
							string lower = str1.ToLower(CultureInfo.InvariantCulture);
							if (!hashtables.Contains(lower))
							{
								hashtables.Add(lower, null);
							}
						}
					}
				}
				finally
				{
					if (zero != IntPtr.Zero)
					{
						NativeMethods.DnsRecordListFree(zero, true);
					}
				}
			}
			return hashtables;
		}
Ejemplo n.º 5
0
        private static Hashtable DnsQueryWrapper(string domainName, string siteName, long dcFlags)
        {
            Hashtable hashtable  = new Hashtable();
            string    recordName = "_ldap._tcp.";
            int       errorCode  = 0;
            int       options    = 0;
            IntPtr    zero       = IntPtr.Zero;

            if ((siteName != null) && (siteName.Length != 0))
            {
                recordName = recordName + siteName + "._sites.";
            }
            if ((dcFlags & 0x40L) != 0L)
            {
                recordName = recordName + "gc._msdcs.";
            }
            else if ((dcFlags & 0x1000L) != 0L)
            {
                recordName = recordName + "dc._msdcs.";
            }
            recordName = recordName + domainName;
            if ((dcFlags & 1L) != 0L)
            {
                options |= 8;
            }
            errorCode = System.DirectoryServices.ActiveDirectory.NativeMethods.DnsQuery(recordName, 0x21, options, IntPtr.Zero, out zero, IntPtr.Zero);
            if (errorCode == 0)
            {
                try
                {
                    PartialDnsRecord record;
                    for (IntPtr ptr2 = zero; ptr2 != IntPtr.Zero; ptr2 = record.next)
                    {
                        record = new PartialDnsRecord();
                        Marshal.PtrToStructure(ptr2, record);
                        if (record.type == 0x21)
                        {
                            DnsRecord structure = new DnsRecord();
                            Marshal.PtrToStructure(ptr2, structure);
                            string key = structure.data.targetName.ToLower(CultureInfo.InvariantCulture);
                            if (!hashtable.Contains(key))
                            {
                                hashtable.Add(key, null);
                            }
                        }
                    }
                    return(hashtable);
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        System.DirectoryServices.ActiveDirectory.NativeMethods.DnsRecordListFree(zero, true);
                    }
                }
            }
            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
            return(hashtable);
        }