/// <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); }
/// <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); }
/// <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; }
/// <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; }