device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-team-port.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 <ctype.h>
25 #include <stdlib.h>
26
27 #include "nm-setting-team-port.h"
28 #include "nm-utils.h"
29 #include "nm-utils-private.h"
30 #include "nm-connection-private.h"
31 #include "nm-setting-connection.h"
32 #include "nm-setting-team.h"
33
34 /**
35  * SECTION:nm-setting-team-port
36  * @short_description: Describes connection properties for team ports
37  *
38  * The #NMSettingTeamPort object is a #NMSetting subclass that describes
39  * optional properties that apply to team ports.
40  **/
41
42 G_DEFINE_TYPE_WITH_CODE (NMSettingTeamPort, nm_setting_team_port, NM_TYPE_SETTING,
43                          _nm_register_setting (TEAM_PORT, 3))
44 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_TEAM_PORT)
45
46 #define NM_SETTING_TEAM_PORT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_TEAM_PORT, NMSettingTeamPortPrivate))
47
48 typedef struct {
49         char *config;
50 } NMSettingTeamPortPrivate;
51
52 enum {
53         PROP_0,
54         PROP_CONFIG,
55         LAST_PROP
56 };
57
58 /**
59  * nm_setting_team_port_new:
60  *
61  * Creates a new #NMSettingTeamPort object with default values.
62  *
63  * Returns: (transfer full): the new empty #NMSettingTeamPort object
64  **/
65 NMSetting *
66 nm_setting_team_port_new (void)
67 {
68         return (NMSetting *) g_object_new (NM_TYPE_SETTING_TEAM_PORT, NULL);
69 }
70
71 /**
72  * nm_setting_team_port_get_config:
73  * @setting: the #NMSettingTeamPort
74  *
75  * Returns: the #NMSettingTeamPort:config property of the setting
76  **/
77 const char *
78 nm_setting_team_port_get_config (NMSettingTeamPort *setting)
79 {
80         g_return_val_if_fail (NM_IS_SETTING_TEAM_PORT (setting), NULL);
81
82         return NM_SETTING_TEAM_PORT_GET_PRIVATE (setting)->config;
83 }
84
85 static gboolean
86 verify (NMSetting *setting, NMConnection *connection, GError **error)
87 {
88         if (connection) {
89                 NMSettingConnection *s_con;
90                 const char *slave_type;
91
92                 s_con = nm_connection_get_setting_connection (connection);
93                 if (!s_con) {
94                         g_set_error (error,
95                                      NM_CONNECTION_ERROR,
96                                      NM_CONNECTION_ERROR_MISSING_SETTING,
97                                      _("missing setting"));
98                         g_prefix_error (error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME);
99                         return FALSE;
100                 }
101
102                 slave_type = nm_setting_connection_get_slave_type (s_con);
103                 if (   slave_type
104                     && strcmp (slave_type, NM_SETTING_TEAM_SETTING_NAME)) {
105                         g_set_error (error,
106                                      NM_CONNECTION_ERROR,
107                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
108                                      _("A connection with a '%s' setting must have the slave-type set to '%s'. Instead it is '%s'"),
109                                      NM_SETTING_TEAM_PORT_SETTING_NAME,
110                                      NM_SETTING_TEAM_SETTING_NAME,
111                                      slave_type);
112                         g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_SLAVE_TYPE);
113                         return FALSE;
114                 }
115         }
116         return TRUE;
117 }
118
119 static void
120 nm_setting_team_port_init (NMSettingTeamPort *setting)
121 {
122 }
123
124 static void
125 set_property (GObject *object, guint prop_id,
126               const GValue *value, GParamSpec *pspec)
127 {
128         NMSettingTeamPortPrivate *priv = NM_SETTING_TEAM_PORT_GET_PRIVATE (object);
129
130         switch (prop_id) {
131         case PROP_CONFIG:
132                 g_free (priv->config);
133                 priv->config = g_value_dup_string (value);
134                 break;
135         default:
136                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137                 break;
138         }
139 }
140
141 static void
142 get_property (GObject *object, guint prop_id,
143               GValue *value, GParamSpec *pspec)
144 {
145         NMSettingTeamPort *setting = NM_SETTING_TEAM_PORT (object);
146
147         switch (prop_id) {
148         case PROP_CONFIG:
149                 g_value_set_string (value, nm_setting_team_port_get_config (setting));
150                 break;
151         default:
152                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153                 break;
154         }
155 }
156
157 static void
158 finalize (GObject *object)
159 {
160         NMSettingTeamPortPrivate *priv = NM_SETTING_TEAM_PORT_GET_PRIVATE (object);
161
162         g_free (priv->config);
163
164         G_OBJECT_CLASS (nm_setting_team_port_parent_class)->finalize (object);
165 }
166
167 static void
168 nm_setting_team_port_class_init (NMSettingTeamPortClass *setting_class)
169 {
170         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
171         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
172
173         g_type_class_add_private (setting_class, sizeof (NMSettingTeamPortPrivate));
174
175         /* virtual methods */
176         object_class->set_property = set_property;
177         object_class->get_property = get_property;
178         object_class->finalize = finalize;
179         parent_class->verify       = verify;
180
181         /* Properties */
182         /**
183          * NMSettingTeamPort:config:
184          *
185          * The JSON configuration for the team port. The property should contain raw
186          * JSON configuration data suitable for teamd, because the value is passed
187          * directly to teamd. If not specified, the default configuration is
188          * used. See man teamd.conf for the format details.
189          **/
190         /* ---ifcfg-rh---
191          * property: config
192          * variable: TEAM_PORT_CONFIG
193          * description: Team port configuration in JSON. See man teamd.conf for details.
194          * ---end---
195          */
196         g_object_class_install_property
197                 (object_class, PROP_CONFIG,
198                  g_param_spec_string (NM_SETTING_TEAM_PORT_CONFIG, "", "",
199                                       NULL,
200                                       G_PARAM_READWRITE |
201                                       NM_SETTING_PARAM_INFERRABLE |
202                                       G_PARAM_STATIC_STRINGS));
203 }