The object that handles DNS resolution.
Use [func@Gio.Resolver.get_default] to get the default resolver.
`GResolver` provides cancellable synchronous and asynchronous DNS resolution, for hostnames ([method@Gio.Resolver.lookup_by_address], [
method@Gio.Resolver.lookup_by_name] and their async variants) and SRV (service) records ([method@Gio.Resolver.lookup_service]).
[class@Gio.NetworkAddress] and [class@Gio.NetworkService] provide wrappers around `GResolver` functionality that also implement [
iface@Gio.SocketConnectable], making it easy to connect to a remote host/service.
The default resolver (see [func@Gio.Resolver.get_default]) 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.
Example: Resolver, lookup by name, sync:
public static int main () {
// Resolve hostname to IP address
try {
Resolver resolver = Resolver.get_default ();
List<InetAddress> addresses = resolver.lookup_by_name ("www.google.com", null);
// Example output:
// ``173.194.35.179``
// ``173.194.35.178``
// ``173.194.35.177``
// ``173.194.35.176``
// ``173.194.35.180``
// ``2a00:1450:4016:801::1013``
foreach (InetAddress address in addresses) {
print ("%s\n", address.to_string ());
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.Resolver.lookup_by_name.vala
Example: Resolver, lookup by name, async:
public static int main () {
MainLoop loop = new MainLoop ();
Resolver resolver = Resolver.get_default ();
resolver.lookup_by_name_async.begin ("www.google.com", null, (obj, res) => {
try {
// Example output:
// ``173.194.35.179``
// ``173.194.35.178``
// ``173.194.35.177``
// ``173.194.35.176``
// ``173.194.35.180``
// ``2a00:1450:4016:801::1013``
List<InetAddress> addresses = resolver.lookup_by_name_async.end (res);
foreach (InetAddress address in addresses) {
print ("%s\n", address.to_string ());
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
loop.quit ();
});
// Block until loop.quit is called:
loop.run ();
return 0;
}
valac --pkg gio-2.0 GLib.Resolver.lookup_by_name_async.vala
- public uint get_timeout ()
Get the timeout applied to all resolver lookups.
- public virtual string lookup_by_address (InetAddress address, Cancellable? cancellable = null) throws Error
Synchronously reverse-resolves address
to determine its
associated hostname.
- public virtual async string lookup_by_address_async (InetAddress address, Cancellable? cancellable = null) throws Error
Begins asynchronously reverse-resolving address
to determine
its associated hostname, and eventually calls callback
, which must call
lookup_by_address_async.end to get the final result.
- public virtual List<InetAddress> lookup_by_name (string hostname, Cancellable? cancellable = null) throws Error
Synchronously resolves hostname
to determine its associated IP
address(es).
- public virtual async List<InetAddress> lookup_by_name_async (string hostname, Cancellable? cancellable = null) throws Error
Begins asynchronously resolving hostname
to determine its
associated IP address(es), and eventually calls callback
, which must call lookup_by_name_async.end
to get the result.
- public virtual List<InetAddress> lookup_by_name_with_flags (string hostname, ResolverNameLookupFlags flags, Cancellable? cancellable = null) throws Error
This differs from
lookup_by_name in that you can modify the lookup behavior
with flags
.
- public virtual async List<InetAddress> lookup_by_name_with_flags_async (string hostname, ResolverNameLookupFlags flags, Cancellable? cancellable = null) throws Error
Begins asynchronously resolving hostname
to determine its
associated IP address(es), and eventually calls callback
, which must call
lookup_by_name_with_flags_async.end to get the result.
- public virtual List<Variant> lookup_records (string rrname, ResolverRecordType record_type, Cancellable? cancellable = null) throws Error
Synchronously performs a DNS record lookup for the given rrname
and returns a list of records as Variant tuples.
- public virtual async List<Variant> lookup_records_async (string rrname, ResolverRecordType record_type, Cancellable? cancellable = null) throws Error
Begins asynchronously performing a DNS lookup for the given rrname
, and eventually calls callback
, which must call lookup_records_async.end to get the
final result.
- public List<SrvTarget> lookup_service (string service, string protocol, string domain, Cancellable? cancellable = null) throws Error
Synchronously performs a DNS SRV lookup for the given service
and protocol
in the given domain
and returns an array of
SrvTarget.
- public async List<SrvTarget> lookup_service_async (string service, string protocol, string domain, Cancellable? cancellable = null) throws Error
Begins asynchronously performing a DNS SRV lookup for the given
service
and protocol
in the given domain
, and eventually calls callback
, which must call
lookup_service_async.end to get the final result.
- public virtual List<SrvTarget> lookup_service_fn (string rrname, Cancellable? cancellable = null) throws Error
- public virtual async List<SrvTarget> lookup_service_fn_async (string rrname, Cancellable? cancellable = null) throws Error
- public void set_default ()
Sets this to be the application's default
resolver (reffing this, and unreffing the previous default resolver, if any).
- public void set_timeout (uint timeout_ms)
Set the timeout applied to all resolver lookups.