~asac/network-manager/main.eni

« back to all changes in this revision

Viewing changes to system-settings/plugins/ifupdown/plugin.c

  • Committer: Alexander Sack
  • Date: 2008-09-21 11:14:48 UTC
  • mfrom: (2752.1.791)
  • Revision ID: asac@jwsdot.com-20080921111448-bg6f03p9ju57m3sq
* merge Sat 2008-09-20 21:06:33 +0000 rev 3543 from upstream; this includes
  this merge contains the initial ifupdown upstream landing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
 
2
 
 
3
/* NetworkManager system settings service (ifupdown)
 
4
 *
 
5
 * Alexander Sack <asac@ubuntu.com>
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 *
 
21
 * (C) Copyright 2007,2008 Canonical Ltd.
 
22
 */
 
23
 
 
24
#include <string.h>
 
25
#include <sys/inotify.h>
 
26
 
 
27
#include <gmodule.h>
 
28
#include <glib-object.h>
 
29
#include <glib/gi18n.h>
 
30
#include <glib/gslist.h>
 
31
#include <nm-setting-connection.h>
 
32
 
 
33
#include "interface_parser.h"
 
34
 
 
35
#include "nm-system-config-interface.h"
 
36
#include "nm-setting-ip4-config.h"
 
37
#include "nm-setting-wireless.h"
 
38
#include "nm-setting-wired.h"
 
39
#include "nm-setting-ppp.h"
 
40
 
 
41
#include "nm-ifupdown-connection.h"
 
42
#include "plugin.h"
 
43
#include "parser.h"
 
44
 
 
45
#include <nm-utils.h>
 
46
#include <sha1.h>
 
47
 
 
48
#include <arpa/inet.h>
 
49
 
 
50
#define IFUPDOWN_PLUGIN_NAME "ifupdown"
 
51
#define IFUPDOWN_PLUGIN_INFO "(C) 2008 Canonical Ltd.  To report bugs please use the NetworkManager mailing list."
 
52
 
 
53
typedef struct {
 
54
 
 
55
        DBusGConnection *g_connection;
 
56
        NMSystemConfigHalManager *hal_mgr;
 
57
 
 
58
        GHashTable *iface_connections;
 
59
        
 
60
} SCPluginIfupdownPrivate;
 
61
 
 
62
static void
 
63
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
 
64
 
 
65
G_DEFINE_TYPE_EXTENDED (SCPluginIfupdown, sc_plugin_ifupdown, G_TYPE_OBJECT, 0,
 
66
                                    G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE,
 
67
                                                                          system_config_interface_init))
 
68
 
 
69
#define SC_PLUGIN_IFUPDOWN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_IFUPDOWN, SCPluginIfupdownPrivate))
 
70
 
 
71
static void
 
72
sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class);
 
73
 
 
74
static void
 
75
SCPluginIfupdown_init (NMSystemConfigInterface *config,
 
76
                                   NMSystemConfigHalManager *hal_manager);
 
77
 
 
78
/* Returns the plugins currently known list of connections.  The returned
 
79
 * list is freed by the system settings service.
 
80
 */
 
81
static GSList*
 
82
SCPluginIfupdown_get_connections (NMSystemConfigInterface *config);
 
83
 
 
84
/*
 
85
 * Return a list of HAL UDIs of devices which NetworkManager should not
 
86
 * manage.  Returned list will be freed by the system settings service, and
 
87
 * each element must be allocated using g_malloc() or its variants.
 
88
 */
 
89
static GSList*
 
90
SCPluginIfupdown_get_unmanaged_devices (NMSystemConfigInterface *config);
 
91
 
 
92
/* Signals */
 
93
 
 
94
/* Emitted when a new connection has been found by the plugin */
 
95
static void 
 
96
SCPluginIfupdown_connection_added (NMSystemConfigInterface *config,
 
97
                                                        NMExportedConnection *connection);
 
98
 
 
99
/* Emitted when the list of unmanaged devices changes */
 
100
static void
 
101
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config);
 
102
 
 
103
static void
 
104
GObject__get_property (GObject *object, guint prop_id,
 
105
                                   GValue *value, GParamSpec *pspec);
 
106
 
 
107
static void
 
108
GObject__dispose (GObject *object);
 
109
 
 
110
static void
 
111
GObject__finalize (GObject *object);
 
112
 
 
113
 
 
114
static void
 
115
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
 
116
{
 
117
        system_config_interface_class->init = SCPluginIfupdown_init;
 
118
        system_config_interface_class->get_connections = SCPluginIfupdown_get_connections;
 
119
        system_config_interface_class->get_unmanaged_devices = SCPluginIfupdown_get_unmanaged_devices;
 
120
        system_config_interface_class->connection_added = SCPluginIfupdown_connection_added;
 
121
        system_config_interface_class->unmanaged_devices_changed = SCPluginIfupdown_unmanaged_devices_changed;
 
122
}
 
123
 
 
124
static void
 
125
sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class)
 
126
{
 
127
        GObjectClass *object_class = G_OBJECT_CLASS (req_class);
 
128
 
 
129
        g_type_class_add_private (req_class, sizeof (SCPluginIfupdownPrivate));
 
130
 
 
131
        object_class->dispose = GObject__dispose;
 
132
        object_class->finalize = GObject__finalize;
 
133
        object_class->get_property = GObject__get_property;
 
134
 
 
135
        g_object_class_override_property (object_class,
 
136
                                          NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
 
137
                                          NM_SYSTEM_CONFIG_INTERFACE_NAME);
 
138
 
 
139
        g_object_class_override_property (object_class,
 
140
                                          NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
 
141
                                          NM_SYSTEM_CONFIG_INTERFACE_INFO);
 
142
 
 
143
        g_object_class_override_property (object_class,
 
144
                                          NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
 
145
                                          NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
 
146
 
 
147
        g_object_class_override_property (object_class,
 
148
                                          NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
 
149
                                          NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
 
150
}
 
151
 
 
152
static void
 
153
SCPluginIfupdown_init (NMSystemConfigInterface *config,
 
154
                                   NMSystemConfigHalManager *hal_manager)
 
155
{
 
156
        SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
 
157
        GHashTable *auto_ifaces = g_hash_table_new (g_str_hash, g_str_equal);
 
158
        if_block *block = NULL;
 
159
 
 
160
        if(!priv->iface_connections)
 
161
                priv->iface_connections = g_hash_table_new (g_str_hash, g_str_equal);
 
162
 
 
163
        PLUGIN_PRINT("SCPlugin-Ifupdown", "init!");
 
164
        priv->hal_mgr = g_object_ref (hal_manager);
 
165
 
 
166
        ifparser_init();
 
167
        block = ifparser_getfirst();
 
168
 
 
169
        while(block) {
 
170
                if(!strcmp("auto", block->type)) {
 
171
                        g_hash_table_insert (auto_ifaces, block->name, "auto");
 
172
                } else if (!strcmp ("iface", block->type) && strcmp ("lo", block->name)) {
 
173
                        NMExportedConnection *connection = g_hash_table_lookup(priv->iface_connections, block->name);
 
174
                        g_hash_table_remove (priv->iface_connections, block->name);
 
175
 
 
176
                        connection = NM_EXPORTED_CONNECTION(nm_ifupdown_connection_new(block));
 
177
                        ifupdown_update_connection_from_if_block (nm_exported_connection_get_connection(connection),
 
178
                                                                                          block);
 
179
 
 
180
                        g_hash_table_insert (priv->iface_connections, block->name, connection);
 
181
                }
 
182
                block = block -> next;
 
183
        }
 
184
 
 
185
        {
 
186
                GList *keys = g_hash_table_get_keys (priv->iface_connections);
 
187
                GList *key_it = keys;
 
188
                while(key_it) {
 
189
                        gpointer val = g_hash_table_lookup(auto_ifaces, key_it->data);
 
190
                        if(val) {
 
191
                                NMExportedConnection *connection =
 
192
                                        g_hash_table_lookup(priv->iface_connections, key_it->data);
 
193
                                NMConnection *wrapped = NULL;
 
194
                                NMSetting *setting;
 
195
                                g_object_get(connection,
 
196
                                                   NM_EXPORTED_CONNECTION_CONNECTION, &wrapped, NULL);
 
197
                                setting = NM_SETTING(nm_connection_get_setting
 
198
                                                                 (wrapped, NM_TYPE_SETTING_CONNECTION));
 
199
                                g_object_set (setting,
 
200
                                                    "autoconnect", TRUE,
 
201
                                                    NULL);
 
202
                                PLUGIN_PRINT("SCPlugin-Ifupdown", "autoconnect");
 
203
                        }
 
204
                        key_it = key_it -> next;
 
205
                }
 
206
        }
 
207
        g_hash_table_unref(auto_ifaces);
 
208
        PLUGIN_PRINT("SCPlugin-Ifupdown", "end _init.");
 
209
}
 
210
 
 
211
 
 
212
/* Returns the plugins currently known list of connections.  The returned
 
213
 * list is freed by the system settings service.
 
214
 */
 
215
static GSList*
 
216
SCPluginIfupdown_get_connections (NMSystemConfigInterface *config)
 
217
{
 
218
        SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
 
219
        GSList *connections = NULL;
 
220
        GList *priv_list = g_hash_table_get_values(priv->iface_connections);
 
221
        GList *it = priv_list;
 
222
        PLUGIN_PRINT("SCPlugin-Ifupdown", "(%d) ... get_connections.", GPOINTER_TO_UINT(config));
 
223
 
 
224
        while(it) {
 
225
                NMExportedConnection *conn = it->data;
 
226
                connections = g_slist_append(connections, conn);
 
227
                it = it->next;
 
228
        }
 
229
        PLUGIN_PRINT("SCPlugin-Ifupdown", "(%d) connections count: %d", GPOINTER_TO_UINT(config), g_slist_length(connections));
 
230
        return connections;
 
231
}
 
232
 
 
233
/*
 
234
 * Return a list of HAL UDIs of devices which NetworkManager should not
 
235
 * manage.  Returned list will be freed by the system settings service, and
 
236
 * each element must be allocated using g_malloc() or its variants.
 
237
 */
 
238
static GSList*
 
239
SCPluginIfupdown_get_unmanaged_devices (NMSystemConfigInterface *config)
 
240
{
 
241
        // XXX implement this.
 
242
        return NULL;
 
243
}
 
244
 
 
245
/* Signals */
 
246
 
 
247
/* Emitted when a new connection has been found by the plugin */
 
248
static void 
 
249
SCPluginIfupdown_connection_added (NMSystemConfigInterface *config,
 
250
                                                        NMExportedConnection *connection)
 
251
{
 
252
        PLUGIN_PRINT("SCPlugin-Ifdown", "connection_added ... started");
 
253
        g_return_if_fail (config != NULL);
 
254
        g_return_if_fail (NM_IS_CONNECTION (connection));
 
255
        PLUGIN_PRINT("SCPlugin-Ifdown", "connection_added ... ended");
 
256
}
 
257
 
 
258
/* Emitted when the list of unmanaged devices changes */
 
259
static void
 
260
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config)
 
261
{
 
262
        PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... started");
 
263
        g_return_if_fail (config != NULL);
 
264
 
 
265
        PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... ended");
 
266
}
 
267
 
 
268
static void
 
269
sc_plugin_ifupdown_init (SCPluginIfupdown *plugin)
 
270
{
 
271
        SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
 
272
        GError *error = NULL;
 
273
 
 
274
        priv->g_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 
275
        if (!priv->g_connection) {
 
276
                PLUGIN_PRINT (IFUPDOWN_PLUGIN_NAME, "    dbus-glib error: %s",
 
277
                              error->message ? error->message : "(unknown)");
 
278
                g_error_free (error);
 
279
        }
 
280
}
 
281
 
 
282
static void
 
283
GObject__get_property (GObject *object, guint prop_id,
 
284
                                   GValue *value, GParamSpec *pspec)
 
285
{
 
286
        switch (prop_id) {
 
287
        case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
 
288
                g_value_set_string (value, IFUPDOWN_PLUGIN_NAME);
 
289
                break;
 
290
        case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
 
291
                g_value_set_string (value, IFUPDOWN_PLUGIN_INFO);
 
292
                break;
 
293
        case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
 
294
                g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
 
295
                break;
 
296
        case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
 
297
                g_value_set_string (value, "");
 
298
                break;
 
299
        default:
 
300
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
301
                break;
 
302
        }
 
303
}
 
304
 
 
305
static void
 
306
GObject__dispose (GObject *object)
 
307
{
 
308
        SCPluginIfupdown *plugin = SC_PLUGIN_IFUPDOWN (object);
 
309
        SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
 
310
 
 
311
        g_object_unref (priv->hal_mgr);
 
312
        G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->dispose (object);
 
313
}
 
314
 
 
315
static void
 
316
GObject__finalize (GObject *object)
 
317
{
 
318
        G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->finalize (object);
 
319
}
 
320
 
 
321
G_MODULE_EXPORT GObject *
 
322
nm_system_config_factory (void)
 
323
{
 
324
        static SCPluginIfupdown *singleton = NULL;
 
325
 
 
326
        if (!singleton)
 
327
                singleton = SC_PLUGIN_IFUPDOWN (g_object_new (SC_TYPE_PLUGIN_IFUPDOWN, NULL));
 
328
        else
 
329
                g_object_ref (singleton);
 
330
 
 
331
        return G_OBJECT (singleton);
 
332
}
 
333