since: 2.22


g_resolver_lookup_by_name (
  GResolver* resolver,
  const gchar* hostname,
  GCancellable* cancellable,
  GError** error


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 G_IO_ERROR_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.

Available since: 2.22



Type: const gchar*

The hostname to look up.

The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.

Type: GCancellable

A GCancellable, or NULL.

The argument can be NULL.
The data is owned by the caller of the function.

Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will left initialized to NULL by the method if there are no errors.
In case of error, the argument will be set to a newly allocated GError; the caller will take ownership of the data, and be responsible for freeing it.

Return value

Type: A list of GInetAddress*

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.)

The caller of the method takes ownership of the data, and is responsible for freeing it.