nm-client.c 127 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * 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 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2007 - 2008 Novell, Inc.
19
 * Copyright 2007 - 2018 Red Hat, Inc.
20 21
 */

22
#include "nm-default.h"
23

24
#include <string.h>
25
#include <libudev.h>
26

27
#include "nm-utils.h"
28
#include "nm-client.h"
29
#include "nm-manager.h"
30
#include "nm-dns-manager.h"
31
#include "nm-remote-settings.h"
32 33
#include "nm-device-ethernet.h"
#include "nm-device-wifi.h"
34
#include "nm-core-internal.h"
35 36
#include "nm-active-connection.h"
#include "nm-vpn-connection.h"
37
#include "nm-remote-connection.h"
Dan Winship's avatar
Dan Winship committed
38
#include "nm-dbus-helpers.h"
39 40 41
#include "nm-wimax-nsp.h"
#include "nm-object-private.h"

42 43 44
#include "introspection/org.freedesktop.NetworkManager.h"
#include "introspection/org.freedesktop.NetworkManager.Device.Wireless.h"
#include "introspection/org.freedesktop.NetworkManager.Device.h"
45
#include "introspection/org.freedesktop.NetworkManager.DnsManager.h"
46 47 48
#include "introspection/org.freedesktop.NetworkManager.Settings.h"
#include "introspection/org.freedesktop.NetworkManager.Settings.Connection.h"
#include "introspection/org.freedesktop.NetworkManager.VPN.Connection.h"
49
#include "introspection/org.freedesktop.NetworkManager.Connection.Active.h"
50 51 52

#include "nm-access-point.h"
#include "nm-active-connection.h"
53
#include "nm-checkpoint.h"
54
#include "nm-device-6lowpan.h"
55 56 57 58
#include "nm-device-adsl.h"
#include "nm-device-bond.h"
#include "nm-device-bridge.h"
#include "nm-device-bt.h"
59
#include "nm-device-dummy.h"
60 61 62 63
#include "nm-device-ethernet.h"
#include "nm-device-generic.h"
#include "nm-device-infiniband.h"
#include "nm-device-ip-tunnel.h"
64
#include "nm-device-macsec.h"
65 66 67
#include "nm-device-macvlan.h"
#include "nm-device-modem.h"
#include "nm-device-olpc-mesh.h"
68
#include "nm-device-ovs-interface.h"
69
#include "nm-device-ovs-port.h"
70
#include "nm-device-ovs-bridge.h"
Beniamino Galvani's avatar
Beniamino Galvani committed
71
#include "nm-device-ppp.h"
72 73 74 75 76 77
#include "nm-device-team.h"
#include "nm-device-tun.h"
#include "nm-device-vlan.h"
#include "nm-device-vxlan.h"
#include "nm-device-wifi.h"
#include "nm-device-wimax.h"
78
#include "nm-device-wireguard.h"
79
#include "nm-device-wpan.h"
80 81 82 83 84 85 86 87 88
#include "nm-dhcp4-config.h"
#include "nm-dhcp6-config.h"
#include "nm-dhcp-config.h"
#include "nm-ip4-config.h"
#include "nm-ip6-config.h"
#include "nm-manager.h"
#include "nm-remote-connection.h"
#include "nm-remote-settings.h"
#include "nm-vpn-connection.h"
Dan Winship's avatar
Dan Winship committed
89

90 91 92 93 94
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);

95
G_DEFINE_TYPE_WITH_CODE (NMClient, nm_client, G_TYPE_OBJECT,
96 97 98 99 100 101
                         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
102 103 104 105 106 107 108
typedef struct {
	NMClient *client;
	GCancellable *cancellable;
	GSimpleAsyncResult *result;
	int pending_init;
} NMClientInitData;

109
typedef struct {
110
	NMManager *manager;
111
	NMRemoteSettings *settings;
112
	NMDnsManager *dns_manager;
113 114
	GDBusObjectManager *object_manager;
	GCancellable *new_object_manager_cancellable;
115
	struct udev *udev;
116
	bool udev_inited:1;
117 118 119 120 121 122 123
} NMClientPrivate;

enum {
	PROP_0,
	PROP_VERSION,
	PROP_STATE,
	PROP_STARTUP,
124
	PROP_NM_RUNNING,
125 126 127 128 129 130 131 132 133
	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,
134 135
	PROP_CONNECTIVITY_CHECK_AVAILABLE,
	PROP_CONNECTIVITY_CHECK_ENABLED,
136 137 138
	PROP_PRIMARY_CONNECTION,
	PROP_ACTIVATING_CONNECTION,
	PROP_DEVICES,
139
	PROP_ALL_DEVICES,
140 141 142
	PROP_CONNECTIONS,
	PROP_HOSTNAME,
	PROP_CAN_MODIFY,
143
	PROP_METERED,
144 145 146
	PROP_DNS_MODE,
	PROP_DNS_RC_MANAGER,
	PROP_DNS_CONFIGURATION,
147
	PROP_CHECKPOINTS,
148 149 150 151 152 153 154

	LAST_PROP
};

enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
155 156
	ANY_DEVICE_ADDED,
	ANY_DEVICE_REMOVED,
157
	PERMISSION_CHANGED,
158 159
	CONNECTION_ADDED,
	CONNECTION_REMOVED,
160 161
	ACTIVE_CONNECTION_ADDED,
	ACTIVE_CONNECTION_REMOVED,
162 163 164 165 166 167

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

168 169
static const GPtrArray empty = { 0, };

170
/*****************************************************************************/
171 172 173 174 175 176 177 178

/**
 * nm_client_error_quark:
 *
 * Registers an error quark for #NMClient if necessary.
 *
 * Returns: the error quark used for #NMClient errors.
 **/
179
NM_CACHED_QUARK_FCN ("nm-client-error-quark", nm_client_error_quark)
180

181
/*****************************************************************************/
182 183 184 185 186 187

static void
nm_client_init (NMClient *client)
{
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201
static gboolean
_nm_client_check_nm_running (NMClient *client, GError **error)
{
	if (nm_client_get_nm_running (client))
		return TRUE;
	else {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
		return FALSE;
	}
}

202
/**
203
 * nm_client_get_version:
204 205
 * @client: a #NMClient
 *
206
 * Gets NetworkManager version.
207
 *
208
 * Returns: string with the version (or %NULL if NetworkManager is not running)
209
 **/
210 211
const char *
nm_client_get_version (NMClient *client)
212 213 214
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

215 216 217
	if (!nm_client_get_nm_running (client))
		return NULL;

218
	return nm_manager_get_version (NM_CLIENT_GET_PRIVATE (client)->manager);
219 220 221
}

/**
222
 * nm_client_get_state:
223 224
 * @client: a #NMClient
 *
225
 * Gets the current daemon state.
226
 *
227
 * Returns: the current %NMState
228
 **/
229 230
NMState
nm_client_get_state (NMClient *client)
231
{
232
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
233

234 235 236
	if (!nm_client_get_nm_running (client))
		return NM_STATE_UNKNOWN;

237
	return nm_manager_get_state (NM_CLIENT_GET_PRIVATE (client)->manager);
238 239 240
}

/**
241
 * nm_client_get_startup:
242 243
 * @client: a #NMClient
 *
244 245
 * Tests whether the daemon is still in the process of activating
 * connections at startup.
246
 *
247
 * Returns: whether the daemon is still starting up
248
 **/
249 250
gboolean
nm_client_get_startup (NMClient *client)
251
{
252 253 254 255
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

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

257
	return nm_manager_get_startup (NM_CLIENT_GET_PRIVATE (client)->manager);
258 259
}

260 261 262 263 264 265 266 267 268 269
/**
 * 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)
270
{
271
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
272

273
	return NM_CLIENT_GET_PRIVATE (client)->manager != NULL;
274 275
}

276 277 278 279 280 281 282 283 284 285
/**
 * 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)
286
{
287
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
288

289 290 291
	if (!nm_client_get_nm_running (client))
		return FALSE;

292
	return nm_manager_networking_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308
/**
 * 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
 **/
gboolean
nm_client_networking_set_enabled (NMClient *client, gboolean enable, GError **error)
309
{
310
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
311

312
	if (!_nm_client_check_nm_running (client, error))
313
		return FALSE;
314

315 316
	return nm_manager_networking_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                          enable, error);
317 318
}

319 320 321 322 323 324 325 326 327 328
/**
 * 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)
329
{
330
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
331

332 333 334
	if (!nm_client_get_nm_running (client))
		return FALSE;

335
	return nm_manager_wireless_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
336 337 338
}

/**
339
 * nm_client_wireless_set_enabled:
340
 * @client: a #NMClient
341
 * @enabled: %TRUE to enable wireless
342
 *
343
 * Enables or disables wireless devices.
344 345
 **/
void
346
nm_client_wireless_set_enabled (NMClient *client, gboolean enabled)
347 348 349
{
	g_return_if_fail (NM_IS_CLIENT (client));

350
	if (!nm_client_get_nm_running (client))
351
		return;
352

353
	nm_manager_wireless_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
354 355
}

356
/**
357 358
 * nm_client_wireless_hardware_get_enabled:
 * @client: a #NMClient
359
 *
360
 * Determines whether the wireless hardware is enabled.
361
 *
362
 * Returns: %TRUE if the wireless hardware is enabled
363
 **/
364 365
gboolean
nm_client_wireless_hardware_get_enabled (NMClient *client)
366
{
367
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
368

369 370 371
	if (!nm_client_get_nm_running (client))
		return FALSE;

372
	return nm_manager_wireless_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
373 374
}

375 376 377 378 379 380 381 382 383 384
/**
 * 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)
385
{
386
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
387

388 389 390
	if (!nm_client_get_nm_running (client))
		return FALSE;

391
	return nm_manager_wwan_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
392 393 394
}

/**
395
 * nm_client_wwan_set_enabled:
396
 * @client: a #NMClient
397
 * @enabled: %TRUE to enable WWAN
398
 *
399
 * Enables or disables WWAN devices.
400 401
 **/
void
402
nm_client_wwan_set_enabled (NMClient *client, gboolean enabled)
403 404
{
	g_return_if_fail (NM_IS_CLIENT (client));
405

406
	if (!_nm_client_check_nm_running (client, NULL))
407
		return;
408

409
	nm_manager_wwan_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
410
}
411

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

425 426 427
	if (!nm_client_get_nm_running (client))
		return FALSE;

428
	return nm_manager_wwan_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
429 430 431
}

/**
432
 * nm_client_wimax_get_enabled:
433 434
 * @client: a #NMClient
 *
435
 * Determines whether WiMAX is enabled.
436
 *
437
 * Returns: %TRUE if WiMAX is enabled
438
 **/
439
gboolean
440
nm_client_wimax_get_enabled (NMClient *client)
441
{
442
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
443

444 445 446
	if (!nm_client_get_nm_running (client))
		return FALSE;

447
	return nm_manager_wimax_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
448 449 450
}

/**
451
 * nm_client_wimax_set_enabled:
452
 * @client: a #NMClient
453
 * @enabled: %TRUE to enable WiMAX
454
 *
455
 * Enables or disables WiMAX devices.
456 457
 **/
void
458
nm_client_wimax_set_enabled (NMClient *client, gboolean enabled)
459 460 461
{
	g_return_if_fail (NM_IS_CLIENT (client));

462
	if (!nm_client_get_nm_running (client))
463 464
		return;

465
	nm_manager_wimax_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
466 467 468
}

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

481 482 483
	if (!nm_client_get_nm_running (client))
		return FALSE;

484
	return nm_manager_wimax_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
485 486
}

487 488 489 490 491 492 493 494 495
/**
 * 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.
496 497
 *
 * Since: 1.10
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
 */
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.
517 518
 *
 * Since: 1.10
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
 */
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.
539 540
 *
 * Since: 1.10
541 542 543 544 545 546 547 548 549 550 551 552
 */
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);
}

553
/**
554
 * nm_client_get_logging:
555
 * @client: a #NMClient
556 557 558 559
 * @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
560
 *
561
 * Gets NetworkManager current logging level and domains.
562
 *
563
 * Returns: %TRUE on success, %FALSE otherwise
564
 **/
565 566
gboolean
nm_client_get_logging (NMClient *client, char **level, char **domains, GError **error)
567
{
568 569 570 571
	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);
572

573
	if (!_nm_client_check_nm_running (client, error))
574
		return FALSE;
575

576 577
	return nm_manager_get_logging (NM_CLIENT_GET_PRIVATE (client)->manager,
	                               level, domains, error);
578 579 580
}

/**
581
 * nm_client_set_logging:
582
 * @client: a #NMClient
583 584 585 586
 * @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
587
 *
588
 * Sets NetworkManager logging level and/or domains.
589
 *
590
 * Returns: %TRUE on success, %FALSE otherwise
591 592
 **/
gboolean
593
nm_client_set_logging (NMClient *client, const char *level, const char *domains, GError **error)
594 595
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
596
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
597

598
	if (!_nm_client_check_nm_running (client, error))
599
		return FALSE;
600

601 602
	return nm_manager_set_logging (NM_CLIENT_GET_PRIVATE (client)->manager,
	                               level, domains, error);
603 604 605
}

/**
606
 * nm_client_get_permission_result:
607
 * @client: a #NMClient
608
 * @permission: the permission for which to return the result, one of #NMClientPermission
609
 *
610 611
 * Requests the result of a specific permission, which indicates whether the
 * client can or cannot perform the action the permission represents
612
 *
613
 * Returns: the permission's result, one of #NMClientPermissionResult
614
 **/
615 616
NMClientPermissionResult
nm_client_get_permission_result (NMClient *client, NMClientPermission permission)
617
{
618 619
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CLIENT_PERMISSION_RESULT_UNKNOWN);

620 621 622
	if (!nm_client_get_nm_running (client))
		return NM_CLIENT_PERMISSION_RESULT_UNKNOWN;

623
	return nm_manager_get_permission_result (NM_CLIENT_GET_PRIVATE (client)->manager, permission);
624 625 626
}

/**
627 628
 * nm_client_get_connectivity:
 * @client: an #NMClient
629
 *
630 631 632 633
 * 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.
634
 *
635 636 637 638
 * Returns: the current connectivity state
 */
NMConnectivityState
nm_client_get_connectivity (NMClient *client)
639
{
640
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
641

642 643 644
	if (!nm_client_get_nm_running (client))
		return NM_CONNECTIVITY_UNKNOWN;

645
	return nm_manager_get_connectivity (NM_CLIENT_GET_PRIVATE (client)->manager);
646 647 648
}

/**
649 650 651 652
 * nm_client_check_connectivity:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @error: return location for a #GError
653
 *
654 655 656 657 658 659 660 661 662 663 664 665 666
 * 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
 */
NMConnectivityState
nm_client_check_connectivity (NMClient *client,
                              GCancellable *cancellable,
                              GError **error)
667
{
668
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
669

670 671 672
	if (!_nm_client_check_nm_running (client, error))
		return NM_CONNECTIVITY_UNKNOWN;

673 674
	return nm_manager_check_connectivity (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                      cancellable, error);
675 676 677 678 679 680 681 682
}

static void
check_connectivity_cb (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
683
	NMConnectivityState connectivity;
684 685
	GError *error = NULL;

686 687 688
	connectivity = nm_manager_check_connectivity_finish (NM_MANAGER (object),
	                                                     result, &error);
	if (!error)
689 690 691 692 693 694
		g_simple_async_result_set_op_res_gssize (simple, connectivity);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
695 696 697
}

/**
698 699 700 701 702
 * nm_client_check_connectivity_async:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @callback: callback to call with the result
 * @user_data: data for @callback.
703
 *
704 705 706 707 708 709 710 711 712 713 714 715
 * 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)
{
	GSimpleAsyncResult *simple;
716
	GError *error = NULL;
717 718

	g_return_if_fail (NM_IS_CLIENT (client));
719 720 721 722 723

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

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_check_connectivity_async);
727 728
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
729 730
	nm_manager_check_connectivity_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                     cancellable, check_connectivity_cb, simple);
731 732 733 734 735 736 737
}

/**
 * nm_client_check_connectivity_finish:
 * @client: an #NMClient
 * @result: the #GAsyncResult
 * @error: return location for a #GError
738
 *
739 740 741 742 743 744 745 746 747
 * 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)
748
{
749
	GSimpleAsyncResult *simple;
750

751 752
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NM_CONNECTIVITY_UNKNOWN);
753 754 755 756 757 758

	simple = G_SIMPLE_ASYNC_RESULT (result);

	if (g_simple_async_result_propagate_error (simple, error))
		return NM_CONNECTIVITY_UNKNOWN;
	return (NMConnectivityState) g_simple_async_result_get_op_res_gssize (simple);
759 760
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
/**
 * 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
 **/
gboolean
nm_client_save_hostname (NMClient *client,
                         const char *hostname,
                         GCancellable *cancellable,
                         GError **error)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

782 783 784
	if (!_nm_client_check_nm_running (client, error))
		return FALSE;

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
	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);
837 838
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	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);
}

871
/*****************************************************************************/
872
/* Devices                                                      */
873
/*****************************************************************************/
874

875
/**
876
 * nm_client_get_devices:
877 878
 * @client: a #NMClient
 *
879
 * Gets all the known network devices.  Use nm_device_get_type() or the
Dan Winship's avatar
Dan Winship committed
880 881 882
 * <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().
883
 *
884 885 886
 * 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.
887
 **/
888 889
const GPtrArray *
nm_client_get_devices (NMClient *client)
890
{
891
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
892

893 894 895
	if (!nm_client_get_nm_running (client))
		return &empty;

896
	return nm_manager_get_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
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
/**
 * 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);

924 925 926
	if (!nm_client_get_nm_running (client))
		return &empty;

927 928 929
	return nm_manager_get_all_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
}

930
/**
931
 * nm_client_get_device_by_path:
932
 * @client: a #NMClient
933
 * @object_path: the object path to search for
934
 *
935 936 937
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @object_path or %NULL if none is found.
938
 **/
939 940
NMDevice *
nm_client_get_device_by_path (NMClient *client, const char *object_path)
941
{
942 943
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (object_path, NULL);
944

945 946 947
	if (!nm_client_get_nm_running (client))
		return NULL;

948
	return nm_manager_get_device_by_path (NM_CLIENT_GET_PRIVATE (client)->manager, object_path);
949 950 951
}

/**
952
 * nm_client_get_device_by_iface:
953
 * @client: a #NMClient
954
 * @iface: the interface name to search for
955
 *
956
 * Gets a #NMDevice from a #NMClient.
957
 *
958
 * Returns: (transfer none): the #NMDevice for the given @iface or %NULL if none is found.
959
 **/
960 961
NMDevice *
nm_client_get_device_by_iface (NMClient *client, const char *iface)
962
{
963 964 965
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (iface, NULL);

966 967 968
	if (!nm_client_get_nm_running (client))
		return NULL;

969
	return nm_manager_get_device_by_iface (NM_CLIENT_GET_PRIVATE (client)->manager, iface);
970 971
}

972
/*****************************************************************************/
973
/* Active Connections                                           */
974
/*****************************************************************************/
975

976
/**
977
 * nm_client_get_active_connections:
978 979
 * @client: a #NMClient
 *
980
 * Gets the active connections.
981
 *
982 983 984
 * 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.
985
 **/
986 987
const GPtrArray *
nm_client_get_active_connections (NMClient *client)
988 989 990
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

991 992 993
	if (!nm_client_get_nm_running (client))
		return &empty;

994
	return nm_manager_get_active_connections (NM_CLIENT_GET_PRIVATE (client)->manager);
995 996 997
}

/**
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
 * 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);

1021 1022 1023
	if (!nm_client_get_nm_running (client))
		return NULL;

1024
	return nm_manager_get_primary_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

/**
 * 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);

1043 1044 1045
	if (!nm_client_get_nm_running (client))
		return NULL;

1046
	return nm_manager_get_activating_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
1047 1048 1049 1050 1051 1052 1053
}

static void
activate_cb (GObject *object,
             GAsyncResult *result,
             gpointer user_data)
{
1054 1055
	GSimpleAsyncResult *simple = user_data;
	NMActiveConnection *ac;
1056 1057
	GError *error = NULL;

1058 1059 1060 1061 1062 1063 1064 1065
	ac = nm_manager_activate_connection_finish (NM_MANAGER (object), result, &error);
	if (ac)
		g_simple_async_result_set_op_res_gpointer (simple, ac, g_object_unref);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
1066 1067 1068 1069
}

/**
 * nm_client_activate_connection_async:
1070
 * @client: a #NMClient
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
 * @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
1083
 *
1084 1085 1086 1087 1088 1089 1090 1091
 * 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.
1092
 *
1093 1094 1095 1096
 * If @connection is not given for a device-based activation, NetworkManager
 * picks the best available connection for the device and activates it.
 *
 * Note that the callback is invoked when NetworkManager has started activating
1097
 * the new connection, not when it finishes. You can use the returned
1098 1099
 * #NMActiveConnection object (in particular, #NMActiveConnection:state) to
 * track the activation to its completion.
1100
 **/
1101 1102 1103 1104 1105 1106 1107 1108
void
nm_client_activate_connection_async (NMClient *client,
                                     NMConnection *connection,
                                     NMDevice *device,
                                     const char *specific_object,
                                     GCancellable *cancellable,
                                     GAsyncReadyCallback callback,
                                     gpointer user_data)
1109
{
1110
	GSimpleAsyncResult *simple;
1111
	GError *error = NULL;
1112 1113 1114 1115 1116 1117 1118

	g_return_if_fail (NM_IS_CLIENT (client));
	if (device)
		g_return_if_fail (NM_IS_DEVICE (device));
	if (connection)
		g_return_if_fail (NM_IS_CONNECTION (connection));

1119 1120
	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
1121 1122 1123
		return;
	}

1124 1125
	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_activate_connection_async);
1126 1127
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
1128 1129 1130
	nm_manager_activate_connection_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                      connection, device, specific_object,
	                                      cancellable, activate_cb, simple);
1131 1132 1133
}

/**
1134 1135 1136 1137
 * nm_client_activate_connection_finish:
 * @client: an #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
1138
 *
1139
 * Gets the result of a call to nm_client_activate_connection_async().
1140
 *
1141 1142
 * Returns: (transfer full): the new #NMActiveConnection on success, %NULL on
 *   failure, in which case @error will be set.
1143
 **/
1144 1145 1146 1147
NMActiveConnection *
nm_client_activate_connection_finish (NMClient *client,
                                      GAsyncResult *result,
                                      GError **error)
1148
{
1149
	GSimpleAsyncResult *simple;
1150

1151 1152
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
1153 1154 1155 1156 1157 1158

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return NULL;
	else
		return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1159 1160
}

1161 1162 1163 1164
static void
add_activate_cb (GObject *object,
                 GAsyncResult *result,
                 gpointer user_data)
1165
{
1166 1167
	GSimpleAsyncResult *simple = user_data;
	NMActiveConnection *ac;
1168
	GError *error = NULL;
1169

1170 1171 1172 1173 1174 1175 1176 1177
	ac = nm_manager_add_and_activate_connection_finish (NM_MANAGER (object), result, &error);
	if (ac)
		g_simple_async_result_set_op_res_gpointer (simple, ac, g_object_unref);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
1178 1179 1180
}

/**