• DnsClient
  • Readme
  • Api Docs
  • MichaCo.net
This site uses "cookies" - small data files stored locally on your computer - to save information about your preferences when using this site. Learn More.

    Show / Hide Table of Contents

    Class DnsQueryExtensions

    Extension methods for the IDnsQuery contract.

    The methods implement common queries which are more complex and have some business logic.

    Testing

    Inheritance
    Object
    DnsQueryExtensions
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: DnsClient
    Assembly: DnsClient.dll
    Syntax
    public static class DnsQueryExtensions

    Methods

    | Improve this Doc View Source

    GetHostEntry(IDnsQuery, IPAddress)

    The GetHostEntry method does a reverse lookup on the IP address, and queries a DNS server for the IP addresses and aliases associated with the resolved hostname.

    IP addresses found are returned in AddressList. CNAME records are used to populate the Aliases.
    The HostName property will be set to the resolved hostname of the address.

    Declaration
    public static IPHostEntry GetHostEntry(this IDnsQuery query, IPAddress address)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    IPAddress address

    The IPAddress to query for.

    Returns
    Type Description
    IPHostEntry

    An IPHostEntry instance that contains address information about the host specified in address. In case the address could not be resolved to a domain name, this method returns null, unless ThrowDnsErrors is set to true, then it might throw a DnsResponseException.

    Remarks

    The method has some logic to populate the Aliases list:

    • In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
    • If only one IPAddress is in the result set, all the aliases found will be returned.
    • If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.

    Examples

    The following code example uses the GetHostEntry(IDnsQuery, IPAddress) method to resolve an IP address to an IPHostEntry instance.

    public static void PrintHostEntry(IPAddress address)
    {
        var lookup = new LookupClient();
        IPHostEntry hostEntry = lookup.GetHostEntry(address);
        Console.WriteLine(hostEntry.HostName);
        foreach (var ip in hostEntry.AddressList)
        {
            Console.WriteLine(ip);
        }
        foreach (var alias in hostEntry.Aliases)
        {
            Console.WriteLine(alias);
        }
    }
    Exceptions
    Type Condition
    ArgumentNullException

    If address is null.

    DnsResponseException

    In case ThrowDnsErrors is set to true and a DNS error occurs.

    | Improve this Doc View Source

    GetHostEntry(IDnsQuery, String)

    The GetHostEntry method queries a DNS server for the IP addresses and aliases associated with the hostNameOrAddress. In case hostNameOrAddress is an IPAddress, GetHostEntry does a reverse lookup on that first to determine the hostname.

    IP addresses found are returned in AddressList. CNAME records are used to populate the Aliases.
    The HostName property will be set to the resolved hostname or hostNameOrAddress.

    Declaration
    public static IPHostEntry GetHostEntry(this IDnsQuery query, string hostNameOrAddress)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String hostNameOrAddress

    The IPAddress or host name to query for.

    Returns
    Type Description
    IPHostEntry

    An IPHostEntry instance that contains address information about the host specified in hostNameOrAddress. In case the hostNameOrAddress could not be resolved to a domain name, this method returns null, unless ThrowDnsErrors is set to true, then it might throw a DnsResponseException.

    Remarks

    The method has some logic to populate the Aliases list:

    • In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
    • If only one IPAddress is in the result set, all the aliases found will be returned.
    • If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.

    Examples

    The following code example uses the GetHostEntry(IDnsQuery, String) method to resolve an IP address or hostname to an IPHostEntry instance.

    public static void PrintHostEntry(string hostOrIp)
    {
        var lookup = new LookupClient();
        IPHostEntry hostEntry = lookup.GetHostEntry(hostOrIp);
        Console.WriteLine(hostEntry.HostName);
        foreach (var ip in hostEntry.AddressList)
        {
            Console.WriteLine(ip);
        }
        foreach (var alias in hostEntry.Aliases)
        {
            Console.WriteLine(alias);
        }
    }
    Exceptions
    Type Condition
    ArgumentNullException

    If hostNameOrAddress is null.

    DnsResponseException

    In case ThrowDnsErrors is set to true and a DNS error occurs.

    | Improve this Doc View Source

    GetHostEntryAsync(IDnsQuery, IPAddress)

    The GetHostEntryAsync method does a reverse lookup on the IP address, and queries a DNS server for the IP addresses and aliases associated with the resolved hostname.

    IP addresses found are returned in AddressList. CNAME records are used to populate the Aliases.
    The HostName property will be set to the resolved hostname of the address.

    Declaration
    public static Task<IPHostEntry> GetHostEntryAsync(this IDnsQuery query, IPAddress address)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    IPAddress address

    The IPAddress to query for.

    Returns
    Type Description
    Task<IPHostEntry>

    An IPHostEntry instance that contains address information about the host specified in address. In case the address could not be resolved to a domain name, this method returns null, unless ThrowDnsErrors is set to true, then it might throw a DnsResponseException.

    Remarks

    The method has some logic to populate the Aliases list:

    • In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
    • If only one IPAddress is in the result set, all the aliases found will be returned.
    • If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.

    Examples

    The following code example uses the GetHostEntryAsync(IDnsQuery, IPAddress) method to resolve an IP address to an IPHostEntry instance.

    public static async Task PrintHostEntry(IPAddress address)
    {
        var lookup = new LookupClient();
        IPHostEntry hostEntry = await lookup.GetHostEntryAsync(address);
        Console.WriteLine(hostEntry.HostName);
        foreach (var ip in hostEntry.AddressList)
        {
            Console.WriteLine(ip);
        }
        foreach (var alias in hostEntry.Aliases)
        {
            Console.WriteLine(alias);
        }
    }
    Exceptions
    Type Condition
    ArgumentNullException

    If address is null.

    DnsResponseException

    In case ThrowDnsErrors is set to true and a DNS error occurs.

    | Improve this Doc View Source

    GetHostEntryAsync(IDnsQuery, String)

    The GetHostEntryAsync method queries a DNS server for the IP addresses and aliases associated with the hostNameOrAddress. In case hostNameOrAddress is an IPAddress, GetHostEntry does a reverse lookup on that first to determine the hostname.

    IP addresses found are returned in AddressList. CNAME records are used to populate the Aliases.
    The HostName property will be set to the resolved hostname or hostNameOrAddress.

    Declaration
    public static Task<IPHostEntry> GetHostEntryAsync(this IDnsQuery query, string hostNameOrAddress)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String hostNameOrAddress

    The IPAddress or host name to query for.

    Returns
    Type Description
    Task<IPHostEntry>

    An IPHostEntry instance that contains address information about the host specified in hostNameOrAddress. In case the hostNameOrAddress could not be resolved to a domain name, this method returns null, unless ThrowDnsErrors is set to true, then it might throw a DnsResponseException.

    Remarks

    The method has some logic to populate the Aliases list:

    • In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
    • If only one IPAddress is in the result set, all the aliases found will be returned.
    • If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.

    Examples

    The following code example uses the GetHostEntryAsync(IDnsQuery, String) method to resolve an IP address or hostname to an IPHostEntry instance.

    public static async Task PrintHostEntry(string hostOrIp)
    {
        var lookup = new LookupClient();
        IPHostEntry hostEntry = await lookup.GetHostEntryAsync(hostOrIp);
        Console.WriteLine(hostEntry.HostName);
        foreach (var ip in hostEntry.AddressList)
        {
            Console.WriteLine(ip);
        }
        foreach (var alias in hostEntry.Aliases)
        {
            Console.WriteLine(alias);
        }
    }
    Exceptions
    Type Condition
    ArgumentNullException

    If hostNameOrAddress is null.

    DnsResponseException

    In case ThrowDnsErrors is set to true and a DNS error occurs.

    | Improve this Doc View Source

    GetHostName(IDnsQuery, IPAddress)

    The GetHostName method queries a DNS server to resolve the hostname of the address via reverse lookup.

    Declaration
    public static string GetHostName(this IDnsQuery query, IPAddress address)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    IPAddress address

    The IPAddress to resolve.

    Returns
    Type Description
    String

    The hostname if the reverse lookup was successful or null, in case the host was not found. If ThrowDnsErrors is set to true, this method will throw an DnsResponseException instead of returning null!

    Exceptions
    Type Condition
    ArgumentNullException

    If addressis null.

    DnsResponseException

    If no host has been found and ThrowDnsErrors is true.

    | Improve this Doc View Source

    GetHostNameAsync(IDnsQuery, IPAddress)

    The GetHostNameAsync method queries a DNS server to resolve the hostname of the address via reverse lookup.

    Declaration
    public static Task<string> GetHostNameAsync(this IDnsQuery query, IPAddress address)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    IPAddress address

    The IPAddress to resolve.

    Returns
    Type Description
    Task<String>

    The hostname if the reverse lookup was successful or null, in case the host was not found. If ThrowDnsErrors is set to true, this method will throw an DnsResponseException instead of returning null!

    Exceptions
    Type Condition
    ArgumentNullException

    If addressis null.

    DnsResponseException

    If no host has been found and ThrowDnsErrors is true.

    | Improve this Doc View Source

    ResolveService(IDnsQuery, String, String, ProtocolType)

    The ResolveService method does a SRV lookup for {serviceName}[.{protocol}].{baseDomain} and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.

    This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.

    Declaration
    public static ServiceHostEntry[] ResolveService(this IDnsQuery query, string baseDomain, string serviceName, ProtocolType protocol)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String baseDomain

    The base domain, which will be appended to the end of the query string.

    String serviceName

    The name of the service to look for. Must not have any _ prefix.

    ProtocolType protocol

    The protocol of the service to query for. Set it to Unknown or Unspecified to not pass any protocol.

    Returns
    Type Description
    ServiceHostEntry[]

    A collection of ServiceHostEntrys.

    Remarks

    The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.

    Exceptions
    Type Condition
    ArgumentNullException

    If baseDomain or serviceName are null.

    See Also
    RFC 2782
    | Improve this Doc View Source

    ResolveService(IDnsQuery, String, String, String)

    The ResolveService method does a SRV lookup for {serviceName}[.{tag}].{baseDomain} and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.

    This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.

    Declaration
    public static ServiceHostEntry[] ResolveService(this IDnsQuery query, string baseDomain, string serviceName, string tag = null)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String baseDomain

    The base domain, which will be appended to the end of the query string.

    String serviceName

    The name of the service to look for. Must not have any _ prefix.

    String tag

    An optional tag. Must not have any _ prefix.

    Returns
    Type Description
    ServiceHostEntry[]

    A collection of ServiceHostEntrys.

    Remarks

    The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.

    Exceptions
    Type Condition
    ArgumentNullException

    If baseDomain or serviceName are null.

    See Also
    RFC 2782
    | Improve this Doc View Source

    ResolveServiceAsync(IDnsQuery, String, String, ProtocolType)

    The ResolveServiceAsync method does a SRV lookup for {serviceName}[.{protocol}].{baseDomain} and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.

    This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.

    Declaration
    public static Task<ServiceHostEntry[]> ResolveServiceAsync(this IDnsQuery query, string baseDomain, string serviceName, ProtocolType protocol)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String baseDomain

    The base domain, which will be appended to the end of the query string.

    String serviceName

    The name of the service to look for. Must not have any _ prefix.

    ProtocolType protocol

    The protocol of the service to query for. Set it to Unknown or Unspecified to not pass any protocol.

    Returns
    Type Description
    Task<ServiceHostEntry[]>

    A collection of ServiceHostEntrys.

    Remarks

    The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.

    Exceptions
    Type Condition
    ArgumentNullException

    If baseDomain or serviceName are null.

    See Also
    RFC 2782
    | Improve this Doc View Source

    ResolveServiceAsync(IDnsQuery, String, String, String)

    The ResolveServiceAsync method does a SRV lookup for {serviceName}[.{tag}].{baseDomain} and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.

    This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.

    Declaration
    public static Task<ServiceHostEntry[]> ResolveServiceAsync(this IDnsQuery query, string baseDomain, string serviceName, string tag = null)
    Parameters
    Type Name Description
    IDnsQuery query

    The IDnsQuery instance.

    String baseDomain

    The base domain, which will be appended to the end of the query string.

    String serviceName

    The name of the service to look for. Must not have any _ prefix.

    String tag

    An optional tag. Must not have any _ prefix.

    Returns
    Type Description
    Task<ServiceHostEntry[]>

    A collection of ServiceHostEntrys.

    Remarks

    The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.

    Exceptions
    Type Condition
    ArgumentNullException

    If baseDomain or serviceName are null.

    See Also
    RFC 2782
    • Improve this Doc
    • View Source
    © 2023 by Michael Conrad. All rights reserved. - MichaCo.net