private static async Task ResolveNameServerAddressesAsync(DnsServer dnsServer, string nsDomain, int port, DnsTransportProtocol protocol, List <NameServerAddress> outNameServers) { try { DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.A, DnsClass.IN)); if (response.Answer.Count > 0) { IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseA(response); foreach (IPAddress address in addresses) { outNameServers.Add(new NameServerAddress(nsDomain, new IPEndPoint(address, port), protocol)); } } } catch { } if (dnsServer.PreferIPv6) { try { DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.AAAA, DnsClass.IN)); if (response.Answer.Count > 0) { IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseAAAA(response); foreach (IPAddress address in addresses) { outNameServers.Add(new NameServerAddress(nsDomain, new IPEndPoint(address, port), protocol)); } } } catch { } } }
private static async Task <IReadOnlyList <NameServerAddress> > GetNameServerAddressesAsync(DnsServer dnsServer, DnsResourceRecord record) { string nsDomain; switch (record.Type) { case DnsResourceRecordType.NS: nsDomain = (record.RDATA as DnsNSRecord).NameServer; break; case DnsResourceRecordType.SOA: nsDomain = (record.RDATA as DnsSOARecord).PrimaryNameServer; break; default: throw new InvalidOperationException(); } List <NameServerAddress> nameServers = new List <NameServerAddress>(2); IReadOnlyList <DnsResourceRecord> glueRecords = record.GetGlueRecords(); if (glueRecords.Count > 0) { foreach (DnsResourceRecord glueRecord in glueRecords) { switch (glueRecord.Type) { case DnsResourceRecordType.A: nameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsARecord).Address)); break; case DnsResourceRecordType.AAAA: if (dnsServer.PreferIPv6) { nameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsAAAARecord).Address)); } break; } } } else { //resolve addresses try { DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.A, DnsClass.IN)); if ((response != null) && (response.Answer.Count > 0)) { IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseA(response); foreach (IPAddress address in addresses) { nameServers.Add(new NameServerAddress(nsDomain, address)); } } } catch { } if (dnsServer.PreferIPv6) { try { DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.AAAA, DnsClass.IN)); if ((response != null) && (response.Answer.Count > 0)) { IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseAAAA(response); foreach (IPAddress address in addresses) { nameServers.Add(new NameServerAddress(nsDomain, address)); } } } catch { } } } return(nameServers); }