Class Resolver

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
Resolver.Resolver$Impl, ThreadedResolver

@Generated("org.javagi.JavaGI") public abstract class Resolver extends GObject

The object that handles DNS resolution. Use getDefault() to get the default resolver.

GResolver provides cancellable synchronous and asynchronous DNS resolution, for hostnames (lookupByAddress(InetAddress, Cancellable), lookupByName(String, Cancellable) and their async variants) and SRV (service) records (lookupService(String, String, String, Cancellable)).

NetworkAddress and NetworkService provide wrappers around GResolver functionality that also implement SocketConnectable, making it easy to connect to a remote host/service.

The default resolver (see getDefault()) has a timeout of 30s set on it since GLib 2.78. Earlier versions of GLib did not support resolver timeouts.

This is an abstract type; subclasses of it implement different resolvers for different platforms and situations.

  • Constructor Details

    • Resolver

      public Resolver(MemorySegment address)
      Create a Resolver instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Resolver

      public Resolver()
      Create a new Resolver.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Resolver class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Resolver asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • freeAddresses

      public static void freeAddresses(List<MemorySegment> addresses)
      Frees addresses (which should be the return value from g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()). (This is a convenience method; you can also simply free the results by hand.)
      Parameters:
      addresses - a GList of GInetAddress
      Since:
      2.22
    • freeTargets

      public static void freeTargets(List<MemorySegment> targets)
      Frees targets (which should be the return value from g_resolver_lookup_service() or g_resolver_lookup_service_finish()). (This is a convenience method; you can also simply free the results by hand.)
      Parameters:
      targets - a GList of GSrvTarget
      Since:
      2.22
    • getDefault

      public static Resolver getDefault()
      Gets the default GResolver. You should unref it when you are done with it. GResolver may use its reference count as a hint about how many threads it should allocate for concurrent DNS resolutions.
      Returns:
      the default GResolver.
      Since:
      2.22
    • getTimeout

      public int getTimeout()
      Get the timeout applied to all resolver lookups. See GResolver:timeout.
      Returns:
      the resolver timeout, in milliseconds, or 0 for no timeout
      Since:
      2.78
    • lookupByAddress

      public String lookupByAddress(InetAddress address, @Nullable Cancellable cancellable) throws GErrorException

      Synchronously reverse-resolves address to determine its associated hostname.

      If the DNS resolution fails, error (if non-null) will be set to a value from GResolverError.

      If cancellable is non-null, it can be used to cancel the operation, in which case error (if non-null) will be set to IOErrorEnum.CANCELLED.

      Parameters:
      address - the address to reverse-resolve
      cancellable - a GCancellable, or null
      Returns:
      a hostname (either ASCII-only, or in ASCII-encoded form), or null on error.
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • lookupByAddressAsync

      public void lookupByAddressAsync(InetAddress address, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously reverse-resolving address to determine its associated hostname, and eventually calls callback, which must call g_resolver_lookup_by_address_finish() to get the final result.
      Parameters:
      address - the address to reverse-resolve
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.22
    • lookupByAddressFinish

      public String lookupByAddressFinish(AsyncResult result) throws GErrorException

      Retrieves the result of a previous call to g_resolver_lookup_by_address_async().

      If the DNS resolution failed, error (if non-null) will be set to a value from GResolverError. If the operation was cancelled, error will be set to IOErrorEnum.CANCELLED.

      Parameters:
      result - the result passed to your GAsyncReadyCallback
      Returns:
      a hostname (either ASCII-only, or in ASCII-encoded form), or null on error.
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • lookupByName

      public List<InetAddress> lookupByName(String hostname, @Nullable Cancellable cancellable) throws GErrorException

      Synchronously resolves hostname to determine its associated IP address(es). hostname may be an ASCII-only or UTF-8 hostname, or the textual form of an IP address (in which case this just becomes a wrapper around g_inet_address_new_from_string()).

      On success, g_resolver_lookup_by_name() will return a non-empty GList of GInetAddress, sorted in order of preference and guaranteed to not contain duplicates. That is, if using the result to connect to hostname, you should attempt to connect to the first address first, then the second if the first fails, etc. If you are using the result to listen on a socket, it is appropriate to add each result using e.g. g_socket_listener_add_address().

      If the DNS resolution fails, error (if non-null) will be set to a value from GResolverError and null will be returned.

      If cancellable is non-null, it can be used to cancel the operation, in which case error (if non-null) will be set to IOErrorEnum.CANCELLED.

      If you are planning to connect to a socket on the resolved IP address, it may be easier to create a GNetworkAddress and use its GSocketConnectable interface.

      Parameters:
      hostname - the hostname to look up
      cancellable - a GCancellable, or null
      Returns:
      a non-empty GList of GInetAddress, or null on error. You must unref each of the addresses and free the list when you are done with it. (You can use g_resolver_free_addresses() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • lookupByNameAsync

      public void lookupByNameAsync(String hostname, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously resolving hostname to determine its associated IP address(es), and eventually calls callback, which must call g_resolver_lookup_by_name_finish() to get the result. See g_resolver_lookup_by_name() for more details.
      Parameters:
      hostname - the hostname to look up the address of
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.22
    • lookupByNameFinish

      public List<InetAddress> lookupByNameFinish(AsyncResult result) throws GErrorException

      Retrieves the result of a call to g_resolver_lookup_by_name_async().

      If the DNS resolution failed, error (if non-null) will be set to a value from GResolverError. If the operation was cancelled, error will be set to IOErrorEnum.CANCELLED.

      Parameters:
      result - the result passed to your GAsyncReadyCallback
      Returns:
      a GList of GInetAddress, or null on error. See g_resolver_lookup_by_name() for more details.
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • lookupByNameWithFlags

      public List<InetAddress> lookupByNameWithFlags(String hostname, Set<ResolverNameLookupFlags> flags, @Nullable Cancellable cancellable) throws GErrorException
      This differs from g_resolver_lookup_by_name() in that you can modify the lookup behavior with flags. For example this can be used to limit results with ResolverNameLookupFlags.IPV4_ONLY.
      Parameters:
      hostname - the hostname to look up
      flags - extra GResolverNameLookupFlags for the lookup
      cancellable - a GCancellable, or null
      Returns:
      a non-empty GList of GInetAddress, or null on error. You must unref each of the addresses and free the list when you are done with it. (You can use g_resolver_free_addresses() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • lookupByNameWithFlags

      public List<InetAddress> lookupByNameWithFlags(String hostname, ResolverNameLookupFlags flags, @Nullable Cancellable cancellable) throws GErrorException
      This differs from g_resolver_lookup_by_name() in that you can modify the lookup behavior with flags. For example this can be used to limit results with ResolverNameLookupFlags.IPV4_ONLY.
      Parameters:
      hostname - the hostname to look up
      flags - extra GResolverNameLookupFlags for the lookup
      cancellable - a GCancellable, or null
      Returns:
      a non-empty GList of GInetAddress, or null on error. You must unref each of the addresses and free the list when you are done with it. (You can use g_resolver_free_addresses() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • lookupByNameWithFlagsAsync

      public void lookupByNameWithFlagsAsync(String hostname, Set<ResolverNameLookupFlags> flags, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously resolving hostname to determine its associated IP address(es), and eventually calls callback, which must call g_resolver_lookup_by_name_with_flags_finish() to get the result. See g_resolver_lookup_by_name() for more details.
      Parameters:
      hostname - the hostname to look up the address of
      flags - extra GResolverNameLookupFlags for the lookup
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.60
    • lookupByNameWithFlagsAsync

      public void lookupByNameWithFlagsAsync(String hostname, ResolverNameLookupFlags flags, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously resolving hostname to determine its associated IP address(es), and eventually calls callback, which must call g_resolver_lookup_by_name_with_flags_finish() to get the result. See g_resolver_lookup_by_name() for more details.
      Parameters:
      hostname - the hostname to look up the address of
      flags - extra GResolverNameLookupFlags for the lookup
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.60
    • lookupByNameWithFlagsFinish

      public List<InetAddress> lookupByNameWithFlagsFinish(AsyncResult result) throws GErrorException

      Retrieves the result of a call to g_resolver_lookup_by_name_with_flags_async().

      If the DNS resolution failed, error (if non-null) will be set to a value from GResolverError. If the operation was cancelled, error will be set to IOErrorEnum.CANCELLED.

      Parameters:
      result - the result passed to your GAsyncReadyCallback
      Returns:
      a GList of GInetAddress, or null on error. See g_resolver_lookup_by_name() for more details.
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • lookupRecords

      public List<Variant> lookupRecords(String rrname, ResolverRecordType recordType, @Nullable Cancellable cancellable) throws GErrorException

      Synchronously performs a DNS record lookup for the given rrname and returns a list of records as GVariant tuples. See GResolverRecordType for information on what the records contain for each recordType.

      If the DNS resolution fails, error (if non-null) will be set to a value from GResolverError and null will be returned.

      If cancellable is non-null, it can be used to cancel the operation, in which case error (if non-null) will be set to IOErrorEnum.CANCELLED.

      Parameters:
      rrname - the DNS name to look up the record for
      recordType - the type of DNS record to look up
      cancellable - a GCancellable, or null
      Returns:
      a non-empty GList of GVariant, or null on error. You must free each of the records and the list when you are done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.34
    • lookupRecordsAsync

      public void lookupRecordsAsync(String rrname, ResolverRecordType recordType, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously performing a DNS lookup for the given rrname, and eventually calls callback, which must call g_resolver_lookup_records_finish() to get the final result. See g_resolver_lookup_records() for more details.
      Parameters:
      rrname - the DNS name to look up the record for
      recordType - the type of DNS record to look up
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.34
    • lookupRecordsFinish

      public List<Variant> lookupRecordsFinish(AsyncResult result) throws GErrorException

      Retrieves the result of a previous call to g_resolver_lookup_records_async(). Returns a non-empty list of records as GVariant tuples. See GResolverRecordType for information on what the records contain.

      If the DNS resolution failed, error (if non-null) will be set to a value from GResolverError. If the operation was cancelled, error will be set to IOErrorEnum.CANCELLED.

      Parameters:
      result - the result passed to your GAsyncReadyCallback
      Returns:
      a non-empty GList of GVariant, or null on error. You must free each of the records and the list when you are done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.34
    • lookupService

      public List<SrvTarget> lookupService(String service, String protocol, String domain, @Nullable Cancellable cancellable) throws GErrorException

      Synchronously performs a DNS SRV lookup for the given service and protocol in the given domain and returns an array of GSrvTarget. domain may be an ASCII-only or UTF-8 hostname. Note also that the service and protocol arguments do not include the leading underscore that appears in the actual DNS entry.

      On success, g_resolver_lookup_service() will return a non-empty GList of GSrvTarget, sorted in order of preference. (That is, you should attempt to connect to the first target first, then the second if the first fails, etc.)

      If the DNS resolution fails, error (if non-null) will be set to a value from GResolverError and null will be returned.

      If cancellable is non-null, it can be used to cancel the operation, in which case error (if non-null) will be set to IOErrorEnum.CANCELLED.

      If you are planning to connect to the service, it is usually easier to create a GNetworkService and use its GSocketConnectable interface.

      Parameters:
      service - the service type to look up (eg, "ldap")
      protocol - the networking protocol to use for service (eg, "tcp")
      domain - the DNS domain to look up the service in
      cancellable - a GCancellable, or null
      Returns:
      a non-empty GList of GSrvTarget, or null on error. You must free each of the targets and the list when you are done with it. (You can use g_resolver_free_targets() to do this.)
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • lookupServiceAsync

      public void lookupServiceAsync(String service, String protocol, String domain, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Begins asynchronously performing a DNS SRV lookup for the given service and protocol in the given domain, and eventually calls callback, which must call g_resolver_lookup_service_finish() to get the final result. See g_resolver_lookup_service() for more details.
      Parameters:
      service - the service type to look up (eg, "ldap")
      protocol - the networking protocol to use for service (eg, "tcp")
      domain - the DNS domain to look up the service in
      cancellable - a GCancellable, or null
      callback - callback to call after resolution completes
      Since:
      2.22
    • lookupServiceFinish

      public List<SrvTarget> lookupServiceFinish(AsyncResult result) throws GErrorException

      Retrieves the result of a previous call to g_resolver_lookup_service_async().

      If the DNS resolution failed, error (if non-null) will be set to a value from GResolverError. If the operation was cancelled, error will be set to IOErrorEnum.CANCELLED.

      Parameters:
      result - the result passed to your GAsyncReadyCallback
      Returns:
      a non-empty GList of GSrvTarget, or null on error. See g_resolver_lookup_service() for more details.
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • setDefault

      public void setDefault()

      Sets this Resolver to be the application's default resolver (reffing resolver, and unreffing the previous default resolver, if any). Future calls to g_resolver_get_default() will return this resolver.

      This can be used if an application wants to perform any sort of DNS caching or "pinning"; it can implement its own GResolver that calls the original default resolver for DNS operations, and implements its own cache policies on top of that, and then set itself as the default resolver for all later code to use.

      Since:
      2.22
    • setTimeout

      public void setTimeout(int timeoutMs)
      Set the timeout applied to all resolver lookups. See GResolver:timeout.
      Parameters:
      timeoutMs - timeout in milliseconds, or 0 for no timeouts
      Since:
      2.78
    • lookupService

      protected List<MemorySegment> lookupService(String rrname, @Nullable Cancellable cancellable) throws GErrorException
      Throws:
      GErrorException
    • lookupServiceAsync

      protected void lookupServiceAsync(String rrname, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
    • reload

      protected void reload()
    • onReload

      Emitted when the resolver notices that the system resolver configuration has changed.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitReload

      public void emitReload()
      Emits the "reload" signal. See onReload(Resolver.ReloadCallback).