Class DnsUtils
Provides DNS helpers: the network's resolvers, and a minimal A-record lookup over UDP.
public static class DnsUtils
- Inheritance
-
DnsUtils
- Inherited Members
Methods
BuildDnsQuery(ushort, string)
Builds a DNS query for a host's A records, recursion desired.
public static byte[] BuildDnsQuery(ushort queryId, string host)
Parameters
queryIdushortThe query's ID.
hoststringThe host name. A dot at its end is left out, and a label with Unicode letters goes in its ASCII form.
Returns
- byte[]
The query's bytes.
Exceptions
- ArgumentException
hostis empty, or no host name: a label of it is empty or longer than 63 characters, or the name longer than 253.
GetHostEntry(string, IPEndPoint, UdpClient, TimeSpan, CancellationToken)
Looks up a host's IPv4 addresses with one query to a resolver, over a given UDP client: the IN A records of the answer section; the names and the other sections are not checked.
public static Task<IPHostEntry> GetHostEntry(string host, IPEndPoint dnsEndPoint, UdpClient udpClient, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
hoststringThe host name.
dnsEndPointIPEndPointThe resolver.
udpClientUdpClientThe UDP client, which this call connects to the resolver; the caller keeps owning it.
timeoutTimeSpanHow long the send and the answer may take.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IPHostEntry>
A task whose result is the host's addresses.
Exceptions
- ArgumentException
hostis empty or white space, or no host name.- ArgumentNullException
dnsEndPointis null.- OperationCanceledException
The timeout passed, or
cancellationTokenwas canceled.- InvalidOperationException
The answer is not this query's, not a response, truncated, or an error.
- Exception
The answer has no A record.
- EndOfStreamException
The answer ends early.
- ArgumentOutOfRangeException
The timeout is negative, other than infinite, or too long.
GetHostEntry(string, IPEndPoint, TimeSpan, CancellationToken)
Looks up a host's IPv4 addresses with one query to a resolver, over a UDP client of its own: the IN A records of the answer section; the names and the other sections are not checked.
public static Task<IPHostEntry> GetHostEntry(string host, IPEndPoint dnsEndPoint, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
hoststringThe host name.
dnsEndPointIPEndPointThe resolver.
timeoutTimeSpanHow long the send and the answer may take.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IPHostEntry>
A task whose result is the host's addresses.
Exceptions
- ArgumentException
hostis empty or white space, or no host name.- ArgumentNullException
dnsEndPointis null.- OperationCanceledException
The timeout passed, or
cancellationTokenwas canceled.- InvalidOperationException
The answer is not this query's, not a response, truncated, or an error.
- Exception
The answer has no A record.
- EndOfStreamException
The answer ends early.
- ArgumentOutOfRangeException
The timeout is negative, other than infinite, or too long.
GetNetworkDnsServers()
Gets the resolvers of the networks the machine reaches beyond itself by: adapters that are up with a gateway, not loopback or tunnel ones.
public static IReadOnlyList<IPAddress> GetNetworkDnsServers()
Returns
- IReadOnlyList<IPAddress>
The resolvers' addresses; none where there are none.
Remarks
IPv6 site-local addresses, as Windows' fec0:0:0:ffff:: placeholders, and loopback ones, as a stub on this
machine, are left out. On Linux, when none is left, as where it names only systemd-resolved's stub, the upstream
list it writes, /run/systemd/resolve/resolv.conf, stands in.
ParseDnsResponse(byte[], ushort)
Reads the IN A records of a DNS response's answer section, after checking its ID and flags: a response, not truncated, as a truncated one may lack some of the name's addresses, and without an error. The names and the other sections are not checked.
public static IPHostEntry ParseDnsResponse(byte[] response, ushort queryId)
Parameters
Returns
- IPHostEntry
The addresses, with an empty host name.
Exceptions
- InvalidOperationException
The answer is not this query's, not a response, truncated, or an error.
- EndOfStreamException
The response ends early.
- Exception
The response has no A record.