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 | { |
94 | nm->priv = g_network_monitor_nm_get_instance_private (self: nm); |
95 | } |
96 | |
97 | static void |
98 | g_network_monitor_nm_get_property (GObject *object, |
99 | guint prop_id, |
100 | GValue *value, |
101 | GParamSpec *pspec) |
102 | { |
103 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
104 | |
105 | switch (prop_id) |
106 | { |
107 | case PROP_NETWORK_AVAILABLE: |
108 | g_value_set_boolean (value, v_boolean: nm->priv->network_available); |
109 | break; |
110 | |
111 | case PROP_NETWORK_METERED: |
112 | g_value_set_boolean (value, v_boolean: nm->priv->network_metered); |
113 | break; |
114 | |
115 | case PROP_CONNECTIVITY: |
116 | g_value_set_enum (value, v_enum: nm->priv->connectivity); |
117 | break; |
118 | |
119 | default: |
120 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
121 | break; |
122 | } |
123 | } |
124 | |
125 | static GNetworkConnectivity |
126 | nm_conn_to_g_conn (int nm_state) |
127 | { |
128 | switch (nm_state) |
129 | { |
130 | case NM_CONNECTIVITY_UNKNOWN: |
131 | return G_NETWORK_CONNECTIVITY_LOCAL; |
132 | case NM_CONNECTIVITY_NONE: |
133 | return G_NETWORK_CONNECTIVITY_LOCAL; |
134 | case NM_CONNECTIVITY_PORTAL: |
135 | return G_NETWORK_CONNECTIVITY_PORTAL; |
136 | case NM_CONNECTIVITY_LIMITED: |
137 | return G_NETWORK_CONNECTIVITY_LIMITED; |
138 | case NM_CONNECTIVITY_FULL: |
139 | return G_NETWORK_CONNECTIVITY_FULL; |
140 | default: |
141 | g_warning ("Unknown NM connectivity state %d" , nm_state); |
142 | return G_NETWORK_CONNECTIVITY_LOCAL; |
143 | } |
144 | } |
145 | |
146 | static gboolean |
147 | nm_metered_to_bool (guint nm_metered) |
148 | { |
149 | switch (nm_metered) |
150 | { |
151 | case 1: /* yes */ |
152 | case 3: /* guess-yes */ |
153 | return TRUE; |
154 | 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 | case 2: /* no */ |
159 | case 4: /* guess-no */ |
160 | return FALSE; |
161 | default: |
162 | g_warning ("Unknown NM metered state %d" , nm_metered); |
163 | return FALSE; |
164 | } |
165 | } |
166 | |
167 | static void |
168 | sync_properties (GNetworkMonitorNM *nm, |
169 | gboolean emit_signals) |
170 | { |
171 | GVariant *v; |
172 | NMState nm_state; |
173 | NMConnectivityState nm_connectivity; |
174 | gboolean new_network_available; |
175 | gboolean new_network_metered; |
176 | GNetworkConnectivity new_connectivity; |
177 | |
178 | v = g_dbus_proxy_get_cached_property (proxy: nm->priv->proxy, property_name: "State" ); |
179 | if (!v) |
180 | return; |
181 | |
182 | nm_state = g_variant_get_uint32 (value: v); |
183 | g_variant_unref (value: v); |
184 | |
185 | v = g_dbus_proxy_get_cached_property (proxy: nm->priv->proxy, property_name: "Connectivity" ); |
186 | if (!v) |
187 | return; |
188 | |
189 | nm_connectivity = g_variant_get_uint32 (value: v); |
190 | g_variant_unref (value: v); |
191 | |
192 | if (nm_state <= NM_STATE_CONNECTED_LOCAL) |
193 | { |
194 | new_network_available = FALSE; |
195 | new_network_metered = FALSE; |
196 | new_connectivity = G_NETWORK_CONNECTIVITY_LOCAL; |
197 | } |
198 | else if (nm_state <= NM_STATE_CONNECTED_SITE) |
199 | { |
200 | new_network_available = TRUE; |
201 | new_network_metered = FALSE; |
202 | if (nm_connectivity == NM_CONNECTIVITY_PORTAL) |
203 | { |
204 | new_connectivity = G_NETWORK_CONNECTIVITY_PORTAL; |
205 | } |
206 | else |
207 | { |
208 | new_connectivity = G_NETWORK_CONNECTIVITY_LIMITED; |
209 | } |
210 | } |
211 | else /* nm_state == NM_STATE_CONNECTED_FULL */ |
212 | { |
213 | |
214 | /* this is only available post NM 1.0 */ |
215 | v = g_dbus_proxy_get_cached_property (proxy: nm->priv->proxy, property_name: "Metered" ); |
216 | if (v == NULL) |
217 | { |
218 | new_network_metered = FALSE; |
219 | } |
220 | else |
221 | { |
222 | new_network_metered = nm_metered_to_bool (nm_metered: g_variant_get_uint32 (value: v)); |
223 | g_variant_unref (value: v); |
224 | } |
225 | |
226 | new_network_available = TRUE; |
227 | new_connectivity = nm_conn_to_g_conn (nm_state: nm_connectivity); |
228 | } |
229 | |
230 | if (!emit_signals) |
231 | { |
232 | nm->priv->network_metered = new_network_metered; |
233 | nm->priv->network_available = new_network_available; |
234 | nm->priv->connectivity = new_connectivity; |
235 | return; |
236 | } |
237 | |
238 | if (new_network_available != nm->priv->network_available) |
239 | { |
240 | nm->priv->network_available = new_network_available; |
241 | g_object_notify (G_OBJECT (nm), property_name: "network-available" ); |
242 | } |
243 | if (new_network_metered != nm->priv->network_metered) |
244 | { |
245 | nm->priv->network_metered = new_network_metered; |
246 | g_object_notify (G_OBJECT (nm), property_name: "network-metered" ); |
247 | } |
248 | if (new_connectivity != nm->priv->connectivity) |
249 | { |
250 | nm->priv->connectivity = new_connectivity; |
251 | g_object_notify (G_OBJECT (nm), property_name: "connectivity" ); |
252 | } |
253 | } |
254 | |
255 | static void |
256 | update_cached_property (GDBusProxy *proxy, |
257 | const char *property_name, |
258 | GVariantDict *dict) |
259 | { |
260 | GVariant *v; |
261 | |
262 | v = g_variant_dict_lookup_value (dict, key: property_name, NULL); |
263 | if (!v) |
264 | return; |
265 | g_dbus_proxy_set_cached_property (proxy, property_name, value: v); |
266 | g_variant_unref (value: v); |
267 | } |
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 | { |
276 | GVariant *asv; |
277 | GVariantDict *dict; |
278 | |
279 | if (g_strcmp0 (str1: signal_name, str2: "PropertiesChanged" ) != 0) |
280 | return; |
281 | |
282 | g_variant_get (value: parameters, format_string: "(@a{sv})" , &asv); |
283 | if (!asv) |
284 | return; |
285 | |
286 | dict = g_variant_dict_new (from_asv: asv); |
287 | g_variant_unref (value: asv); |
288 | if (!dict) |
289 | { |
290 | g_warning ("Failed to handle PropertiesChanged signal from NetworkManager" ); |
291 | return; |
292 | } |
293 | |
294 | update_cached_property (proxy: nm->priv->proxy, property_name: "Connectivity" , dict); |
295 | |
296 | g_variant_dict_unref (dict); |
297 | |
298 | sync_properties (nm, TRUE); |
299 | } |
300 | |
301 | static gboolean |
302 | has_property (GDBusProxy *proxy, |
303 | const char *property_name) |
304 | { |
305 | char **props; |
306 | gboolean prop_found = FALSE; |
307 | |
308 | props = g_dbus_proxy_get_cached_property_names (proxy); |
309 | |
310 | if (!props) |
311 | return FALSE; |
312 | |
313 | prop_found = g_strv_contains (strv: (const gchar * const *) props, str: property_name); |
314 | g_strfreev (str_array: props); |
315 | return prop_found; |
316 | } |
317 | |
318 | static gboolean |
319 | g_network_monitor_nm_initable_init (GInitable *initable, |
320 | GCancellable *cancellable, |
321 | GError **error) |
322 | { |
323 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (initable); |
324 | GDBusProxy *proxy; |
325 | GInitableIface *parent_iface; |
326 | gchar *name_owner = NULL; |
327 | |
328 | parent_iface = g_type_interface_peek_parent (G_NETWORK_MONITOR_NM_GET_INITABLE_IFACE (initable)); |
329 | if (!parent_iface->init (initable, cancellable, error)) |
330 | return FALSE; |
331 | |
332 | proxy = g_dbus_proxy_new_for_bus_sync (bus_type: G_BUS_TYPE_SYSTEM, |
333 | flags: G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, |
334 | NULL, |
335 | name: "org.freedesktop.NetworkManager" , |
336 | object_path: "/org/freedesktop/NetworkManager" , |
337 | interface_name: "org.freedesktop.NetworkManager" , |
338 | cancellable, |
339 | error); |
340 | if (!proxy) |
341 | return FALSE; |
342 | |
343 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
344 | |
345 | if (!name_owner) |
346 | { |
347 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_FAILED, |
348 | _("NetworkManager not running" )); |
349 | g_object_unref (object: proxy); |
350 | return FALSE; |
351 | } |
352 | |
353 | g_free (mem: name_owner); |
354 | |
355 | /* Verify it has the PrimaryConnection and Connectivity properties */ |
356 | if (!has_property (proxy, property_name: "Connectivity" )) |
357 | { |
358 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_FAILED, |
359 | _("NetworkManager version too old" )); |
360 | g_object_unref (object: proxy); |
361 | return FALSE; |
362 | } |
363 | |
364 | nm->priv->signal_id = g_signal_connect (G_OBJECT (proxy), "g-signal" , |
365 | G_CALLBACK (proxy_signal_cb), nm); |
366 | nm->priv->proxy = proxy; |
367 | sync_properties (nm, FALSE); |
368 | |
369 | return TRUE; |
370 | } |
371 | |
372 | static void |
373 | g_network_monitor_nm_finalize (GObject *object) |
374 | { |
375 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
376 | |
377 | if (nm->priv->proxy != NULL && |
378 | nm->priv->signal_id != 0) |
379 | { |
380 | g_signal_handler_disconnect (instance: nm->priv->proxy, |
381 | handler_id: nm->priv->signal_id); |
382 | nm->priv->signal_id = 0; |
383 | } |
384 | g_clear_object (&nm->priv->proxy); |
385 | |
386 | G_OBJECT_CLASS (g_network_monitor_nm_parent_class)->finalize (object); |
387 | } |
388 | |
389 | static void |
390 | g_network_monitor_nm_class_init (GNetworkMonitorNMClass *nl_class) |
391 | { |
392 | GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class); |
393 | |
394 | gobject_class->finalize = g_network_monitor_nm_finalize; |
395 | gobject_class->get_property = g_network_monitor_nm_get_property; |
396 | |
397 | g_object_class_override_property (oclass: gobject_class, property_id: PROP_NETWORK_AVAILABLE, name: "network-available" ); |
398 | g_object_class_override_property (oclass: gobject_class, property_id: PROP_NETWORK_METERED, name: "network-metered" ); |
399 | g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONNECTIVITY, name: "connectivity" ); |
400 | } |
401 | |
402 | static void |
403 | g_network_monitor_nm_iface_init (GNetworkMonitorInterface *monitor_iface) |
404 | { |
405 | } |
406 | |
407 | static void |
408 | g_network_monitor_nm_initable_iface_init (GInitableIface *iface) |
409 | { |
410 | iface->init = g_network_monitor_nm_initable_init; |
411 | } |
412 | |