nm-ip6-config.c 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * libnm_glib -- Access network status & information from glib applications
 *
 * 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 (C) 2007 - 2008 Novell, Inc.
21
 * Copyright (C) 2008 - 2011 Red Hat, Inc.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

#include <string.h>

#include <nm-setting-ip6-config.h>
#include "nm-ip6-config.h"
#include "NetworkManager.h"
#include "nm-types-private.h"
#include "nm-object-private.h"
#include "nm-utils.h"

G_DEFINE_TYPE (NMIP6Config, nm_ip6_config, NM_TYPE_OBJECT)

#define NM_IP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP6_CONFIG, NMIP6ConfigPrivate))

typedef struct {
	DBusGProxy *proxy;

40
	char *gateway;
41
	GSList *addresses;
42
	GSList *routes;
43 44
	GSList *nameservers;
	GPtrArray *domains;
45
	GPtrArray *searches;
46 47 48 49
} NMIP6ConfigPrivate;

enum {
	PROP_0,
50
	PROP_GATEWAY,
51
	PROP_ADDRESSES,
52
	PROP_ROUTES,
53 54
	PROP_NAMESERVERS,
	PROP_DOMAINS,
55
	PROP_SEARCHES,
56 57 58 59 60 61 62 63 64 65 66

	LAST_PROP
};

/**
 * nm_ip6_config_new:
 * @connection: the #DBusGConnection
 * @object_path: the DBus object path of the device
 *
 * Creates a new #NMIP6Config.
 *
Dan Williams's avatar
Dan Williams committed
67
 * Returns: (transfer full): a new IP6 configuration
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 **/
GObject *
nm_ip6_config_new (DBusGConnection *connection, const char *object_path)
{
	return (GObject *) g_object_new (NM_TYPE_IP6_CONFIG,
									 NM_OBJECT_DBUS_CONNECTION, connection,
									 NM_OBJECT_DBUS_PATH, object_path,
									 NULL);
}

static gboolean
demarshal_ip6_address_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);

	g_slist_foreach (priv->addresses, (GFunc) nm_ip6_address_unref, NULL);
	g_slist_free (priv->addresses);
	priv->addresses = NULL;

	priv->addresses = nm_utils_ip6_addresses_from_gvalue (value);
	_nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES);

	return TRUE;
}

static gboolean
demarshal_ip6_nameserver_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	if (!_nm_ip6_address_array_demarshal (value, (GSList **) field))
		return FALSE;

99
	if (pspec && !strcmp (pspec->name, NM_IP6_CONFIG_NAMESERVERS))
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		_nm_object_queue_notify (object, NM_IP6_CONFIG_NAMESERVERS);

	return TRUE;
}

static gboolean
demarshal_domains (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	if (!_nm_string_array_demarshal (value, (GPtrArray **) field))
		return FALSE;

	_nm_object_queue_notify (object, NM_IP6_CONFIG_DOMAINS);
	return TRUE;
}

115 116 117 118 119 120 121 122 123 124
static gboolean
demarshal_searches (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	if (!_nm_string_array_demarshal (value, (GPtrArray **) field))
		return FALSE;

	_nm_object_queue_notify (object, NM_IP6_CONFIG_SEARCHES);
	return TRUE;
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static gboolean
demarshal_ip6_routes_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);

	g_slist_foreach (priv->routes, (GFunc) nm_ip6_route_unref, NULL);
	g_slist_free (priv->routes);
	priv->routes = NULL;

	priv->routes = nm_utils_ip6_routes_from_gvalue (value);
	_nm_object_queue_notify (object, NM_IP6_CONFIG_ROUTES);

	return TRUE;
}

static void
141
register_properties (NMIP6Config *config)
142 143
{
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config);
144
	const NMPropertiesInfo property_info[] = {
145
		{ NM_IP6_CONFIG_GATEWAY,      &priv->gateway, },
146
		{ NM_IP6_CONFIG_ADDRESSES,    &priv->addresses, demarshal_ip6_address_array },
147
		{ NM_IP6_CONFIG_ROUTES,       &priv->routes, demarshal_ip6_routes_array },
148 149
		{ NM_IP6_CONFIG_NAMESERVERS,  &priv->nameservers, demarshal_ip6_nameserver_array },
		{ NM_IP6_CONFIG_DOMAINS,      &priv->domains, demarshal_domains },
150
		{ NM_IP6_CONFIG_SEARCHES,     &priv->searches, demarshal_searches },
151 152 153
		{ NULL },
	};

154 155 156
	_nm_object_register_properties (NM_OBJECT (config),
	                                priv->proxy,
	                                property_info);
157 158
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/**
 * nm_ip6_config_get_gateway:
 * @config: a #NMIP6Config
 *
 * Gets the IP6 gateway.
 *
 * Returns: the IPv6 gateway of the configuration.
 *
 * Since: 0.9.10
 **/
const char *
nm_ip6_config_get_gateway (NMIP6Config *config)
{
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);

	_nm_object_ensure_inited (NM_OBJECT (config));
	return NM_IP6_CONFIG_GET_PRIVATE (config)->gateway;
}

178 179 180 181 182 183
/**
 * nm_ip6_config_get_addresses:
 * @config: a #NMIP6Config
 *
 * Gets the IP6 addresses (containing the address, prefix, and gateway).
 *
184 185 186
 * Returns: (element-type NetworkManager.IP6Address): the #GSList containing
 * #NMIP6Address<!-- -->es. This is the internal copy used by the configuration
 * and must not be modified.
187 188 189 190
 **/
const GSList *
nm_ip6_config_get_addresses (NMIP6Config *config)
{
191
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);
192

193 194
	_nm_object_ensure_inited (NM_OBJECT (config));
	return NM_IP6_CONFIG_GET_PRIVATE (config)->addresses;
195 196
}

197
/* FIXME: like in libnm_util, in6_addr is not introspectable, so skipping here */
198
/**
199
 * nm_ip6_config_get_nameservers: (skip)
200 201 202 203
 * @config: a #NMIP6Config
 *
 * Gets the domain name servers (DNS).
 *
204 205 206
 * Returns: a #GSList containing elements of type 'struct in6_addr' which
 * contain the addresses of nameservers of the configuration.  This is the
 * internal copy used by the configuration and must not be modified.
207 208 209 210 211 212
 **/
const GSList *
nm_ip6_config_get_nameservers (NMIP6Config *config)
{
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);

213 214
	_nm_object_ensure_inited (NM_OBJECT (config));
	return NM_IP6_CONFIG_GET_PRIVATE (config)->nameservers;
215 216 217 218 219 220 221 222
}

/**
 * nm_ip6_config_get_domains:
 * @config: a #NMIP6Config
 *
 * Gets the domain names.
 *
223 224
 * Returns: (element-type utf8): the #GPtrArray containing domains as strings.
 * This is the internal copy used by the configuration, and must not be modified.
225 226 227 228 229 230
 **/
const GPtrArray *
nm_ip6_config_get_domains (NMIP6Config *config)
{
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);

231 232
	_nm_object_ensure_inited (NM_OBJECT (config));
	return handle_ptr_array_return (NM_IP6_CONFIG_GET_PRIVATE (config)->domains);
233 234
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/**
 * nm_ip6_config_get_searches:
 * @config: a #NMIP6Config
 *
 * Gets the dns searches.
 *
 * Returns: (element-type utf8): the #GPtrArray containing dns searches as strings.
 * This is the internal copy used by the configuration, and must not be modified.
 *
 * Since: 0.9.10
 **/
const GPtrArray *
nm_ip6_config_get_searches (NMIP6Config *config)
{
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);

	_nm_object_ensure_inited (NM_OBJECT (config));
	return handle_ptr_array_return (NM_IP6_CONFIG_GET_PRIVATE (config)->searches);
}

255 256 257 258 259 260
/**
 * nm_ip6_config_get_routes:
 * @config: a #NMIP6Config
 *
 * Gets the routes.
 *
261 262 263
 * Returns: (element-type NetworkManager.IP6Route): the #GSList containing
 * #NMIP6Route<!-- -->s. This is the internal copy used by the configuration,
 * and must not be modified.
264 265 266 267
 **/
const GSList *
nm_ip6_config_get_routes (NMIP6Config *config)
{
268
	g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL);
269

270 271
	_nm_object_ensure_inited (NM_OBJECT (config));
	return NM_IP6_CONFIG_GET_PRIVATE (config)->routes;
272 273
}

274 275
static void
constructed (GObject *object)
276
{
277
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);
278

279
	G_OBJECT_CLASS (nm_ip6_config_parent_class)->constructed (object);
280

281
	priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_IP6_CONFIG);
282
	register_properties (NM_IP6_CONFIG (object));
283 284 285 286 287 288 289
}

static void
finalize (GObject *object)
{
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);

290 291
	g_free (priv->gateway);

292 293 294 295 296 297 298 299 300 301 302 303 304 305
	g_slist_foreach (priv->addresses, (GFunc) nm_ip6_address_unref, NULL);
	g_slist_free (priv->addresses);

	g_slist_foreach (priv->routes, (GFunc) nm_ip6_route_unref, NULL);
	g_slist_free (priv->routes);

	g_slist_foreach (priv->nameservers, (GFunc) g_free, NULL);
	g_slist_free (priv->nameservers);

	if (priv->domains) {
		g_ptr_array_foreach (priv->domains, (GFunc) g_free, NULL);
		g_ptr_array_free (priv->domains, TRUE);
	}

306 307 308 309 310
	if (priv->searches) {
		g_ptr_array_foreach (priv->searches, (GFunc) g_free, NULL);
		g_ptr_array_free (priv->searches, TRUE);
	}

311 312 313 314 315 316 317 318 319 320 321 322 323 324
	g_object_unref (priv->proxy);

	G_OBJECT_CLASS (nm_ip6_config_parent_class)->finalize (object);
}

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
	NMIP6Config *self = NM_IP6_CONFIG (object);
	NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (self);

325 326
	_nm_object_ensure_inited (NM_OBJECT (object));

327
	switch (prop_id) {
328 329 330
	case PROP_GATEWAY:
		g_value_set_string (value, nm_ip6_config_get_gateway (self));
		break;
331 332 333
	case PROP_ADDRESSES:
		nm_utils_ip6_addresses_to_gvalue (priv->addresses, value);
		break;
334 335 336
	case PROP_ROUTES:
		nm_utils_ip6_routes_to_gvalue (priv->routes, value);
		break;
337 338 339 340 341 342
	case PROP_NAMESERVERS:
		g_value_set_boxed (value, nm_ip6_config_get_nameservers (self));
		break;
	case PROP_DOMAINS:
		g_value_set_boxed (value, nm_ip6_config_get_domains (self));
		break;
343 344
	case PROP_SEARCHES:
		g_value_set_boxed (value, nm_ip6_config_get_searches (self));
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_ip6_config_init (NMIP6Config *config)
{
}

static void
nm_ip6_config_class_init (NMIP6ConfigClass *config_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (config_class);

	g_type_class_add_private (config_class, sizeof (NMIP6ConfigPrivate));

	/* virtual methods */
365
	object_class->constructed = constructed;
366 367 368 369 370
	object_class->get_property = get_property;
	object_class->finalize = finalize;

	/* properties */

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	/**
	 * NMIP6Config:gateway:
	 *
	 * The IPv6 gateway as string
	 *
	 * Since: 0.9.10
	 **/
	g_object_class_install_property
	    (object_class, PROP_ROUTES,
	     g_param_spec_string (NM_IP6_CONFIG_ROUTES,
	                          "Gateway",
	                          "Gateway",
	                          NULL,
	                          G_PARAM_READABLE));

386 387 388 389 390 391 392 393
	/**
	 * NMIP6Config:addresses:
	 *
	 * The #GPtrArray containing the IPv6 addresses;  use
	 * nm_utils_ip6_addresses_from_gvalue() to return a #GSList of
	 * #NMSettingIP6Address objects that is more usable than the raw data.
	 **/
	g_object_class_install_property
Thomas Haller's avatar
Thomas Haller committed
394 395 396 397 398 399
	    (object_class, PROP_ADDRESSES,
	     g_param_spec_boxed (NM_IP6_CONFIG_ADDRESSES,
	                         "Addresses",
	                         "Addresses",
	                         NM_TYPE_IP6_ADDRESS_OBJECT_ARRAY,
	                         G_PARAM_READABLE));
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	/**
	 * NMIP6Config:routes:
	 *
	 * The #GPtrArray containing the IPv6 routes;  use
	 * nm_utils_ip6_routes_from_gvalue() to return a #GSList of
	 * #NMSettingIP6Address objects that is more usable than the raw data.
	 **/
	g_object_class_install_property
	    (object_class, PROP_ROUTES,
	     g_param_spec_boxed (NM_IP6_CONFIG_ROUTES,
	                         "Routes",
	                         "Routes",
	                         NM_TYPE_IP6_ROUTE_OBJECT_ARRAY,
	                         G_PARAM_READABLE));

416 417 418 419 420 421 422
	/**
	 * NMIP6Config:nameservers:
	 *
	 * The #GPtrArray containing elements of type 'struct ip6_addr' which
	 * contain the addresses of nameservers of the configuration.
	 **/
	g_object_class_install_property
Thomas Haller's avatar
Thomas Haller committed
423 424 425 426 427 428
	    (object_class, PROP_NAMESERVERS,
	     g_param_spec_boxed (NM_IP6_CONFIG_NAMESERVERS,
	                        "Nameservers",
	                        "Nameservers",
	                        NM_TYPE_IP6_ADDRESS_ARRAY,
	                        G_PARAM_READABLE));
429 430 431 432 433 434 435

	/**
	 * NMIP6Config:domains:
	 *
	 * The #GPtrArray containing domain strings of the configuration.
	 **/
	g_object_class_install_property
Thomas Haller's avatar
Thomas Haller committed
436 437 438 439 440 441
	    (object_class, PROP_DOMAINS,
	     g_param_spec_boxed (NM_IP6_CONFIG_DOMAINS,
	                        "Domains",
	                        "Domains",
	                        NM_TYPE_STRING_ARRAY,
	                        G_PARAM_READABLE));
442 443

	/**
444
	 * NMIP6Config:searches:
445
	 *
446 447 448
	 * The #GPtrArray containing dns search strings of the configuration.
	 *
	 * Since: 0.9.10
449 450
	 **/
	g_object_class_install_property
451 452 453 454 455 456 457
	    (object_class, PROP_SEARCHES,
	     g_param_spec_boxed (NM_IP6_CONFIG_SEARCHES,
	                        "Searches",
	                        "DNS Searches",
	                        NM_TYPE_STRING_ARRAY,
	                        G_PARAM_READABLE));

458 459
}