/src/glib/gio/gnetworkmonitornm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright 2014 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.1 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, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <errno.h> |
22 | | #include <string.h> |
23 | | #include <unistd.h> |
24 | | |
25 | | #include "gnetworkmonitornm.h" |
26 | | #include "gioerror.h" |
27 | | #include "ginitable.h" |
28 | | #include "giomodule-priv.h" |
29 | | #include "glibintl.h" |
30 | | #include "glib/gstdio.h" |
31 | | #include "gnetworkingprivate.h" |
32 | | #include "gnetworkmonitor.h" |
33 | | #include "gdbusproxy.h" |
34 | | |
35 | | static void g_network_monitor_nm_iface_init (GNetworkMonitorInterface *iface); |
36 | | static void g_network_monitor_nm_initable_iface_init (GInitableIface *iface); |
37 | | |
38 | | enum |
39 | | { |
40 | | PROP_0, |
41 | | |
42 | | PROP_NETWORK_AVAILABLE, |
43 | | PROP_NETWORK_METERED, |
44 | | PROP_CONNECTIVITY |
45 | | }; |
46 | | |
47 | | typedef enum { |
48 | | NM_CONNECTIVITY_UNKNOWN, |
49 | | NM_CONNECTIVITY_NONE, |
50 | | NM_CONNECTIVITY_PORTAL, |
51 | | NM_CONNECTIVITY_LIMITED, |
52 | | NM_CONNECTIVITY_FULL |
53 | | } NMConnectivityState; |
54 | | |
55 | | /* Copied from https://developer.gnome.org/libnm-util/stable/libnm-util-NetworkManager.html#NMState; |
56 | | * used inline to avoid a NetworkManager dependency from GLib. */ |
57 | | typedef enum { |
58 | | NM_STATE_UNKNOWN = 0, |
59 | | NM_STATE_ASLEEP = 10, |
60 | | NM_STATE_DISCONNECTED = 20, |
61 | | NM_STATE_DISCONNECTING = 30, |
62 | | NM_STATE_CONNECTING = 40, |
63 | | NM_STATE_CONNECTED_LOCAL = 50, |
64 | | NM_STATE_CONNECTED_SITE = 60, |
65 | | NM_STATE_CONNECTED_GLOBAL = 70, |
66 | | } NMState; |
67 | | |
68 | | struct _GNetworkMonitorNMPrivate |
69 | | { |
70 | | GDBusProxy *proxy; |
71 | | guint signal_id; |
72 | | |
73 | | GNetworkConnectivity connectivity; |
74 | | gboolean network_available; |
75 | | gboolean network_metered; |
76 | | }; |
77 | | |
78 | | #define g_network_monitor_nm_get_type _g_network_monitor_nm_get_type |
79 | | G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorNM, g_network_monitor_nm, G_TYPE_NETWORK_MONITOR_NETLINK, |
80 | | G_ADD_PRIVATE (GNetworkMonitorNM) |
81 | | G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR, |
82 | | g_network_monitor_nm_iface_init) |
83 | | G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, |
84 | | g_network_monitor_nm_initable_iface_init) |
85 | | _g_io_modules_ensure_extension_points_registered (); |
86 | | g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME, |
87 | | g_define_type_id, |
88 | | "networkmanager", |
89 | | 30)) |
90 | | |
91 | | static void |
92 | | g_network_monitor_nm_init (GNetworkMonitorNM *nm) |
93 | 0 | { |
94 | 0 | nm->priv = g_network_monitor_nm_get_instance_private (nm); |
95 | 0 | } |
96 | | |
97 | | static void |
98 | | g_network_monitor_nm_get_property (GObject *object, |
99 | | guint prop_id, |
100 | | GValue *value, |
101 | | GParamSpec *pspec) |
102 | 0 | { |
103 | 0 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
104 | |
|
105 | 0 | switch (prop_id) |
106 | 0 | { |
107 | 0 | case PROP_NETWORK_AVAILABLE: |
108 | 0 | g_value_set_boolean (value, nm->priv->network_available); |
109 | 0 | break; |
110 | | |
111 | 0 | case PROP_NETWORK_METERED: |
112 | 0 | g_value_set_boolean (value, nm->priv->network_metered); |
113 | 0 | break; |
114 | | |
115 | 0 | case PROP_CONNECTIVITY: |
116 | 0 | g_value_set_enum (value, nm->priv->connectivity); |
117 | 0 | break; |
118 | | |
119 | 0 | default: |
120 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
121 | 0 | break; |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | static GNetworkConnectivity |
126 | | nm_conn_to_g_conn (int nm_state) |
127 | 0 | { |
128 | 0 | switch (nm_state) |
129 | 0 | { |
130 | 0 | case NM_CONNECTIVITY_UNKNOWN: |
131 | 0 | return G_NETWORK_CONNECTIVITY_LOCAL; |
132 | 0 | case NM_CONNECTIVITY_NONE: |
133 | 0 | return G_NETWORK_CONNECTIVITY_LOCAL; |
134 | 0 | case NM_CONNECTIVITY_PORTAL: |
135 | 0 | return G_NETWORK_CONNECTIVITY_PORTAL; |
136 | 0 | case NM_CONNECTIVITY_LIMITED: |
137 | 0 | return G_NETWORK_CONNECTIVITY_LIMITED; |
138 | 0 | case NM_CONNECTIVITY_FULL: |
139 | 0 | return G_NETWORK_CONNECTIVITY_FULL; |
140 | 0 | default: |
141 | 0 | g_warning ("Unknown NM connectivity state %d", nm_state); |
142 | 0 | return G_NETWORK_CONNECTIVITY_LOCAL; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | static gboolean |
147 | | nm_metered_to_bool (guint nm_metered) |
148 | 0 | { |
149 | 0 | switch (nm_metered) |
150 | 0 | { |
151 | 0 | case 1: /* yes */ |
152 | 0 | case 3: /* guess-yes */ |
153 | 0 | return TRUE; |
154 | 0 | case 0: /* unknown */ |
155 | | /* We default to FALSE in the unknown-because-you're-not-running-NM |
156 | | * case, so we should return FALSE in the |
157 | | * unknown-when-you-are-running-NM case too. */ |
158 | 0 | case 2: /* no */ |
159 | 0 | case 4: /* guess-no */ |
160 | 0 | return FALSE; |
161 | 0 | default: |
162 | 0 | g_warning ("Unknown NM metered state %d", nm_metered); |
163 | 0 | return FALSE; |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | | static void |
168 | | sync_properties (GNetworkMonitorNM *nm, |
169 | | gboolean emit_signals) |
170 | 0 | { |
171 | 0 | GVariant *v; |
172 | 0 | NMState nm_state; |
173 | 0 | NMConnectivityState nm_connectivity; |
174 | 0 | gboolean new_network_available; |
175 | 0 | gboolean new_network_metered; |
176 | 0 | GNetworkConnectivity new_connectivity; |
177 | |
|
178 | 0 | v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "State"); |
179 | 0 | if (!v) |
180 | 0 | return; |
181 | | |
182 | 0 | nm_state = g_variant_get_uint32 (v); |
183 | 0 | g_variant_unref (v); |
184 | |
|
185 | 0 | v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Connectivity"); |
186 | 0 | if (!v) |
187 | 0 | return; |
188 | | |
189 | 0 | nm_connectivity = g_variant_get_uint32 (v); |
190 | 0 | g_variant_unref (v); |
191 | |
|
192 | 0 | if (nm_state <= NM_STATE_CONNECTED_LOCAL) |
193 | 0 | { |
194 | 0 | new_network_available = FALSE; |
195 | 0 | new_network_metered = FALSE; |
196 | 0 | new_connectivity = G_NETWORK_CONNECTIVITY_LOCAL; |
197 | 0 | } |
198 | 0 | else if (nm_state <= NM_STATE_CONNECTED_SITE) |
199 | 0 | { |
200 | 0 | new_network_available = TRUE; |
201 | 0 | new_network_metered = FALSE; |
202 | 0 | if (nm_connectivity == NM_CONNECTIVITY_PORTAL) |
203 | 0 | { |
204 | 0 | new_connectivity = G_NETWORK_CONNECTIVITY_PORTAL; |
205 | 0 | } |
206 | 0 | else |
207 | 0 | { |
208 | 0 | new_connectivity = G_NETWORK_CONNECTIVITY_LIMITED; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | else /* nm_state == NM_STATE_CONNECTED_FULL */ |
212 | 0 | { |
213 | | |
214 | | /* this is only available post NM 1.0 */ |
215 | 0 | v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Metered"); |
216 | 0 | if (v == NULL) |
217 | 0 | { |
218 | 0 | new_network_metered = FALSE; |
219 | 0 | } |
220 | 0 | else |
221 | 0 | { |
222 | 0 | new_network_metered = nm_metered_to_bool (g_variant_get_uint32 (v)); |
223 | 0 | g_variant_unref (v); |
224 | 0 | } |
225 | |
|
226 | 0 | new_network_available = TRUE; |
227 | 0 | new_connectivity = nm_conn_to_g_conn (nm_connectivity); |
228 | 0 | } |
229 | |
|
230 | 0 | if (!emit_signals) |
231 | 0 | { |
232 | 0 | nm->priv->network_metered = new_network_metered; |
233 | 0 | nm->priv->network_available = new_network_available; |
234 | 0 | nm->priv->connectivity = new_connectivity; |
235 | 0 | return; |
236 | 0 | } |
237 | | |
238 | 0 | if (new_network_available != nm->priv->network_available) |
239 | 0 | { |
240 | 0 | nm->priv->network_available = new_network_available; |
241 | 0 | g_object_notify (G_OBJECT (nm), "network-available"); |
242 | 0 | } |
243 | 0 | if (new_network_metered != nm->priv->network_metered) |
244 | 0 | { |
245 | 0 | nm->priv->network_metered = new_network_metered; |
246 | 0 | g_object_notify (G_OBJECT (nm), "network-metered"); |
247 | 0 | } |
248 | 0 | if (new_connectivity != nm->priv->connectivity) |
249 | 0 | { |
250 | 0 | nm->priv->connectivity = new_connectivity; |
251 | 0 | g_object_notify (G_OBJECT (nm), "connectivity"); |
252 | 0 | } |
253 | 0 | } |
254 | | |
255 | | static void |
256 | | update_cached_property (GDBusProxy *proxy, |
257 | | const char *property_name, |
258 | | GVariantDict *dict) |
259 | 0 | { |
260 | 0 | GVariant *v; |
261 | |
|
262 | 0 | v = g_variant_dict_lookup_value (dict, property_name, NULL); |
263 | 0 | if (!v) |
264 | 0 | return; |
265 | 0 | g_dbus_proxy_set_cached_property (proxy, property_name, v); |
266 | 0 | g_variant_unref (v); |
267 | 0 | } |
268 | | |
269 | | static void |
270 | | proxy_signal_cb (GDBusProxy *proxy, |
271 | | const gchar *sender_name, |
272 | | const gchar *signal_name, |
273 | | GVariant *parameters, |
274 | | GNetworkMonitorNM *nm) |
275 | 0 | { |
276 | 0 | GVariant *asv; |
277 | 0 | GVariantDict *dict; |
278 | |
|
279 | 0 | if (g_strcmp0 (signal_name, "PropertiesChanged") != 0) |
280 | 0 | return; |
281 | | |
282 | 0 | g_variant_get (parameters, "(@a{sv})", &asv); |
283 | 0 | if (!asv) |
284 | 0 | return; |
285 | | |
286 | 0 | dict = g_variant_dict_new (asv); |
287 | 0 | g_variant_unref (asv); |
288 | 0 | if (!dict) |
289 | 0 | { |
290 | 0 | g_warning ("Failed to handle PropertiesChanged signal from NetworkManager"); |
291 | 0 | return; |
292 | 0 | } |
293 | | |
294 | 0 | update_cached_property (nm->priv->proxy, "Connectivity", dict); |
295 | |
|
296 | 0 | g_variant_dict_unref (dict); |
297 | |
|
298 | 0 | sync_properties (nm, TRUE); |
299 | 0 | } |
300 | | |
301 | | static gboolean |
302 | | has_property (GDBusProxy *proxy, |
303 | | const char *property_name) |
304 | 0 | { |
305 | 0 | char **props; |
306 | 0 | gboolean prop_found = FALSE; |
307 | |
|
308 | 0 | props = g_dbus_proxy_get_cached_property_names (proxy); |
309 | |
|
310 | 0 | if (!props) |
311 | 0 | return FALSE; |
312 | | |
313 | 0 | prop_found = g_strv_contains ((const gchar * const *) props, property_name); |
314 | 0 | g_strfreev (props); |
315 | 0 | return prop_found; |
316 | 0 | } |
317 | | |
318 | | static gboolean |
319 | | g_network_monitor_nm_initable_init (GInitable *initable, |
320 | | GCancellable *cancellable, |
321 | | GError **error) |
322 | 0 | { |
323 | 0 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (initable); |
324 | 0 | GDBusProxy *proxy; |
325 | 0 | GInitableIface *parent_iface; |
326 | 0 | gchar *name_owner = NULL; |
327 | |
|
328 | 0 | parent_iface = g_type_interface_peek_parent (G_NETWORK_MONITOR_NM_GET_INITABLE_IFACE (initable)); |
329 | 0 | if (!parent_iface->init (initable, cancellable, error)) |
330 | 0 | return FALSE; |
331 | | |
332 | 0 | proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, |
333 | 0 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, |
334 | 0 | NULL, |
335 | 0 | "org.freedesktop.NetworkManager", |
336 | 0 | "/org/freedesktop/NetworkManager", |
337 | 0 | "org.freedesktop.NetworkManager", |
338 | 0 | cancellable, |
339 | 0 | error); |
340 | 0 | if (!proxy) |
341 | 0 | return FALSE; |
342 | | |
343 | 0 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
344 | |
|
345 | 0 | if (!name_owner) |
346 | 0 | { |
347 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
348 | 0 | _("NetworkManager not running")); |
349 | 0 | g_object_unref (proxy); |
350 | 0 | return FALSE; |
351 | 0 | } |
352 | | |
353 | 0 | g_free (name_owner); |
354 | | |
355 | | /* Verify it has the PrimaryConnection and Connectivity properties */ |
356 | 0 | if (!has_property (proxy, "Connectivity")) |
357 | 0 | { |
358 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
359 | 0 | _("NetworkManager version too old")); |
360 | 0 | g_object_unref (proxy); |
361 | 0 | return FALSE; |
362 | 0 | } |
363 | | |
364 | 0 | nm->priv->signal_id = g_signal_connect (G_OBJECT (proxy), "g-signal", |
365 | 0 | G_CALLBACK (proxy_signal_cb), nm); |
366 | 0 | nm->priv->proxy = proxy; |
367 | 0 | sync_properties (nm, FALSE); |
368 | |
|
369 | 0 | return TRUE; |
370 | 0 | } |
371 | | |
372 | | static void |
373 | | g_network_monitor_nm_finalize (GObject *object) |
374 | 0 | { |
375 | 0 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
376 | |
|
377 | 0 | if (nm->priv->proxy != NULL && |
378 | 0 | nm->priv->signal_id != 0) |
379 | 0 | { |
380 | 0 | g_signal_handler_disconnect (nm->priv->proxy, |
381 | 0 | nm->priv->signal_id); |
382 | 0 | nm->priv->signal_id = 0; |
383 | 0 | } |
384 | 0 | g_clear_object (&nm->priv->proxy); |
385 | |
|
386 | 0 | G_OBJECT_CLASS (g_network_monitor_nm_parent_class)->finalize (object); |
387 | 0 | } |
388 | | |
389 | | static void |
390 | | g_network_monitor_nm_class_init (GNetworkMonitorNMClass *nl_class) |
391 | 0 | { |
392 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class); |
393 | |
|
394 | 0 | gobject_class->finalize = g_network_monitor_nm_finalize; |
395 | 0 | gobject_class->get_property = g_network_monitor_nm_get_property; |
396 | |
|
397 | 0 | g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available"); |
398 | 0 | g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered"); |
399 | 0 | g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity"); |
400 | 0 | } |
401 | | |
402 | | static void |
403 | | g_network_monitor_nm_iface_init (GNetworkMonitorInterface *monitor_iface) |
404 | 0 | { |
405 | 0 | } |
406 | | |
407 | | static void |
408 | | g_network_monitor_nm_initable_iface_init (GInitableIface *iface) |
409 | 0 | { |
410 | 0 | iface->init = g_network_monitor_nm_initable_init; |
411 | 0 | } |