gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gio / gresolver.c
index 6a735e8..4c29d58 100644 (file)
  * #GNetworkAddress and #GNetworkService provide wrappers around
  * #GResolver functionality that also implement #GSocketConnectable,
  * making it easy to connect to a remote host/service.
+ *
+ * The default resolver (see g_resolver_get_default()) has a timeout of 30s set
+ * on it since GLib 2.78. Earlier versions of GLib did not support resolver
+ * timeouts.
  */
 
+typedef enum {
+  PROP_TIMEOUT = 1,
+} GResolverProperty;
+
+static GParamSpec *props[PROP_TIMEOUT + 1] = { NULL, };
+
 enum {
   RELOAD,
   LAST_SIGNAL
@@ -65,11 +75,11 @@ enum {
 static guint signals[LAST_SIGNAL] = { 0 };
 
 struct _GResolverPrivate {
+  unsigned timeout_ms;
+
 #ifdef G_OS_UNIX
   GMutex mutex;
   time_t resolv_conf_timestamp;  /* protected by @mutex */
-#else
-  int dummy;
 #endif
 };
 
@@ -152,6 +162,42 @@ g_resolver_real_lookup_service_finish (GResolver            *resolver,
 }
 
 static void
+g_resolver_get_property (GObject    *object,
+                         guint       prop_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
+{
+  GResolver *self = G_RESOLVER (object);
+
+  switch ((GResolverProperty) prop_id)
+    {
+    case PROP_TIMEOUT:
+      g_value_set_uint (value, g_resolver_get_timeout (self));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_resolver_set_property (GObject      *object,
+                         guint         prop_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+  GResolver *self = G_RESOLVER (object);
+
+  switch ((GResolverProperty) prop_id)
+    {
+    case PROP_TIMEOUT:
+      g_resolver_set_timeout (self, g_value_get_uint (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
 g_resolver_finalize (GObject *object)
 {
 #ifdef G_OS_UNIX
@@ -168,6 +214,8 @@ g_resolver_class_init (GResolverClass *resolver_class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (resolver_class);
 
+  object_class->get_property = g_resolver_get_property;
+  object_class->set_property = g_resolver_set_property;
   object_class->finalize = g_resolver_finalize;
 
   /* Automatically pass these over to the lookup_records methods */
@@ -176,6 +224,31 @@ g_resolver_class_init (GResolverClass *resolver_class)
   resolver_class->lookup_service_finish = g_resolver_real_lookup_service_finish;
 
   /**
+   * GResolver:timeout:
+   *
+   * The timeout applied to all resolver lookups, in milliseconds.
+   *
+   * This may be changed through the lifetime of the #GResolver. The new value
+   * will apply to any lookups started after the change, but not to any
+   * already-ongoing lookups.
+   *
+   * If this is `0`, no timeout is applied to lookups.
+   *
+   * No timeout was applied to lookups before this property was added in
+   * GLib 2.78.
+   *
+   * Since: 2.78
+   */
+  props[PROP_TIMEOUT] =
+    g_param_spec_uint ("timeout",
+                       P_("Timeout"),
+                       P_("Timeout (ms) applied to all resolver lookups"),
+                       0, G_MAXUINT, 0,
+                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+  g_object_class_install_properties (object_class, G_N_ELEMENTS (props), props);
+
+  /**
    * GResolver::reload:
    * @resolver: a #GResolver
    *
@@ -230,7 +303,9 @@ g_resolver_get_default (void)
 
   G_LOCK (default_resolver);
   if (!default_resolver)
-    default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
+    default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER,
+                                     "timeout", 30000,
+                                     NULL);
   ret = g_object_ref (default_resolver);
   G_UNLOCK (default_resolver);
 
@@ -688,8 +763,8 @@ lookup_by_name_finish_real (GResolver     *resolver,
  * @hostname: the hostname to look up the address of
  * @flags: extra #GResolverNameLookupFlags for the lookup
  * @cancellable: (nullable): a #GCancellable, or %NULL
- * @callback: (scope async): callback to call after resolution completes
- * @user_data: (closure): data for @callback
+ * @callback: (scope async) (closure user_data): callback to call after resolution completes
+ * @user_data: data for @callback
  *
  * Begins asynchronously resolving @hostname to determine its
  * associated IP address(es), and eventually calls @callback, which
@@ -719,8 +794,8 @@ g_resolver_lookup_by_name_with_flags_async (GResolver                *resolver,
  * @resolver: a #GResolver
  * @hostname: the hostname to look up the address of
  * @cancellable: (nullable): a #GCancellable, or %NULL
- * @callback: (scope async): callback to call after resolution completes
- * @user_data: (closure): data for @callback
+ * @callback: (scope async) (closure user_data): callback to call after resolution completes
+ * @user_data: data for @callback
  *
  * Begins asynchronously resolving @hostname to determine its
  * associated IP address(es), and eventually calls @callback, which
@@ -866,8 +941,8 @@ g_resolver_lookup_by_address (GResolver     *resolver,
  * @resolver: a #GResolver
  * @address: the address to reverse-resolve
  * @cancellable: (nullable): a #GCancellable, or %NULL
- * @callback: (scope async): callback to call after resolution completes
- * @user_data: (closure): data for @callback
+ * @callback: (scope async) (closure user_data): callback to call after resolution completes
+ * @user_data: data for @callback
  *
  * Begins asynchronously reverse-resolving @address to determine its
  * associated hostname, and eventually calls @callback, which must
@@ -1017,8 +1092,8 @@ g_resolver_lookup_service (GResolver     *resolver,
  * @protocol: the networking protocol to use for @service (eg, "tcp")
  * @domain: the DNS domain to look up the service in
  * @cancellable: (nullable): a #GCancellable, or %NULL
- * @callback: (scope async): callback to call after resolution completes
- * @user_data: (closure): data for @callback
+ * @callback: (scope async) (closure user_data): callback to call after resolution completes
+ * @user_data: data for @callback
  *
  * Begins asynchronously performing a DNS SRV lookup for the given
  * @service and @protocol in the given @domain, and eventually calls
@@ -1166,8 +1241,8 @@ g_resolver_lookup_records (GResolver            *resolver,
  * @rrname: the DNS name to look up the record for
  * @record_type: the type of DNS record to look up
  * @cancellable: (nullable): a #GCancellable, or %NULL
- * @callback: (scope async): callback to call after resolution completes
- * @user_data: (closure): data for @callback
+ * @callback: (scope async) (closure user_data): callback to call after resolution completes
+ * @user_data: data for @callback
  *
  * Begins asynchronously performing a DNS lookup for the given
  * @rrname, and eventually calls @callback, which must call
@@ -1245,6 +1320,50 @@ g_resolver_get_serial (GResolver *resolver)
 }
 
 /**
+ * g_resolver_get_timeout:
+ * @resolver: a #GResolver
+ *
+ * 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
+ */
+unsigned
+g_resolver_get_timeout (GResolver *resolver)
+{
+  GResolverPrivate *priv = g_resolver_get_instance_private (resolver);
+
+  g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
+
+  return priv->timeout_ms;
+}
+
+/**
+ * g_resolver_set_timeout:
+ * @resolver: a #GResolver
+ * @timeout_ms: timeout in milliseconds, or `0` for no timeouts
+ *
+ * Set the timeout applied to all resolver lookups. See #GResolver:timeout.
+ *
+ * Since: 2.78
+ */
+void
+g_resolver_set_timeout (GResolver *resolver,
+                        unsigned   timeout_ms)
+{
+  GResolverPrivate *priv = g_resolver_get_instance_private (resolver);
+
+  g_return_if_fail (G_IS_RESOLVER (resolver));
+
+  if (priv->timeout_ms == timeout_ms)
+    return;
+
+  priv->timeout_ms = timeout_ms;
+  g_object_notify_by_pspec (G_OBJECT (resolver), props[PROP_TIMEOUT]);
+}
+
+/**
  * g_resolver_error_quark:
  *
  * Gets the #GResolver Error Quark.