device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-team.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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.
7  *
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.
12  *
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.
17  *
18  * Copyright 2013 Jiri Pirko <jiri@resnulli.us>
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24 #include <stdlib.h>
25 #include <dbus/dbus-glib.h>
26
27 #include "nm-setting-team.h"
28 #include "nm-param-spec-specialized.h"
29 #include "nm-utils.h"
30 #include "nm-utils-private.h"
31 #include "nm-dbus-glib-types.h"
32 #include "nm-setting-private.h"
33
34 /**
35  * SECTION:nm-setting-team
36  * @short_description: Describes connection properties for teams
37  * @include: nm-setting-team.h
38  *
39  * The #NMSettingTeam object is a #NMSetting subclass that describes properties
40  * necessary for team connections.
41  *
42  * Since: 0.9.10
43  **/
44
45 /**
46  * nm_setting_team_error_quark:
47  *
48  * Registers an error quark for #NMSettingTeam if necessary.
49  *
50  * Returns: the error quark used for #NMSettingTeam errors.
51  *
52  * Since: 0.9.10
53  **/
54 GQuark
55 nm_setting_team_error_quark (void)
56 {
57         static GQuark quark;
58
59         if (G_UNLIKELY (!quark))
60                 quark = g_quark_from_static_string ("nm-setting-team-error-quark");
61         return quark;
62 }
63
64
65 G_DEFINE_TYPE_WITH_CODE (NMSettingTeam, nm_setting_team, NM_TYPE_SETTING,
66                          _nm_register_setting (NM_SETTING_TEAM_SETTING_NAME,
67                                                g_define_type_id,
68                                                1,
69                                                NM_SETTING_TEAM_ERROR))
70 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_TEAM)
71
72 #define NM_SETTING_TEAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_TEAM, NMSettingTeamPrivate))
73
74 typedef struct {
75         char *interface_name;
76         char *config;
77 } NMSettingTeamPrivate;
78
79 enum {
80         PROP_0,
81         PROP_INTERFACE_NAME,
82         PROP_CONFIG,
83         LAST_PROP
84 };
85
86 /**
87  * nm_setting_team_new:
88  *
89  * Creates a new #NMSettingTeam object with default values.
90  *
91  * Returns: (transfer full): the new empty #NMSettingTeam object
92  *
93  * Since: 0.9.10
94  **/
95 NMSetting *
96 nm_setting_team_new (void)
97 {
98         return (NMSetting *) g_object_new (NM_TYPE_SETTING_TEAM, NULL);
99 }
100
101 /**
102  * nm_setting_team_get_interface_name:
103  * @setting: the #NMSettingTeam
104  *
105  * Returns: the #NMSettingTeam:interface-name property of the setting
106  *
107  * Since: 0.9.10
108  **/
109 const char *
110 nm_setting_team_get_interface_name (NMSettingTeam *setting)
111 {
112         g_return_val_if_fail (NM_IS_SETTING_TEAM (setting), NULL);
113
114         return NM_SETTING_TEAM_GET_PRIVATE (setting)->interface_name;
115 }
116
117 /**
118  * nm_setting_team_get_config:
119  * @setting: the #NMSettingTeam
120  *
121  * Returns: the #NMSettingTeam:config property of the setting
122  *
123  * Since: 0.9.10
124  **/
125 const char *
126 nm_setting_team_get_config (NMSettingTeam *setting)
127 {
128         g_return_val_if_fail (NM_IS_SETTING_TEAM (setting), NULL);
129
130         return NM_SETTING_TEAM_GET_PRIVATE (setting)->config;
131 }
132
133 static gboolean
134 verify (NMSetting *setting, GSList *all_settings, GError **error)
135 {
136         NMSettingTeamPrivate *priv = NM_SETTING_TEAM_GET_PRIVATE (setting);
137
138         return _nm_setting_verify_deprecated_virtual_iface_name (
139                  priv->interface_name, FALSE,
140                  NM_SETTING_TEAM_SETTING_NAME, NM_SETTING_TEAM_INTERFACE_NAME,
141                  NM_SETTING_TEAM_ERROR,
142                  NM_SETTING_TEAM_ERROR_INVALID_PROPERTY,
143                  NM_SETTING_TEAM_ERROR_MISSING_PROPERTY,
144                  all_settings, error);
145 }
146
147 static const char *
148 get_virtual_iface_name (NMSetting *setting)
149 {
150         NMSettingTeam *self = NM_SETTING_TEAM (setting);
151
152         return nm_setting_team_get_interface_name (self);
153 }
154
155 static void
156 nm_setting_team_init (NMSettingTeam *setting)
157 {
158 }
159
160 static void
161 finalize (GObject *object)
162 {
163         NMSettingTeamPrivate *priv = NM_SETTING_TEAM_GET_PRIVATE (object);
164
165         g_free (priv->interface_name);
166         g_free (priv->config);
167
168         G_OBJECT_CLASS (nm_setting_team_parent_class)->finalize (object);
169 }
170
171 static void
172 set_property (GObject *object, guint prop_id,
173               const GValue *value, GParamSpec *pspec)
174 {
175         NMSettingTeamPrivate *priv = NM_SETTING_TEAM_GET_PRIVATE (object);
176
177         switch (prop_id) {
178         case PROP_INTERFACE_NAME:
179                 g_free (priv->interface_name);
180                 priv->interface_name = g_value_dup_string (value);
181                 break;
182         case PROP_CONFIG:
183                 g_free (priv->config);
184                 priv->config = g_value_dup_string (value);
185                 break;
186         default:
187                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188                 break;
189         }
190 }
191
192 static void
193 get_property (GObject *object, guint prop_id,
194               GValue *value, GParamSpec *pspec)
195 {
196         NMSettingTeam *setting = NM_SETTING_TEAM (object);
197
198         switch (prop_id) {
199         case PROP_INTERFACE_NAME:
200                 g_value_set_string (value, nm_setting_team_get_interface_name (setting));
201                 break;
202         case PROP_CONFIG:
203                 g_value_set_string (value, nm_setting_team_get_config (setting));
204                 break;
205         default:
206                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207                 break;
208         }
209 }
210
211 static void
212 nm_setting_team_class_init (NMSettingTeamClass *setting_class)
213 {
214         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
215         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
216
217         g_type_class_add_private (setting_class, sizeof (NMSettingTeamPrivate));
218
219         /* virtual methods */
220         object_class->set_property = set_property;
221         object_class->get_property = get_property;
222         object_class->finalize     = finalize;
223         parent_class->verify       = verify;
224         parent_class->get_virtual_iface_name = get_virtual_iface_name;
225
226         /* Properties */
227         /**
228          * NMSettingTeam:interface-name:
229          *
230          * The name of the virtual in-kernel team network interface
231          **/
232         g_object_class_install_property
233                 (object_class, PROP_INTERFACE_NAME,
234                  g_param_spec_string (NM_SETTING_TEAM_INTERFACE_NAME, "", "",
235                                       NULL,
236                                       G_PARAM_READWRITE |
237                                       NM_SETTING_PARAM_INFERRABLE |
238                                       G_PARAM_STATIC_STRINGS));
239
240         /**
241          * NMSettingTeam:config:
242          *
243          * The JSON configuration for the team network interface.  The property
244          * should contain raw JSON configuration data suitable for teamd, because
245          * the value is passed directly to teamd. If not specified, the default
246          * configuration is used.  See man teamd.conf for the format details.
247          **/
248         g_object_class_install_property
249                 (object_class, PROP_CONFIG,
250                  g_param_spec_string (NM_SETTING_TEAM_CONFIG, "", "",
251                                       NULL,
252                                       G_PARAM_READWRITE |
253                                       NM_SETTING_PARAM_INFERRABLE |
254                                       G_PARAM_STATIC_STRINGS));
255 }