docs: Small improvements to glib-mkenums man page
[glib.git] / gio / gnetworkmonitorbase.c
blobd42f6d4ab9c3f9e1a250c76fb3e39f4b70ddadfd
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include "gnetworkmonitorbase.h"
22 #include "ginetaddress.h"
23 #include "ginetaddressmask.h"
24 #include "ginetsocketaddress.h"
25 #include "ginitable.h"
26 #include "gioerror.h"
27 #include "giomodule-priv.h"
28 #include "gnetworkmonitor.h"
29 #include "gsocketaddressenumerator.h"
30 #include "gsocketconnectable.h"
31 #include "gtask.h"
32 #include "glibintl.h"
34 static void g_network_monitor_base_iface_init (GNetworkMonitorInterface *iface);
35 static void g_network_monitor_base_initable_iface_init (GInitableIface *iface);
37 enum
39 PROP_0,
41 PROP_NETWORK_AVAILABLE,
42 PROP_NETWORK_METERED,
43 PROP_CONNECTIVITY
46 struct _GNetworkMonitorBasePrivate
48 GPtrArray *networks;
49 gboolean have_ipv4_default_route;
50 gboolean have_ipv6_default_route;
51 gboolean is_available;
53 GMainContext *context;
54 GSource *network_changed_source;
55 gboolean initializing;
58 static guint network_changed_signal = 0;
60 static void queue_network_changed (GNetworkMonitorBase *monitor);
62 G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorBase, g_network_monitor_base, G_TYPE_OBJECT,
63 G_ADD_PRIVATE (GNetworkMonitorBase)
64 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
65 g_network_monitor_base_initable_iface_init)
66 G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR,
67 g_network_monitor_base_iface_init)
68 _g_io_modules_ensure_extension_points_registered ();
69 g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
70 g_define_type_id,
71 "base",
72 0))
74 static void
75 g_network_monitor_base_init (GNetworkMonitorBase *monitor)
77 monitor->priv = g_network_monitor_base_get_instance_private (monitor);
78 monitor->priv->networks = g_ptr_array_new_with_free_func (g_object_unref);
79 monitor->priv->context = g_main_context_get_thread_default ();
80 if (monitor->priv->context)
81 g_main_context_ref (monitor->priv->context);
83 monitor->priv->initializing = TRUE;
84 queue_network_changed (monitor);
87 static void
88 g_network_monitor_base_constructed (GObject *object)
90 GNetworkMonitorBase *monitor = G_NETWORK_MONITOR_BASE (object);
92 if (G_OBJECT_TYPE (monitor) == G_TYPE_NETWORK_MONITOR_BASE)
94 GInetAddressMask *mask;
96 /* We're the dumb base class, not a smarter subclass. So just
97 * assume that the network is available.
99 mask = g_inet_address_mask_new_from_string ("0.0.0.0/0", NULL);
100 g_network_monitor_base_add_network (monitor, mask);
101 g_object_unref (mask);
103 mask = g_inet_address_mask_new_from_string ("::/0", NULL);
104 g_network_monitor_base_add_network (monitor, mask);
105 g_object_unref (mask);
109 static void
110 g_network_monitor_base_get_property (GObject *object,
111 guint prop_id,
112 GValue *value,
113 GParamSpec *pspec)
115 GNetworkMonitorBase *monitor = G_NETWORK_MONITOR_BASE (object);
117 switch (prop_id)
119 case PROP_NETWORK_AVAILABLE:
120 g_value_set_boolean (value, monitor->priv->is_available);
121 break;
123 case PROP_NETWORK_METERED:
124 /* Default to FALSE in the unknown case. */
125 g_value_set_boolean (value, FALSE);
126 break;
128 case PROP_CONNECTIVITY:
129 g_value_set_enum (value,
130 monitor->priv->is_available ?
131 G_NETWORK_CONNECTIVITY_FULL :
132 G_NETWORK_CONNECTIVITY_LOCAL);
133 break;
135 default:
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 break;
142 static void
143 g_network_monitor_base_finalize (GObject *object)
145 GNetworkMonitorBase *monitor = G_NETWORK_MONITOR_BASE (object);
147 g_ptr_array_free (monitor->priv->networks, TRUE);
148 if (monitor->priv->network_changed_source)
150 g_source_destroy (monitor->priv->network_changed_source);
151 g_source_unref (monitor->priv->network_changed_source);
153 if (monitor->priv->context)
154 g_main_context_unref (monitor->priv->context);
156 G_OBJECT_CLASS (g_network_monitor_base_parent_class)->finalize (object);
159 static void
160 g_network_monitor_base_class_init (GNetworkMonitorBaseClass *monitor_class)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (monitor_class);
164 gobject_class->constructed = g_network_monitor_base_constructed;
165 gobject_class->get_property = g_network_monitor_base_get_property;
166 gobject_class->finalize = g_network_monitor_base_finalize;
168 g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available");
169 g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered");
170 g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity");
173 static gboolean
174 g_network_monitor_base_can_reach_sockaddr (GNetworkMonitorBase *base,
175 GSocketAddress *sockaddr)
177 GInetAddress *iaddr;
178 int i;
180 if (!G_IS_INET_SOCKET_ADDRESS (sockaddr))
181 return FALSE;
183 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sockaddr));
184 for (i = 0; i < base->priv->networks->len; i++)
186 if (g_inet_address_mask_matches (base->priv->networks->pdata[i], iaddr))
187 return TRUE;
190 return FALSE;
193 static gboolean
194 g_network_monitor_base_can_reach (GNetworkMonitor *monitor,
195 GSocketConnectable *connectable,
196 GCancellable *cancellable,
197 GError **error)
199 GNetworkMonitorBase *base = G_NETWORK_MONITOR_BASE (monitor);
200 GSocketAddressEnumerator *enumerator;
201 GSocketAddress *addr;
203 if (base->priv->networks->len == 0)
205 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NETWORK_UNREACHABLE,
206 _("Network unreachable"));
207 return FALSE;
210 enumerator = g_socket_connectable_proxy_enumerate (connectable);
211 addr = g_socket_address_enumerator_next (enumerator, cancellable, error);
212 if (!addr)
214 /* Either the user cancelled, or DNS resolution failed */
215 g_object_unref (enumerator);
216 return FALSE;
219 if (base->priv->have_ipv4_default_route &&
220 base->priv->have_ipv6_default_route)
222 g_object_unref (enumerator);
223 g_object_unref (addr);
224 return TRUE;
227 while (addr)
229 if (g_network_monitor_base_can_reach_sockaddr (base, addr))
231 g_object_unref (addr);
232 g_object_unref (enumerator);
233 return TRUE;
236 g_object_unref (addr);
237 addr = g_socket_address_enumerator_next (enumerator, cancellable, error);
239 g_object_unref (enumerator);
241 if (error && !*error)
243 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE,
244 _("Host unreachable"));
246 return FALSE;
249 static void
250 can_reach_async_got_address (GObject *object,
251 GAsyncResult *result,
252 gpointer user_data)
254 GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (object);
255 GTask *task = user_data;
256 GNetworkMonitorBase *base = g_task_get_source_object (task);
257 GSocketAddress *addr;
258 GError *error = NULL;
260 addr = g_socket_address_enumerator_next_finish (enumerator, result, &error);
261 if (!addr)
263 if (error)
265 /* Either the user cancelled, or DNS resolution failed */
266 g_task_return_error (task, error);
267 g_object_unref (task);
268 return;
270 else
272 /* Resolved all addresses, none matched */
273 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE,
274 _("Host unreachable"));
275 g_object_unref (task);
276 return;
280 if (g_network_monitor_base_can_reach_sockaddr (base, addr))
282 g_object_unref (addr);
283 g_task_return_boolean (task, TRUE);
284 g_object_unref (task);
285 return;
287 g_object_unref (addr);
289 g_socket_address_enumerator_next_async (enumerator,
290 g_task_get_cancellable (task),
291 can_reach_async_got_address, task);
294 static void
295 g_network_monitor_base_can_reach_async (GNetworkMonitor *monitor,
296 GSocketConnectable *connectable,
297 GCancellable *cancellable,
298 GAsyncReadyCallback callback,
299 gpointer user_data)
301 GTask *task;
302 GSocketAddressEnumerator *enumerator;
304 task = g_task_new (monitor, cancellable, callback, user_data);
305 g_task_set_source_tag (task, g_network_monitor_base_can_reach_async);
307 if (G_NETWORK_MONITOR_BASE (monitor)->priv->networks->len == 0)
309 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NETWORK_UNREACHABLE,
310 _("Network unreachable"));
311 g_object_unref (task);
312 return;
315 enumerator = g_socket_connectable_proxy_enumerate (connectable);
316 g_socket_address_enumerator_next_async (enumerator, cancellable,
317 can_reach_async_got_address, task);
318 g_object_unref (enumerator);
321 static gboolean
322 g_network_monitor_base_can_reach_finish (GNetworkMonitor *monitor,
323 GAsyncResult *result,
324 GError **error)
326 g_return_val_if_fail (g_task_is_valid (result, monitor), FALSE);
328 return g_task_propagate_boolean (G_TASK (result), error);
331 static void
332 g_network_monitor_base_iface_init (GNetworkMonitorInterface *monitor_iface)
334 monitor_iface->can_reach = g_network_monitor_base_can_reach;
335 monitor_iface->can_reach_async = g_network_monitor_base_can_reach_async;
336 monitor_iface->can_reach_finish = g_network_monitor_base_can_reach_finish;
338 network_changed_signal = g_signal_lookup ("network-changed", G_TYPE_NETWORK_MONITOR);
341 static gboolean
342 g_network_monitor_base_initable_init (GInitable *initable,
343 GCancellable *cancellable,
344 GError **error)
346 return TRUE;
349 static void
350 g_network_monitor_base_initable_iface_init (GInitableIface *iface)
352 iface->init = g_network_monitor_base_initable_init;
355 static gboolean
356 emit_network_changed (gpointer user_data)
358 GNetworkMonitorBase *monitor = user_data;
359 gboolean is_available;
361 g_object_ref (monitor);
363 if (monitor->priv->initializing)
364 monitor->priv->initializing = FALSE;
365 else
367 is_available = (monitor->priv->have_ipv4_default_route ||
368 monitor->priv->have_ipv6_default_route);
369 if (monitor->priv->is_available != is_available)
371 monitor->priv->is_available = is_available;
372 g_object_notify (G_OBJECT (monitor), "network-available");
375 g_signal_emit (monitor, network_changed_signal, 0, is_available);
378 g_source_unref (monitor->priv->network_changed_source);
379 monitor->priv->network_changed_source = NULL;
381 g_object_unref (monitor);
382 return FALSE;
385 static void
386 queue_network_changed (GNetworkMonitorBase *monitor)
388 if (!monitor->priv->network_changed_source)
390 GSource *source;
392 source = g_idle_source_new ();
393 /* Use G_PRIORITY_HIGH_IDLE priority so that multiple
394 * network-change-related notifications coming in at
395 * G_PRIORITY_DEFAULT will get coalesced into one signal
396 * emission.
398 g_source_set_priority (source, G_PRIORITY_HIGH_IDLE);
399 g_source_set_callback (source, emit_network_changed, monitor, NULL);
400 g_source_set_name (source, "[gio] emit_network_changed");
401 g_source_attach (source, monitor->priv->context);
402 monitor->priv->network_changed_source = source;
405 /* Normally we wait to update is_available until we emit the signal,
406 * to keep things consistent. But when we're first creating the
407 * object, we want it to be correct right away.
409 if (monitor->priv->initializing)
411 monitor->priv->is_available = (monitor->priv->have_ipv4_default_route ||
412 monitor->priv->have_ipv6_default_route);
417 * g_network_monitor_base_add_network:
418 * @monitor: the #GNetworkMonitorBase
419 * @network: a #GInetAddressMask
421 * Adds @network to @monitor's list of available networks.
423 * Since: 2.32
425 void
426 g_network_monitor_base_add_network (GNetworkMonitorBase *monitor,
427 GInetAddressMask *network)
429 int i;
431 for (i = 0; i < monitor->priv->networks->len; i++)
433 if (g_inet_address_mask_equal (monitor->priv->networks->pdata[i], network))
434 return;
437 g_ptr_array_add (monitor->priv->networks, g_object_ref (network));
438 if (g_inet_address_mask_get_length (network) == 0)
440 switch (g_inet_address_mask_get_family (network))
442 case G_SOCKET_FAMILY_IPV4:
443 monitor->priv->have_ipv4_default_route = TRUE;
444 break;
445 case G_SOCKET_FAMILY_IPV6:
446 monitor->priv->have_ipv6_default_route = TRUE;
447 break;
448 default:
449 break;
453 /* Don't emit network-changed when multicast-link-local routing
454 * changes. This rather arbitrary decision is mostly because it
455 * seems to change quite often...
457 if (g_inet_address_get_is_mc_link_local (g_inet_address_mask_get_address (network)))
458 return;
460 queue_network_changed (monitor);
464 * g_network_monitor_base_remove_network:
465 * @monitor: the #GNetworkMonitorBase
466 * @network: a #GInetAddressMask
468 * Removes @network from @monitor's list of available networks.
470 * Since: 2.32
472 void
473 g_network_monitor_base_remove_network (GNetworkMonitorBase *monitor,
474 GInetAddressMask *network)
476 int i;
478 for (i = 0; i < monitor->priv->networks->len; i++)
480 if (g_inet_address_mask_equal (monitor->priv->networks->pdata[i], network))
482 g_ptr_array_remove_index_fast (monitor->priv->networks, i);
484 if (g_inet_address_mask_get_length (network) == 0)
486 switch (g_inet_address_mask_get_family (network))
488 case G_SOCKET_FAMILY_IPV4:
489 monitor->priv->have_ipv4_default_route = FALSE;
490 break;
491 case G_SOCKET_FAMILY_IPV6:
492 monitor->priv->have_ipv6_default_route = FALSE;
493 break;
494 default:
495 break;
499 queue_network_changed (monitor);
500 return;
506 * g_network_monitor_base_set_networks:
507 * @monitor: the #GNetworkMonitorBase
508 * @networks: (array length=length): an array of #GInetAddressMask
509 * @length: length of @networks
511 * Drops @monitor's current list of available networks and replaces
512 * it with @networks.
514 void
515 g_network_monitor_base_set_networks (GNetworkMonitorBase *monitor,
516 GInetAddressMask **networks,
517 gint length)
519 int i;
521 g_ptr_array_set_size (monitor->priv->networks, 0);
522 monitor->priv->have_ipv4_default_route = FALSE;
523 monitor->priv->have_ipv6_default_route = FALSE;
525 for (i = 0; i < length; i++)
526 g_network_monitor_base_add_network (monitor, networks[i]);