device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / 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
26 #include "nm-setting-team.h"
27 #include "nm-utils.h"
28 #include "nm-utils-private.h"
29 #include "nm-connection-private.h"
30
31 /**
32  * SECTION:nm-setting-team
33  * @short_description: Describes connection properties for teams
34  *
35  * The #NMSettingTeam object is a #NMSetting subclass that describes properties
36  * necessary for team connections.
37  **/
38
39 G_DEFINE_TYPE_WITH_CODE (NMSettingTeam, nm_setting_team, NM_TYPE_SETTING,
40                          _nm_register_setting (TEAM, 1))
41 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_TEAM)
42
43 #define NM_SETTING_TEAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_TEAM, NMSettingTeamPrivate))
44
45 typedef struct {
46         char *config;
47 } NMSettingTeamPrivate;
48
49 enum {
50         PROP_0,
51         PROP_CONFIG,
52         LAST_PROP
53 };
54
55 /**
56  * nm_setting_team_new:
57  *
58  * Creates a new #NMSettingTeam object with default values.
59  *
60  * Returns: (transfer full): the new empty #NMSettingTeam object
61  **/
62 NMSetting *
63 nm_setting_team_new (void)
64 {
65         return (NMSetting *) g_object_new (NM_TYPE_SETTING_TEAM, NULL);
66 }
67
68 /**
69  * nm_setting_team_get_config:
70  * @setting: the #NMSettingTeam
71  *
72  * Returns: the #NMSettingTeam:config property of the setting
73  **/
74 const char *
75 nm_setting_team_get_config (NMSettingTeam *setting)
76 {
77         g_return_val_if_fail (NM_IS_SETTING_TEAM (setting), NULL);
78
79         return NM_SETTING_TEAM_GET_PRIVATE (setting)->config;
80 }
81
82 static gboolean
83 verify (NMSetting *setting, NMConnection *connection, GError **error)
84 {
85         return _nm_connection_verify_required_interface_name (connection, error);
86 }
87
88 static void
89 nm_setting_team_init (NMSettingTeam *setting)
90 {
91 }
92
93 static void
94 finalize (GObject *object)
95 {
96         NMSettingTeamPrivate *priv = NM_SETTING_TEAM_GET_PRIVATE (object);
97
98         g_free (priv->config);
99
100         G_OBJECT_CLASS (nm_setting_team_parent_class)->finalize (object);
101 }
102
103 static void
104 set_property (GObject *object, guint prop_id,
105               const GValue *value, GParamSpec *pspec)
106 {
107         NMSettingTeamPrivate *priv = NM_SETTING_TEAM_GET_PRIVATE (object);
108
109         switch (prop_id) {
110         case PROP_CONFIG:
111                 g_free (priv->config);
112                 priv->config = g_value_dup_string (value);
113                 break;
114         default:
115                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116                 break;
117         }
118 }
119
120 static void
121 get_property (GObject *object, guint prop_id,
122               GValue *value, GParamSpec *pspec)
123 {
124         NMSettingTeam *setting = NM_SETTING_TEAM (object);
125
126         switch (prop_id) {
127         case PROP_CONFIG:
128                 g_value_set_string (value, nm_setting_team_get_config (setting));
129                 break;
130         default:
131                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132                 break;
133         }
134 }
135
136 static void
137 nm_setting_team_class_init (NMSettingTeamClass *setting_class)
138 {
139         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
140         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
141
142         g_type_class_add_private (setting_class, sizeof (NMSettingTeamPrivate));
143
144         /* virtual methods */
145         object_class->set_property = set_property;
146         object_class->get_property = get_property;
147         object_class->finalize     = finalize;
148         parent_class->verify       = verify;
149
150         /* Properties */
151         /**
152          * NMSettingTeam:config:
153          *
154          * The JSON configuration for the team network interface.  The property
155          * should contain raw JSON configuration data suitable for teamd, because
156          * the value is passed directly to teamd. If not specified, the default
157          * configuration is used.  See man teamd.conf for the format details.
158          **/
159         /* ---ifcfg-rh---
160          * property: config
161          * variable: TEAM_CONFIG
162          * description: Team configuration in JSON. See man teamd.conf for details.
163          * ---end---
164          */
165         g_object_class_install_property
166                 (object_class, PROP_CONFIG,
167                  g_param_spec_string (NM_SETTING_TEAM_CONFIG, "", "",
168                                       NULL,
169                                       G_PARAM_READWRITE |
170                                       NM_SETTING_PARAM_INFERRABLE |
171                                       G_PARAM_STATIC_STRINGS));
172
173         /* ---dbus---
174          * property: interface-name
175          * format: string
176          * description: Deprecated in favor of connection.interface-name, but can
177          *   be used for backward-compatibility with older daemons, to set the
178          *   team's interface name.
179          * ---end---
180          */
181         _nm_setting_class_add_dbus_only_property (parent_class, "interface-name",
182                                                   G_VARIANT_TYPE_STRING,
183                                                   _nm_setting_get_deprecated_virtual_interface_name,
184                                                   NULL);
185 }