1
Vote

Resolve fails if dnsServer provided as an IP without PTR

description

Hi.

If dnsServer = "10.20.30.40" then Resolve tries to get PTR info on this IP.
And code fails on line System.Net.Dns.GetHostEntry(dnsServer) if system DNS has no PTR info on provided IP address.

Here is code snippet:
        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);
                    }
            }

comments

archimed7592 wrote Apr 27, 2014 at 11:47 AM

FYI, for now I've modified code like this:
            IPAddress ipa = null;
            if (!IPAddress.TryParse(dnsServer, out ipa))
            {
                // Connect to DNS server and get the record for the current server.
                IPHostEntry ipe = System.Net.Dns.GetHostEntry(dnsServer);
                ipa = ipe.AddressList[0];
            }
            IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);