1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
3
/* NetworkManager system settings service (ifupdown)
5
* Alexander Sack <asac@ubuntu.com>
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.
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.
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.
21
* (C) Copyright 2007,2008 Canonical Ltd.
25
#include <sys/inotify.h>
28
#include <glib-object.h>
29
#include <glib/gi18n.h>
30
#include <glib/gslist.h>
31
#include <nm-setting-connection.h>
33
#include "interface_parser.h"
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"
41
#include "nm-ifupdown-connection.h"
48
#include <arpa/inet.h>
50
#define IFUPDOWN_PLUGIN_NAME "ifupdown"
51
#define IFUPDOWN_PLUGIN_INFO "(C) 2008 Canonical Ltd. To report bugs please use the NetworkManager mailing list."
55
DBusGConnection *g_connection;
56
NMSystemConfigHalManager *hal_mgr;
58
GHashTable *iface_connections;
60
} SCPluginIfupdownPrivate;
63
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
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))
69
#define SC_PLUGIN_IFUPDOWN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_IFUPDOWN, SCPluginIfupdownPrivate))
72
sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class);
75
SCPluginIfupdown_init (NMSystemConfigInterface *config,
76
NMSystemConfigHalManager *hal_manager);
78
/* Returns the plugins currently known list of connections. The returned
79
* list is freed by the system settings service.
82
SCPluginIfupdown_get_connections (NMSystemConfigInterface *config);
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.
90
SCPluginIfupdown_get_unmanaged_devices (NMSystemConfigInterface *config);
94
/* Emitted when a new connection has been found by the plugin */
96
SCPluginIfupdown_connection_added (NMSystemConfigInterface *config,
97
NMExportedConnection *connection);
99
/* Emitted when the list of unmanaged devices changes */
101
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config);
104
GObject__get_property (GObject *object, guint prop_id,
105
GValue *value, GParamSpec *pspec);
108
GObject__dispose (GObject *object);
111
GObject__finalize (GObject *object);
115
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
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;
125
sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class)
127
GObjectClass *object_class = G_OBJECT_CLASS (req_class);
129
g_type_class_add_private (req_class, sizeof (SCPluginIfupdownPrivate));
131
object_class->dispose = GObject__dispose;
132
object_class->finalize = GObject__finalize;
133
object_class->get_property = GObject__get_property;
135
g_object_class_override_property (object_class,
136
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
137
NM_SYSTEM_CONFIG_INTERFACE_NAME);
139
g_object_class_override_property (object_class,
140
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
141
NM_SYSTEM_CONFIG_INTERFACE_INFO);
143
g_object_class_override_property (object_class,
144
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
145
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
147
g_object_class_override_property (object_class,
148
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
149
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
153
SCPluginIfupdown_init (NMSystemConfigInterface *config,
154
NMSystemConfigHalManager *hal_manager)
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;
160
if(!priv->iface_connections)
161
priv->iface_connections = g_hash_table_new (g_str_hash, g_str_equal);
163
PLUGIN_PRINT("SCPlugin-Ifupdown", "init!");
164
priv->hal_mgr = g_object_ref (hal_manager);
167
block = ifparser_getfirst();
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);
176
connection = NM_EXPORTED_CONNECTION(nm_ifupdown_connection_new(block));
177
ifupdown_update_connection_from_if_block (nm_exported_connection_get_connection(connection),
180
g_hash_table_insert (priv->iface_connections, block->name, connection);
182
block = block -> next;
186
GList *keys = g_hash_table_get_keys (priv->iface_connections);
187
GList *key_it = keys;
189
gpointer val = g_hash_table_lookup(auto_ifaces, key_it->data);
191
NMExportedConnection *connection =
192
g_hash_table_lookup(priv->iface_connections, key_it->data);
193
NMConnection *wrapped = NULL;
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,
202
PLUGIN_PRINT("SCPlugin-Ifupdown", "autoconnect");
204
key_it = key_it -> next;
207
g_hash_table_unref(auto_ifaces);
208
PLUGIN_PRINT("SCPlugin-Ifupdown", "end _init.");
212
/* Returns the plugins currently known list of connections. The returned
213
* list is freed by the system settings service.
216
SCPluginIfupdown_get_connections (NMSystemConfigInterface *config)
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));
225
NMExportedConnection *conn = it->data;
226
connections = g_slist_append(connections, conn);
229
PLUGIN_PRINT("SCPlugin-Ifupdown", "(%d) connections count: %d", GPOINTER_TO_UINT(config), g_slist_length(connections));
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.
239
SCPluginIfupdown_get_unmanaged_devices (NMSystemConfigInterface *config)
241
// XXX implement this.
247
/* Emitted when a new connection has been found by the plugin */
249
SCPluginIfupdown_connection_added (NMSystemConfigInterface *config,
250
NMExportedConnection *connection)
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");
258
/* Emitted when the list of unmanaged devices changes */
260
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config)
262
PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... started");
263
g_return_if_fail (config != NULL);
265
PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... ended");
269
sc_plugin_ifupdown_init (SCPluginIfupdown *plugin)
271
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
272
GError *error = NULL;
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);
283
GObject__get_property (GObject *object, guint prop_id,
284
GValue *value, GParamSpec *pspec)
287
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
288
g_value_set_string (value, IFUPDOWN_PLUGIN_NAME);
290
case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
291
g_value_set_string (value, IFUPDOWN_PLUGIN_INFO);
293
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
294
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
296
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
297
g_value_set_string (value, "");
300
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306
GObject__dispose (GObject *object)
308
SCPluginIfupdown *plugin = SC_PLUGIN_IFUPDOWN (object);
309
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
311
g_object_unref (priv->hal_mgr);
312
G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->dispose (object);
316
GObject__finalize (GObject *object)
318
G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->finalize (object);
321
G_MODULE_EXPORT GObject *
322
nm_system_config_factory (void)
324
static SCPluginIfupdown *singleton = NULL;
327
singleton = SC_PLUGIN_IFUPDOWN (g_object_new (SC_TYPE_PLUGIN_IFUPDOWN, NULL));
329
g_object_ref (singleton);
331
return G_OBJECT (singleton);