~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/network/net-device.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2011 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib-object.h>
 
25
#include <glib/gi18n.h>
 
26
#include <arpa/inet.h>
 
27
#include <netinet/ether.h>
 
28
 
 
29
#include <nm-device-ethernet.h>
 
30
#include <nm-device-modem.h>
 
31
#include <nm-device-wifi.h>
 
32
#include <nm-device-wimax.h>
 
33
#include <nm-device-infiniband.h>
 
34
#include <nm-utils.h>
 
35
 
 
36
#include "net-device.h"
 
37
 
 
38
#define NET_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE, NetDevicePrivate))
 
39
 
 
40
struct _NetDevicePrivate
 
41
{
 
42
        NMDevice                        *nm_device;
 
43
        guint                            changed_id;
 
44
};
 
45
 
 
46
enum {
 
47
        PROP_0,
 
48
        PROP_DEVICE,
 
49
        PROP_LAST
 
50
};
 
51
 
 
52
G_DEFINE_TYPE (NetDevice, net_device, NET_TYPE_OBJECT)
 
53
 
 
54
/* return value must be freed by caller with g_free() */
 
55
static gchar *
 
56
get_mac_address_of_connection (NMConnection *connection)
 
57
{
 
58
        if (!connection)
 
59
                return NULL;
 
60
 
 
61
        const GByteArray *mac = NULL;
 
62
 
 
63
        /* check the connection type */
 
64
        if (nm_connection_is_type (connection,
 
65
                                   NM_SETTING_WIRELESS_SETTING_NAME)) {
 
66
                /* check wireless settings */
 
67
                NMSettingWireless *s_wireless = nm_connection_get_setting_wireless (connection);
 
68
                if (!s_wireless)
 
69
                        return NULL;
 
70
                mac = nm_setting_wireless_get_mac_address (s_wireless);
 
71
                if (mac)
 
72
                        return nm_utils_hwaddr_ntoa (mac->data, ARPHRD_ETHER);
 
73
        } else if (nm_connection_is_type (connection,
 
74
                                          NM_SETTING_WIRED_SETTING_NAME)) {
 
75
                /* check wired settings */
 
76
                NMSettingWired *s_wired = nm_connection_get_setting_wired (connection);
 
77
                if (!s_wired)
 
78
                        return NULL;
 
79
                mac = nm_setting_wired_get_mac_address (s_wired);
 
80
                if (mac)
 
81
                        return nm_utils_hwaddr_ntoa (mac->data, ARPHRD_ETHER);
 
82
        } else if (nm_connection_is_type (connection,
 
83
                                          NM_SETTING_WIMAX_SETTING_NAME)) {
 
84
                /* check wimax settings */
 
85
                NMSettingWimax *s_wimax = nm_connection_get_setting_wimax (connection);
 
86
                if (!s_wimax)
 
87
                        return NULL;
 
88
                mac = nm_setting_wimax_get_mac_address (s_wimax);
 
89
                if (mac)
 
90
                        return nm_utils_hwaddr_ntoa (mac->data, ARPHRD_ETHER);
 
91
        } else if (nm_connection_is_type (connection,
 
92
                                          NM_SETTING_INFINIBAND_SETTING_NAME)) {
 
93
                /* check infiniband settings */
 
94
                NMSettingInfiniband *s_infiniband = \
 
95
                        nm_connection_get_setting_infiniband (connection);
 
96
                if (!s_infiniband)
 
97
                        return NULL;
 
98
                mac = nm_setting_infiniband_get_mac_address (s_infiniband);
 
99
                if (mac)
 
100
                        return nm_utils_hwaddr_ntoa (mac->data,
 
101
                                                     ARPHRD_INFINIBAND);
 
102
        }
 
103
        /* no MAC address found */
 
104
        return NULL;
 
105
}
 
106
 
 
107
/* return value must not be freed! */
 
108
static const gchar *
 
109
get_mac_address_of_device (NMDevice *device)
 
110
{
 
111
        const gchar *mac = NULL;
 
112
        switch (nm_device_get_device_type (device)) {
 
113
        case NM_DEVICE_TYPE_WIFI:
 
114
        {
 
115
                NMDeviceWifi *device_wifi = NM_DEVICE_WIFI (device);
 
116
                mac = nm_device_wifi_get_hw_address (device_wifi);
 
117
                break;
 
118
        }
 
119
        case NM_DEVICE_TYPE_ETHERNET:
 
120
        {
 
121
                NMDeviceEthernet *device_ethernet = NM_DEVICE_ETHERNET (device);
 
122
                mac = nm_device_ethernet_get_hw_address (device_ethernet);
 
123
                break;
 
124
        }
 
125
        case NM_DEVICE_TYPE_WIMAX:
 
126
        {
 
127
                NMDeviceWimax *device_wimax = NM_DEVICE_WIMAX (device);
 
128
                mac = nm_device_wimax_get_hw_address (device_wimax);
 
129
                break;
 
130
        }
 
131
        case NM_DEVICE_TYPE_INFINIBAND:
 
132
        {
 
133
                NMDeviceInfiniband *device_infiniband = \
 
134
                        NM_DEVICE_INFINIBAND (device);
 
135
                mac = nm_device_infiniband_get_hw_address (device_infiniband);
 
136
                break;
 
137
        }
 
138
        default:
 
139
                break;
 
140
        }
 
141
        /* no MAC address found */
 
142
        return mac;
 
143
}
 
144
 
 
145
/* returns TRUE if both MACs are equal */
 
146
static gboolean
 
147
compare_mac_device_with_mac_connection (NMDevice *device,
 
148
                                        NMConnection *connection)
 
149
{
 
150
        const gchar *mac_dev = NULL;
 
151
        gchar *mac_conn = NULL;
 
152
 
 
153
        mac_dev = get_mac_address_of_device (device);
 
154
        if (mac_dev != NULL) {
 
155
                mac_conn = get_mac_address_of_connection (connection);
 
156
                if (mac_conn) {
 
157
                        /* compare both MACs */
 
158
                        if (g_strcmp0 (mac_dev, mac_conn) == 0) {
 
159
                                g_free (mac_conn);
 
160
                                return TRUE;
 
161
                        }
 
162
                        g_free (mac_conn);
 
163
                }
 
164
        }
 
165
        return FALSE;
 
166
}
 
167
 
 
168
static GSList *
 
169
valid_connections_for_device (NMRemoteSettings *remote_settings,
 
170
                              NetDevice *device)
 
171
{
 
172
        GSList *all, *filtered, *iterator, *valid;
 
173
        NMConnection *connection;
 
174
        NMSettingConnection *s_con;
 
175
 
 
176
        all = nm_remote_settings_list_connections (remote_settings);
 
177
        filtered = nm_device_filter_connections (device->priv->nm_device, all);
 
178
        g_slist_free (all);
 
179
 
 
180
        valid = NULL;
 
181
        for (iterator = filtered; iterator; iterator = iterator->next) {
 
182
                connection = iterator->data;
 
183
                s_con = nm_connection_get_setting_connection (connection);
 
184
                if (!s_con)
 
185
                        continue;
 
186
 
 
187
                if (nm_setting_connection_get_master (s_con))
 
188
                        continue;
 
189
 
 
190
                valid = g_slist_prepend (valid, connection);
 
191
        }
 
192
        g_slist_free (filtered);
 
193
 
 
194
        return g_slist_reverse (valid);
 
195
}
 
196
 
 
197
NMConnection *
 
198
net_device_get_find_connection (NetDevice *device)
 
199
{
 
200
        GSList *list, *iterator;
 
201
        NMConnection *connection = NULL;
 
202
        NMActiveConnection *ac;
 
203
        NMRemoteSettings *remote_settings;
 
204
 
 
205
        /* is the device available in a active connection? */
 
206
        remote_settings = net_object_get_remote_settings (NET_OBJECT (device));
 
207
        ac = nm_device_get_active_connection (device->priv->nm_device);
 
208
        if (ac) {
 
209
                return (NMConnection*)nm_remote_settings_get_connection_by_path (remote_settings,
 
210
                                        nm_active_connection_get_connection (ac));
 
211
        }
 
212
 
 
213
        /* not found in active connections - check all available connections */
 
214
        list = valid_connections_for_device (remote_settings, device);
 
215
        if (list != NULL) {
 
216
                /* if list has only one connection, use this connection */
 
217
                if (g_slist_length (list) == 1) {
 
218
                        connection = list->data;
 
219
                        goto out;
 
220
                }
 
221
 
 
222
                /* is there connection with the MAC address of the device? */
 
223
                for (iterator = list; iterator; iterator = iterator->next) {
 
224
                        connection = iterator->data;
 
225
                        if (compare_mac_device_with_mac_connection (device->priv->nm_device,
 
226
                                                                    connection)) {
 
227
                                goto out;
 
228
                        }
 
229
                }
 
230
        }
 
231
 
 
232
        /* no connection found for the given device */
 
233
        connection = NULL;
 
234
out:
 
235
        g_slist_free (list);
 
236
        return connection;
 
237
}
 
238
 
 
239
static void
 
240
state_changed_cb (NMDevice *device,
 
241
                  NMDeviceState new_state,
 
242
                  NMDeviceState old_state,
 
243
                  NMDeviceStateReason reason,
 
244
                  NetDevice *net_device)
 
245
{
 
246
        net_object_emit_changed (NET_OBJECT (net_device));
 
247
        net_object_refresh (NET_OBJECT (net_device));
 
248
}
 
249
 
 
250
NMDevice *
 
251
net_device_get_nm_device (NetDevice *device)
 
252
{
 
253
        return device->priv->nm_device;
 
254
}
 
255
 
 
256
static void
 
257
net_device_edit (NetObject *object)
 
258
{
 
259
        const gchar *uuid;
 
260
        gchar *cmdline;
 
261
        GError *error = NULL;
 
262
        NetDevice *device = NET_DEVICE (object);
 
263
        NMConnection *connection;
 
264
 
 
265
        connection = net_device_get_find_connection (device);
 
266
        uuid = nm_connection_get_uuid (connection);
 
267
        cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid);
 
268
        g_debug ("Launching '%s'\n", cmdline);
 
269
        if (!g_spawn_command_line_async (cmdline, &error)) {
 
270
                g_warning ("Failed to launch nm-connection-editor: %s", error->message);
 
271
                g_error_free (error);
 
272
        }
 
273
        g_free (cmdline);
 
274
}
 
275
 
 
276
/**
 
277
 * net_device_get_property:
 
278
 **/
 
279
static void
 
280
net_device_get_property (GObject *device_,
 
281
                         guint prop_id,
 
282
                         GValue *value,
 
283
                         GParamSpec *pspec)
 
284
{
 
285
        NetDevice *net_device = NET_DEVICE (device_);
 
286
        NetDevicePrivate *priv = net_device->priv;
 
287
 
 
288
        switch (prop_id) {
 
289
        case PROP_DEVICE:
 
290
                g_value_set_object (value, priv->nm_device);
 
291
                break;
 
292
        default:
 
293
                G_OBJECT_WARN_INVALID_PROPERTY_ID (net_device, prop_id, pspec);
 
294
                break;
 
295
        }
 
296
}
 
297
 
 
298
/**
 
299
 * net_device_set_property:
 
300
 **/
 
301
static void
 
302
net_device_set_property (GObject *device_,
 
303
                         guint prop_id,
 
304
                         const GValue *value,
 
305
                         GParamSpec *pspec)
 
306
{
 
307
        NetDevice *net_device = NET_DEVICE (device_);
 
308
        NetDevicePrivate *priv = net_device->priv;
 
309
 
 
310
        switch (prop_id) {
 
311
        case PROP_DEVICE:
 
312
                if (priv->changed_id != 0) {
 
313
                        g_signal_handler_disconnect (priv->nm_device,
 
314
                                                     priv->changed_id);
 
315
                }
 
316
                priv->nm_device = g_value_dup_object (value);
 
317
                priv->changed_id = g_signal_connect (priv->nm_device,
 
318
                                                     "state-changed",
 
319
                                                     G_CALLBACK (state_changed_cb),
 
320
                                                     net_device);
 
321
                break;
 
322
        default:
 
323
                G_OBJECT_WARN_INVALID_PROPERTY_ID (net_device, prop_id, pspec);
 
324
                break;
 
325
        }
 
326
}
 
327
 
 
328
static void
 
329
net_device_finalize (GObject *object)
 
330
{
 
331
        NetDevice *device = NET_DEVICE (object);
 
332
        NetDevicePrivate *priv = device->priv;
 
333
 
 
334
        if (priv->changed_id != 0) {
 
335
                g_signal_handler_disconnect (priv->nm_device,
 
336
                                             priv->changed_id);
 
337
        }
 
338
        if (priv->nm_device != NULL)
 
339
                g_object_unref (priv->nm_device);
 
340
 
 
341
        G_OBJECT_CLASS (net_device_parent_class)->finalize (object);
 
342
}
 
343
 
 
344
static void
 
345
net_device_class_init (NetDeviceClass *klass)
 
346
{
 
347
        GParamSpec *pspec;
 
348
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
349
        NetObjectClass *parent_class = NET_OBJECT_CLASS (klass);
 
350
 
 
351
        object_class->finalize = net_device_finalize;
 
352
        object_class->get_property = net_device_get_property;
 
353
        object_class->set_property = net_device_set_property;
 
354
        parent_class->edit = net_device_edit;
 
355
 
 
356
        pspec = g_param_spec_object ("nm-device", NULL, NULL,
 
357
                                     NM_TYPE_DEVICE,
 
358
                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
 
359
        g_object_class_install_property (object_class, PROP_DEVICE, pspec);
 
360
 
 
361
        g_type_class_add_private (klass, sizeof (NetDevicePrivate));
 
362
}
 
363
 
 
364
static void
 
365
net_device_init (NetDevice *device)
 
366
{
 
367
        device->priv = NET_DEVICE_GET_PRIVATE (device);
 
368
}
 
369
 
 
370
NetDevice *
 
371
net_device_new (void)
 
372
{
 
373
        NetDevice *device;
 
374
        device = g_object_new (NET_TYPE_DEVICE,
 
375
                               "removable", FALSE,
 
376
                               NULL);
 
377
        return NET_DEVICE (device);
 
378
}
 
379