#include "ginetsocketaddress.h"
#include "gsimpleasyncresult.h"
#include "gsrvtarget.h"
+#include "gthreadedresolver.h"
#ifdef G_OS_UNIX
-#include "gunixresolver.h"
-#endif
-#ifdef G_OS_WIN32
-#include "gwin32resolver.h"
+#include <sys/stat.h>
#endif
#include <stdlib.h>
-#include "gioalias.h"
/**
* SECTION:gresolver
* making it easy to connect to a remote host/service.
*/
+enum {
+ RELOAD,
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+struct _GResolverPrivate {
+#ifdef G_OS_UNIX
+ time_t resolv_conf_timestamp;
+#else
+ int dummy;
+#endif
+};
+
/**
* GResolver:
*
{
volatile GType type;
+ g_type_class_add_private (resolver_class, sizeof (GResolverPrivate));
+
/* Make sure _g_networking_init() has been called */
type = g_inet_address_get_type ();
+ (type); /* To avoid -Wunused-but-set-variable */
/* Initialize _g_resolver_addrinfo_hints */
#ifdef AI_ADDRCONFIG
*/
_g_resolver_addrinfo_hints.ai_socktype = SOCK_STREAM;
_g_resolver_addrinfo_hints.ai_protocol = IPPROTO_TCP;
+
+ /**
+ * GResolver::reload:
+ * @resolver: a #GResolver
+ *
+ * Emitted when the resolver notices that the system resolver
+ * configuration has changed.
+ **/
+ signals[RELOAD] =
+ g_signal_new (I_("reload"),
+ G_TYPE_RESOLVER,
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GResolverClass, reload),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
}
static void
g_resolver_init (GResolver *resolver)
{
+#ifdef G_OS_UNIX
+ struct stat st;
+#endif
+
+ resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver, G_TYPE_RESOLVER, GResolverPrivate);
+
+#ifdef G_OS_UNIX
+ if (stat (_PATH_RESCONF, &st) == 0)
+ resolver->priv->resolv_conf_timestamp = st.st_mtime;
+#endif
}
static GResolver *default_resolver;
*
* 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/processes, etc it should allocate for concurrent DNS
- * resolutions.
+ * many threads it should allocate for concurrent DNS resolutions.
*
- * Return value: the default #GResolver.
+ * Return value: (transfer full): the default #GResolver.
*
* Since: 2.22
*/
g_resolver_get_default (void)
{
if (!default_resolver)
- {
- if (g_thread_supported ())
- default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
- else
- {
-#if defined(G_OS_UNIX)
- default_resolver = g_object_new (G_TYPE_UNIX_RESOLVER, NULL);
-#elif defined(G_OS_WIN32)
- default_resolver = g_object_new (G_TYPE_WIN32_RESOLVER, NULL);
-#endif
- }
- }
+ default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
return g_object_ref (default_resolver);
}
}
+static void
+g_resolver_maybe_reload (GResolver *resolver)
+{
+#ifdef G_OS_UNIX
+ struct stat st;
+
+ if (stat (_PATH_RESCONF, &st) == 0)
+ {
+ if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
+ {
+ resolver->priv->resolv_conf_timestamp = st.st_mtime;
+ res_init ();
+ g_signal_emit (resolver, signals[RELOAD], 0);
+ }
+ }
+#endif
+}
+
+/* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
+static void
+remove_duplicates (GList *addrs)
+{
+ GList *l;
+ GList *ll;
+ GList *lll;
+
+ /* TODO: if this is too slow (it's O(n^2) but n is typically really
+ * small), we can do something more clever but note that we must not
+ * change the order of elements...
+ */
+ for (l = addrs; l != NULL; l = l->next)
+ {
+ GInetAddress *address = G_INET_ADDRESS (l->data);
+ for (ll = l->next; ll != NULL; ll = lll)
+ {
+ GInetAddress *other_address = G_INET_ADDRESS (ll->data);
+ lll = ll->next;
+ if (g_inet_address_equal (address, other_address))
+ {
+ g_object_unref (other_address);
+ /* we never return the first element */
+ g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
+ }
+ }
+ }
+}
+
+
/**
* g_resolver_lookup_by_name:
* @resolver: a #GResolver
* @hostname: the hostname to look up
- * @cancellable: a #GCancellable, or %NULL
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: return location for a #GError, or %NULL
*
* Synchronously resolves @hostname to determine its associated IP
* a wrapper around g_inet_address_new_from_string()).
*
* On success, g_resolver_lookup_by_name() will return a #GList of
- * #GInetAddress, sorted in order of preference. (That is, you should
- * attempt to connect to the first address first, then the second if
- * the first fails, etc.)
+ * #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.
* address, it may be easier to create a #GNetworkAddress and use its
* #GSocketConnectable interface.
*
- * Return value: a #GList of #GInetAddress, or %NULL on error. You
+ * Return value: (element-type GInetAddress) (transfer full): a #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.)
*
if (g_hostname_is_non_ascii (hostname))
hostname = ascii_hostname = g_hostname_to_ascii (hostname);
+ g_resolver_maybe_reload (resolver);
addrs = G_RESOLVER_GET_CLASS (resolver)->
lookup_by_name (resolver, hostname, cancellable, error);
+ remove_duplicates (addrs);
+
g_free (ascii_hostname);
return addrs;
}
* g_resolver_lookup_by_name_async:
* @resolver: a #GResolver
* @hostname: the hostname to look up the address of
- * @cancellable: a #GCancellable, or %NULL
- * @callback: callback to call after resolution completes
- * @user_data: data for @callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): callback to call after resolution completes
+ * @user_data: (closure): data for @callback
*
* Begins asynchronously resolving @hostname to determine its
* associated IP address(es), and eventually calls @callback, which
if (addr)
{
GSimpleAsyncResult *simple;
- GList *addrs;
simple = g_simple_async_result_new (G_OBJECT (resolver),
callback, user_data,
g_resolver_lookup_by_name_async);
- addrs = g_list_append (NULL, addr);
- g_simple_async_result_set_op_res_gpointer (simple, addrs, (GDestroyNotify)g_resolver_free_addresses);
+ g_simple_async_result_set_op_res_gpointer (simple, addr, g_object_unref);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
return;
if (g_hostname_is_non_ascii (hostname))
hostname = ascii_hostname = g_hostname_to_ascii (hostname);
+ g_resolver_maybe_reload (resolver);
G_RESOLVER_GET_CLASS (resolver)->
lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
* a value from #GResolverError. If the operation was cancelled,
* @error will be set to %G_IO_ERROR_CANCELLED.
*
- * Return value: a #GList of #GInetAddress, or %NULL on error. See
- * g_resolver_lookup_by_name() for more details.
+ * Return value: (element-type GInetAddress) (transfer full): a #GList
+ * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
+ * for more details.
*
* Since: 2.22
*/
GAsyncResult *result,
GError **error)
{
+ GList *addrs;
+
g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
if (G_IS_SIMPLE_ASYNC_RESULT (result))
/* Handle the stringified-IP-addr case */
if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
{
- GList *addrs;
+ GInetAddress *addr;
- addrs = g_simple_async_result_get_op_res_gpointer (simple);
- g_simple_async_result_set_op_res_gpointer (simple, NULL, NULL);
- return addrs;
+ addr = g_simple_async_result_get_op_res_gpointer (simple);
+ return g_list_append (NULL, g_object_ref (addr));
}
}
- return G_RESOLVER_GET_CLASS (resolver)->
+ addrs = G_RESOLVER_GET_CLASS (resolver)->
lookup_by_name_finish (resolver, result, error);
+
+ remove_duplicates (addrs);
+
+ return addrs;
}
/**
- * g_resolver_free_addresses:
+ * g_resolver_free_addresses: (skip)
* @addresses: a #GList of #GInetAddress
*
* Frees @addresses (which should be the return value from
* g_resolver_lookup_by_address:
* @resolver: a #GResolver
* @address: the address to reverse-resolve
- * @cancellable: a #GCancellable, or %NULL
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: return location for a #GError, or %NULL
*
* Synchronously reverse-resolves @address to determine its
g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
+ g_resolver_maybe_reload (resolver);
return G_RESOLVER_GET_CLASS (resolver)->
lookup_by_address (resolver, address, cancellable, error);
}
* g_resolver_lookup_by_address_async:
* @resolver: a #GResolver
* @address: the address to reverse-resolve
- * @cancellable: a #GCancellable, or %NULL
- * @callback: callback to call after resolution completes
- * @user_data: data for @callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): callback to call after resolution completes
+ * @user_data: (closure): data for @callback
*
* Begins asynchronously reverse-resolving @address to determine its
* associated hostname, and eventually calls @callback, which must
g_return_if_fail (G_IS_RESOLVER (resolver));
g_return_if_fail (G_IS_INET_ADDRESS (address));
+ g_resolver_maybe_reload (resolver);
G_RESOLVER_GET_CLASS (resolver)->
lookup_by_address_async (resolver, address, cancellable, callback, user_data);
}
* @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
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: return location for a #GError, or %NULL
*
* Synchronously performs a DNS SRV lookup for the given @service and
* to create a #GNetworkService and use its #GSocketConnectable
* interface.
*
- * Return value: a #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.)
+ * Return value: (element-type GSrvTarget) (transfer full): a #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.)
*
* Since: 2.22
*/
rrname = g_resolver_get_service_rrname (service, protocol, domain);
+ g_resolver_maybe_reload (resolver);
targets = G_RESOLVER_GET_CLASS (resolver)->
lookup_service (resolver, rrname, cancellable, error);
* @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
- * @user_data: data for @callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): callback to call after resolution completes
+ * @user_data: (closure): data for @callback
*
* Begins asynchronously performing a DNS SRV lookup for the given
* @service and @protocol in the given @domain, and eventually calls
rrname = g_resolver_get_service_rrname (service, protocol, domain);
+ g_resolver_maybe_reload (resolver);
G_RESOLVER_GET_CLASS (resolver)->
lookup_service_async (resolver, rrname, cancellable, callback, user_data);
* a value from #GResolverError. If the operation was cancelled,
* @error will be set to %G_IO_ERROR_CANCELLED.
*
- * Return value: a #GList of #GSrvTarget, or %NULL on error. See
- * g_resolver_lookup_service() for more details.
+ * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
+ * or %NULL on error. See g_resolver_lookup_service() for more details.
*
* Since: 2.22
*/
}
/**
- * g_resolver_free_targets:
+ * g_resolver_free_targets: (skip)
* @targets: a #GList of #GSrvTarget
*
* Frees @targets (which should be the return value from
void
_g_resolver_address_to_sockaddr (GInetAddress *address,
struct sockaddr_storage *sa,
- gsize *sa_len)
+ gsize *len)
{
GSocketAddress *sockaddr;
sockaddr = g_inet_socket_address_new (address, 0);
g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
- *sa_len = g_socket_address_get_native_size (sockaddr);
+ *len = g_socket_address_get_native_size (sockaddr);
g_object_unref (sockaddr);
}
GETSHORT (type, p);
GETSHORT (qclass, p);
GETLONG (ttl, p);
+ ttl = ttl; /* To avoid -Wunused-but-set-variable */
GETSHORT (rdlength, p);
if (type != T_SRV || qclass != C_IN)
}
#endif
-
-#define __G_RESOLVER_C__
-#include "gioaliasdef.c"