Ejemplo n.º 1
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 async Task <DnsQueryResponse> Resolve(string dnsServer, string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            byte[] bDnsQuery = this.BuildDnsRequest(host, queryType, queryClass, protocol, messageSecurityProvider);

            new System.Net.Sockets.Socket(System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp).Dispose();

            // Connect to DNS server and get the record for the current server.
            IPHostEntry ipe = await Dns.GetHostEntryAsync(dnsServer);

            IPAddress  ipa  = ipe.AddressList[0];
            IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);

            byte[] recvBytes = null;

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

                break;
            }

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

                break;
            }

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

            DnsQueryResponse dnsQR = new DnsQueryResponse();

            dnsQR.ParseResponse(recvBytes, protocol);

            return(dnsQR);
        }
Ejemplo n.º 2
0
        private byte[] BuildDnsRequest(string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Combind the NsFlags with our constant flags
            ushort flags = (ushort)((ushort)_queryResponse | (ushort)_opCode | (ushort)_nsFlags);

            this._flags = flags;

            //NOTE: This limits the librarys ablity to issue multiple queries per request.
            this._nsType  = queryType;
            this._nsClass = queryClass;
            this._name    = host;

            if (messageSecurityProvider != null)
            {
                messageSecurityProvider.SecureMessage(this);
            }

            byte[] bDnsQuery = GetMessageBytes();

            // Add two byte prefix that contains the packet length per RFC 1035 section 4.2.2
            if (protocol == ProtocolType.Tcp)
            {
                // 4.2.2. TCP usageMessages sent over TCP connections use server port 53 (decimal).
                // The message is prefixed with a two byte length field which gives the message
                // length, excluding the two byte length field.  This length field allows the
                // low-level processing to assemble a complete message before beginning to parse
                // it.
                int len = bDnsQuery.Length;
                Array.Resize <byte>(ref bDnsQuery, len + 2);
                Array.Copy(bDnsQuery, 0, bDnsQuery, 2, len);
                bDnsQuery[0] = (byte)((len >> 8) & 0xFF);
                bDnsQuery[1] = (byte)((len & 0xFF));
            }

            return(bDnsQuery);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
        private byte[] BuildDnsRequest(string host, NsType queryType, NsClass queryClass, ProtocolType protocol, IMessageSecurityProvider messageSecurityProvider)
        {
            // Combind the NsFlags with our constant flags
            ushort flags = (ushort)((ushort)_queryResponse | (ushort)_opCode | (ushort)_nsFlags);
            this._flags = flags;

            //NOTE: This limits the librarys ablity to issue multiple queries per request.
            this._nsType = queryType;
            this._nsClass = queryClass;
            this._name = host;

            if(messageSecurityProvider != null)
            {
                messageSecurityProvider.SecureMessage(this);
            }

            byte[] bDnsQuery = GetMessageBytes();

            // Add two byte prefix that contains the packet length per RFC 1035 section 4.2.2
            if (protocol == ProtocolType.Tcp)
            {
                // 4.2.2. TCP usageMessages sent over TCP connections use server port 53 (decimal).
                // The message is prefixed with a two byte length field which gives the message
                // length, excluding the two byte length field.  This length field allows the
                // low-level processing to assemble a complete message before beginning to parse
                // it.
                int len = bDnsQuery.Length;
                Array.Resize<byte>(ref bDnsQuery, len + 2);
                Array.Copy(bDnsQuery, 0, bDnsQuery, 2, len);
                bDnsQuery[0] = (byte)((len >> 8) & 0xFF);
                bDnsQuery[1] = (byte)((len & 0xFF));
            }

            return bDnsQuery;
        }
Ejemplo n.º 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.
            _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;
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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;
        }