Пример #1
0
 private static extern int DnsQuery(
     [MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName,
     DnsRecordTypes wType,
     DnsQueryOptions options,
     ref IP4_ARRAY dnsServerIpArray,
     ref IntPtr ppQueryResults,
     int pReserved);
Пример #2
0
 private static extern int DnsQuery(
     [In] string pszName,
     DnsRecordType wType,
     DnsQueryOptions options,
     IntPtr aipServers,
     ref IntPtr ppQueryResults,
     int pReserved);
Пример #3
0
 private static extern int DnsQuery(
     [In] string pszName,
     DnsRecordType wType,
     DnsQueryOptions options,
     IntPtr pExtra,
     [Out] out IntPtr ppQueryResults,
     IntPtr pReserved
     );
Пример #4
0
 public DnsGetIpQueryParam(string hostname, DnsQueryOptions options = DnsQueryOptions.Default, int timeout = -1) : base(options, timeout)
 {
     if (hostname._IsEmpty())
     {
         throw new ArgumentException("hostname is empty.");
     }
     this.Hostname = hostname._NonNullTrim();
 }
Пример #5
0
    public DnsQueryParamBase(DnsQueryOptions options = DnsQueryOptions.Default, int timeout = -1)
    {
        if (timeout <= 0)
        {
            timeout = CoresConfig.TcpIpStackDefaultSettings.DnsTimeout;
        }

        this.Timeout = timeout;
    }
        internal static IAsyncResult BeginQuery(string domain)
        {
            string text = "_autodiscover._tcp." + domain;

            AutoDiscoverDnsReader.DnsReaderTracer.TraceDebug <string>(0L, "Issuing a DNS query for {0}.", text);
            DnsQueryOptions options = DnsQueryOptions.None;

            if (Configuration.BypassDnsCache.Value)
            {
                options = DnsQueryOptions.BypassCache;
            }
            return(AutoDiscoverDnsReader.dns.BeginRetrieveSrvRecords(text, options, null, null));
        }
Пример #7
0
        private static DnsQueryOptions GetDnsOptions(ProtocolOption protocolOption)
        {
            DnsQueryOptions dnsQueryOptions = DnsQueryOptions.None;

            switch (protocolOption)
            {
            case ProtocolOption.UseUdpOnly:
                dnsQueryOptions |= DnsQueryOptions.AcceptTruncatedResponse;
                break;

            case ProtocolOption.UseTcpOnly:
                dnsQueryOptions |= DnsQueryOptions.UseTcpOnly;
                break;
            }
            return(dnsQueryOptions);
        }
Пример #8
0
        public static TRecordType[] Resolve <TRecordType>(string name, DnsQueryOptions options = DnsQueryOptions.None)
            where TRecordType : RecordBase
        {
            var mappingAttribute = typeof(TRecordType).GetCustomAttribute <DnsTypeMappingAttribute>();

            if (mappingAttribute == null)
            {
                throw new InvalidOperationException("No type mapping defined.");
            }

            using (var context = new DnsNativeContext())
            {
                return(context.Resolve(mappingAttribute.StructType, name, mappingAttribute.RecordType, options)
                       .Select(rs => (TRecordType)Activator.CreateInstance(typeof(TRecordType), BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { rs }, null))
                       .ToArray());
            }
        }
Пример #9
0
        internal static List <string> GetRecords(string domain,
                                                 DnsRecordTypes recordType,
                                                 DnsQueryOptions options = DEFAULT_QUERY_OPTIONS)
        {
            var recordsArray = IntPtr.Zero;
            var records      = new List <string>();

            try
            {
                var result = NativeMethods.DnsQuery(
                    domain,
                    recordType,
                    options,
                    IntPtr.Zero,
                    ref recordsArray,
                    IntPtr.Zero);

                if (result != 0)
                {
                    return(records);
                }

                DNS_RECORD record;

                for (var recordPtr = recordsArray; !recordPtr.Equals(IntPtr.Zero); recordPtr = record.pNext)
                {
                    record = (DNS_RECORD)Marshal.PtrToStructure(recordPtr, typeof(DNS_RECORD));
                    if (record.wType == (int)recordType)
                    {
                        var item = Marshal.PtrToStringAuto(record.Data.PTR.pNameHost);
                        records.Add(item);
                    }
                }
            }
            finally
            {
                if (recordsArray != IntPtr.Zero)
                {
                    NativeMethods.DnsRecordListFree(recordsArray, DNS_FREE_TYPE.DnsFreeFlat);
                }
            }

            return(records);
        }
Пример #10
0
        internal IRecordStruct[] Resolve(Type structType, string name, DnsRecordType type, DnsQueryOptions options)
        {
            var result = DnsQuery(ref name, type, options, IntPtr.Zero, ref _queryResultsPtr, IntPtr.Zero);

            if (result != 0)
                throw new DnsQueryException(result);

            var records = new List<IRecordStruct>();
            dynamic record;

            for (var recordPtr = _queryResultsPtr; recordPtr != IntPtr.Zero; recordPtr = record.pNext)
            {
                record = (dynamic)Marshal.PtrToStructure(recordPtr, structType);

                records.Add(record);
            }

            return records.ToArray();
        }
Пример #11
0
        internal static List <IPAddress> GetARecords(string domain, DnsQueryOptions options = DEFAULT_QUERY_OPTIONS)
        {
            var recordsArray = IntPtr.Zero;
            var records      = new List <IPAddress>();
            const DnsRecordTypes queryType = DnsRecordTypes.DNS_TYPE_A;

            try
            {
                var result = NativeMethods.DnsQuery(
                    domain,
                    queryType,
                    options,
                    IntPtr.Zero,
                    ref recordsArray,
                    IntPtr.Zero);

                if (result != 0)
                {
                    return(records);
                }

                DNS_RECORD record;

                for (var recordPtr = recordsArray; !recordPtr.Equals(IntPtr.Zero); recordPtr = record.pNext)
                {
                    record = (DNS_RECORD)Marshal.PtrToStructure(recordPtr, typeof(DNS_RECORD));
                    if (record.wType == (int)queryType)
                    {
                        var ip = record.Data.A.IPAddressObject;
                        records.Add(ip);
                    }
                }
            }
            finally
            {
                if (recordsArray != IntPtr.Zero)
                {
                    NativeMethods.DnsRecordListFree(recordsArray, DNS_FREE_TYPE.DnsFreeFlat);
                }
            }

            return(records);
        }
Пример #12
0
 public static extern int DnsQuery([In] string lpstrName, DnsRecordTypes wType, DnsQueryOptions Options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
Пример #13
0
 private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordType wType,
     DnsQueryOptions Options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
Пример #14
0
 public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName, DnsQueryTypes wType, DnsQueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);
Пример #15
0
        internal IRecordStruct[] Resolve(Type structType, string name, DnsRecordType type, DnsQueryOptions options)
        {
            var result = DnsQuery(ref name, type, options, IntPtr.Zero, ref _queryResultsPtr, IntPtr.Zero);

            if (result != 0)
            {
                throw new DnsQueryException(result);
            }

            var     records = new List <IRecordStruct>();
            dynamic record;

            for (var recordPtr = _queryResultsPtr; recordPtr != IntPtr.Zero; recordPtr = record.pNext)
            {
                record = (dynamic)Marshal.PtrToStructure(recordPtr, structType);

                records.Add(record);
            }

            return(records.ToArray());
        }
        private async Task OnQueryReceived(object sender, QueryReceivedEventArgs e)
        {
            if (e.Query is DnsMessage message)
            {
                var response = message.CreateResponseInstance();

                if (message.Questions.Count == 1)
                {
                    DnsClient dnsClient;
                    var       options = new DnsQueryOptions
                    {
                        IsEDnsEnabled      = true,
                        IsRecursionDesired = message.IsRecursionDesired,
                        IsDnsSecOk         = message.IsDnsSecOk,
                        IsCheckingDisabled = message.IsCheckingDisabled
                    };

                    var existEcs = ExistEcs(message.EDnsOptions);
                    if (existEcs)
                    {
                        foreach (var option in message.EDnsOptions.Options)
                        {
                            options.EDnsOptions.Options.Add(option);
                        }
                    }

                    var question = message.Questions[0];
                    Console.WriteLine($@"DNS query: {question.Name}");
                    if (IsLocal(question.Name))
                    {
                        response.ReturnCode = ReturnCode.NxDomain;
                        e.Response          = response;
                        return;
                    }
                    else if (IsOnList(question.Name))
                    {
                        dnsClient = UpStreamDns;
                        if (!existEcs)
                        {
                            if (UpStreamEcs != null)
                            {
                                options.EDnsOptions.Options.Add(UpStreamEcs);
                            }
                            else                             //if (question.RecordType != RecordType.Ptr)
                            {
                                options.EDnsOptions.Options.Add(new ClientSubnetOption(32, e.RemoteEndpoint.Address));
                            }
                        }
                    }
                    else
                    {
                        dnsClient = PureDns;
                        if (!existEcs)
                        {
                            if (PureEcs != null)
                            {
                                options.EDnsOptions.Options.Add(PureEcs);
                            }
                            else                             //if (question.RecordType != RecordType.Ptr)
                            {
                                options.EDnsOptions.Options.Add(new ClientSubnetOption(32, e.RemoteEndpoint.Address));
                            }
                        }
                    }

                    // send query to upstream server
                    var upstreamResponse = await dnsClient.ResolveAsync(question.Name, question.RecordType, question.RecordClass, options);

                    // if got an answer, copy it to the message sent to the client
                    if (upstreamResponse != null)
                    {
                        upstreamResponse.TransactionID = response.TransactionID;
                        if (!existEcs)
                        {
                            upstreamResponse.EDnsOptions = response.EDnsOptions;
                        }
                        response            = upstreamResponse;
                        response.ReturnCode = ReturnCode.NoError;
                        e.Response          = response;
                    }
                }
                else
                {
                    response.ReturnCode = ReturnCode.FormatError;
                    e.Response          = response;
                }
            }
        }
 private static extern int DnsQuery(
     [In] string pszName,
     DnsRecordType wType,
     DnsQueryOptions options,
     IntPtr aipServers,
     ref IntPtr ppQueryResults,
     int pReserved);
Пример #18
0
 public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordTypes wType,
                                   DnsQueryOptions Options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
Пример #19
0
 public static extern int DnsQueryWithServerIp([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordTypes wType,
                                               DnsQueryOptions Options, ref IP4_ARRAY dnsServerIpArray, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
Пример #20
0
        public static IEnumerable <DnsRecord> QuerySrvRecord(string query, DnsQueryOptions options = DefaultOptions)
        {
            var error = DnsQuery(
                query,
                DnsRecordType.SRV,
                options,
                IntPtr.Zero,
                out IntPtr ppQueryResults,
                IntPtr.Zero
                );

            if (Debug && ppQueryResults != IntPtr.Zero)
            {
                // IntPtr.Size + 2 + 2 + 2 + 4 + 4 + 4;

                var dump = ppQueryResults.DumpHex((uint)Marshal.SizeOf <Win32DnsRecord>());

                System.Diagnostics.Debug.WriteLine(dump);
            }

            var records = new List <DnsRecord>();

            try
            {
                if (IgnoredErrors.Contains(error))
                {
                    return(records);
                }

                if (error != 0)
                {
                    throw new Win32Exception(error);
                }

                Win32DnsRecord dnsRecord;

                for (var pNext = ppQueryResults; pNext != IntPtr.Zero; pNext = dnsRecord.pNext)
                {
                    dnsRecord = Marshal.PtrToStructure <Win32DnsRecord>(pNext);

                    switch (dnsRecord.wType)
                    {
                    case DnsRecordType.SRV:
                        var srvRecord = Marshal.PtrToStructure <Win32SrvRecord>(pNext);

                        records.Add(new DnsRecord
                        {
                            Target     = srvRecord.pNameTarget,
                            Name       = srvRecord.pName,
                            Port       = srvRecord.wPort,
                            Priority   = srvRecord.wPriority,
                            TimeToLive = srvRecord.dwTtl,
                            Type       = srvRecord.wType,
                            Weight     = srvRecord.wWeight
                        });
                        break;

                    case DnsRecordType.A:
                        var aRecord = Marshal.PtrToStructure <Win32ARecord>(pNext);
                        records.Add(new DnsRecord
                        {
                            Type   = aRecord.wType,
                            Name   = aRecord.pName,
                            Target = new IPAddress(aRecord.IPAddress).ToString()
                        });
                        break;

                    case DnsRecordType.AAAA:
                        var aaaaRecord = Marshal.PtrToStructure <Win32AAAARecord>(pNext);

                        records.Add(new DnsRecord
                        {
                            Type   = aaaaRecord.wType,
                            Name   = aaaaRecord.pName,
                            Target = new IPAddress(aaaaRecord.IPAddress).ToString()
                        });
                        break;
                    }
                }
            }
            finally
            {
                DnsRecordListFree(ppQueryResults, DnsFreeType.DnsFreeRecordList);
            }

            var merged = records.Where(r => r.Type != DnsRecordType.SRV).GroupBy(r => r.Name);

            foreach (var srv in records.Where(r => r.Type == DnsRecordType.SRV))
            {
                var c1 = merged.Where(m => m.Key.Equals(srv.Target, StringComparison.InvariantCultureIgnoreCase));

                var canon = c1.SelectMany(r => r);

                srv.Canonical = canon.ToList();
            }

            return(records);
        }
Пример #21
0
 public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName, DnsQueryTypes wType, DnsQueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);