nm-client.c 142 KB
Newer Older
Lubomir Rintel's avatar
Lubomir Rintel committed
1
// SPDX-License-Identifier: LGPL-2.1+
2
/*
3 4
 * Copyright (C) 2007 - 2008 Novell, Inc.
 * Copyright (C) 2007 - 2018 Red Hat, Inc.
5 6
 */

7
#include "nm-default.h"
8

9 10
#include "nm-client.h"

11
#include <libudev.h>
12

13
#include "nm-glib-aux/nm-dbus-aux.h"
14
#include "nm-utils.h"
15
#include "nm-manager.h"
16
#include "nm-dns-manager.h"
17
#include "nm-remote-settings.h"
18 19
#include "nm-device-ethernet.h"
#include "nm-device-wifi.h"
20
#include "nm-core-internal.h"
21 22
#include "nm-active-connection.h"
#include "nm-vpn-connection.h"
23
#include "nm-remote-connection.h"
Dan Winship's avatar
Dan Winship committed
24
#include "nm-dbus-helpers.h"
25 26 27
#include "nm-wimax-nsp.h"
#include "nm-object-private.h"

28 29
#include "introspection/org.freedesktop.NetworkManager.h"
#include "introspection/org.freedesktop.NetworkManager.Device.Wireless.h"
Thomas Haller's avatar
Thomas Haller committed
30
#include "introspection/org.freedesktop.NetworkManager.Device.WifiP2P.h"
31
#include "introspection/org.freedesktop.NetworkManager.Device.h"
32
#include "introspection/org.freedesktop.NetworkManager.DnsManager.h"
33 34 35
#include "introspection/org.freedesktop.NetworkManager.Settings.h"
#include "introspection/org.freedesktop.NetworkManager.Settings.Connection.h"
#include "introspection/org.freedesktop.NetworkManager.VPN.Connection.h"
36
#include "introspection/org.freedesktop.NetworkManager.Connection.Active.h"
37 38 39

#include "nm-access-point.h"
#include "nm-active-connection.h"
40
#include "nm-checkpoint.h"
41
#include "nm-device-6lowpan.h"
42 43 44 45
#include "nm-device-adsl.h"
#include "nm-device-bond.h"
#include "nm-device-bridge.h"
#include "nm-device-bt.h"
46
#include "nm-device-dummy.h"
47 48 49 50
#include "nm-device-ethernet.h"
#include "nm-device-generic.h"
#include "nm-device-infiniband.h"
#include "nm-device-ip-tunnel.h"
51
#include "nm-device-macsec.h"
52 53 54
#include "nm-device-macvlan.h"
#include "nm-device-modem.h"
#include "nm-device-olpc-mesh.h"
55
#include "nm-device-ovs-bridge.h"
56
#include "nm-device-ovs-interface.h"
57
#include "nm-device-ovs-port.h"
Beniamino Galvani's avatar
Beniamino Galvani committed
58
#include "nm-device-ppp.h"
59 60 61 62
#include "nm-device-team.h"
#include "nm-device-tun.h"
#include "nm-device-vlan.h"
#include "nm-device-vxlan.h"
63
#include "nm-device-wifi-p2p.h"
64 65
#include "nm-device-wifi.h"
#include "nm-device-wimax.h"
66
#include "nm-device-wireguard.h"
67
#include "nm-device-wpan.h"
68
#include "nm-dhcp-config.h"
69 70 71 72 73
#include "nm-dhcp4-config.h"
#include "nm-dhcp6-config.h"
#include "nm-ip4-config.h"
#include "nm-ip6-config.h"
#include "nm-manager.h"
74
#include "nm-wifi-p2p-peer.h"
75 76 77
#include "nm-remote-connection.h"
#include "nm-remote-settings.h"
#include "nm-vpn-connection.h"
Dan Winship's avatar
Dan Winship committed
78

79 80 81 82 83
void _nm_device_wifi_set_wireless_enabled (NMDeviceWifi *device, gboolean enabled);

static void nm_client_initable_iface_init (GInitableIface *iface);
static void nm_client_async_initable_iface_init (GAsyncInitableIface *iface);

84
G_DEFINE_TYPE_WITH_CODE (NMClient, nm_client, G_TYPE_OBJECT,
85 86 87 88 89 90
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_client_initable_iface_init);
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_client_async_initable_iface_init);
                         )

#define NM_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CLIENT, NMClientPrivate))

Thomas Haller's avatar
Thomas Haller committed
91 92 93 94 95 96 97
typedef struct {
	NMClient *client;
	GCancellable *cancellable;
	GSimpleAsyncResult *result;
	int pending_init;
} NMClientInitData;

98
typedef struct {
99
	NMManager *manager;
100
	NMRemoteSettings *settings;
101
	NMDnsManager *dns_manager;
102 103
	GDBusObjectManager *object_manager;
	GCancellable *new_object_manager_cancellable;
104
	char *name_owner_cached;
105
	struct udev *udev;
106
	bool udev_inited:1;
107 108 109 110 111 112 113
} NMClientPrivate;

enum {
	PROP_0,
	PROP_VERSION,
	PROP_STATE,
	PROP_STARTUP,
114
	PROP_NM_RUNNING,
115 116 117 118 119 120 121 122 123
	PROP_NETWORKING_ENABLED,
	PROP_WIRELESS_ENABLED,
	PROP_WIRELESS_HARDWARE_ENABLED,
	PROP_WWAN_ENABLED,
	PROP_WWAN_HARDWARE_ENABLED,
	PROP_WIMAX_ENABLED,
	PROP_WIMAX_HARDWARE_ENABLED,
	PROP_ACTIVE_CONNECTIONS,
	PROP_CONNECTIVITY,
124 125
	PROP_CONNECTIVITY_CHECK_AVAILABLE,
	PROP_CONNECTIVITY_CHECK_ENABLED,
126 127 128
	PROP_PRIMARY_CONNECTION,
	PROP_ACTIVATING_CONNECTION,
	PROP_DEVICES,
129
	PROP_ALL_DEVICES,
130 131 132
	PROP_CONNECTIONS,
	PROP_HOSTNAME,
	PROP_CAN_MODIFY,
133
	PROP_METERED,
134 135 136
	PROP_DNS_MODE,
	PROP_DNS_RC_MANAGER,
	PROP_DNS_CONFIGURATION,
137
	PROP_CHECKPOINTS,
138 139 140 141 142 143 144

	LAST_PROP
};

enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
145 146
	ANY_DEVICE_ADDED,
	ANY_DEVICE_REMOVED,
147
	PERMISSION_CHANGED,
148 149
	CONNECTION_ADDED,
	CONNECTION_REMOVED,
150 151
	ACTIVE_CONNECTION_ADDED,
	ACTIVE_CONNECTION_REMOVED,
152 153 154 155 156 157

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

158 159
static const GPtrArray empty = { 0, };

160
/*****************************************************************************/
161 162 163 164 165 166 167 168

/**
 * nm_client_error_quark:
 *
 * Registers an error quark for #NMClient if necessary.
 *
 * Returns: the error quark used for #NMClient errors.
 **/
169
NM_CACHED_QUARK_FCN ("nm-client-error-quark", nm_client_error_quark)
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
GDBusConnection *
_nm_client_get_dbus_connection (NMClient *client)
{
	NMClientPrivate *priv;

	nm_assert (NM_IS_CLIENT (client));

	priv = NM_CLIENT_GET_PRIVATE (client);

	if (!priv->object_manager)
		return NULL;

	return g_dbus_object_manager_client_get_connection (G_DBUS_OBJECT_MANAGER_CLIENT (priv->object_manager));
}

const char *
_nm_client_get_dbus_name_owner (NMClient *client)
{
	NMClientPrivate *priv;

	nm_assert (NM_IS_CLIENT (client));

	priv = NM_CLIENT_GET_PRIVATE (client);

	nm_clear_g_free (&priv->name_owner_cached);

	if (!priv->object_manager)
		return NULL;

	priv->name_owner_cached = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (priv->object_manager));
	return priv->name_owner_cached;
}

/*****************************************************************************/

208 209 210 211 212
static void
nm_client_init (NMClient *client)
{
}

213 214 215
static gboolean
_nm_client_check_nm_running (NMClient *client, GError **error)
{
216 217
	if (!nm_client_get_nm_running (client)) {
		_nm_object_set_error_nm_not_running (error);
218 219
		return FALSE;
	}
220
	return TRUE;
221 222
}

223
/**
224
 * nm_client_get_version:
225 226
 * @client: a #NMClient
 *
227
 * Gets NetworkManager version.
228
 *
229
 * Returns: string with the version (or %NULL if NetworkManager is not running)
230
 **/
231 232
const char *
nm_client_get_version (NMClient *client)
233 234 235
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

236 237 238
	if (!nm_client_get_nm_running (client))
		return NULL;

239
	return nm_manager_get_version (NM_CLIENT_GET_PRIVATE (client)->manager);
240 241 242
}

/**
243
 * nm_client_get_state:
244 245
 * @client: a #NMClient
 *
246
 * Gets the current daemon state.
247
 *
248
 * Returns: the current %NMState
249
 **/
250 251
NMState
nm_client_get_state (NMClient *client)
252
{
253
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
254

255 256 257
	if (!nm_client_get_nm_running (client))
		return NM_STATE_UNKNOWN;

258
	return nm_manager_get_state (NM_CLIENT_GET_PRIVATE (client)->manager);
259 260 261
}

/**
262
 * nm_client_get_startup:
263 264
 * @client: a #NMClient
 *
265 266
 * Tests whether the daemon is still in the process of activating
 * connections at startup.
267
 *
268
 * Returns: whether the daemon is still starting up
269
 **/
270 271
gboolean
nm_client_get_startup (NMClient *client)
272
{
273 274 275 276
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

	if (!nm_client_get_nm_running (client))
		return FALSE;
277

278
	return nm_manager_get_startup (NM_CLIENT_GET_PRIVATE (client)->manager);
279 280
}

281 282 283 284 285 286 287 288 289 290
/**
 * nm_client_get_nm_running:
 * @client: a #NMClient
 *
 * Determines whether the daemon is running.
 *
 * Returns: %TRUE if the daemon is running
 **/
gboolean
nm_client_get_nm_running (NMClient *client)
291
{
292
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
293

294
	return NM_CLIENT_GET_PRIVATE (client)->manager != NULL;
295 296
}

297 298 299 300 301 302 303 304 305 306
/**
 * nm_client_networking_get_enabled:
 * @client: a #NMClient
 *
 * Whether networking is enabled or disabled.
 *
 * Returns: %TRUE if networking is enabled, %FALSE if networking is disabled
 **/
gboolean
nm_client_networking_get_enabled (NMClient *client)
307
{
308
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
309

310 311 312
	if (!nm_client_get_nm_running (client))
		return FALSE;

313
	return nm_manager_networking_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
314 315
}

316 317 318 319 320 321 322 323 324 325 326
/**
 * nm_client_networking_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to set networking enabled, %FALSE to set networking disabled
 * @error: (allow-none): return location for a #GError, or %NULL
 *
 * Enables or disables networking.  When networking is disabled, all controlled
 * interfaces are disconnected and deactivated.  When networking is enabled,
 * all controlled interfaces are available for activation.
 *
 * Returns: %TRUE on success, %FALSE otherwise
327 328
 *
 * Deprecated: 1.22, use nm_client_networking_set_enabled_async() or GDBusConnection
329 330 331
 **/
gboolean
nm_client_networking_set_enabled (NMClient *client, gboolean enable, GError **error)
332
{
333 334
	const char *name_owner;

335
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
336

337
	/* FIXME(libnm-async-api): add nm_client_networking_set_enabled_async(). */
338 339 340 341

	name_owner = _nm_client_get_dbus_name_owner (client);
	if (!name_owner) {
		_nm_object_set_error_nm_not_running (error);
342
		return FALSE;
343
	}
344

345 346 347 348
	return _nm_manager_networking_set_enabled (_nm_client_get_dbus_connection (client),
	                                           name_owner,
	                                           enable,
	                                           error);
349 350
}

351 352 353 354 355 356 357 358 359 360
/**
 * nm_client_wireless_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether the wireless is enabled.
 *
 * Returns: %TRUE if wireless is enabled
 **/
gboolean
nm_client_wireless_get_enabled (NMClient *client)
361
{
362
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
363

364 365 366
	if (!nm_client_get_nm_running (client))
		return FALSE;

367
	return nm_manager_wireless_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
368 369 370
}

/**
371
 * nm_client_wireless_set_enabled:
372
 * @client: a #NMClient
373
 * @enabled: %TRUE to enable wireless
374
 *
375
 * Enables or disables wireless devices.
376 377 378
 *
 * Deprecated: 1.22, use nm_client_wireless_set_enabled_async() or GDBusConnection
 */
379
void
380
nm_client_wireless_set_enabled (NMClient *client, gboolean enabled)
381 382 383
{
	g_return_if_fail (NM_IS_CLIENT (client));

384
	/* FIXME(libnm-async-api): add nm_client_wireless_set_enabled_async(). */
385
	if (!nm_client_get_nm_running (client))
386
		return;
387

388
	nm_manager_wireless_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
389 390
}

391
/**
392 393
 * nm_client_wireless_hardware_get_enabled:
 * @client: a #NMClient
394
 *
395
 * Determines whether the wireless hardware is enabled.
396
 *
397
 * Returns: %TRUE if the wireless hardware is enabled
398
 **/
399 400
gboolean
nm_client_wireless_hardware_get_enabled (NMClient *client)
401
{
402
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
403

404 405 406
	if (!nm_client_get_nm_running (client))
		return FALSE;

407
	return nm_manager_wireless_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
408 409
}

410 411 412 413 414 415 416 417 418 419
/**
 * nm_client_wwan_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether WWAN is enabled.
 *
 * Returns: %TRUE if WWAN is enabled
 **/
gboolean
nm_client_wwan_get_enabled (NMClient *client)
420
{
421
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
422

423 424 425
	if (!nm_client_get_nm_running (client))
		return FALSE;

426
	return nm_manager_wwan_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
427 428 429
}

/**
430
 * nm_client_wwan_set_enabled:
431
 * @client: a #NMClient
432
 * @enabled: %TRUE to enable WWAN
433
 *
434
 * Enables or disables WWAN devices.
435 436
 **/
void
437
nm_client_wwan_set_enabled (NMClient *client, gboolean enabled)
438 439
{
	g_return_if_fail (NM_IS_CLIENT (client));
440

441
	if (!_nm_client_check_nm_running (client, NULL))
442
		return;
443

444
	nm_manager_wwan_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
445
}
446

447
/**
448 449
 * nm_client_wwan_hardware_get_enabled:
 * @client: a #NMClient
450
 *
451
 * Determines whether the WWAN hardware is enabled.
452
 *
453
 * Returns: %TRUE if the WWAN hardware is enabled
454
 **/
455 456
gboolean
nm_client_wwan_hardware_get_enabled (NMClient *client)
457
{
458
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
459

460 461 462
	if (!nm_client_get_nm_running (client))
		return FALSE;

463
	return nm_manager_wwan_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
464 465 466
}

/**
467
 * nm_client_wimax_get_enabled:
468 469
 * @client: a #NMClient
 *
470
 * Determines whether WiMAX is enabled.
471
 *
472
 * Returns: %TRUE if WiMAX is enabled
473
 **/
474
gboolean
475
nm_client_wimax_get_enabled (NMClient *client)
476
{
477
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
478

479 480 481
	if (!nm_client_get_nm_running (client))
		return FALSE;

482
	return nm_manager_wimax_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
483 484 485
}

/**
486
 * nm_client_wimax_set_enabled:
487
 * @client: a #NMClient
488
 * @enabled: %TRUE to enable WiMAX
489
 *
490
 * Enables or disables WiMAX devices.
491 492
 **/
void
493
nm_client_wimax_set_enabled (NMClient *client, gboolean enabled)
494 495 496
{
	g_return_if_fail (NM_IS_CLIENT (client));

497
	if (!nm_client_get_nm_running (client))
498 499
		return;

500
	nm_manager_wimax_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
501 502 503
}

/**
504
 * nm_client_wimax_hardware_get_enabled:
505 506
 * @client: a #NMClient
 *
507
 * Determines whether the WiMAX hardware is enabled.
508
 *
509
 * Returns: %TRUE if the WiMAX hardware is enabled
510 511
 **/
gboolean
512
nm_client_wimax_hardware_get_enabled (NMClient *client)
513
{
514
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
515

516 517 518
	if (!nm_client_get_nm_running (client))
		return FALSE;

519
	return nm_manager_wimax_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
520 521
}

522 523 524 525 526 527 528 529 530
/**
 * nm_client_connectivity_check_get_available:
 * @client: a #NMClient
 *
 * Determine whether connectivity checking is available.  This
 * requires that the URI of a connectivity service has been set in the
 * configuration file.
 *
 * Returns: %TRUE if connectivity checking is available.
531 532
 *
 * Since: 1.10
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
 */
gboolean
nm_client_connectivity_check_get_available (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

	if (!nm_client_get_nm_running (client))
		return FALSE;

	return nm_manager_connectivity_check_get_available (NM_CLIENT_GET_PRIVATE (client)->manager);
}

/**
 * nm_client_connectivity_check_get_enabled:
 * @client: a #NMClient
 *
 * Determine whether connectivity checking is enabled.
 *
 * Returns: %TRUE if connectivity checking is enabled.
552 553
 *
 * Since: 1.10
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
 */
gboolean
nm_client_connectivity_check_get_enabled (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

	if (!nm_client_get_nm_running (client))
		return FALSE;

	return nm_manager_connectivity_check_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
}

/**
 * nm_client_connectivity_check_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to enable connectivity checking
 *
 * Enable or disable connectivity checking.  Note that if a
 * connectivity checking URI has not been configured, this will not
 * have any effect.
574 575
 *
 * Since: 1.10
576 577 578 579 580 581 582 583 584 585 586 587
 */
void
nm_client_connectivity_check_set_enabled (NMClient *client, gboolean enabled)
{
	g_return_if_fail (NM_IS_CLIENT (client));

	if (!nm_client_get_nm_running (client))
		return;

	nm_manager_connectivity_check_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
/**
 * nm_client_connectivity_check_get_uri:
 * @client: a #NMClient
 *
 * Get the URI that will be queried to determine if there is internet
 * connectivity.
 *
 * Returns: (transfer none): the connectivity URI in use
 *
 * Since: 1.20
 */
const char *
nm_client_connectivity_check_get_uri (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	if (!nm_client_get_nm_running (client))
		return NULL;

	return nm_manager_connectivity_check_get_uri (NM_CLIENT_GET_PRIVATE (client)->manager);
}

610
/**
611
 * nm_client_get_logging:
612
 * @client: a #NMClient
613 614 615 616
 * @level: (allow-none): return location for logging level string
 * @domains: (allow-none): return location for log domains string. The string is
 *   a list of domains separated by ","
 * @error: (allow-none): return location for a #GError, or %NULL
617
 *
618
 * Gets NetworkManager current logging level and domains.
619
 *
620
 * Returns: %TRUE on success, %FALSE otherwise
621 622
 *
 * Deprecated: 1.22, use nm_client_get_logging_async() or GDBusConnection
623
 **/
624
gboolean
625 626 627 628
nm_client_get_logging (NMClient *client,
                       char **level,
                       char **domains,
                       GError **error)
629
{
630 631
	gs_unref_variant GVariant *ret = NULL;

632 633 634 635
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (level == NULL || *level == NULL, FALSE);
	g_return_val_if_fail (domains == NULL || *domains == NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
636

637 638
	/* FIXME(libnm-async-api): add nm_client_get_logging_async(). */

639 640 641 642 643 644 645 646 647 648 649 650
	ret = _nm_object_dbus_call_sync (client,
	                                 NULL,
	                                 NM_DBUS_PATH,
	                                 NM_DBUS_INTERFACE,
	                                 "GetLogging",
	                                 g_variant_new ("()"),
	                                 G_VARIANT_TYPE ("(ss)"),
	                                 G_DBUS_CALL_FLAGS_NONE,
	                                 NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                                 TRUE,
	                                 error);
	if (!ret)
651
		return FALSE;
652

653 654 655 656 657
	g_variant_get (ret,
	               "(ss)",
	               level,
	               domains);
	return TRUE;
658 659 660
}

/**
661
 * nm_client_set_logging:
662
 * @client: a #NMClient
663 664 665 666
 * @level: (allow-none): logging level to set (%NULL or an empty string for no change)
 * @domains: (allow-none): logging domains to set. The string should be a list of log
 *   domains separated by ",". (%NULL or an empty string for no change)
 * @error: (allow-none): return location for a #GError, or %NULL
667
 *
668
 * Sets NetworkManager logging level and/or domains.
669
 *
670
 * Returns: %TRUE on success, %FALSE otherwise
671 672
 *
 * Deprecated: 1.22, use nm_client_set_logging_async() or GDBusConnection
673 674
 **/
gboolean
675
nm_client_set_logging (NMClient *client, const char *level, const char *domains, GError **error)
676 677
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
678
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
679

680 681
	/* FIXME(libnm-async-api): add nm_client_set_logging_async(). */

682 683 684 685 686 687 688 689 690 691 692 693
	return _nm_object_dbus_call_sync_void (client,
	                                       NULL,
	                                       NM_DBUS_PATH,
	                                       NM_DBUS_INTERFACE,
	                                       "SetLogging",
	                                       g_variant_new ("(ss)",
	                                                      level ?: "",
	                                                      domains ?: ""),
	                                       G_DBUS_CALL_FLAGS_NONE,
	                                       NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                                       TRUE,
	                                       error);
694 695 696
}

/**
697
 * nm_client_get_permission_result:
698
 * @client: a #NMClient
699
 * @permission: the permission for which to return the result, one of #NMClientPermission
700
 *
701 702
 * Requests the result of a specific permission, which indicates whether the
 * client can or cannot perform the action the permission represents
703
 *
704
 * Returns: the permission's result, one of #NMClientPermissionResult
705
 **/
706 707
NMClientPermissionResult
nm_client_get_permission_result (NMClient *client, NMClientPermission permission)
708
{
709 710
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CLIENT_PERMISSION_RESULT_UNKNOWN);

711 712 713
	if (!nm_client_get_nm_running (client))
		return NM_CLIENT_PERMISSION_RESULT_UNKNOWN;

714
	return nm_manager_get_permission_result (NM_CLIENT_GET_PRIVATE (client)->manager, permission);
715 716 717
}

/**
718 719
 * nm_client_get_connectivity:
 * @client: an #NMClient
720
 *
721 722 723 724
 * Gets the current network connectivity state. Contrast
 * nm_client_check_connectivity() and
 * nm_client_check_connectivity_async(), which re-check the
 * connectivity state first before returning any information.
725
 *
726 727 728 729
 * Returns: the current connectivity state
 */
NMConnectivityState
nm_client_get_connectivity (NMClient *client)
730
{
731
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
732

733 734 735
	if (!nm_client_get_nm_running (client))
		return NM_CONNECTIVITY_UNKNOWN;

736
	return nm_manager_get_connectivity (NM_CLIENT_GET_PRIVATE (client)->manager);
737 738 739
}

/**
740 741 742 743
 * nm_client_check_connectivity:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @error: return location for a #GError
744
 *
745 746 747 748 749 750 751 752
 * Updates the network connectivity state and returns the (new)
 * current state. Contrast nm_client_get_connectivity(), which returns
 * the most recent known state without re-checking.
 *
 * This is a blocking call; use nm_client_check_connectivity_async()
 * if you do not want to block.
 *
 * Returns: the (new) current connectivity state
753 754
 *
 * Deprecated: 1.22, use nm_client_check_connectivity_async() or GDBusConnection
755 756 757 758 759
 */
NMConnectivityState
nm_client_check_connectivity (NMClient *client,
                              GCancellable *cancellable,
                              GError **error)
760
{
761 762 763
	gs_unref_variant GVariant *ret = NULL;
	guint32 connectivity;

764
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
765

766 767 768 769 770 771 772 773 774 775 776 777
	ret = _nm_object_dbus_call_sync (client,
	                                 cancellable,
	                                 NM_DBUS_PATH,
	                                 NM_DBUS_INTERFACE,
	                                 "CheckConnectivity",
	                                 g_variant_new ("()"),
	                                 G_VARIANT_TYPE ("(u)"),
	                                 G_DBUS_CALL_FLAGS_NONE,
	                                 NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                                 TRUE,
	                                 error);
	if (!ret)
778 779
		return NM_CONNECTIVITY_UNKNOWN;

780 781 782 783 784 785 786 787 788 789 790 791 792
	g_variant_get (ret,
	               "(u)",
	               &connectivity);

	/* upon receiving the synchronous response, we hack the NMClient state
	 * and update the property outside the ordered D-Bus messages (like
	 * "PropertiesChanged" signals).
	 *
	 * This is really ugly, we shouldn't do this. */
	_nm_manager_set_connectivity_hack (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                   connectivity);

	return connectivity;
793 794
}

795
/**
796 797 798 799 800
 * nm_client_check_connectivity_async:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @callback: callback to call with the result
 * @user_data: data for @callback.
801
 *
802 803 804 805 806 807 808 809 810 811 812 813
 * Asynchronously updates the network connectivity state and invokes
 * @callback when complete. Contrast nm_client_get_connectivity(),
 * which (immediately) returns the most recent known state without
 * re-checking, and nm_client_check_connectivity(), which blocks.
 */
void
nm_client_check_connectivity_async (NMClient *client,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data)
{
	g_return_if_fail (NM_IS_CLIENT (client));
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

	_nm_object_dbus_call (client,
	                      nm_client_check_connectivity_async,
	                      cancellable,
	                      callback,
	                      user_data,
	                      NM_DBUS_PATH,
	                      NM_DBUS_INTERFACE,
	                      "CheckConnectivity",
	                      g_variant_new ("()"),
	                      G_VARIANT_TYPE ("(u)"),
	                      G_DBUS_CALL_FLAGS_NONE,
	                      NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                      nm_dbus_connection_call_finish_variant_strip_dbus_error_cb);
829 830 831 832 833 834 835
}

/**
 * nm_client_check_connectivity_finish:
 * @client: an #NMClient
 * @result: the #GAsyncResult
 * @error: return location for a #GError
836
 *
837 838 839 840 841 842 843 844 845
 * Retrieves the result of an nm_client_check_connectivity_async()
 * call.
 *
 * Returns: the (new) current connectivity state
 */
NMConnectivityState
nm_client_check_connectivity_finish (NMClient *client,
                                     GAsyncResult *result,
                                     GError **error)
846
{
847 848
	gs_unref_variant GVariant *ret = NULL;
	guint32 connectivity;
849

850
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
851
	g_return_val_if_fail (nm_g_task_is_valid (client, result, nm_client_check_connectivity_async), NM_CONNECTIVITY_UNKNOWN);
852

853 854
	ret = g_task_propagate_pointer (G_TASK (result), error);
	if (!ret)
855
		return NM_CONNECTIVITY_UNKNOWN;
856 857 858 859 860

	g_variant_get (ret,
	               "(u)",
	               &connectivity);
	return connectivity;
861 862
}

863 864 865 866 867 868 869 870 871 872 873 874
/**
 * nm_client_save_hostname:
 * @client: the %NMClient
 * @hostname: (allow-none): the new persistent hostname to set, or %NULL to
 *   clear any existing persistent hostname
 * @cancellable: a #GCancellable, or %NULL
 * @error: return location for #GError
 *
 * Requests that the machine's persistent hostname be set to the specified value
 * or cleared.
 *
 * Returns: %TRUE if the request was successful, %FALSE if it failed
875 876
 *
 * Deprecated: 1.22, use nm_client_save_hostname_async() or GDBusConnection
877 878 879 880 881 882 883 884 885
 **/
gboolean
nm_client_save_hostname (NMClient *client,
                         const char *hostname,
                         GCancellable *cancellable,
                         GError **error)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

886 887 888
	if (!_nm_client_check_nm_running (client, error))
		return FALSE;

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	return nm_remote_settings_save_hostname (NM_CLIENT_GET_PRIVATE (client)->settings,
	                                         hostname, cancellable, error);
}

static void
save_hostname_cb (GObject *object,
                  GAsyncResult *result,
                  gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	GError *error = NULL;

	if (nm_remote_settings_save_hostname_finish (NM_REMOTE_SETTINGS (object), result, &error))
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

/**
 * nm_client_save_hostname_async:
 * @client: the %NMClient
 * @hostname: (allow-none): the new persistent hostname to set, or %NULL to
 *   clear any existing persistent hostname
 * @cancellable: a #GCancellable, or %NULL
 * @callback: (scope async): callback to be called when the operation completes
 * @user_data: (closure): caller-specific data passed to @callback
 *
 * Requests that the machine's persistent hostname be set to the specified value
 * or cleared.
 **/
void
nm_client_save_hostname_async (NMClient *client,
                               const char *hostname,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	GSimpleAsyncResult *simple;
	GError *error = NULL;

	g_return_if_fail (NM_IS_CLIENT (client));

	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
		return;
	}

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_save_hostname_async);
941 942
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	nm_remote_settings_save_hostname_async (NM_CLIENT_GET_PRIVATE (client)->settings,
	                                        hostname,
	                                        cancellable, save_hostname_cb, simple);
}

/**
 * nm_client_save_hostname_finish:
 * @client: the %NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: return location for #GError
 *
 * Gets the result of an nm_client_save_hostname_async() call.
 *
 * Returns: %TRUE if the request was successful, %FALSE if it failed
 **/
gboolean
nm_client_save_hostname_finish (NMClient *client,
                                GAsyncResult *result,
                                GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;
	else
		return g_simple_async_result_get_op_res_gboolean (simple);
}

975
/*****************************************************************************/
976
/* Devices                                                      */
977
/*****************************************************************************/
978

979
/**
980
 * nm_client_get_devices:
981 982
 * @client: a #NMClient
 *
983
 * Gets all the known network devices.  Use nm_device_get_type() or the
Dan Winship's avatar
Dan Winship committed
984 985 986
 * <literal>NM_IS_DEVICE_XXXX</literal> functions to determine what kind of
 * device member of the returned array is, and then you may use device-specific
 * methods such as nm_device_ethernet_get_hw_address().
987
 *
988 989 990
 * Returns: (transfer none) (element-type NMDevice): a #GPtrArray
 * containing all the #NMDevices.  The returned array is owned by the
 * #NMClient object and should not be modified.
991
 **/
992 993
const GPtrArray *
nm_client_get_devices (NMClient *client)
994
{
995
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
996

997 998 999
	if (!nm_client_get_nm_running (client))
		return &empty;

1000
	return nm_manager_get_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
1001 1002
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/**
 * nm_client_get_all_devices:
 * @client: a #NMClient
 *
 * Gets both real devices and device placeholders (eg, software devices which
 * do not currently exist, but could be created automatically by NetworkManager
 * if one of their NMDevice::ActivatableConnections was activated).  Use
 * nm_device_is_real() to determine whether each device is a real device or
 * a placeholder.
 *
 * Use nm_device_get_type() or the NM_IS_DEVICE_XXXX() functions to determine
 * what kind of device each member of the returned array is, and then you may
 * use device-specific methods such as nm_device_ethernet_get_hw_address().
 *
 * Returns: (transfer none) (element-type NMDevice): a #GPtrArray
 * containing all the #NMDevices.  The returned array is owned by the
 * #NMClient object and should not be modified.
 *
 * Since: 1.2
 **/
const GPtrArray *
nm_client_get_all_devices (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

1028 1029 1030
	if (!nm_client_get_nm_running (client))
		return &empty;

1031 1032 1033
	return nm_manager_get_all_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
}

1034
/**
1035
 * nm_client_get_device_by_path:
1036
 * @client: a #NMClient
1037
 * @object_path: the object path to search for
1038
 *
1039 1040 1041
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @object_path or %NULL if none is found.
1042
 **/
1043 1044
NMDevice *
nm_client_get_device_by_path (NMClient *client, const char *object_path)
1045
{
1046 1047
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (object_path, NULL);
1048

1049 1050 1051
	if (!nm_client_get_nm_running (client))
		return NULL;

1052
	return nm_manager_get_device_by_path (NM_CLIENT_GET_PRIVATE (client)->manager, object_path);
1053 1054 1055
}

/**
1056
 * nm_client_get_device_by_iface:
1057
 * @client: a #NMClient
1058
 * @iface: the interface name to search for
1059
 *
1060
 * Gets a #NMDevice from a #NMClient.
1061
 *
1062
 * Returns: (transfer none): the #NMDevice for the given @iface or %NULL if none is found.
1063
 **/
1064 1065
NMDevice *
nm_client_get_device_by_iface (NMClient *client, const char *iface)
1066
{
1067 1068 1069
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (iface, NULL);

1070 1071 1072
	if (!nm_client_get_nm_running (client))
		return NULL;

1073
	return nm_manager_get_device_by_iface (NM_CLIENT_GET_PRIVATE (client)->manager, iface);
1074 1075
}

1076
/*****************************************************************************/
1077
/* Active Connections                                           */
1078
/*****************************************************************************/
1079

1080
/**
1081
 * nm_client_get_active_connections:
1082 1083
 * @client: a #NMClient
 *
1084
 * Gets the active connections.
1085
 *
1086 1087 1088
 * Returns: (transfer none) (element-type NMActiveConnection): a #GPtrArray
 *  containing all the active #NMActiveConnections.
 * The returned array is owned by the client and should not be modified.
1089
 **/
1090 1091
const GPtrArray *
nm_client_get_active_connections (NMClient *client)
1092 1093 1094
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

1095 1096 1097
	if (!nm_client_get_nm_running (client))
		return &empty;

1098
	return nm_manager_get_active_connections (NM_CLIENT_GET_PRIVATE (client)->manager);
1099 1100 1101
}

/**
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
 * nm_client_get_primary_connection:
 * @client: an #NMClient
 *
 * Gets the #NMActiveConnection corresponding to the primary active
 * network device.
 *
 * In particular, when there is no VPN active, or the VPN does not
 * have the default route, this returns the active connection that has
 * the default route. If there is a VPN active with the default route,
 * then this function returns the active connection that contains the
 * route to the VPN endpoint.
 *
 * If there is no default route, or the default route is over a
 * non-NetworkManager-recognized device, this will return %NULL.
 *
 * Returns: (transfer none): the appropriate #NMActiveConnection, if
 * any
 */
NMActiveConnection *
nm_client_get_primary_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

1125 1126 1127
	if (!nm_client_get_nm_running (client))
		return NULL;

1128
	return nm_manager_get_primary_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
}

/**
 * nm_client_get_activating_connection:
 * @client: an #NMClient
 *
 * Gets the #NMActiveConnection corresponding to a
 * currently-activating connection that is expected to become the new
 * #NMClient:primary-connection upon successful activation.
 *
 * Returns: (transfer none): the appropriate #NMActiveConnection, if
 * any.
 */
NMActiveConnection *
nm_client_get_activating_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

1147 1148 1149
	if (!nm_client_get_nm_running (client))
		return NULL;

1150
	return nm_manager_get_activating_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
1151 1152 1153
}

static void
Thomas Haller's avatar
Thomas Haller committed
1154 1155 1156
activate_connection_cb (GObject *object,
                        GAsyncResult *result,
                        gpointer user_data)
1157
{
Thomas Haller's avatar
Thomas Haller committed
1158 1159 1160 1161
	NMClient *self;
	gs_unref_object GTask *task = user_data;
	gs_unref_variant GVariant *ret = NULL;
	const char *v_active_connection;
1162 1163
	GError *error = NULL;

Thomas Haller's avatar
Thomas Haller committed
1164 1165 1166 1167 1168 1169
	ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
	if (!ret) {
		g_dbus_error_strip_remote_error (error);
		g_task_return_error (task, error);
		return;
	}
1170

Thomas Haller's avatar
Thomas Haller committed
1171 1172 1173 1174 1175 1176 1177 1178 1179
	self = g_task_get_source_object (task);

	g_variant_get (ret, "(&o)", &v_active_connection);

	nm_manager_wait_for_active_connection (NM_CLIENT_GET_PRIVATE (self)->manager,
	                                       v_active_connection,
	                                       NULL,
	                                       NULL,
	                                       g_steal_pointer (&task));
1180 1181 1182 1183
}

/**
 * nm_client_activate_connection_async:
1184
 * @client: a #NMClient
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
 * @connection: (allow-none): an #NMConnection
 * @device: (allow-none): the #NMDevice
 * @specific_object: (allow-none): the object path of a connection-type-specific
 *   object this activation should use. This parameter is currently ignored for
 *   wired and mobile broadband connections, and the value of %NULL should be used
 *   (ie, no specific object).  For Wi-Fi or WiMAX connections, pass the object
 *   path of a #NMAccessPoint or #NMWimaxNsp owned by @device, which you can
 *   get using nm_object_get_path(), and which will be used to complete the
 *   details of the newly added connection.
 * @cancellable: a #GCancellable, or %NULL
 * @callback: callback to be called when the activation has started
 * @user_data: caller-specific data passed to @callback
1197
 *
1198 1199 1200 1201 1202 1203 1204 1205
 * Asynchronously starts a connection to a particular network using the
 * configuration settings from @connection and the network device @device.
 * Certain connection types also take a "specific object" which is the object
 * path of a connection- specific object, like an #NMAccessPoint for Wi-Fi
 * connections, or an #NMWimaxNsp for WiMAX connections, to which you wish to
 * connect.  If the specific object is not given, NetworkManager can, in some
 * cases, automatically determine which network to connect to given the settings
 * in @connection.
1206
 *