device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-device-infiniband.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 2011 - 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-setting-connection.h"
26 #include "nm-setting-infiniband.h"
27 #include "nm-utils.h"
28
29 #include "nm-device-infiniband.h"
30 #include "nm-device-private.h"
31 #include "nm-object-private.h"
32
33 G_DEFINE_TYPE (NMDeviceInfiniband, nm_device_infiniband, NM_TYPE_DEVICE)
34
35 #define NM_DEVICE_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_INFINIBAND, NMDeviceInfinibandPrivate))
36
37 typedef struct {
38         char *hw_address;
39         gboolean carrier;
40 } NMDeviceInfinibandPrivate;
41
42 enum {
43         PROP_0,
44         PROP_HW_ADDRESS,
45         PROP_CARRIER,
46
47         LAST_PROP
48 };
49
50 /**
51  * nm_device_infiniband_get_hw_address:
52  * @device: a #NMDeviceInfiniband
53  *
54  * Gets the hardware (MAC) address of the #NMDeviceInfiniband
55  *
56  * Returns: the hardware address. This is the internal string used by the
57  * device, and must not be modified.
58  **/
59 const char *
60 nm_device_infiniband_get_hw_address (NMDeviceInfiniband *device)
61 {
62         g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), NULL);
63
64         return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->hw_address;
65 }
66
67 /**
68  * nm_device_infiniband_get_carrier:
69  * @device: a #NMDeviceInfiniband
70  *
71  * Whether the device has carrier.
72  *
73  * Returns: %TRUE if the device has carrier
74  **/
75 gboolean
76 nm_device_infiniband_get_carrier (NMDeviceInfiniband *device)
77 {
78         g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), FALSE);
79
80         return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->carrier;
81 }
82
83 static gboolean
84 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
85 {
86         NMSettingInfiniband *s_infiniband;
87         const char *hwaddr, *setting_hwaddr;
88
89         if (!NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_compatible (device, connection, error))
90                 return FALSE;
91
92         if (!nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME)) {
93                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
94                                      _("The connection was not an InfiniBand connection."));
95                 return FALSE;
96         }
97
98         hwaddr = nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
99         if (hwaddr) {
100                 if (!nm_utils_hwaddr_valid (hwaddr, INFINIBAND_ALEN)) {
101                         g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
102                                              _("Invalid device MAC address."));
103                         return FALSE;
104                 }
105
106                 s_infiniband = nm_connection_get_setting_infiniband (connection);
107                 setting_hwaddr = nm_setting_infiniband_get_mac_address (s_infiniband);
108                 if (setting_hwaddr && !nm_utils_hwaddr_matches (setting_hwaddr, -1, hwaddr, -1)) {
109                         g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
110                                              _("The MACs of the device and the connection didn't match."));
111                         return FALSE;
112                 }
113         }
114
115         return TRUE;
116 }
117
118 static GType
119 get_setting_type (NMDevice *device)
120 {
121         return NM_TYPE_SETTING_INFINIBAND;
122 }
123
124 static const char *
125 get_hw_address (NMDevice *device)
126 {
127         return nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
128 }
129
130 /***********************************************************/
131
132 static void
133 nm_device_infiniband_init (NMDeviceInfiniband *device)
134 {
135         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_INFINIBAND);
136 }
137
138 static void
139 init_dbus (NMObject *object)
140 {
141         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
142         const NMPropertiesInfo property_info[] = {
143                 { NM_DEVICE_INFINIBAND_HW_ADDRESS, &priv->hw_address },
144                 { NM_DEVICE_INFINIBAND_CARRIER,    &priv->carrier },
145                 { NULL },
146         };
147
148         NM_OBJECT_CLASS (nm_device_infiniband_parent_class)->init_dbus (object);
149
150         _nm_object_register_properties (object,
151                                         NM_DBUS_INTERFACE_DEVICE_INFINIBAND,
152                                         property_info);
153 }
154
155 static void
156 finalize (GObject *object)
157 {
158         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
159
160         g_free (priv->hw_address);
161
162         G_OBJECT_CLASS (nm_device_infiniband_parent_class)->finalize (object);
163 }
164
165 static void
166 get_property (GObject *object,
167               guint prop_id,
168               GValue *value,
169               GParamSpec *pspec)
170 {
171         NMDeviceInfiniband *device = NM_DEVICE_INFINIBAND (object);
172
173         switch (prop_id) {
174         case PROP_HW_ADDRESS:
175                 g_value_set_string (value, nm_device_infiniband_get_hw_address (device));
176                 break;
177         case PROP_CARRIER:
178                 g_value_set_boolean (value, nm_device_infiniband_get_carrier (device));
179                 break;
180         default:
181                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182                 break;
183         }
184 }
185
186 static void
187 nm_device_infiniband_class_init (NMDeviceInfinibandClass *ib_class)
188 {
189         GObjectClass *object_class = G_OBJECT_CLASS (ib_class);
190         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (ib_class);
191         NMDeviceClass *device_class = NM_DEVICE_CLASS (ib_class);
192
193         g_type_class_add_private (ib_class, sizeof (NMDeviceInfinibandPrivate));
194
195         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_INFINIBAND);
196
197         /* virtual methods */
198         object_class->finalize = finalize;
199         object_class->get_property = get_property;
200
201         nm_object_class->init_dbus = init_dbus;
202
203         device_class->connection_compatible = connection_compatible;
204         device_class->get_setting_type = get_setting_type;
205         device_class->get_hw_address = get_hw_address;
206
207         /* properties */
208
209         /**
210          * NMDeviceInfiniband:hw-address:
211          *
212          * The hardware (MAC) address of the device.
213          **/
214         g_object_class_install_property
215                 (object_class, PROP_HW_ADDRESS,
216                  g_param_spec_string (NM_DEVICE_INFINIBAND_HW_ADDRESS, "", "",
217                                       NULL,
218                                       G_PARAM_READABLE |
219                                       G_PARAM_STATIC_STRINGS));
220
221         /**
222          * NMDeviceInfiniband:carrier:
223          *
224          * Whether the device has carrier.
225          **/
226         g_object_class_install_property
227                 (object_class, PROP_CARRIER,
228                  g_param_spec_boolean (NM_DEVICE_INFINIBAND_CARRIER, "", "",
229                                        FALSE,
230                                        G_PARAM_READABLE |
231                                        G_PARAM_STATIC_STRINGS));
232
233 }