gnetworkmonitornm.c 11.5 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright 2014 Red Hat, Inc.
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
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
 *
 * 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,
43
  PROP_NETWORK_METERED,
44 45 46 47 48 49 50 51 52 53 54
  PROP_CONNECTIVITY
};

typedef enum {
  NM_CONNECTIVITY_UNKNOWN,
  NM_CONNECTIVITY_NONE,
  NM_CONNECTIVITY_PORTAL,
  NM_CONNECTIVITY_LIMITED,
  NM_CONNECTIVITY_FULL
} NMConnectivityState;

55 56 57 58 59 60 61 62 63 64 65 66 67
/* 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;

68 69 70 71 72 73
struct _GNetworkMonitorNMPrivate
{
  GDBusProxy *proxy;

  GNetworkConnectivity connectivity;
  gboolean network_available;
74
  gboolean network_metered;
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
};

#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;

110 111 112 113
    case PROP_NETWORK_METERED:
      g_value_set_boolean (value, nm->priv->network_metered);
      break;

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
    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;
    }
}

145 146 147 148 149 150 151 152
static gboolean
nm_metered_to_bool (guint nm_metered)
{
  switch (nm_metered)
    {
      case 1: /* yes */
      case 3: /* guess-yes */
        return TRUE;
153 154 155 156
      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. */
157 158 159 160 161 162 163 164 165
      case 2: /* no */
      case 4: /* guess-no */
        return FALSE;
      default:
        g_warning ("Unknown NM metered state %d", nm_metered);
        return FALSE;
    }
}

166 167 168 169 170
static void
sync_properties (GNetworkMonitorNM *nm,
                 gboolean           emit_signals)
{
  GVariant *v;
171
  NMState nm_state;
172 173
  NMConnectivityState nm_connectivity;
  gboolean new_network_available;
174
  gboolean new_network_metered;
175 176
  GNetworkConnectivity new_connectivity;

177 178 179 180 181 182 183
  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);

184
  v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Connectivity");
185 186 187
  if (!v)
    return;

188 189 190
  nm_connectivity = g_variant_get_uint32 (v);
  g_variant_unref (v);

191
  if (nm_state <= NM_STATE_CONNECTED_LOCAL)
192 193
    {
      new_network_available = FALSE;
194
      new_network_metered = FALSE;
195 196
      new_connectivity = G_NETWORK_CONNECTIVITY_LOCAL;
    }
197 198
  else if (nm_state <= NM_STATE_CONNECTED_SITE)
    {
199
      new_network_available = TRUE;
200 201 202 203 204 205 206 207 208 209 210
      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 */
211
    {
212

213
      /* this is only available post NM 1.0 */
214 215 216 217 218 219 220 221 222 223 224
      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);
        }

225 226 227 228 229 230
      new_network_available = TRUE;
      new_connectivity = nm_conn_to_g_conn (nm_connectivity);
    }

  if (!emit_signals)
    {
231
      nm->priv->network_metered = new_network_metered;
232 233 234 235 236 237 238 239 240 241
      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");
    }
242 243 244
  if (new_network_metered != nm->priv->network_metered)
    {
      nm->priv->network_metered = new_network_metered;
245
      g_object_notify (G_OBJECT (nm), "network-metered");
246
    }
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  if (new_connectivity != nm->priv->connectivity)
    {
      nm->priv->connectivity = new_connectivity;
      g_object_notify (G_OBJECT (nm), "connectivity");
    }
}

static void
update_cached_property (GDBusProxy   *proxy,
                        const char   *property_name,
                        GVariantDict *dict)
{
  GVariant *v;

  v = g_variant_dict_lookup_value (dict, property_name, NULL);
  if (!v)
    return;
  g_dbus_proxy_set_cached_property (proxy, property_name, v);
265
  g_variant_unref (v);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

static void
proxy_signal_cb (GDBusProxy        *proxy,
                 gchar             *sender_name,
                 gchar             *signal_name,
                 GVariant          *parameters,
                 GNetworkMonitorNM *nm)
{
  GVariant *asv;
  GVariantDict *dict;

  if (g_strcmp0 (signal_name, "PropertiesChanged") != 0)
    return;

  g_variant_get (parameters, "(@a{sv})", &asv);
  if (!asv)
    return;

  dict = g_variant_dict_new (asv);
286
  g_variant_unref (asv);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  if (!dict)
    {
      g_warning ("Failed to handle PropertiesChanged signal from NetworkManager");
      return;
    }

  update_cached_property (nm->priv->proxy, "Connectivity", dict);

  g_variant_dict_unref (dict);

  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);
308 309 310 311

  if (!props)
    return FALSE;

312
  prop_found = g_strv_contains ((const gchar * const *) props, property_name);
313 314 315 316 317 318 319 320 321 322 323 324
  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;
325
  gchar *name_owner = NULL;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

  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;

342 343 344 345
  name_owner = g_dbus_proxy_get_name_owner (proxy);

  if (!name_owner)
    {
346 347
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   _("NetworkManager not running"));
348 349 350 351 352 353
      g_object_unref (proxy);
      return FALSE;
    }

  g_free (name_owner);

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  /* 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;
    }

  g_signal_connect (G_OBJECT (proxy), "g-signal",
                    G_CALLBACK (proxy_signal_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);

  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");
390
  g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered");
391 392 393 394 395 396 397 398 399 400 401 402 403
  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;
}