1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2014 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include "gnetworkmonitornm.h"
29 #include "ginitable.h"
30 #include "giomodule-priv.h"
32 #include "glib/gstdio.h"
33 #include "gnetworkingprivate.h"
34 #include "gnetworkmonitor.h"
35 #include "gdbusproxy.h"
37 static void g_network_monitor_nm_iface_init (GNetworkMonitorInterface *iface);
38 static void g_network_monitor_nm_initable_iface_init (GInitableIface *iface);
44 PROP_NETWORK_AVAILABLE,
50 NM_CONNECTIVITY_UNKNOWN,
52 NM_CONNECTIVITY_PORTAL,
53 NM_CONNECTIVITY_LIMITED,
55 } NMConnectivityState;
57 /* Copied from https://developer.gnome.org/libnm-util/stable/libnm-util-NetworkManager.html#NMState;
58 * used inline to avoid a NetworkManager dependency from GLib. */
62 NM_STATE_DISCONNECTED = 20,
63 NM_STATE_DISCONNECTING = 30,
64 NM_STATE_CONNECTING = 40,
65 NM_STATE_CONNECTED_LOCAL = 50,
66 NM_STATE_CONNECTED_SITE = 60,
67 NM_STATE_CONNECTED_GLOBAL = 70,
70 struct _GNetworkMonitorNMPrivate
75 GNetworkConnectivity connectivity;
76 gboolean network_available;
77 gboolean network_metered;
80 #define g_network_monitor_nm_get_type _g_network_monitor_nm_get_type
81 G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorNM, g_network_monitor_nm, G_TYPE_NETWORK_MONITOR_NETLINK,
82 G_ADD_PRIVATE (GNetworkMonitorNM)
83 G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR,
84 g_network_monitor_nm_iface_init)
85 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
86 g_network_monitor_nm_initable_iface_init)
87 _g_io_modules_ensure_extension_points_registered ();
88 g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
94 g_network_monitor_nm_init (GNetworkMonitorNM *nm)
96 nm->priv = g_network_monitor_nm_get_instance_private (nm);
100 g_network_monitor_nm_get_property (GObject *object,
105 GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object);
109 case PROP_NETWORK_AVAILABLE:
110 g_value_set_boolean (value, nm->priv->network_available);
113 case PROP_NETWORK_METERED:
114 g_value_set_boolean (value, nm->priv->network_metered);
117 case PROP_CONNECTIVITY:
118 g_value_set_enum (value, nm->priv->connectivity);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 static GNetworkConnectivity
128 nm_conn_to_g_conn (int nm_state)
132 case NM_CONNECTIVITY_UNKNOWN:
133 return G_NETWORK_CONNECTIVITY_LOCAL;
134 case NM_CONNECTIVITY_NONE:
135 return G_NETWORK_CONNECTIVITY_LOCAL;
136 case NM_CONNECTIVITY_PORTAL:
137 return G_NETWORK_CONNECTIVITY_PORTAL;
138 case NM_CONNECTIVITY_LIMITED:
139 return G_NETWORK_CONNECTIVITY_LIMITED;
140 case NM_CONNECTIVITY_FULL:
141 return G_NETWORK_CONNECTIVITY_FULL;
143 g_warning ("Unknown NM connectivity state %d", nm_state);
144 return G_NETWORK_CONNECTIVITY_LOCAL;
149 nm_metered_to_bool (guint nm_metered)
154 case 3: /* guess-yes */
156 case 0: /* unknown */
157 /* We default to FALSE in the unknown-because-you're-not-running-NM
158 * case, so we should return FALSE in the
159 * unknown-when-you-are-running-NM case too. */
161 case 4: /* guess-no */
164 g_warning ("Unknown NM metered state %d", nm_metered);
170 sync_properties (GNetworkMonitorNM *nm,
171 gboolean emit_signals)
175 NMConnectivityState nm_connectivity;
176 gboolean new_network_available;
177 gboolean new_network_metered;
178 GNetworkConnectivity new_connectivity;
180 v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "State");
184 nm_state = g_variant_get_uint32 (v);
187 v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Connectivity");
191 nm_connectivity = g_variant_get_uint32 (v);
194 if (nm_state <= NM_STATE_CONNECTED_LOCAL)
196 new_network_available = FALSE;
197 new_network_metered = FALSE;
198 new_connectivity = G_NETWORK_CONNECTIVITY_LOCAL;
200 else if (nm_state <= NM_STATE_CONNECTED_SITE)
202 new_network_available = TRUE;
203 new_network_metered = FALSE;
204 if (nm_connectivity == NM_CONNECTIVITY_PORTAL)
206 new_connectivity = G_NETWORK_CONNECTIVITY_PORTAL;
210 new_connectivity = G_NETWORK_CONNECTIVITY_LIMITED;
213 else /* nm_state == NM_STATE_CONNECTED_FULL */
216 /* this is only available post NM 1.0 */
217 v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Metered");
220 new_network_metered = FALSE;
224 new_network_metered = nm_metered_to_bool (g_variant_get_uint32 (v));
228 new_network_available = TRUE;
229 new_connectivity = nm_conn_to_g_conn (nm_connectivity);
234 nm->priv->network_metered = new_network_metered;
235 nm->priv->network_available = new_network_available;
236 nm->priv->connectivity = new_connectivity;
240 if (new_network_available != nm->priv->network_available)
242 nm->priv->network_available = new_network_available;
243 g_object_notify (G_OBJECT (nm), "network-available");
245 if (new_network_metered != nm->priv->network_metered)
247 nm->priv->network_metered = new_network_metered;
248 g_object_notify (G_OBJECT (nm), "network-metered");
250 if (new_connectivity != nm->priv->connectivity)
252 nm->priv->connectivity = new_connectivity;
253 g_object_notify (G_OBJECT (nm), "connectivity");
258 proxy_properties_changed_cb (GDBusProxy *proxy,
259 GVariant *changed_properties,
260 GStrv invalidated_properties,
261 GNetworkMonitorNM *nm)
263 sync_properties (nm, TRUE);
267 has_property (GDBusProxy *proxy,
268 const char *property_name)
271 gboolean prop_found = FALSE;
273 props = g_dbus_proxy_get_cached_property_names (proxy);
278 prop_found = g_strv_contains ((const gchar * const *) props, property_name);
284 g_network_monitor_nm_initable_init (GInitable *initable,
285 GCancellable *cancellable,
288 GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (initable);
290 GInitableIface *parent_iface;
291 gchar *name_owner = NULL;
293 parent_iface = g_type_interface_peek_parent (G_NETWORK_MONITOR_NM_GET_INITABLE_IFACE (initable));
294 if (!parent_iface->init (initable, cancellable, error))
297 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
298 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
300 "org.freedesktop.NetworkManager",
301 "/org/freedesktop/NetworkManager",
302 "org.freedesktop.NetworkManager",
308 name_owner = g_dbus_proxy_get_name_owner (proxy);
312 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
313 _("NetworkManager not running"));
314 g_object_unref (proxy);
320 /* Verify it has the PrimaryConnection and Connectivity properties */
321 if (!has_property (proxy, "Connectivity"))
323 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
324 _("NetworkManager version too old"));
325 g_object_unref (proxy);
329 nm->priv->signal_id = g_signal_connect (G_OBJECT (proxy), "g-properties-changed",
330 G_CALLBACK (proxy_properties_changed_cb), nm);
331 nm->priv->proxy = proxy;
332 sync_properties (nm, FALSE);
338 g_network_monitor_nm_finalize (GObject *object)
340 GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object);
342 if (nm->priv->proxy != NULL &&
343 nm->priv->signal_id != 0)
345 g_signal_handler_disconnect (nm->priv->proxy,
346 nm->priv->signal_id);
347 nm->priv->signal_id = 0;
349 g_clear_object (&nm->priv->proxy);
351 G_OBJECT_CLASS (g_network_monitor_nm_parent_class)->finalize (object);
355 g_network_monitor_nm_class_init (GNetworkMonitorNMClass *nl_class)
357 GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
359 gobject_class->finalize = g_network_monitor_nm_finalize;
360 gobject_class->get_property = g_network_monitor_nm_get_property;
362 g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available");
363 g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered");
364 g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity");
368 g_network_monitor_nm_iface_init (GNetworkMonitorInterface *monitor_iface)
373 g_network_monitor_nm_initable_iface_init (GInitableIface *iface)
375 iface->init = g_network_monitor_nm_initable_init;