Docs: don't use <footnote>
[platform/upstream/glib.git] / gio / gnetworkmonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright 2011 Red Hat, Inc
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "glib.h"
23 #include "glibintl.h"
24
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"
31 #include "gtask.h"
32
33 /**
34  * SECTION:gnetworkmonitor
35  * @title: GNetworkMonitor
36  * @short_description: Network status monitor
37  * @include: gio/gio.h
38  *
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.
42  */
43
44 /**
45  * GNetworkMonitor:
46  *
47  * #GNetworkMonitor monitors the status of network connections and
48  * indicates when a possibly-user-visible change has occurred.
49  *
50  * Since: 2.32
51  */
52
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);)
55
56
57 enum {
58   NETWORK_CHANGED,
59   LAST_SIGNAL
60 };
61
62 static guint signals[LAST_SIGNAL] = { 0 };
63
64 /**
65  * g_network_monitor_get_default:
66  *
67  * Gets the default #GNetworkMonitor for the system.
68  *
69  * Returns: (transfer none): a #GNetworkMonitor
70  *
71  * Since: 2.32
72  */
73 GNetworkMonitor *
74 g_network_monitor_get_default (void)
75 {
76   return _g_io_module_get_default (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
77                                    "GIO_USE_NETWORK_MONITOR",
78                                    NULL);
79 }
80
81 /**
82  * g_network_monitor_get_network_available:
83  * @monitor: the #GNetworkMonitor
84  *
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.
89  *
90  * Return value: whether the network is available
91  *
92  * Since: 2.32
93  */
94 gboolean
95 g_network_monitor_get_network_available (GNetworkMonitor *monitor)
96 {
97   gboolean available = FALSE;
98
99   g_object_get (G_OBJECT (monitor), "network-available", &available, NULL);
100   return available;
101 }
102
103 /**
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
109  *
110  * Attempts to determine whether or not the host pointed to by
111  * @connectable can be reached, without actually trying to connect to
112  * it.
113  *
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.
117  *
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).
122  *
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().
127  *
128  * Return value: %TRUE if @connectable is reachable, %FALSE if not.
129  *
130  * Since: 2.32
131  */
132 gboolean
133 g_network_monitor_can_reach (GNetworkMonitor     *monitor,
134                              GSocketConnectable  *connectable,
135                              GCancellable        *cancellable,
136                              GError             **error)
137 {
138   GNetworkMonitorInterface *iface;
139
140   iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
141   return iface->can_reach (monitor, connectable, cancellable, error);
142 }
143
144 static void
145 g_network_monitor_real_can_reach_async (GNetworkMonitor     *monitor,
146                                         GSocketConnectable  *connectable,
147                                         GCancellable        *cancellable,
148                                         GAsyncReadyCallback  callback,
149                                         gpointer             user_data)
150 {
151   GTask *task;
152   GError *error = NULL;
153
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);
157   else
158     g_task_return_error (task, error);
159   g_object_unref (task);
160 }
161
162 /**
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
170  *
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.
174  *
175  * For more details, see g_network_monitor_can_reach().
176  *
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.
180  */
181 void
182 g_network_monitor_can_reach_async (GNetworkMonitor     *monitor,
183                                    GSocketConnectable  *connectable,
184                                    GCancellable        *cancellable,
185                                    GAsyncReadyCallback  callback,
186                                    gpointer             user_data)
187 {
188   GNetworkMonitorInterface *iface;
189
190   iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
191   iface->can_reach_async (monitor, connectable, cancellable, callback, user_data);
192 }
193
194 static gboolean
195 g_network_monitor_real_can_reach_finish (GNetworkMonitor  *monitor,
196                                          GAsyncResult     *result,
197                                          GError          **error)
198 {
199   g_return_val_if_fail (g_task_is_valid (result, monitor), FALSE);
200
201   return g_task_propagate_boolean (G_TASK (result), error);
202 }
203
204 /**
205  * g_network_monitor_can_reach_finish:
206  * @monitor: a #GNetworkMonitor
207  * @result: a #GAsyncResult
208  * @error: return location for errors, or %NULL
209  *
210  * Finishes an async network connectivity test.
211  * See g_network_monitor_can_reach_async().
212  *
213  * Return value: %TRUE if network is reachable, %FALSE if not.
214  */
215 gboolean
216 g_network_monitor_can_reach_finish (GNetworkMonitor     *monitor,
217                                     GAsyncResult        *result,
218                                     GError             **error)
219 {
220   GNetworkMonitorInterface *iface;
221
222   iface = G_NETWORK_MONITOR_GET_INTERFACE (monitor);
223   return iface->can_reach_finish (monitor, result, error);
224 }
225
226 static void
227 g_network_monitor_default_init (GNetworkMonitorInterface *iface)
228 {
229   iface->can_reach_async  = g_network_monitor_real_can_reach_async;
230   iface->can_reach_finish = g_network_monitor_real_can_reach_finish;
231
232   /**
233    * GNetworkMonitor::network-changed:
234    * @monitor: a #GNetworkMonitor
235    * @available: the current value of #GNetworkMonitor:network-available
236    *
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
241    * reachable.
242    *
243    * Since: 2.32
244    */
245   signals[NETWORK_CHANGED] =
246     g_signal_new (I_("network-changed"),
247                   G_TYPE_NETWORK_MONITOR,
248                   G_SIGNAL_RUN_LAST,
249                   G_STRUCT_OFFSET (GNetworkMonitorInterface, network_changed),
250                   NULL, NULL,
251                   g_cclosure_marshal_VOID__BOOLEAN,
252                   G_TYPE_NONE, 1,
253                   G_TYPE_BOOLEAN);
254
255   /**
256    * GNetworkMonitor:network-available:
257    *
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.
260    *
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
273    * in its UI.)
274    *
275    * See also #GNetworkMonitor::network-changed.
276    *
277    * Since: 2.32
278    */
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"),
283                                                              FALSE,
284                                                              G_PARAM_READABLE |
285                                                              G_PARAM_STATIC_STRINGS));
286 }