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