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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gnetworkmonitor.h"
26 #include "ginetaddress.h"
27 #include "ginetsocketaddress.h"
28 #include "ginitable.h"
29 #include "gioenumtypes.h"
30 #include "giomodule-priv.h"
34 * SECTION:gnetworkmonitor
35 * @title: GNetworkMonitor
36 * @short_description: Network status monitor
39 * #GNetworkMonitor provides an easy-to-use cross-platform API
40 * for monitoring network connectivity. On Linux, the implementation
41 * is based on the kernel's netlink interface.
47 * #GNetworkMonitor monitors the status of network connections and
48 * indicates when a possibly-user-visible change has occurred.
53 G_DEFINE_INTERFACE_WITH_CODE (GNetworkMonitor, g_network_monitor, G_TYPE_OBJECT,
54 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE);)
62 static guint signals[LAST_SIGNAL] = { 0 };
65 * g_network_monitor_get_default:
67 * Gets the default #GNetworkMonitor for the system.
69 * Returns: (transfer none): a #GNetworkMonitor
74 g_network_monitor_get_default (void)
76 return _g_io_module_get_default (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
77 "GIO_USE_NETWORK_MONITOR",
82 * g_network_monitor_get_network_available:
83 * @monitor: the #GNetworkMonitor
85 * Checks if the network is available. "Available" here means that the
86 * system has a default route available for at least one of IPv4 or
87 * IPv6. It does not necessarily imply that the public Internet is
88 * reachable. See #GNetworkMonitor:network-available for more details.
90 * Return value: whether the network is available
95 g_network_monitor_get_network_available (GNetworkMonitor *monitor)
97 gboolean available = FALSE;
99 g_object_get (G_OBJECT (monitor), "network-available", &available, NULL);
104 * g_network_monitor_can_reach:
105 * @monitor: a #GNetworkMonitor
106 * @connectable: a #GSocketConnectable
107 * @cancellable: (allow-none): a #GCancellable, or %NULL
108 * @error: return location for a #GError, or %NULL
110 * Attempts to determine whether or not the host pointed to by
111 * @connectable can be reached, without actually trying to connect to
114 * This may return %TRUE even when #GNetworkMonitor:network-available
115 * is %FALSE, if, for example, @monitor can determine that
116 * @connectable refers to a host on a local network.
118 * If @monitor believes that an attempt to connect to @connectable
119 * will succeed, it will return %TRUE. Otherwise, it will return
120 * %FALSE and set @error to an appropriate error (such as
121 * %G_IO_ERROR_HOST_UNREACHABLE).
123 * Note that although this does not attempt to connect to
124 * @connectable, it may still block for a brief period of time (eg,
125 * trying to do multicast DNS on the local network), so if you do not
126 * want to block, you should use g_network_monitor_can_reach_async().
128 * Return value: %TRUE if @connectable is reachable, %FALSE if not.
133 g_network_monitor_can_reach (GNetworkMonitor *monitor,
134 GSocketConnectable *connectable,
135 GCancellable *cancellable,
138 GNetworkMonitorInterface *iface;
140 iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
141 return iface->can_reach (monitor, connectable, cancellable, error);
145 g_network_monitor_real_can_reach_async (GNetworkMonitor *monitor,
146 GSocketConnectable *connectable,
147 GCancellable *cancellable,
148 GAsyncReadyCallback callback,
152 GError *error = NULL;
154 task = g_task_new (monitor, cancellable, callback, user_data);
155 if (g_network_monitor_can_reach (monitor, connectable, cancellable, &error))
156 g_task_return_boolean (task, TRUE);
158 g_task_return_error (task, error);
159 g_object_unref (task);
163 * g_network_monitor_can_reach_async:
164 * @monitor: a #GNetworkMonitor
165 * @connectable: a #GSocketConnectable
166 * @cancellable: (allow-none): a #GCancellable, or %NULL
167 * @callback: (scope async): a #GAsyncReadyCallback to call when the
168 * request is satisfied
169 * @user_data: (closure): the data to pass to callback function
171 * Asynchronously attempts to determine whether or not the host
172 * pointed to by @connectable can be reached, without actually
173 * trying to connect to it.
175 * For more details, see g_network_monitor_can_reach().
177 * When the operation is finished, @callback will be called.
178 * You can then call g_network_monitor_can_reach_finish()
179 * to get the result of the operation.
182 g_network_monitor_can_reach_async (GNetworkMonitor *monitor,
183 GSocketConnectable *connectable,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
188 GNetworkMonitorInterface *iface;
190 iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
191 iface->can_reach_async (monitor, connectable, cancellable, callback, user_data);
195 g_network_monitor_real_can_reach_finish (GNetworkMonitor *monitor,
196 GAsyncResult *result,
199 g_return_val_if_fail (g_task_is_valid (result, monitor), FALSE);
201 return g_task_propagate_boolean (G_TASK (result), error);
205 * g_network_monitor_can_reach_finish:
206 * @monitor: a #GNetworkMonitor
207 * @result: a #GAsyncResult
208 * @error: return location for errors, or %NULL
210 * Finishes an async network connectivity test.
211 * See g_network_monitor_can_reach_async().
213 * Return value: %TRUE if network is reachable, %FALSE if not.
216 g_network_monitor_can_reach_finish (GNetworkMonitor *monitor,
217 GAsyncResult *result,
220 GNetworkMonitorInterface *iface;
222 iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
223 return iface->can_reach_finish (monitor, result, error);
227 g_network_monitor_default_init (GNetworkMonitorInterface *iface)
229 iface->can_reach_async = g_network_monitor_real_can_reach_async;
230 iface->can_reach_finish = g_network_monitor_real_can_reach_finish;
233 * GNetworkMonitor::network-changed:
234 * @monitor: a #GNetworkMonitor
235 * @available: the current value of #GNetworkMonitor:network-available
237 * Emitted when the network configuration changes. If @available is
238 * %TRUE, then some hosts may be reachable that were not reachable
239 * before, while others that were reachable before may no longer be
240 * reachable. If @available is %FALSE, then no remote hosts are
245 signals[NETWORK_CHANGED] =
246 g_signal_new (I_("network-changed"),
247 G_TYPE_NETWORK_MONITOR,
249 G_STRUCT_OFFSET (GNetworkMonitorInterface, network_changed),
251 g_cclosure_marshal_VOID__BOOLEAN,
256 * GNetworkMonitor:network-available:
258 * Whether the network is considered available. That is, whether the
259 * system has a default route for at least one of IPv4 or IPv6.
261 * Real-world networks are of course much more complicated than
262 * this; the machine may be connected to a wifi hotspot that
263 * requires payment before allowing traffic through, or may be
264 * connected to a functioning router that has lost its own upstream
265 * connectivity. Some hosts might only be accessible when a VPN is
266 * active. Other hosts might only be accessible when the VPN is
267 * <emphasis>not</emphasis> active. Thus, it is best to use
268 * g_network_monitor_can_reach() or
269 * g_network_monitor_can_reach_async() to test for reachability on a
270 * host-by-host basis. (On the other hand, when the property is
271 * %FALSE, the application can reasonably expect that no remote
272 * hosts at all are reachable, and should indicate this to the user
275 * See also #GNetworkMonitor::network-changed.
279 g_object_interface_install_property (iface,
280 g_param_spec_boolean ("network-available",
281 P_("Network available"),
282 P_("Whether the network is available"),
285 G_PARAM_STATIC_STRINGS));