device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-ip4-config.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2014 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26 #include <dbus/dbus-glib.h>
27
28 #include "nm-setting-ip4-config.h"
29 #include "nm-param-spec-specialized.h"
30 #include "nm-utils.h"
31 #include "nm-dbus-glib-types.h"
32 #include "nm-setting-private.h"
33
34 /**
35  * SECTION:nm-setting-ip4-config
36  * @short_description: Describes IPv4 addressing, routing, and name service properties
37  * @include: nm-setting-ip4-config.h
38  *
39  * The #NMSettingIP4Config object is a #NMSetting subclass that describes
40  * properties related to IPv4 addressing, routing, and Domain Name Service
41  **/
42
43 /**
44  * nm_setting_ip4_config_error_quark:
45  *
46  * Registers an error quark for #NMSettingIP4Config if necessary.
47  *
48  * Returns: the error quark used for #NMSettingIP4Config errors.
49  **/
50 GQuark
51 nm_setting_ip4_config_error_quark (void)
52 {
53         static GQuark quark;
54
55         if (G_UNLIKELY (!quark))
56                 quark = g_quark_from_static_string ("nm-setting-ip4-config-error-quark");
57         return quark;
58 }
59
60 G_DEFINE_BOXED_TYPE (NMIP4Address, nm_ip4_address, nm_ip4_address_dup, nm_ip4_address_unref)
61 G_DEFINE_BOXED_TYPE (NMIP4Route, nm_ip4_route, nm_ip4_route_dup, nm_ip4_route_unref)
62
63 G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING,
64                          _nm_register_setting (NM_SETTING_IP4_CONFIG_SETTING_NAME,
65                                                g_define_type_id,
66                                                4,
67                                                NM_SETTING_IP4_CONFIG_ERROR))
68 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG)
69
70 #define NM_SETTING_IP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP4_CONFIG, NMSettingIP4ConfigPrivate))
71
72 typedef struct {
73         char *method;
74         GArray *dns;        /* array of guint32; elements in network byte order */
75         GSList *dns_search; /* list of strings */
76         GSList *addresses;  /* array of NMIP4Address */
77         GSList *routes;     /* array of NMIP4Route */
78         gint64  route_metric;
79         gboolean ignore_auto_routes;
80         gboolean ignore_auto_dns;
81         char *dhcp_client_id;
82         gboolean dhcp_send_hostname;
83         char *dhcp_hostname;
84         int dhcp_timeout;
85         gboolean never_default;
86         gboolean may_fail;
87 } NMSettingIP4ConfigPrivate;
88
89 enum {
90         PROP_0,
91         PROP_METHOD,
92         PROP_DNS,
93         PROP_DNS_SEARCH,
94         PROP_ADDRESSES,
95         PROP_ROUTES,
96         PROP_ROUTE_METRIC,
97         PROP_IGNORE_AUTO_ROUTES,
98         PROP_IGNORE_AUTO_DNS,
99         PROP_DHCP_CLIENT_ID,
100         PROP_DHCP_TIMEOUT,
101         PROP_DHCP_SEND_HOSTNAME,
102         PROP_DHCP_HOSTNAME,
103         PROP_NEVER_DEFAULT,
104         PROP_MAY_FAIL,
105
106         LAST_PROP
107 };
108
109 /**
110  * nm_setting_ip4_config_new:
111  *
112  * Creates a new #NMSettingIP4Config object with default values.
113  *
114  * Returns: (transfer full): the new empty #NMSettingIP4Config object
115  **/
116 NMSetting *
117 nm_setting_ip4_config_new (void)
118 {
119         return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP4_CONFIG, NULL);
120 }
121
122 /**
123  * nm_setting_ip4_config_get_method:
124  * @setting: the #NMSettingIP4Config
125  *
126  * Returns: the #NMSettingIP4Config:method property of the setting
127  **/
128 const char *
129 nm_setting_ip4_config_get_method (NMSettingIP4Config *setting)
130 {
131         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
132
133         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->method;
134 }
135
136 /**
137  * nm_setting_ip4_config_get_num_dns:
138  * @setting: the #NMSettingIP4Config
139  *
140  * Returns: the number of configured DNS servers
141  **/
142 guint32
143 nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting)
144 {
145         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
146
147         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns->len;
148 }
149
150 /**
151  * nm_setting_ip4_config_get_dns:
152  * @setting: the #NMSettingIP4Config
153  * @i: index number of the DNS server to return
154  *
155  * Returns: the IPv4 address (network byte order) of the DNS server at index
156  * @i
157  **/
158 guint32
159 nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i)
160 {
161         NMSettingIP4ConfigPrivate *priv;
162
163         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
164
165         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
166         g_return_val_if_fail (i <= priv->dns->len, 0);
167
168         return g_array_index (priv->dns, guint32, i);
169 }
170
171 /**
172  * nm_setting_ip4_config_add_dns:
173  * @setting: the #NMSettingIP4Config
174  * @dns: the IPv4 address (network byte order) of the DNS server to add
175  *
176  * Adds a new DNS server to the setting.
177  *
178  * Returns: %TRUE if the DNS server was added; %FALSE if the server was already
179  * known
180  **/
181 gboolean
182 nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, guint32 dns)
183 {
184         NMSettingIP4ConfigPrivate *priv;
185         int i;
186
187         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
188
189         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
190         for (i = 0; i < priv->dns->len; i++) {
191                 if (dns == g_array_index (priv->dns, guint32, i))
192                         return FALSE;
193         }
194
195         g_array_append_val (priv->dns, dns);
196         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS);
197         return TRUE;
198 }
199
200 /**
201  * nm_setting_ip4_config_remove_dns:
202  * @setting: the #NMSettingIP4Config
203  * @i: index number of the DNS server to remove
204  *
205  * Removes the DNS server at index @i.
206  **/
207 void
208 nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i)
209 {
210         NMSettingIP4ConfigPrivate *priv;
211
212         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
213
214         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
215         g_return_if_fail (i <= priv->dns->len);
216
217         g_array_remove_index (priv->dns, i);
218         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS);
219 }
220
221 /**
222  * nm_setting_ip4_config_remove_dns_by_value:
223  * @setting: the #NMSettingIP4Config
224  * @dns: the DNS server to remove
225  *
226  * Removes the DNS server @dns.
227  *
228  * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not.
229  * domain was already known
230  *
231  * Since: 0.9.10
232  **/
233 gboolean
234 nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, guint32 dns)
235 {
236         NMSettingIP4ConfigPrivate *priv;
237         int i;
238
239         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
240
241         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
242         for (i = 0; i < priv->dns->len; i++) {
243                 if (dns == g_array_index (priv->dns, guint32, i)) {
244                         g_array_remove_index (priv->dns, i);
245                         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS);
246                         return TRUE;
247                 }
248         }
249         return FALSE;
250 }
251
252 /**
253  * nm_setting_ip4_config_clear_dns:
254  * @setting: the #NMSettingIP4Config
255  *
256  * Removes all configured DNS servers.
257  **/
258 void
259 nm_setting_ip4_config_clear_dns (NMSettingIP4Config *setting)
260 {
261         NMSettingIP4ConfigPrivate *priv;
262
263         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
264
265         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
266         g_array_remove_range (priv->dns, 0, priv->dns->len);
267         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS);
268 }
269
270 /**
271  * nm_setting_ip4_config_get_num_dns_searches:
272  * @setting: the #NMSettingIP4Config
273  *
274  * Returns: the number of configured DNS search domains
275  **/
276 guint32
277 nm_setting_ip4_config_get_num_dns_searches (NMSettingIP4Config *setting)
278 {
279         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
280
281         return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search);
282 }
283
284 /**
285  * nm_setting_ip4_config_get_dns_search:
286  * @setting: the #NMSettingIP4Config
287  * @i: index number of the DNS search domain to return
288  *
289  * Returns: the DNS search domain at index @i
290  **/
291 const char *
292 nm_setting_ip4_config_get_dns_search (NMSettingIP4Config *setting, guint32 i)
293 {
294         NMSettingIP4ConfigPrivate *priv;
295
296         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
297
298         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
299         g_return_val_if_fail (i <= g_slist_length (priv->dns_search), NULL);
300
301         return (const char *) g_slist_nth_data (priv->dns_search, i);
302 }
303
304 /**
305  * nm_setting_ip4_config_add_dns_search:
306  * @setting: the #NMSettingIP4Config
307  * @dns_search: the search domain to add
308  *
309  * Adds a new DNS search domain to the setting.
310  *
311  * Returns: %TRUE if the DNS search domain was added; %FALSE if the search
312  * domain was already known
313  **/
314 gboolean
315 nm_setting_ip4_config_add_dns_search (NMSettingIP4Config *setting,
316                                       const char *dns_search)
317 {
318         NMSettingIP4ConfigPrivate *priv;
319         GSList *iter;
320
321         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
322         g_return_val_if_fail (dns_search != NULL, FALSE);
323         g_return_val_if_fail (dns_search[0] != '\0', FALSE);
324
325         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
326         for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) {
327                 if (!strcmp (dns_search, (char *) iter->data))
328                         return FALSE;
329         }
330
331         priv->dns_search = g_slist_append (priv->dns_search, g_strdup (dns_search));
332         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH);
333         return TRUE;
334 }
335
336 /**
337  * nm_setting_ip4_config_remove_dns_search:
338  * @setting: the #NMSettingIP4Config
339  * @i: index number of the DNS search domain
340  *
341  * Removes the DNS search domain at index @i.
342  **/
343 void
344 nm_setting_ip4_config_remove_dns_search (NMSettingIP4Config *setting, guint32 i)
345 {
346         NMSettingIP4ConfigPrivate *priv;
347         GSList *elt;
348
349         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
350
351         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
352         elt = g_slist_nth (priv->dns_search, i);
353         g_return_if_fail (elt != NULL);
354
355         g_free (elt->data);
356         priv->dns_search = g_slist_delete_link (priv->dns_search, elt);
357         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH);
358 }
359
360 /**
361  * nm_setting_ip4_config_remove_dns_search_by_value:
362  * @setting: the #NMSettingIP4Config
363  * @dns_search: the search domain to remove
364  *
365  * Removes the DNS search domain @dns_search.
366  *
367  * Returns: %TRUE if the DNS search domain was found and removed; %FALSE if it was not.
368  *
369  * Since 0.9.10
370  **/
371 gboolean
372 nm_setting_ip4_config_remove_dns_search_by_value (NMSettingIP4Config *setting,
373                                                   const char *dns_search)
374 {
375         NMSettingIP4ConfigPrivate *priv;
376         GSList *iter;
377
378         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
379         g_return_val_if_fail (dns_search != NULL, FALSE);
380         g_return_val_if_fail (dns_search[0] != '\0', FALSE);
381
382         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
383         for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) {
384                 if (!strcmp (dns_search, (char *) iter->data)) {
385                         priv->dns_search = g_slist_delete_link (priv->dns_search, iter);
386                         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH);
387                         return TRUE;
388                 }
389         }
390         return FALSE;
391 }
392
393 /**
394  * nm_setting_ip4_config_clear_dns_searches:
395  * @setting: the #NMSettingIP4Config
396  *
397  * Removes all configured DNS search domains.
398  **/
399 void
400 nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting)
401 {
402         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
403
404         g_slist_free_full (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search, g_free);
405         NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search = NULL;
406         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH);
407 }
408
409 /**
410  * nm_setting_ip4_config_get_num_addresses:
411  * @setting: the #NMSettingIP4Config
412  *
413  * Returns: the number of configured addresses
414  **/
415 guint32
416 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting)
417 {
418         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
419
420         return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->addresses);
421 }
422
423 /**
424  * nm_setting_ip4_config_get_address:
425  * @setting: the #NMSettingIP4Config
426  * @i: index number of the address to return
427  *
428  * Returns: the address at index @i
429  **/
430 NMIP4Address *
431 nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i)
432 {
433         NMSettingIP4ConfigPrivate *priv;
434
435         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
436
437         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
438         g_return_val_if_fail (i <= g_slist_length (priv->addresses), NULL);
439
440         return (NMIP4Address *) g_slist_nth_data (priv->addresses, i);
441 }
442
443 /**
444  * nm_setting_ip4_config_add_address:
445  * @setting: the #NMSettingIP4Config
446  * @address: the new address to add
447  *
448  * Adds a new IPv4 address and associated information to the setting.  The
449  * given address is duplicated internally and is not changed by this function.
450  *
451  * Returns: %TRUE if the address was added; %FALSE if the address was already
452  * known.
453  **/
454 gboolean
455 nm_setting_ip4_config_add_address (NMSettingIP4Config *setting,
456                                    NMIP4Address *address)
457 {
458         NMSettingIP4ConfigPrivate *priv;
459         NMIP4Address *copy;
460         GSList *iter;
461
462         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
463         g_return_val_if_fail (address != NULL, FALSE);
464
465         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
466         for (iter = priv->addresses; iter; iter = g_slist_next (iter)) {
467                 if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address))
468                         return FALSE;
469         }
470
471         copy = nm_ip4_address_dup (address);
472         priv->addresses = g_slist_append (priv->addresses, copy);
473         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES);
474         return TRUE;
475 }
476
477 /**
478  * nm_setting_ip4_config_remove_address:
479  * @setting: the #NMSettingIP4Config
480  * @i: index number of the address to remove
481  *
482  * Removes the address at index @i.
483  **/
484 void
485 nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i)
486 {
487         NMSettingIP4ConfigPrivate *priv;
488         GSList *elt;
489
490         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
491
492         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
493         elt = g_slist_nth (priv->addresses, i);
494         g_return_if_fail (elt != NULL);
495
496         nm_ip4_address_unref ((NMIP4Address *) elt->data);
497         priv->addresses = g_slist_delete_link (priv->addresses, elt);
498         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES);
499 }
500
501 /**
502  * nm_setting_ip4_config_remove_address_by_value:
503  * @setting: the #NMSettingIP4Config
504  * @address: the IP address to remove
505  *
506  * Removes the address @address.
507  *
508  * Returns: %TRUE if the address was found and removed; %FALSE if it was not.
509  *
510  * Since: 0.9.10
511  **/
512 gboolean
513 nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting,
514                                                NMIP4Address *address)
515 {
516         NMSettingIP4ConfigPrivate *priv;
517         GSList *iter;
518
519         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
520         g_return_val_if_fail (address != NULL, FALSE);
521
522         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
523         for (iter = priv->addresses; iter; iter = g_slist_next (iter)) {
524                 if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address)) {
525                         nm_ip4_address_unref ((NMIP4Address *) iter->data);
526                         priv->addresses = g_slist_delete_link (priv->addresses, iter);
527                         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES);
528                         return TRUE;
529                 }
530         }
531         return FALSE;
532 }
533
534 /**
535  * nm_setting_ip4_config_clear_addresses:
536  * @setting: the #NMSettingIP4Config
537  *
538  * Removes all configured addresses.
539  **/
540 void
541 nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting)
542 {
543         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
544
545         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
546
547         g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
548         priv->addresses = NULL;
549         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES);
550 }
551
552 /**
553  * nm_setting_ip4_config_get_num_routes:
554  * @setting: the #NMSettingIP4Config
555  *
556  * Returns: the number of configured routes
557  **/
558 guint32
559 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting)
560 {
561         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
562
563         return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->routes);
564 }
565
566 /**
567  * nm_setting_ip4_config_get_route:
568  * @setting: the #NMSettingIP4Config
569  * @i: index number of the route to return
570  *
571  * Returns: the route at index @i
572  **/
573 NMIP4Route *
574 nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i)
575 {
576         NMSettingIP4ConfigPrivate *priv;
577
578         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
579
580         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
581         g_return_val_if_fail (i <= g_slist_length (priv->routes), NULL);
582
583         return (NMIP4Route *) g_slist_nth_data (priv->routes, i);
584 }
585
586 /**
587  * nm_setting_ip4_config_add_route:
588  * @setting: the #NMSettingIP4Config
589  * @route: the route to add
590  *
591  * Adds a new IPv4 route and associated information to the setting.  The
592  * given route is duplicated internally and is not changed by this function.
593  *
594  * Returns: %TRUE if the route was added; %FALSE if the route was already known.
595  **/
596 gboolean
597 nm_setting_ip4_config_add_route (NMSettingIP4Config *setting,
598                                  NMIP4Route *route)
599 {
600         NMSettingIP4ConfigPrivate *priv;
601         NMIP4Route *copy;
602         GSList *iter;
603
604         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
605         g_return_val_if_fail (route != NULL, FALSE);
606
607         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
608         for (iter = priv->routes; iter; iter = g_slist_next (iter)) {
609                 if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route))
610                         return FALSE;
611         }
612
613         copy = nm_ip4_route_dup (route);
614         priv->routes = g_slist_append (priv->routes, copy);
615         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
616         return TRUE;
617 }
618
619 /**
620  * nm_setting_ip4_config_remove_route:
621  * @setting: the #NMSettingIP4Config
622  * @i: index number of the route
623  *
624  * Removes the route at index @i.
625  **/
626 void
627 nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i)
628 {
629         NMSettingIP4ConfigPrivate *priv;
630         GSList *elt;
631
632         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
633
634         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
635         elt = g_slist_nth (priv->routes, i);
636         g_return_if_fail (elt != NULL);
637
638         nm_ip4_route_unref ((NMIP4Route *) elt->data);
639         priv->routes = g_slist_delete_link (priv->routes, elt);
640         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
641 }
642
643 /**
644  * nm_setting_ip4_config_remove_route_by_value:
645  * @setting: the #NMSettingIP4Config
646  * @route: the route to remove
647  *
648  * Removes the route @route.
649  *
650  * Returns: %TRUE if the route was found and removed; %FALSE if it was not.
651  *
652  * Since: 0.9.10
653  **/
654 gboolean
655 nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting,
656                                              NMIP4Route *route)
657 {
658         NMSettingIP4ConfigPrivate *priv;
659         GSList *iter;
660
661         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
662         g_return_val_if_fail (route != NULL, FALSE);
663
664         priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
665         for (iter = priv->routes; iter; iter = g_slist_next (iter)) {
666                 if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) {
667                         nm_ip4_route_unref ((NMIP4Route *) iter->data);
668                         priv->routes = g_slist_delete_link (priv->routes, iter);
669                         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
670                         return TRUE;
671                 }
672         }
673         return FALSE;
674 }
675
676 /**
677  * nm_setting_ip4_config_clear_routes:
678  * @setting: the #NMSettingIP4Config
679  *
680  * Removes all configured routes.
681  **/
682 void
683 nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting)
684 {
685         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
686
687         g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting));
688
689         g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
690         priv->routes = NULL;
691         g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
692 }
693
694 /**
695  * nm_setting_ip4_config_get_route_metric:
696  * @setting: the #NMSettingIP4Config
697  *
698  * Returns the value contained in the #NMSettingIP4Config:route-metric
699  * property.
700  *
701  * Returns: the route metric that is used for IPv4 routes that don't explicitly
702  * specify a metric. See #NMSettingIP4Config:route-metric for more details.
703  *
704  * Since: 1.0
705  **/
706 gint64
707 nm_setting_ip4_config_get_route_metric (NMSettingIP4Config *setting)
708 {
709         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), -1);
710
711         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->route_metric;
712 }
713
714 /**
715  * nm_setting_ip4_config_get_ignore_auto_routes:
716  * @setting: the #NMSettingIP4Config
717  *
718  * Returns the value contained in the #NMSettingIP4Config:ignore-auto-routes
719  * property.
720  *
721  * Returns: %TRUE if automatically configured (ie via DHCP) routes should be
722  * ignored.
723  **/
724 gboolean
725 nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting)
726 {
727         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
728
729         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->ignore_auto_routes;
730 }
731
732 /**
733  * nm_setting_ip4_config_get_ignore_auto_dns:
734  * @setting: the #NMSettingIP4Config
735  *
736  * Returns the value contained in the #NMSettingIP4Config:ignore-auto-dns
737  * property.
738  *
739  * Returns: %TRUE if automatically configured (ie via DHCP) DNS information
740  * should be ignored.
741  **/
742 gboolean
743 nm_setting_ip4_config_get_ignore_auto_dns (NMSettingIP4Config *setting)
744 {
745         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
746
747         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->ignore_auto_dns;
748 }
749
750 /**
751  * nm_setting_ip4_config_get_dhcp_client_id:
752  * @setting: the #NMSettingIP4Config
753  *
754  * Returns the value contained in the #NMSettingIP4Config:dhcp-client-id
755  * property.
756  *
757  * Returns: the configured Client ID to send to the DHCP server when requesting
758  * addresses via DHCP.
759  **/
760 const char *
761 nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting)
762 {
763         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
764
765         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_client_id;
766 }
767
768 /**
769  * nm_setting_ip4_config_get_dhcp_send_hostname:
770  * @setting: the #NMSettingIP4Config
771  *
772  * Returns the value contained in the #NMSettingIP4Config:dhcp-send-hostname
773  * property.
774  *
775  * Returns: %TRUE if NetworkManager should send the machine hostname to the
776  * DHCP server when requesting addresses to allow the server to automatically
777  * update DNS information for this machine.
778  **/
779 gboolean
780 nm_setting_ip4_config_get_dhcp_send_hostname (NMSettingIP4Config *setting)
781 {
782         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
783
784         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_send_hostname;
785 }
786
787 /**
788  * nm_setting_ip4_config_get_dhcp_hostname:
789  * @setting: the #NMSettingIP4Config
790  *
791  * Returns the value contained in the #NMSettingIP4Config:dhcp-hostname
792  * property.
793  *
794  * Returns: the configured hostname to send to the DHCP server
795  **/
796 const char *
797 nm_setting_ip4_config_get_dhcp_hostname (NMSettingIP4Config *setting)
798 {
799         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
800
801         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_hostname;
802 }
803
804 /**
805  * nm_setting_ip4_config_get_dhcp_timeout:
806  * @setting: the #NMSettingIP4Config
807  *
808  * Returns the value contained in the #NMSettingIP4Config:dhcp-timeout
809  * property.
810  *
811  * Returns: The number of seconds after which unfinished DHCP transaction
812  * fails or zero for "default".
813  **/
814 int
815 nm_setting_ip4_config_get_dhcp_timeout (NMSettingIP4Config *setting)
816 {
817         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0);
818
819         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_timeout;
820 }
821
822 /**
823  * nm_setting_ip4_config_get_never_default:
824  * @setting: the #NMSettingIP4Config
825  *
826  * Returns the value contained in the #NMSettingIP4Config:never-default
827  * property.
828  *
829  * Returns: %TRUE if this connection should never be the default connection
830  * for IPv4 addressing
831  **/
832 gboolean
833 nm_setting_ip4_config_get_never_default (NMSettingIP4Config *setting)
834 {
835         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
836
837         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->never_default;
838 }
839
840 /**
841  * nm_setting_ip4_config_get_may_fail:
842  * @setting: the #NMSettingIP4Config
843  *
844  * Returns the value contained in the #NMSettingIP4Config:may-fail
845  * property.
846  *
847  * Returns: %TRUE if this connection doesn't require IPv4 addressing to complete
848  * for the connection to succeed.
849  **/
850 gboolean
851 nm_setting_ip4_config_get_may_fail (NMSettingIP4Config *setting)
852 {
853         g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE);
854
855         return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->may_fail;
856 }
857
858 static gboolean
859 verify (NMSetting *setting, GSList *all_settings, GError **error)
860 {
861         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
862         GSList *iter;
863         int i;
864
865         if (!priv->method) {
866                 g_set_error_literal (error,
867                                      NM_SETTING_IP4_CONFIG_ERROR,
868                                      NM_SETTING_IP4_CONFIG_ERROR_MISSING_PROPERTY,
869                                      _("property is missing"));
870                 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD);
871                 return FALSE;
872         }
873
874         if (!strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) {
875                 if (!priv->addresses) {
876                         g_set_error (error,
877                                      NM_SETTING_IP4_CONFIG_ERROR,
878                                      NM_SETTING_IP4_CONFIG_ERROR_MISSING_PROPERTY,
879                                      _("this property cannot be empty for '%s=%s'"),
880                                      NM_SETTING_IP4_CONFIG_METHOD, priv->method);
881                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES);
882                         return FALSE;
883                 }
884         } else if (   !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)
885                    || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)
886                    || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) {
887                 if (priv->dns && priv->dns->len) {
888                         g_set_error (error,
889                                      NM_SETTING_IP4_CONFIG_ERROR,
890                                      NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
891                                      _("this property is not allowed for '%s=%s'"),
892                                      NM_SETTING_IP4_CONFIG_METHOD, priv->method);
893                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS);
894                         return FALSE;
895                 }
896
897                 if (g_slist_length (priv->dns_search)) {
898                         g_set_error (error,
899                                      NM_SETTING_IP4_CONFIG_ERROR,
900                                      NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
901                                      _("this property is not allowed for '%s=%s'"),
902                                      NM_SETTING_IP4_CONFIG_METHOD, priv->method);
903                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS_SEARCH);
904                         return FALSE;
905                 }
906
907                 /* Shared allows IP addresses; link-local and disabled do not */
908                 if (strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0) {
909                         if (g_slist_length (priv->addresses)) {
910                                 g_set_error (error,
911                                              NM_SETTING_IP4_CONFIG_ERROR,
912                                              NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
913                                              _("this property is not allowed for '%s=%s'"),
914                                              NM_SETTING_IP4_CONFIG_METHOD, priv->method);
915                                 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES);
916                                 return FALSE;
917                         }
918                 }
919         } else if (!strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
920                 /* nothing to do */
921         } else {
922                 g_set_error_literal (error,
923                                      NM_SETTING_IP4_CONFIG_ERROR,
924                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
925                                      _("property is invalid"));
926                 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD);
927                 return FALSE;
928         }
929
930         if (priv->dhcp_client_id && !strlen (priv->dhcp_client_id)) {
931                 g_set_error_literal (error,
932                                      NM_SETTING_IP4_CONFIG_ERROR,
933                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
934                                      _("property is empty"));
935                 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID);
936                 return FALSE;
937         }
938
939         if (priv->dhcp_hostname && !strlen (priv->dhcp_hostname)) {
940                 g_set_error_literal (error,
941                                      NM_SETTING_IP4_CONFIG_ERROR,
942                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
943                                      _("property is empty"));
944                 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME);
945                 return FALSE;
946         }
947
948         /* Validate addresses */
949         for (iter = priv->addresses, i = 0; iter; iter = g_slist_next (iter), i++) {
950                 NMIP4Address *addr = (NMIP4Address *) iter->data;
951                 guint32 prefix = nm_ip4_address_get_prefix (addr);
952
953                 if (!nm_ip4_address_get_address (addr)) {
954                         g_set_error (error,
955                                      NM_SETTING_IP4_CONFIG_ERROR,
956                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
957                                      _("%d. IPv4 address is invalid"),
958                                      i+1);
959                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES);
960                         return FALSE;
961                 }
962
963                 if (!prefix || prefix > 32) {
964                         g_set_error (error,
965                                      NM_SETTING_IP4_CONFIG_ERROR,
966                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
967                                      _("%d. IPv4 address has invalid prefix"),
968                                      i+1);
969                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES);
970                         return FALSE;
971                 }
972         }
973
974         /* Validate routes */
975         for (iter = priv->routes, i = 0; iter; iter = g_slist_next (iter), i++) {
976                 NMIP4Route *route = (NMIP4Route *) iter->data;
977                 guint32 prefix = nm_ip4_route_get_prefix (route);
978
979                 if (!prefix || prefix > 32) {
980                         g_set_error (error,
981                                      NM_SETTING_IP4_CONFIG_ERROR,
982                                      NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY,
983                                      _("%d. route has invalid prefix"),
984                                      i+1);
985                         g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES);
986                         return FALSE;
987                 }
988         }
989
990         return TRUE;
991 }
992
993
994 static void
995 nm_setting_ip4_config_init (NMSettingIP4Config *setting)
996 {
997         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
998
999
1000         priv->dns = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3);
1001 }
1002
1003 static void
1004 finalize (GObject *object)
1005 {
1006         NMSettingIP4Config *self = NM_SETTING_IP4_CONFIG (object);
1007         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (self);
1008
1009         g_free (priv->method);
1010         g_free (priv->dhcp_hostname);
1011         g_free (priv->dhcp_client_id);
1012
1013         g_array_free (priv->dns, TRUE);
1014
1015         g_slist_free_full (priv->dns_search, g_free);
1016         g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
1017         g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
1018
1019         G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object);
1020 }
1021
1022 static void
1023 set_property (GObject *object, guint prop_id,
1024               const GValue *value, GParamSpec *pspec)
1025 {
1026         NMSettingIP4Config *setting = NM_SETTING_IP4_CONFIG (object);
1027         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
1028
1029         switch (prop_id) {
1030         case PROP_METHOD:
1031                 g_free (priv->method);
1032                 priv->method = g_value_dup_string (value);
1033                 break;
1034         case PROP_DNS:
1035                 g_array_free (priv->dns, TRUE);
1036                 priv->dns = g_value_dup_boxed (value);
1037                 if (!priv->dns)
1038                         priv->dns = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3);
1039                 break;
1040         case PROP_DNS_SEARCH:
1041                 g_slist_free_full (priv->dns_search, g_free);
1042                 priv->dns_search = g_value_dup_boxed (value);
1043                 break;
1044         case PROP_ADDRESSES:
1045                 g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
1046                 priv->addresses = nm_utils_ip4_addresses_from_gvalue (value);
1047                 break;
1048         case PROP_ROUTES:
1049                 g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
1050                 priv->routes = nm_utils_ip4_routes_from_gvalue (value);
1051                 break;
1052         case PROP_ROUTE_METRIC:
1053                 priv->route_metric = g_value_get_int64 (value);
1054                 break;
1055         case PROP_IGNORE_AUTO_ROUTES:
1056                 priv->ignore_auto_routes = g_value_get_boolean (value);
1057                 break;
1058         case PROP_IGNORE_AUTO_DNS:
1059                 priv->ignore_auto_dns = g_value_get_boolean (value);
1060                 break;
1061         case PROP_DHCP_CLIENT_ID:
1062                 g_free (priv->dhcp_client_id);
1063                 priv->dhcp_client_id = g_value_dup_string (value);
1064                 break;
1065         case PROP_DHCP_SEND_HOSTNAME:
1066                 priv->dhcp_send_hostname = g_value_get_boolean (value);
1067                 break;
1068         case PROP_DHCP_HOSTNAME:
1069                 g_free (priv->dhcp_hostname);
1070                 priv->dhcp_hostname = g_value_dup_string (value);
1071                 break;
1072         case PROP_DHCP_TIMEOUT:
1073                 priv->dhcp_timeout = g_value_get_uint (value);
1074                 break;
1075         case PROP_NEVER_DEFAULT:
1076                 priv->never_default = g_value_get_boolean (value);
1077                 break;
1078         case PROP_MAY_FAIL:
1079                 priv->may_fail = g_value_get_boolean (value);
1080                 break;
1081         default:
1082                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1083                 break;
1084         }
1085 }
1086
1087 static void
1088 get_property (GObject *object, guint prop_id,
1089               GValue *value, GParamSpec *pspec)
1090 {
1091         NMSettingIP4Config *setting = NM_SETTING_IP4_CONFIG (object);
1092         NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
1093
1094         switch (prop_id) {
1095         case PROP_METHOD:
1096                 g_value_set_string (value, nm_setting_ip4_config_get_method (setting));
1097                 break;
1098         case PROP_DNS:
1099                 g_value_set_boxed (value, priv->dns);
1100                 break;
1101         case PROP_DNS_SEARCH:
1102                 g_value_set_boxed (value, priv->dns_search);
1103                 break;
1104         case PROP_ADDRESSES:
1105                 nm_utils_ip4_addresses_to_gvalue (priv->addresses, value);
1106                 break;
1107         case PROP_ROUTES:
1108                 nm_utils_ip4_routes_to_gvalue (priv->routes, value);
1109                 break;
1110         case PROP_ROUTE_METRIC:
1111                 g_value_set_int64 (value, priv->route_metric);
1112                 break;
1113         case PROP_IGNORE_AUTO_ROUTES:
1114                 g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting));
1115                 break;
1116         case PROP_IGNORE_AUTO_DNS:
1117                 g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_dns (setting));
1118                 break;
1119         case PROP_DHCP_CLIENT_ID:
1120                 g_value_set_string (value, nm_setting_ip4_config_get_dhcp_client_id (setting));
1121                 break;
1122         case PROP_DHCP_SEND_HOSTNAME:
1123                 g_value_set_boolean (value, nm_setting_ip4_config_get_dhcp_send_hostname (setting));
1124                 break;
1125         case PROP_DHCP_HOSTNAME:
1126                 g_value_set_string (value, nm_setting_ip4_config_get_dhcp_hostname (setting));
1127                 break;
1128         case PROP_DHCP_TIMEOUT:
1129                 g_value_set_uint (value, nm_setting_ip4_config_get_dhcp_timeout (setting));
1130                 break;
1131         case PROP_NEVER_DEFAULT:
1132                 g_value_set_boolean (value, priv->never_default);
1133                 break;
1134         case PROP_MAY_FAIL:
1135                 g_value_set_boolean (value, priv->may_fail);
1136                 break;
1137         default:
1138                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1139                 break;
1140         }
1141 }
1142
1143 static void
1144 nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class)
1145 {
1146         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
1147         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
1148
1149         g_type_class_add_private (setting_class, sizeof (NMSettingIP4ConfigPrivate));
1150
1151         /* virtual methods */
1152         object_class->set_property = set_property;
1153         object_class->get_property = get_property;
1154         object_class->finalize     = finalize;
1155         parent_class->verify       = verify;
1156
1157         /* Properties */
1158         /**
1159          * NMSettingIP4Config:method:
1160          *
1161          * IPv4 configuration method.  If "auto" is specified then the appropriate
1162          * automatic method (DHCP, PPP, etc) is used for the interface and most
1163          * other properties can be left unset.  If "link-local" is specified, then a
1164          * link-local address in the 169.254/16 range will be assigned to the
1165          * interface.  If "manual" is specified, static IP addressing is used and at
1166          * least one IP address must be given in the "addresses" property.  If
1167          * "shared" is specified (indicating that this connection will provide
1168          * network access to other computers) then the interface is assigned an
1169          * address in the 10.42.x.1/24 range and a DHCP and forwarding DNS server
1170          * are started, and the interface is NAT-ed to the current default network
1171          * connection.  "disabled" means IPv4 will not be used on this connection.
1172          * This property must be set.
1173          **/
1174         g_object_class_install_property
1175                 (object_class, PROP_METHOD,
1176                  g_param_spec_string (NM_SETTING_IP4_CONFIG_METHOD, "", "",
1177                                       NULL,
1178                                       G_PARAM_READWRITE |
1179                                       NM_SETTING_PARAM_INFERRABLE |
1180                                       G_PARAM_STATIC_STRINGS));
1181
1182         /**
1183          * NMSettingIP4Config:dns:
1184          *
1185          * List of DNS servers (network byte order).  For the "auto" method, these
1186          * DNS servers are appended to those (if any) returned by automatic
1187          * configuration.  DNS servers cannot be used with the "shared",
1188          * "link-local", or "disabled" methods as there is no upstream network.  In
1189          * all other methods, these DNS servers are used as the only DNS servers for
1190          * this connection.
1191          **/
1192         g_object_class_install_property
1193                 (object_class, PROP_DNS,
1194                  _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_DNS, "", "",
1195                                              DBUS_TYPE_G_UINT_ARRAY,
1196                                              G_PARAM_READWRITE |
1197                                              G_PARAM_STATIC_STRINGS));
1198
1199         /**
1200          * NMSettingIP4Config:dns-search:
1201          *
1202          * List of DNS search domains.  For the "auto" method, these search domains
1203          * are appended to those returned by automatic configuration. Search domains
1204          * cannot be used with the "shared", "link-local", or "disabled" methods as
1205          * there is no upstream network.  In all other methods, these search domains
1206          * are used as the only search domains for this connection.
1207          **/
1208         g_object_class_install_property
1209                 (object_class, PROP_DNS_SEARCH,
1210                  _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_DNS_SEARCH, "", "",
1211                                              DBUS_TYPE_G_LIST_OF_STRING,
1212                                              G_PARAM_READWRITE |
1213                                              G_PARAM_STATIC_STRINGS));
1214
1215         /**
1216          * NMSettingIP4Config:addresses:
1217          *
1218          * Array of IPv4 address structures.  Each IPv4 address structure is
1219          * composed of 3 32-bit values; the first being the IPv4 address (network
1220          * byte order), the second the prefix (1 - 32), and last the IPv4 gateway
1221          * (network byte order). The gateway may be left as 0 if no gateway exists
1222          * for that subnet.  For the "auto" method, given IP addresses are appended
1223          * to those returned by automatic configuration.  Addresses cannot be used
1224          * with the "shared", "link-local", or "disabled" methods as addressing is
1225          * either automatic or disabled with these methods.
1226          **/
1227         g_object_class_install_property
1228                 (object_class, PROP_ADDRESSES,
1229                  _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_ADDRESSES, "", "",
1230                                              DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT,
1231                                              G_PARAM_READWRITE |
1232                                              NM_SETTING_PARAM_INFERRABLE |
1233                                              G_PARAM_STATIC_STRINGS));
1234
1235         /**
1236          * NMSettingIP4Config:routes:
1237          *
1238          * Array of IPv4 route structures.  Each IPv4 route structure is composed of
1239          * 4 32-bit values; the first being the destination IPv4 network or address
1240          * (network byte order), the second the destination network or address
1241          * prefix (1 - 32), the third being the next-hop (network byte order) if
1242          * any, and the fourth being the route metric. For the "auto" method, given
1243          * IP routes are appended to those returned by automatic configuration.
1244          * Routes cannot be used with the "shared", "link-local", or "disabled"
1245          * methods because there is no upstream network.
1246          **/
1247         g_object_class_install_property
1248                 (object_class, PROP_ROUTES,
1249                  _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_ROUTES, "", "",
1250                                              DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT,
1251                                              G_PARAM_READWRITE |
1252                                              NM_SETTING_PARAM_INFERRABLE |
1253                                              G_PARAM_STATIC_STRINGS));
1254
1255         /**
1256          * NMSettingIP4Config:route-metric:
1257          *
1258          * The default metric for routes that don't explicitly specify a metric.
1259          * The default value -1 means that the metric is choosen automatically
1260          * based on the device type.
1261          * The metric applies to dynamic routes, manual (static) routes that
1262          * don't have an explicit metric setting, address prefix routes, and
1263          * the default route.
1264          * As the linux kernel accepts zero (0) as a valid metric, zero is
1265          * a valid value.
1266          *
1267          * Since: 1.0
1268          **/
1269         g_object_class_install_property
1270             (object_class, PROP_ROUTE_METRIC,
1271              g_param_spec_int64 (NM_SETTING_IP4_CONFIG_ROUTE_METRIC, "", "",
1272                                  -1, G_MAXUINT32, -1,
1273                                  G_PARAM_READWRITE |
1274                                  G_PARAM_CONSTRUCT |
1275                                  G_PARAM_STATIC_STRINGS));
1276
1277         /**
1278          * NMSettingIP4Config:ignore-auto-routes:
1279          *
1280          * When the method is set to "auto" and this property to %TRUE,
1281          * automatically configured routes are ignored and only routes specified in
1282          * the #NMSettingIP4Config:routes property, if any, are used.
1283          **/
1284         g_object_class_install_property
1285                 (object_class, PROP_IGNORE_AUTO_ROUTES,
1286                  g_param_spec_boolean (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, "", "",
1287                                        FALSE,
1288                                        G_PARAM_READWRITE |
1289                                        G_PARAM_CONSTRUCT |
1290                                        G_PARAM_STATIC_STRINGS));
1291
1292         /**
1293          * NMSettingIP4Config:ignore-auto-dns:
1294          *
1295          * When the method is set to "auto" and this property to %TRUE,
1296          * automatically configured nameservers and search domains are ignored and
1297          * only nameservers and search domains specified in the
1298          * #NMSettingIP4Config:dns and #NMSettingIP4Config:dns-search properties, if
1299          * any, are used.
1300          **/
1301         g_object_class_install_property
1302                 (object_class, PROP_IGNORE_AUTO_DNS,
1303                  g_param_spec_boolean (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, "", "",
1304                                        FALSE,
1305                                        G_PARAM_READWRITE |
1306                                        G_PARAM_CONSTRUCT |
1307                                        G_PARAM_STATIC_STRINGS));
1308
1309         /**
1310          * NMSettingIP4Config:dhcp-client-id:
1311          *
1312          * A string sent to the DHCP server to identify the local machine which the
1313          * DHCP server may use to customize the DHCP lease and options.
1314          **/
1315         g_object_class_install_property
1316                 (object_class, PROP_DHCP_CLIENT_ID,
1317                  g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "", "",
1318                                       NULL,
1319                                       G_PARAM_READWRITE |
1320                                       G_PARAM_STATIC_STRINGS));
1321
1322         /**
1323          * NMSettingIP4Config:dhcp-send-hostname:
1324          *
1325          * If %TRUE, a hostname is sent to the DHCP server when acquiring a lease.
1326          * Some DHCP servers use this hostname to update DNS databases, essentially
1327          * providing a static hostname for the computer.  If the
1328          * #NMSettingIP4Config:dhcp-hostname property is empty and this property is
1329          * %TRUE, the current persistent hostname of the computer is sent.
1330          **/
1331         g_object_class_install_property
1332                 (object_class, PROP_DHCP_SEND_HOSTNAME,
1333                  g_param_spec_boolean (NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, "", "",
1334                                        TRUE,
1335                                        G_PARAM_READWRITE |
1336                                        G_PARAM_CONSTRUCT |
1337                                        G_PARAM_STATIC_STRINGS));
1338
1339         /**
1340          * NMSettingIP4Config:dhcp-hostname:
1341          *
1342          * If the #NMSettingIP4Config:dhcp-send-hostname property is %TRUE, then the
1343          * specified name will be sent to the DHCP server when acquiring a lease.
1344          **/
1345         g_object_class_install_property
1346                 (object_class, PROP_DHCP_HOSTNAME,
1347                  g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, "", "",
1348                                       NULL,
1349                                       G_PARAM_READWRITE |
1350                                       NM_SETTING_PARAM_INFERRABLE |
1351                                       G_PARAM_STATIC_STRINGS));
1352
1353         /**
1354          * NMSettingIP4Config:dhcp-timeout:
1355          *
1356          * Number of seconds after which the unfinished DHCP transaction fails
1357          * or zero for default.
1358          **/
1359         g_object_class_install_property
1360                 (object_class, PROP_DHCP_TIMEOUT,
1361                  g_param_spec_uint (NM_SETTING_IP4_CONFIG_DHCP_TIMEOUT, "", "",
1362                                     0, G_MAXUINT32, 0,
1363                                     G_PARAM_READWRITE |
1364                                     G_PARAM_STATIC_STRINGS));
1365
1366         /**
1367          * NMSettingIP4Config:never-default:
1368          *
1369          * If %TRUE, this connection will never be the default IPv4 connection,
1370          * meaning it will never be assigned the default route by NetworkManager.
1371          **/
1372         g_object_class_install_property
1373                 (object_class, PROP_NEVER_DEFAULT,
1374                  g_param_spec_boolean (NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, "", "",
1375                                        FALSE,
1376                                        G_PARAM_READWRITE |
1377                                        G_PARAM_CONSTRUCT |
1378                                        G_PARAM_STATIC_STRINGS));
1379
1380         /**
1381          * NMSettingIP4Config:may-fail:
1382          *
1383          * If %TRUE, allow overall network configuration to proceed even if IPv4
1384          * configuration times out.  Note that at least one IP configuration must
1385          * succeed or overall network configuration will still fail.  For example,
1386          * in IPv6-only networks, setting this property to %TRUE allows the overall
1387          * network configuration to succeed if IPv4 configuration fails but IPv6
1388          * configuration completes successfully.
1389          **/
1390         g_object_class_install_property
1391                 (object_class, PROP_MAY_FAIL,
1392                  g_param_spec_boolean (NM_SETTING_IP4_CONFIG_MAY_FAIL, "", "",
1393                                        TRUE,
1394                                        G_PARAM_READWRITE |
1395                                        G_PARAM_CONSTRUCT |
1396                                        G_PARAM_STATIC_STRINGS));
1397 }
1398
1399
1400 struct NMIP4Address {
1401         guint32 refcount;
1402         guint32 address;   /* network byte order */
1403         guint32 prefix;
1404         guint32 gateway;   /* network byte order */
1405 };
1406
1407 /**
1408  * nm_ip4_address_new:
1409  *
1410  * Creates and returns a new #NMIP4Address object.
1411  *
1412  * Returns: (transfer full): the new empty #NMIP4Address object
1413  **/
1414 NMIP4Address *
1415 nm_ip4_address_new (void)
1416 {
1417         NMIP4Address *address;
1418
1419         address = g_malloc0 (sizeof (NMIP4Address));
1420         address->refcount = 1;
1421         return address;
1422 }
1423
1424 /**
1425  * nm_ip4_address_dup:
1426  * @source: the #NMIP4Address object to copy
1427  *
1428  * Copies a given #NMIP4Address object and returns the copy.
1429  *
1430  * Returns: (transfer full): the copy of the given #NMIP4Address copy
1431  **/
1432 NMIP4Address *
1433 nm_ip4_address_dup (NMIP4Address *source)
1434 {
1435         NMIP4Address *address;
1436
1437         g_return_val_if_fail (source != NULL, NULL);
1438         g_return_val_if_fail (source->refcount > 0, NULL);
1439
1440         address = nm_ip4_address_new ();
1441         address->address = source->address;
1442         address->prefix = source->prefix;
1443         address->gateway = source->gateway;
1444
1445         return address;
1446 }
1447
1448 /**
1449  * nm_ip4_address_ref:
1450  * @address: the #NMIP4Address
1451  *
1452  * Increases the reference count of the object.
1453  **/
1454 void
1455 nm_ip4_address_ref (NMIP4Address *address)
1456 {
1457         g_return_if_fail (address != NULL);
1458         g_return_if_fail (address->refcount > 0);
1459
1460         address->refcount++;
1461 }
1462
1463 /**
1464  * nm_ip4_address_unref:
1465  * @address: the #NMIP4Address
1466  *
1467  * Decreases the reference count of the object.  If the reference count
1468  * reaches zero, the object will be destroyed.
1469  **/
1470 void
1471 nm_ip4_address_unref (NMIP4Address *address)
1472 {
1473         g_return_if_fail (address != NULL);
1474         g_return_if_fail (address->refcount > 0);
1475
1476         address->refcount--;
1477         if (address->refcount == 0) {
1478                 memset (address, 0, sizeof (NMIP4Address));
1479                 g_free (address);
1480         }
1481 }
1482
1483 /**
1484  * nm_ip4_address_compare:
1485  * @address: the #NMIP4Address
1486  * @other: the #NMIP4Address to compare @address to.
1487  *
1488  * Determines if two #NMIP4Address objects contain the same values.
1489  *
1490  * Returns: %TRUE if the objects contain the same values, %FALSE if they do not.
1491  **/
1492 gboolean
1493 nm_ip4_address_compare (NMIP4Address *address, NMIP4Address *other)
1494 {
1495         g_return_val_if_fail (address != NULL, FALSE);
1496         g_return_val_if_fail (address->refcount > 0, FALSE);
1497
1498         g_return_val_if_fail (other != NULL, FALSE);
1499         g_return_val_if_fail (other->refcount > 0, FALSE);
1500
1501         if (   address->address != other->address
1502             || address->prefix != other->prefix
1503             || address->gateway != other->gateway)
1504                 return FALSE;
1505         return TRUE;
1506 }
1507
1508 /**
1509  * nm_ip4_address_get_address:
1510  * @address: the #NMIP4Address
1511  *
1512  * Gets the IPv4 address property of this address object.
1513  *
1514  * Returns: the IPv4 address in network byte order
1515  **/
1516 guint32
1517 nm_ip4_address_get_address (NMIP4Address *address)
1518 {
1519         g_return_val_if_fail (address != NULL, 0);
1520         g_return_val_if_fail (address->refcount > 0, 0);
1521
1522         return address->address;
1523 }
1524
1525 /**
1526  * nm_ip4_address_set_address:
1527  * @address: the #NMIP4Address
1528  * @addr: the IPv4 address in network byte order
1529  *
1530  * Sets the IPv4 address property of this object.
1531  **/
1532 void
1533 nm_ip4_address_set_address (NMIP4Address *address, guint32 addr)
1534 {
1535         g_return_if_fail (address != NULL);
1536         g_return_if_fail (address->refcount > 0);
1537
1538         address->address = addr;
1539 }
1540
1541 /**
1542  * nm_ip4_address_get_prefix:
1543  * @address: the #NMIP4Address
1544  *
1545  * Gets the IPv4 address prefix (ie "24" or "30" etc) property of this address
1546  * object.
1547  *
1548  * Returns: the IPv4 address prefix
1549  **/
1550 guint32
1551 nm_ip4_address_get_prefix (NMIP4Address *address)
1552 {
1553         g_return_val_if_fail (address != NULL, 0);
1554         g_return_val_if_fail (address->refcount > 0, 0);
1555
1556         return address->prefix;
1557 }
1558
1559 /**
1560  * nm_ip4_address_set_prefix:
1561  * @address: the #NMIP4Address
1562  * @prefix: the address prefix, a number between 1 and 32 inclusive
1563  *
1564  * Sets the IPv4 address prefix.
1565  **/
1566 void
1567 nm_ip4_address_set_prefix (NMIP4Address *address, guint32 prefix)
1568 {
1569         g_return_if_fail (address != NULL);
1570         g_return_if_fail (address->refcount > 0);
1571         g_return_if_fail (prefix <= 32);
1572         g_return_if_fail (prefix > 0);
1573
1574         address->prefix = prefix;
1575 }
1576
1577 /**
1578  * nm_ip4_address_get_gateway:
1579  * @address: the #NMIP4Address
1580  *
1581  * Gets the IPv4 default gateway property of this address object.
1582  *
1583  * Returns: the IPv4 gateway address in network byte order
1584  **/
1585 guint32
1586 nm_ip4_address_get_gateway (NMIP4Address *address)
1587 {
1588         g_return_val_if_fail (address != NULL, 0);
1589         g_return_val_if_fail (address->refcount > 0, 0);
1590
1591         return address->gateway;
1592 }
1593
1594 /**
1595  * nm_ip4_address_set_gateway:
1596  * @address: the #NMIP4Address
1597  * @gateway: the IPv4 default gateway in network byte order
1598  *
1599  * Sets the IPv4 default gateway property of this address object.
1600  **/
1601 void
1602 nm_ip4_address_set_gateway (NMIP4Address *address, guint32 gateway)
1603 {
1604         g_return_if_fail (address != NULL);
1605         g_return_if_fail (address->refcount > 0);
1606
1607         address->gateway = gateway;
1608 }
1609
1610
1611 struct NMIP4Route {
1612         guint32 refcount;
1613
1614         guint32 dest;   /* network byte order */
1615         guint32 prefix;
1616         guint32 next_hop;   /* network byte order */
1617         guint32 metric;    /* lower metric == more preferred */
1618 };
1619
1620 /**
1621  * nm_ip4_route_new:
1622  *
1623  * Creates and returns a new #NMIP4Route object.
1624  *
1625  * Returns: (transfer full): the new empty #NMIP4Route object
1626  **/
1627 NMIP4Route *
1628 nm_ip4_route_new (void)
1629 {
1630         NMIP4Route *route;
1631
1632         route = g_malloc0 (sizeof (NMIP4Route));
1633         route->refcount = 1;
1634         return route;
1635 }
1636
1637 /**
1638  * nm_ip4_route_dup:
1639  * @source: the #NMIP4Route object to copy
1640  *
1641  * Copies a given #NMIP4Route object and returns the copy.
1642  *
1643  * Returns: (transfer full): the copy of the given #NMIP4Route copy
1644  **/
1645 NMIP4Route *
1646 nm_ip4_route_dup (NMIP4Route *source)
1647 {
1648         NMIP4Route *route;
1649
1650         g_return_val_if_fail (source != NULL, NULL);
1651         g_return_val_if_fail (source->refcount > 0, NULL);
1652
1653         route = nm_ip4_route_new ();
1654         route->dest = source->dest;
1655         route->prefix = source->prefix;
1656         route->next_hop = source->next_hop;
1657         route->metric = source->metric;
1658
1659         return route;
1660 }
1661
1662 /**
1663  * nm_ip4_route_ref:
1664  * @route: the #NMIP4Route
1665  *
1666  * Increases the reference count of the object.
1667  **/
1668 void
1669 nm_ip4_route_ref (NMIP4Route *route)
1670 {
1671         g_return_if_fail (route != NULL);
1672         g_return_if_fail (route->refcount > 0);
1673
1674         route->refcount++;
1675 }
1676
1677 /**
1678  * nm_ip4_route_unref:
1679  * @route: the #NMIP4Route
1680  *
1681  * Decreases the reference count of the object.  If the reference count
1682  * reaches zero, the object will be destroyed.
1683  **/
1684 void
1685 nm_ip4_route_unref (NMIP4Route *route)
1686 {
1687         g_return_if_fail (route != NULL);
1688         g_return_if_fail (route->refcount > 0);
1689
1690         route->refcount--;
1691         if (route->refcount == 0) {
1692                 memset (route, 0, sizeof (NMIP4Route));
1693                 g_free (route);
1694         }
1695 }
1696
1697 /**
1698  * nm_ip4_route_compare:
1699  * @route: the #NMIP4Route
1700  * @other: the #NMIP4Route to compare @route to.
1701  *
1702  * Determines if two #NMIP4Route objects contain the same values.
1703  *
1704  * Returns: %TRUE if the objects contain the same values, %FALSE if they do not.
1705  **/
1706 gboolean
1707 nm_ip4_route_compare (NMIP4Route *route, NMIP4Route *other)
1708 {
1709         g_return_val_if_fail (route != NULL, FALSE);
1710         g_return_val_if_fail (route->refcount > 0, FALSE);
1711
1712         g_return_val_if_fail (other != NULL, FALSE);
1713         g_return_val_if_fail (other->refcount > 0, FALSE);
1714
1715         if (   route->dest != other->dest
1716             || route->prefix != other->prefix
1717             || route->next_hop != other->next_hop
1718             || route->metric != other->metric)
1719                 return FALSE;
1720         return TRUE;
1721 }
1722
1723 /**
1724  * nm_ip4_route_get_dest:
1725  * @route: the #NMIP4Route
1726  *
1727  * Gets the IPv4 destination address property of this route object.
1728  *
1729  * Returns: the IPv4 address in network byte order
1730  **/
1731 guint32
1732 nm_ip4_route_get_dest (NMIP4Route *route)
1733 {
1734         g_return_val_if_fail (route != NULL, 0);
1735         g_return_val_if_fail (route->refcount > 0, 0);
1736
1737         return route->dest;
1738 }
1739
1740 /**
1741  * nm_ip4_route_set_dest:
1742  * @route: the #NMIP4Route
1743  * @dest: the destination address in network byte order
1744  *
1745  * Sets the IPv4 destination address property of this route object.
1746  **/
1747 void
1748 nm_ip4_route_set_dest (NMIP4Route *route, guint32 dest)
1749 {
1750         g_return_if_fail (route != NULL);
1751         g_return_if_fail (route->refcount > 0);
1752
1753         route->dest = dest;
1754 }
1755
1756 /**
1757  * nm_ip4_route_get_prefix:
1758  * @route: the #NMIP4Route
1759  *
1760  * Gets the IPv4 prefix (ie "24" or "30" etc) of this route.
1761  *
1762  * Returns: the IPv4 prefix
1763  **/
1764 guint32
1765 nm_ip4_route_get_prefix (NMIP4Route *route)
1766 {
1767         g_return_val_if_fail (route != NULL, 0);
1768         g_return_val_if_fail (route->refcount > 0, 0);
1769
1770         return route->prefix;
1771 }
1772
1773 /**
1774  * nm_ip4_route_set_prefix:
1775  * @route: the #NMIP4Route
1776  * @prefix: the prefix, a number between 1 and 32 inclusive
1777  *
1778  * Sets the IPv4 prefix of this route.
1779  **/
1780 void
1781 nm_ip4_route_set_prefix (NMIP4Route *route, guint32 prefix)
1782 {
1783         g_return_if_fail (route != NULL);
1784         g_return_if_fail (route->refcount > 0);
1785         g_return_if_fail (prefix <= 32);
1786         g_return_if_fail (prefix > 0);
1787
1788         route->prefix = prefix;
1789 }
1790
1791 /**
1792  * nm_ip4_route_get_next_hop:
1793  * @route: the #NMIP4Route
1794  *
1795  * Gets the IPv4 address of the next hop of this route.
1796  *
1797  * Returns: the IPv4 address in network byte order
1798  **/
1799 guint32
1800 nm_ip4_route_get_next_hop (NMIP4Route *route)
1801 {
1802         g_return_val_if_fail (route != NULL, 0);
1803         g_return_val_if_fail (route->refcount > 0, 0);
1804
1805         return route->next_hop;
1806 }
1807
1808 /**
1809  * nm_ip4_route_set_next_hop:
1810  * @route: the #NMIP4Route
1811  * @next_hop: the IPv4 address of the next hop in network byte order
1812  *
1813  * Sets the IPv4 address of the next hop of this route.
1814  **/
1815 void
1816 nm_ip4_route_set_next_hop (NMIP4Route *route, guint32 next_hop)
1817 {
1818         g_return_if_fail (route != NULL);
1819         g_return_if_fail (route->refcount > 0);
1820
1821         route->next_hop = next_hop;
1822 }
1823
1824 /**
1825  * nm_ip4_route_get_metric:
1826  * @route: the #NMIP4Route
1827  *
1828  * Gets the route metric property of this route object; lower values indicate
1829  * "better" or more preferred routes.
1830  *
1831  * Returns: the route metric
1832  **/
1833 guint32
1834 nm_ip4_route_get_metric (NMIP4Route *route)
1835 {
1836         g_return_val_if_fail (route != NULL, 0);
1837         g_return_val_if_fail (route->refcount > 0, 0);
1838
1839         return route->metric;
1840 }
1841
1842 /**
1843  * nm_ip4_route_set_metric:
1844  * @route: the #NMIP4Route
1845  * @metric: the route metric
1846  *
1847  * Sets the route metric property of this route object; lower values indicate
1848  * "better" or more preferred routes.
1849  **/
1850 void
1851 nm_ip4_route_set_metric (NMIP4Route *route, guint32 metric)
1852 {
1853         g_return_if_fail (route != NULL);
1854         g_return_if_fail (route->refcount > 0);
1855
1856         route->metric = metric;
1857 }