Class DNSUtil


  • public class DNSUtil
    extends Object
    Utility class to perform DNS lookups for XMPP services.
    Author:
    Matt Tucker
    • Constructor Detail

      • DNSUtil

        public DNSUtil()
    • Method Detail

      • resolveXMPPDomain

        public static List<DNSUtil.HostAddress> resolveXMPPDomain​(String domain,
                                                                  int defaultPort)
        Returns a sorted list of host names and ports that the specified XMPP domain can be reached at for server-to-server communication. DNS lookups for a SRV records in the form "_xmpp-server._tcp.example.com" and "_xmpps-server._tcp.example.com" are attempted, in line with section 3.2 of XMPP Core and XEP-0368. If those lookup fail to provide any records, a lookup in the older form of "_jabber._tcp.example.com" is attempted since servers that implement an older version of the protocol may be listed using that notation. If that lookup fails as well, it's assumed that the XMPP server lives at the host resolved by a DNS A lookup at the specified domain on the specified default port.

        As an example, a lookup for "example.com" may return "im.example.com:5269".

        Parameters:
        domain - the domain.
        defaultPort - default port to return if the DNS look up fails.
        Returns:
        a list of HostAddresses, which encompasses the hostname and port that the XMPP server can be reached at for the specified domain.
        See Also:
        XMPP CORE, XEP-0368
      • getDnsOverride

        public static Map<String,​DNSUtil.HostAddress> getDnsOverride()
        Returns the internal DNS that allows to specify target IP addresses and ports to use for domains. The internal DNS will be checked up before performing an actual DNS SRV lookup.
        Returns:
        the internal DNS that allows to specify target IP addresses and ports to use for domains.
      • setDnsOverride

        public static void setDnsOverride​(Map<String,​DNSUtil.HostAddress> dnsOverride)
        Sets the internal DNS that allows to specify target IP addresses and ports to use for domains. The internal DNS will be checked up before performing an actual DNS SRV lookup.
        Parameters:
        dnsOverride - the internal DNS that allows to specify target IP addresses and ports to use for domains.
      • srvLookup

        public static List<DNSUtil.WeightedHostAddress> srvLookup​(String service,
                                                                  String proto,
                                                                  String name)
        Performs a DNS SRV lookup. Does not take into account any DNS overrides configured in this class. The results returned by this method are ordered by priority (ascending), and order of equal priority entries is randomized by weight, as defined in the DNS SRV specification.
        Parameters:
        service - the symbolic name of the desired service (cannot be null).
        proto - the transport protocol of the desired service; this is usually either TCP or UDP (cannot be null).
        name - the domain name for which this record is valid (cannot be null).
        Returns:
        An ordered of results (possibly empty, never null).
      • isNameCoveredByPattern

        public static boolean isNameCoveredByPattern​(String name,
                                                     String pattern)
        Checks if the provided DNS pattern matches the provided name. For example, this method will: return true for name: xmpp.example.org, pattern: *.example.org return false for name: xmpp.example.org, pattern: example.org This method is not case sensitive.
        Parameters:
        name - The name to check against a pattern (cannot be null or empty).
        pattern - the pattern (cannot be null or empty).
        Returns:
        true when the name is covered by the pattern, otherwise false.