Summary description for DnsQueryResponse.
Inheritance: DnsQueryBase
Example #1
0
        private static void OutputResults(DnsQueryResponse response)
        {
            Console.WriteLine("Bytes received: " + response.BytesReceived);

            Console.WriteLine("Name: " + response.Name);
            Console.WriteLine("OpCode: " + response.NsClass);
            Console.WriteLine("NsFlags: " + response.NsFlags);
            Console.WriteLine("NsType: " + response.NsType);
            Console.WriteLine("RCode: " + response.RCode);
            Console.WriteLine("OpCode: " + response.OpCode);

            // Enumerate the Answer Records
            Console.WriteLine("Answers:");
            foreach (IDnsRecord record in response.Answers)
            {
                Console.WriteLine(record.Answer);
                Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
                Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
                Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
                Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
                Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);
                Console.WriteLine();
            }

            foreach (IDnsRecord record in response.AuthoritiveNameServers)
            {
                Console.WriteLine(record.Answer);
                Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
                Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
                Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
                Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
                Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);
                Console.WriteLine();
            }

            foreach (IDnsRecord record in response.AdditionalRRecords)
            {
                Console.WriteLine(record.Answer);
                Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
                Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
                Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
                Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
                Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);
                Console.WriteLine();
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dnsServer"></param>
        /// <param name="host"></param>
        /// <param name="queryType"></param>
        /// <param name="queryClass"></param>
        /// <param name="protocol"></param>
        /// <param name="messageSecurityProvider">The instance of the message security provider to use to secure the DNS request.</param>
        /// <returns>A <see cref="T:DnDns.Net.Dns.DnsQueryResponse"></see> instance that contains the Dns Answer for the request query.</returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Net.DnsPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public DnsQueryResponse Resolve(string dnsServer, string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Do stack walk and Demand all callers have DnsPermission.
            _dnsPermissions.Demand();

            byte[] bDnsQuery = this.BuildDnsRequest(host, queryType, queryClass, protocol, messageSecurityProvider);

            // Connect to DNS server and get the record for the current server.
            IPHostEntry ipe  = System.Net.Dns.GetHostEntry(dnsServer);
            IPAddress   ipa  = ipe.AddressList[0];
            IPEndPoint  ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);

            byte[] recvBytes = null;

            switch (protocol)
            {
            case ProtocolType.Tcp:
            {
                recvBytes = ResolveTcp(bDnsQuery, ipep);
                break;
            }

            case ProtocolType.Udp:
            {
                recvBytes = ResolveUdp(bDnsQuery, ipep);
                break;
            }

            default:
            {
                throw new InvalidOperationException("Invalid Protocol: " + protocol);
            }
            }

            Trace.Assert(recvBytes != null, "Failed to retrieve data from the remote DNS server.");

            DnsQueryResponse dnsQR = new DnsQueryResponse();

            dnsQR.ParseResponse(recvBytes, protocol);

            return(dnsQR);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dnsServer"></param>
        /// <param name="host"></param>
        /// <param name="queryType"></param>
        /// <param name="queryClass"></param>
        /// <param name="protocol"></param>
        /// <param name="messageSecurityProvider">The instance of the message security provider to use to secure the DNS request.</param>
        /// <returns>A <see cref="T:DnDns.Net.Dns.DnsQueryResponse"></see> instance that contains the Dns Answer for the request query.</returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Net.DnsPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public DnsQueryResponse Resolve(string dnsServer, string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Do stack walk and Demand all callers have DnsPermission.
            // FIXME _dnsPermissions.Demand();

            DnsQueryResponse dnsQR = new DnsQueryResponse();

            // Try a native query if it is supported.
            if (Tools.HasSystemDns)
            // CS0162 will fire when HasSystemDns is a constant.
#pragma warning disable 162
            {
                // See https://www.dns-oarc.net/oarc/services/replysizetest - 4k likely plenty.
                byte[] answer     = new byte[4096];
                int    answerSize = Tools.SystemResQuery(host, queryClass, queryType, answer);
                if (0 < answerSize)
                {
                    dnsQR.ParseResponse(answer, answerSize);
                    return(dnsQR);
                }
                else
                {
                    return(null);
                }
            }

            byte[] recvBytes = null;
            byte[] bDnsQuery = this.BuildDnsRequest(host, queryType, queryClass, protocol, messageSecurityProvider);

            IPAddress[] ipas = System.Net.Dns.GetHostAddresses(dnsServer);
            IPEndPoint  ipep = null;
            foreach (var addr in ipas)
            {
                if (addr.AddressFamily == AddressFamily.InterNetwork)
                {
                    ipep = new IPEndPoint(addr, (int)UdpServices.Domain);
                    break;
                }
            }
            if (null == ipep)
            {
                throw new Exception(string.Format("No IPv4 address found for hostname {0}", dnsServer));
            }

            switch (protocol)
            {
            case ProtocolType.Tcp:
            {
                recvBytes = ResolveTcp(bDnsQuery, ipep);
                break;
            }

            case ProtocolType.Udp:
            {
                recvBytes = ResolveUdp(bDnsQuery, ipep);
                break;
            }

            default:
            {
                throw new InvalidOperationException("Invalid Protocol: " + protocol);
            }
            }

            Trace.Assert(recvBytes != null, "Failed to retrieve data from the remote DNS server.");

            dnsQR.ParseResponse(recvBytes);

            return(dnsQR);
        }
Example #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dnsServer"></param>
        /// <param name="host"></param>
        /// <param name="queryType"></param>
        /// <param name="queryClass"></param>
        /// <param name="protocol"></param>
        /// <param name="messageSecurityProvider">The instance of the message security provider to use to secure the DNS request.</param>
        /// <returns>A <see cref="T:DnDns.Net.Dns.DnsQueryResponse"></see> instance that contains the Dns Answer for the request query.</returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Net.DnsPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public DnsQueryResponse Resolve(string dnsServer, string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Do stack walk and Demand all callers have DnsPermission.
            _dnsPermissions.Demand();

            byte[] bDnsQuery = this.BuildDnsRequest(host, queryType, queryClass, protocol, messageSecurityProvider);

            // Connect to DNS server and get the record for the current server.
            IPHostEntry ipe = System.Net.Dns.GetHostEntry(dnsServer);
            IPAddress ipa = ipe.AddressList[0];
            IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);

            byte[] recvBytes = null;

            switch (protocol)
            {
                case ProtocolType.Tcp:
                    {
                        recvBytes = ResolveTcp(bDnsQuery, ipep);
                        break;
                    }
                case ProtocolType.Udp:
                    {
                        recvBytes = ResolveUdp(bDnsQuery, ipep);
                        break;
                    }
                default:
                    {
                        throw new InvalidOperationException("Invalid Protocol: " + protocol);
                    }
            }

            Trace.Assert(recvBytes != null, "Failed to retrieve data from the remote DNS server.");

            DnsQueryResponse dnsQR = new DnsQueryResponse();

            dnsQR.ParseResponse(recvBytes, protocol);

            return dnsQR;
        }
Example #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dnsServer"></param>
        /// <param name="host"></param>
        /// <param name="queryType"></param>
        /// <param name="queryClass"></param>
        /// <param name="protocol"></param>
        /// <param name="messageSecurityProvider">The instance of the message security provider to use to secure the DNS request.</param>
        /// <returns>A <see cref="T:DnDns.Net.Dns.DnsQueryResponse"></see> instance that contains the Dns Answer for the request query.</returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Net.DnsPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public DnsQueryResponse Resolve(string dnsServer, string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Do stack walk and Demand all callers have DnsPermission.
            // FIXME _dnsPermissions.Demand();

            DnsQueryResponse dnsQR = new DnsQueryResponse();
            // Try a native query if it is supported.
            if (Tools.HasSystemDns)
                // CS0162 will fire when HasSystemDns is a constant.
            #pragma warning disable 162
            {
                // See https://www.dns-oarc.net/oarc/services/replysizetest - 4k likely plenty.
                byte[] answer = new byte[4096];
                int answerSize = Tools.SystemResQuery (host, queryClass, queryType, answer);
                if (0 < answerSize) {
                    dnsQR.ParseResponse (answer, answerSize);
                    return dnsQR;
                } else {
                    return null;
                }
            }

            byte[] recvBytes = null;
            byte[] bDnsQuery = this.BuildDnsRequest(host, queryType, queryClass, protocol, messageSecurityProvider);

            IPAddress[] ipas = System.Net.Dns.GetHostAddresses (dnsServer);
            IPEndPoint ipep = null;
            foreach (var addr in ipas) {
                if (addr.AddressFamily == AddressFamily.InterNetwork) {
                    ipep = new IPEndPoint(addr, (int)UdpServices.Domain);
                    break;
                }
            }
            if (null == ipep) {
                throw new Exception (string.Format ("No IPv4 address found for hostname {0}", dnsServer));
            }

            switch (protocol)
            {
                case ProtocolType.Tcp:
                    {
                        recvBytes = ResolveTcp(bDnsQuery, ipep);
                        break;
                    }
                case ProtocolType.Udp:
                    {
                        recvBytes = ResolveUdp(bDnsQuery, ipep);
                        break;
                    }
                default:
                    {
                        throw new InvalidOperationException("Invalid Protocol: " + protocol);
                    }
            }

            Trace.Assert(recvBytes != null, "Failed to retrieve data from the remote DNS server.");

            dnsQR.ParseResponse(recvBytes);

            return dnsQR;
        }
Example #6
0
        bool ParseDnsResponse(DnsQueryResponse ur)
        {
            if (ur.RCode != RCode.NoError)
                return false;

            foreach (IDnsRecord record in ur.Answers) {
                //Console.WriteLine (record.Answer + ", " + record.DnsHeader.NsType);
                if (record.DnsHeader.NsType == NsType.A)
                    AList.Add (IPAddress.Parse (record.Answer.Replace ("Address: ", "")));
                if (record.DnsHeader.NsType == NsType.AAAA && Proxy.IPv6)
                    AList.Add (IPAddress.Parse (record.Answer.Replace ("Address: ", "")));
                if (record.DnsHeader.NsType == NsType.CNAME)
                    NameList.Add (record.Answer.Replace ("Host: ", "").Trim ('.'));
            }
            return true;
        }