1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2007 - 2011 Novell, Inc.
19 * Copyright 2008 - 2014 Red Hat, Inc.
22 #include "nm-default.h"
26 #include "nm-ip-config.h"
27 #include "nm-ip4-config.h"
28 #include "nm-ip6-config.h"
29 #include "nm-setting-ip-config.h"
30 #include "nm-dbus-interface.h"
31 #include "nm-object-private.h"
33 #include "nm-core-internal.h"
35 G_DEFINE_ABSTRACT_TYPE (NMIPConfig, nm_ip_config, NM_TYPE_OBJECT)
37 #define NM_IP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP_CONFIG, NMIPConfigPrivate))
48 gboolean new_style_data;
66 nm_ip_config_init (NMIPConfig *config)
68 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (config);
70 priv->addresses = g_ptr_array_new ();
71 priv->routes = g_ptr_array_new ();
72 priv->nameservers = g_new0 (char *, 1);
73 priv->domains = g_new0 (char *, 1);
74 priv->searches = g_new0 (char *, 1);
75 priv->wins = g_new0 (char *, 1);
79 demarshal_ip_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
81 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
83 if (priv->new_style_data)
86 g_ptr_array_unref (priv->addresses);
87 if (NM_IS_IP4_CONFIG (object))
88 priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL);
90 priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL);
91 _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES);
97 demarshal_ip_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
99 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
101 priv->new_style_data = TRUE;
103 g_ptr_array_unref (priv->addresses);
104 if (NM_IS_IP4_CONFIG (object))
105 priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET);
107 priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET6);
108 _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES);
114 demarshal_ip_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
120 g_strfreev (*obj_field);
122 if (NM_IS_IP4_CONFIG (object))
123 *obj_field = nm_utils_ip4_dns_from_variant (value);
125 *obj_field = nm_utils_ip6_dns_from_variant (value);
127 _nm_object_queue_notify (object, pspec->name);
132 demarshal_ip_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
134 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
136 if (priv->new_style_data)
139 g_ptr_array_unref (priv->routes);
140 if (NM_IS_IP4_CONFIG (object))
141 priv->routes = nm_utils_ip4_routes_from_variant (value);
143 priv->routes = nm_utils_ip6_routes_from_variant (value);
144 _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES);
150 demarshal_ip_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
152 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
154 priv->new_style_data = TRUE;
156 g_ptr_array_unref (priv->routes);
157 if (NM_IS_IP4_CONFIG (object))
158 priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET);
160 priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET6);
161 _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES);
167 init_dbus (NMObject *object)
169 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
170 const NMPropertiesInfo property_info[] = {
171 { NM_IP_CONFIG_GATEWAY, &priv->gateway, },
172 { NM_IP_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip_addresses },
173 { "address-data", &priv->addresses, demarshal_ip_address_data },
174 { NM_IP_CONFIG_ROUTES, &priv->routes, demarshal_ip_routes },
175 { "route-data", &priv->routes, demarshal_ip_route_data },
176 { NM_IP_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip_array },
177 { NM_IP_CONFIG_DOMAINS, &priv->domains, },
178 { NM_IP_CONFIG_SEARCHES, &priv->searches, },
179 { NM_IP_CONFIG_WINS_SERVERS, &priv->wins, demarshal_ip_array },
183 NM_OBJECT_CLASS (nm_ip_config_parent_class)->init_dbus (object);
185 _nm_object_register_properties (object,
186 (NM_IS_IP4_CONFIG (object) ?
187 NM_DBUS_INTERFACE_IP4_CONFIG :
188 NM_DBUS_INTERFACE_IP6_CONFIG),
193 finalize (GObject *object)
195 NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
197 g_free (priv->gateway);
199 g_ptr_array_unref (priv->addresses);
200 g_ptr_array_unref (priv->routes);
202 g_strfreev (priv->nameservers);
203 g_strfreev (priv->domains);
204 g_strfreev (priv->searches);
205 g_strfreev (priv->wins);
207 G_OBJECT_CLASS (nm_ip_config_parent_class)->finalize (object);
211 get_property (GObject *object,
216 NMIPConfig *self = NM_IP_CONFIG (object);
220 g_value_set_int (value, nm_ip_config_get_family (self));
223 g_value_set_string (value, nm_ip_config_get_gateway (self));
226 g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_addresses (self),
227 (NMUtilsCopyFunc) nm_ip_address_dup,
228 (GDestroyNotify) nm_ip_address_unref));
231 g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_routes (self),
232 (NMUtilsCopyFunc) nm_ip_route_dup,
233 (GDestroyNotify) nm_ip_route_unref));
235 case PROP_NAMESERVERS:
236 g_value_set_boxed (value, (char **) nm_ip_config_get_nameservers (self));
239 g_value_set_boxed (value, (char **) nm_ip_config_get_domains (self));
242 g_value_set_boxed (value, (char **) nm_ip_config_get_searches (self));
244 case PROP_WINS_SERVERS:
245 g_value_set_boxed (value, (char **) nm_ip_config_get_wins_servers (self));
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 nm_ip_config_class_init (NMIPConfigClass *config_class)
256 GObjectClass *object_class = G_OBJECT_CLASS (config_class);
257 NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class);
259 g_type_class_add_private (config_class, sizeof (NMIPConfigPrivate));
261 /* virtual methods */
262 object_class->get_property = get_property;
263 object_class->finalize = finalize;
265 nm_object_class->init_dbus = init_dbus;
272 * The IP address family of the configuration; either
273 * <literal>AF_INET</literal> or <literal>AF_INET6</literal>.
275 g_object_class_install_property
276 (object_class, PROP_FAMILY,
277 g_param_spec_int (NM_IP_CONFIG_FAMILY, "", "",
280 G_PARAM_STATIC_STRINGS));
283 * NMIPConfig:gateway:
285 * The IP gateway address of the configuration as string.
287 g_object_class_install_property
288 (object_class, PROP_GATEWAY,
289 g_param_spec_string (NM_IP_CONFIG_GATEWAY, "", "",
292 G_PARAM_STATIC_STRINGS));
295 * NMIPConfig:addresses:
297 * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration.
299 g_object_class_install_property
300 (object_class, PROP_ADDRESSES,
301 g_param_spec_boxed (NM_IP_CONFIG_ADDRESSES, "", "",
304 G_PARAM_STATIC_STRINGS));
309 * A #GPtrArray containing the routes (#NMIPRoute) of the configuration.
311 g_object_class_install_property
312 (object_class, PROP_ROUTES,
313 g_param_spec_boxed (NM_IP_CONFIG_ROUTES, "", "",
316 G_PARAM_STATIC_STRINGS));
319 * NMIPConfig:nameservers:
321 * The array containing name server IP addresses of the configuration.
323 g_object_class_install_property
324 (object_class, PROP_NAMESERVERS,
325 g_param_spec_boxed (NM_IP_CONFIG_NAMESERVERS, "", "",
328 G_PARAM_STATIC_STRINGS));
331 * NMIPConfig:domains:
333 * The array containing domain strings of the configuration.
335 g_object_class_install_property
336 (object_class, PROP_DOMAINS,
337 g_param_spec_boxed (NM_IP_CONFIG_DOMAINS, "", "",
340 G_PARAM_STATIC_STRINGS));
343 * NMIPConfig:searches:
345 * The array containing DNS search strings of the configuration.
347 g_object_class_install_property
348 (object_class, PROP_SEARCHES,
349 g_param_spec_boxed (NM_IP_CONFIG_SEARCHES, "", "",
352 G_PARAM_STATIC_STRINGS));
355 * NMIPConfig:wins-servers:
357 * The array containing WINS server IP addresses of the configuration.
358 * (This will always be empty for IPv6 configurations.)
360 g_object_class_install_property
361 (object_class, PROP_WINS_SERVERS,
362 g_param_spec_boxed (NM_IP_CONFIG_WINS_SERVERS, "", "",
365 G_PARAM_STATIC_STRINGS));
369 * nm_ip_config_get_family:
370 * @config: a #NMIPConfig
372 * Gets the IP address family
374 * Returns: the IP address family; either <literal>AF_INET</literal> or
375 * <literal>AF_INET6</literal>
378 nm_ip_config_get_family (NMIPConfig *config)
380 g_return_val_if_fail (NM_IS_IP_CONFIG (config), AF_UNSPEC);
382 return NM_IS_IP4_CONFIG (config) ? AF_INET : AF_INET6;
386 * nm_ip_config_get_gateway:
387 * @config: a #NMIPConfig
389 * Gets the IP gateway address.
391 * Returns: (transfer none): the IP address of the gateway.
394 nm_ip_config_get_gateway (NMIPConfig *config)
396 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
398 return NM_IP_CONFIG_GET_PRIVATE (config)->gateway;
402 * nm_ip_config_get_addresses:
403 * @config: a #NMIPConfig
405 * Gets the IP addresses (containing the address, prefix, and gateway).
407 * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray
408 * containing #NMIPAddress<!-- -->es. This is the internal copy used by the
409 * configuration and must not be modified. The library never modifies the
410 * returned array and thus it is safe for callers to reference and keep using it.
413 nm_ip_config_get_addresses (NMIPConfig *config)
415 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
417 return NM_IP_CONFIG_GET_PRIVATE (config)->addresses;
421 * nm_ip_config_get_nameservers:
422 * @config: a #NMIPConfig
424 * Gets the domain name servers (DNS).
426 * Returns: (transfer none): the array of nameserver IP addresses
429 nm_ip_config_get_nameservers (NMIPConfig *config)
431 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
433 return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->nameservers;
437 * nm_ip_config_get_domains:
438 * @config: a #NMIPConfig
440 * Gets the domain names.
442 * Returns: (transfer none): the array of domains.
443 * (This is never %NULL, though it may be 0-length).
446 nm_ip_config_get_domains (NMIPConfig *config)
448 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
450 return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->domains;
454 * nm_ip_config_get_searches:
455 * @config: a #NMIPConfig
457 * Gets the DNS searches.
459 * Returns: (transfer none): the array of DNS search strings.
460 * (This is never %NULL, though it may be 0-length).
463 nm_ip_config_get_searches (NMIPConfig *config)
465 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
467 return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->searches;
471 * nm_ip_config_get_wins_servers:
472 * @config: a #NMIPConfig
474 * Gets the Windows Internet Name Service servers (WINS).
476 * Returns: (transfer none): the arry of WINS server IP address strings.
477 * (This is never %NULL, though it may be 0-length.)
480 nm_ip_config_get_wins_servers (NMIPConfig *config)
482 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
484 return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->wins;
488 * nm_ip_config_get_routes:
489 * @config: a #NMIPConfig
493 * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing
494 * #NMIPRoutes. This is the internal copy used by the configuration, and must
495 * not be modified. The library never modifies the returned array and thus it is
496 * safe for callers to reference and keep using it.
500 nm_ip_config_get_routes (NMIPConfig *config)
502 g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
504 return NM_IP_CONFIG_GET_PRIVATE (config)->routes;