2007-05-07 Tambet Ingo <tambet@ximian.com>

* libnm-util/nm-connection.c: 
	* libnm-util/nm-connection.h: 
	* libnm-util/nm-setting.c:
	* libnm-util/nm-setting.h: Add.

	* libnm-util/Makefile.am: Build the added files.



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2568 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Tambet Ingo 2007-05-07 14:39:20 +00:00 committed by Tambet Ingo
parent 40db9193d3
commit 6c51badc69
6 changed files with 670 additions and 2 deletions

View file

@ -1,5 +1,12 @@
2007-05-07 Tambet Ingo <tambet@ximian.com>
* libnm-util/nm-connection.c:
* libnm-util/nm-connection.h:
* libnm-util/nm-setting.c:
* libnm-util/nm-setting.h: Add.
* libnm-util/Makefile.am: Build the added files.
* src/nm-dbus-manager.c
(proxy_name_owner_changed, nm_dbus_manager_class_init): Remove the
DbusConnection argument from 'name-owner-changed' signal. The manager

View file

@ -36,7 +36,11 @@ libnm_util_la_SOURCES= \
dbus-method-dispatcher.c \
dbus-method-dispatcher.h \
dbus-dict-helpers.c \
dbus-dict-helpers.h
dbus-dict-helpers.h \
nm-connection.c \
nm-connection.h \
nm-setting.c \
nm-setting.h
if !WITH_GCRYPT
libnm_util_la_SOURCES += gnome-keyring-md5.c gnome-keyring-md5.h
@ -59,7 +63,9 @@ libnm_util_include_HEADERS = \
cipher-wpa-psk-passphrase.h \
dbus-helpers.h \
dbus-method-dispatcher.h \
dbus-dict-helpers.h
dbus-dict-helpers.h \
nm-connection.h \
nm-setting.h
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libnm-util.pc

164
libnm-util/nm-connection.c Normal file
View file

@ -0,0 +1,164 @@
#include "nm-connection.h"
static GHashTable *registered_setting_creators = NULL;
static void
register_default_creators (void)
{
int i;
const struct {
const char *name;
NMSettingCreateFn fn;
} default_map[] = {
{ "info", nm_setting_info_new_from_hash },
{ "802-3-ethernet", nm_setting_wired_new_from_hash },
{ "802-11-wireless", nm_setting_wireless_new_from_hash },
{ NULL, NULL}
};
for (i = 0; default_map[i].name; i++)
nm_setting_parser_register (default_map[i].name, default_map[i].fn);
}
void
nm_setting_parser_register (const char *name, NMSettingCreateFn creator)
{
g_return_if_fail (name != NULL);
g_return_if_fail (creator != NULL);
if (!registered_setting_creators)
registered_setting_creators = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, NULL);
if (g_hash_table_lookup (registered_setting_creators, name))
g_warning ("Already have a creator function for '%s', overriding", name);
g_hash_table_insert (registered_setting_creators, g_strdup (name), creator);
}
void
nm_setting_parser_unregister (const char *name)
{
if (registered_setting_creators)
g_hash_table_remove (registered_setting_creators, name);
}
static void
parse_one_setting (gpointer key, gpointer value, gpointer user_data)
{
NMConnection *connection = (NMConnection *) user_data;
NMSettingCreateFn fn;
NMSetting *setting;
fn = (NMSettingCreateFn) g_hash_table_lookup (registered_setting_creators, key);
if (fn) {
setting = fn ((GHashTable *) value);
if (setting)
nm_connection_add_setting (connection, setting);
} else
g_warning ("Unknown setting '%s'", (char *) key);
}
NMConnection *
nm_connection_new (void)
{
NMConnection *connection;
if (!registered_setting_creators)
register_default_creators ();
connection = g_slice_new0 (NMConnection);
connection->settings = g_hash_table_new (g_str_hash, g_str_equal);
return connection;
}
NMConnection *
nm_connection_new_from_hash (GHashTable *hash)
{
NMConnection *connection;
g_return_val_if_fail (hash != NULL, NULL);
if (!registered_setting_creators)
register_default_creators ();
connection = nm_connection_new ();
g_hash_table_foreach (hash, parse_one_setting, connection);
if (g_hash_table_size (connection->settings) < 1) {
g_warning ("No settings found.");
nm_connection_destroy (connection);
return NULL;
}
if (!nm_settings_verify (connection->settings)) {
nm_connection_destroy (connection);
return NULL;
}
return connection;
}
void
nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
{
g_return_if_fail (connection != NULL);
g_return_if_fail (setting != NULL);
g_hash_table_insert (connection->settings, setting->name, setting);
}
NMSetting *
nm_connection_get_setting (NMConnection *connection, const char *setting_name)
{
g_return_val_if_fail (connection != NULL, NULL);
g_return_val_if_fail (setting_name != NULL, NULL);
return (NMSetting *) g_hash_table_lookup (connection->settings, setting_name);
}
static void
add_one_setting_to_hash (gpointer key, gpointer data, gpointer user_data)
{
NMSetting *setting = (NMSetting *) data;
GHashTable *connection_hash = (GHashTable *) user_data;
GHashTable *setting_hash;
setting_hash = nm_setting_to_hash (setting);
if (setting_hash)
g_hash_table_insert (connection_hash,
g_strdup (setting->name),
setting_hash);
}
GHashTable *
nm_connection_to_hash (NMConnection *connection)
{
GHashTable *connection_hash;
g_return_val_if_fail (connection != NULL, NULL);
connection_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_hash_table_destroy);
g_hash_table_foreach (connection->settings, add_one_setting_to_hash, connection_hash);
/* Don't send empty hashes */
if (g_hash_table_size (connection_hash) < 1) {
g_hash_table_destroy (connection_hash);
connection_hash = NULL;
}
return connection_hash;
}
void
nm_connection_destroy (NMConnection *connection)
{
g_return_if_fail (connection != NULL);
g_hash_table_destroy (connection->settings);
g_slice_free (NMConnection, connection);
}

View file

@ -0,0 +1,28 @@
#ifndef NM_CONNECTION_H
#define NM_CONNECTION_H
#include <glib.h>
#include "nm-setting.h"
typedef struct {
GHashTable *settings;
} NMConnection;
NMConnection *nm_connection_new (void);
NMConnection *nm_connection_new_from_hash (GHashTable *hash);
void nm_connection_add_setting (NMConnection *connection,
NMSetting *setting);
NMSetting *nm_connection_get_setting (NMConnection *connection,
const char *setting_name);
GHashTable *nm_connection_to_hash (NMConnection *connection);
void nm_connection_destroy (NMConnection *connection);
void nm_setting_parser_register (const char *name,
NMSettingCreateFn creator);
void nm_setting_parser_unregister (const char *name);
#endif /* NM_CONNECTION_H */

401
libnm-util/nm-setting.c Normal file
View file

@ -0,0 +1,401 @@
#include <glib-object.h>
#include "nm-setting.h"
static void
dump_one_setting (gpointer key, gpointer value, gpointer user_data)
{
NMSetting *setting = (NMSetting *) value;
g_message ("Setting '%s'", setting->name);
if (setting->dump_fn)
setting->dump_fn (setting);
g_message ("-------------------");
}
typedef struct {
gboolean success;
GHashTable *all_settings;
} VerifySettingsInfo;
static void
verify_one_setting (gpointer key, gpointer value, gpointer user_data)
{
NMSetting *setting = (NMSetting *) value;
VerifySettingsInfo *info = (VerifySettingsInfo *) user_data;
if (info->success && setting->verify_fn) {
info->success = setting->verify_fn (setting, info->all_settings);
}
}
gboolean
nm_settings_verify (GHashTable *all_settings)
{
gpointer p;
VerifySettingsInfo info;
/* Debug dump */
g_hash_table_foreach (all_settings, dump_one_setting, NULL);
/* First, make sure there's at least 'info' setting */
p = g_hash_table_lookup (all_settings, "info");
if (!p) {
g_warning ("'info' setting not present.");
return FALSE;
}
/* Now, run the verify function of each setting */
info.success = TRUE;
info.all_settings = all_settings;
g_hash_table_foreach (all_settings, verify_one_setting, &info);
return info.success;
}
GHashTable *
nm_setting_to_hash (NMSetting *setting)
{
g_return_val_if_fail (setting != NULL, NULL);
g_return_val_if_fail (setting->hash_fn != NULL, NULL);
return setting->hash_fn (setting);
}
void
nm_setting_destroy (NMSetting *setting)
{
char *name;
g_return_if_fail (setting != NULL);
name = setting->name;
if (setting->destroy_fn)
setting->destroy_fn (setting);
g_free (name);
}
/***********************************************************************/
/* Helper functions for converting NMSetting to hash table. */
static void
destroy_gvalue (gpointer data)
{
GValue *value = (GValue *) data;
g_value_unset (value);
g_slice_free (GValue, value);
}
static GHashTable *
setting_hash_new (void)
{
return g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
destroy_gvalue);
}
static GValue *
string_to_gvalue (const char *str)
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_STRING);
g_value_set_string (val, str);
return val;
}
static GValue *
boolean_to_gvalue (gboolean b)
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_BOOLEAN);
g_value_set_boolean (val, b);
return val;
}
static GValue *
int_to_gvalue (int i)
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_INT);
g_value_set_int (val, i);
return val;
}
/***********************************************************************/
static void
setting_info_dump (NMSetting *setting)
{
NMSettingInfo *self = (NMSettingInfo *) setting;
g_message ("info name: %s", self->name);
g_message ("devtype: %s", self->devtype);
g_message ("autoconnect: %s", self->autoconnect ? "Yes" : "No");
}
static gboolean
setting_info_verify (NMSetting *setting, GHashTable *all_settings)
{
NMSettingInfo *self = (NMSettingInfo *) setting;
/* Make sure the corresponding 'devtype' item is present */
if (!g_hash_table_lookup (all_settings, self->devtype))
return FALSE;
return TRUE;
}
static GHashTable *
setting_info_hash (NMSetting *setting)
{
NMSettingInfo *self = (NMSettingInfo *) setting;
GHashTable *hash;
hash = setting_hash_new ();
g_hash_table_insert (hash, "name", string_to_gvalue (self->name));
g_hash_table_insert (hash, "devtype", string_to_gvalue (self->devtype));
g_hash_table_insert (hash, "autoconnect", boolean_to_gvalue (self->autoconnect));
return hash;
}
static void
setting_info_destroy (NMSetting *setting)
{
NMSettingInfo *self = (NMSettingInfo *) setting;
g_free (self->name);
g_free (self->devtype);
g_slice_free (NMSettingInfo, self);
}
NMSetting *
nm_setting_info_new (void)
{
NMSetting *setting;
setting = (NMSetting *) g_slice_new0 (NMSettingInfo);
setting->name = g_strdup ("info");
setting->verify_fn = setting_info_verify;
setting->hash_fn = setting_info_hash;
setting->dump_fn = setting_info_dump;
setting->destroy_fn = setting_info_destroy;
return setting;
}
NMSetting *
nm_setting_info_new_from_hash (GHashTable *settings)
{
NMSettingInfo *self;
NMSetting *setting;
GValue *value;
g_return_val_if_fail (settings != NULL, NULL);
setting = nm_setting_info_new ();
self = (NMSettingInfo *) setting;
value = (GValue *) g_hash_table_lookup (settings, "name");
if (value && G_VALUE_HOLDS_STRING (value))
self->name = g_strdup (g_value_get_string (value));
else {
g_warning ("Missing or invalid info name");
goto err;
}
value = (GValue *) g_hash_table_lookup (settings, "devtype");
if (value && G_VALUE_HOLDS_STRING (value))
self->devtype = g_strdup (g_value_get_string (value));
else {
g_warning ("Missing or invalid devtype");
goto err;
}
value = (GValue *) g_hash_table_lookup (settings, "autoconnect");
if (value && G_VALUE_HOLDS_BOOLEAN (value))
self->autoconnect = g_value_get_boolean (value);
return setting;
err:
setting_info_destroy (setting);
return NULL;
}
static void
setting_wired_dump (NMSetting *setting)
{
NMSettingWired *self = (NMSettingWired *) setting;
g_message ("MTU: %d", self->mtu);
}
static gboolean
setting_wired_verify (NMSetting *setting, GHashTable *all_settings)
{
return TRUE;
}
static GHashTable *
setting_wired_hash (NMSetting *setting)
{
NMSettingWired *self = (NMSettingWired *) setting;
GHashTable *hash;
hash = setting_hash_new ();
g_hash_table_insert (hash, "mtu", int_to_gvalue (self->mtu));
return hash;
}
static void
setting_wired_destroy (NMSetting *setting)
{
NMSettingWired *self = (NMSettingWired *) setting;
g_slice_free (NMSettingWired, self);
}
NMSetting *
nm_setting_wired_new (void)
{
NMSetting *setting;
setting = (NMSetting *) g_slice_new0 (NMSettingWired);
setting->name = g_strdup ("802-3-ethernet");
setting->verify_fn = setting_wired_verify;
setting->hash_fn = setting_wired_hash;
setting->dump_fn = setting_wired_dump;
setting->destroy_fn = setting_wired_destroy;
return setting;
}
NMSetting *
nm_setting_wired_new_from_hash (GHashTable *settings)
{
NMSettingWired *self;
NMSetting *setting;
GValue *value;
g_return_val_if_fail (settings != NULL, NULL);
setting = nm_setting_wired_new ();
self = (NMSettingWired *) setting;
value = (GValue *) g_hash_table_lookup (settings, "mtu");
if (value && G_VALUE_HOLDS_INT (value))
self->mtu = g_value_get_int (value);
return setting;
}
static void
setting_wireless_dump (NMSetting *setting)
{
NMSettingWireless *self = (NMSettingWireless *) setting;
g_message ("ssid: %s", self->ssid);
g_message ("mode: %d", self->mode);
}
static gboolean
setting_wireless_verify (NMSetting *setting, GHashTable *all_settings)
{
return TRUE;
}
static GHashTable *
setting_wireless_hash (NMSetting *setting)
{
NMSettingWireless *self = (NMSettingWireless *) setting;
GHashTable *hash;
hash = setting_hash_new ();
g_hash_table_insert (hash, "ssid", string_to_gvalue (self->ssid));
g_hash_table_insert (hash, "mode", int_to_gvalue (self->mode));
return hash;
}
static void
setting_wireless_destroy (NMSetting *setting)
{
NMSettingWireless *self = (NMSettingWireless *) setting;
g_free (self->ssid);
g_slice_free (NMSettingWireless, self);
}
NMSetting *
nm_setting_wireless_new (void)
{
NMSetting *setting;
setting = (NMSetting *) g_slice_new0 (NMSettingWireless);
setting->name = g_strdup ("802-11-wireless");
setting->verify_fn = setting_wireless_verify;
setting->hash_fn = setting_wireless_hash;
setting->dump_fn = setting_wireless_dump;
setting->destroy_fn = setting_wireless_destroy;
return setting;
}
NMSetting *
nm_setting_wireless_new_from_hash (GHashTable *settings)
{
NMSettingWireless *self;
NMSetting *setting;
GValue *value;
g_return_val_if_fail (settings != NULL, NULL);
setting = nm_setting_wireless_new ();
self = (NMSettingWireless *) setting;
value = (GValue *) g_hash_table_lookup (settings, "ssid");
if (value && G_VALUE_HOLDS_STRING (value))
self->ssid = g_strdup (g_value_get_string (value));
else {
g_warning ("Missing or invalid ssid");
goto err;
}
value = (GValue *) g_hash_table_lookup (settings, "mode");
if (value && G_VALUE_HOLDS_INT (value)) {
self->mode = g_value_get_int (value);
} else {
g_warning ("Missing or invalid mode");
goto err;
}
return setting;
err:
setting_wireless_destroy (setting);
return NULL;
}

62
libnm-util/nm-setting.h Normal file
View file

@ -0,0 +1,62 @@
#ifndef NM_SETTING_H
#define NM_SETTING_H
#include <glib.h>
typedef struct _NMSetting NMSetting;
typedef NMSetting *(*NMSettingCreateFn) (GHashTable *settings);
typedef gboolean (*NMSettingVerifyFn) (NMSetting *setting,
GHashTable *all_settings);
typedef GHashTable *(*NMSettingToHashFn) (NMSetting *setting);
typedef void (*NMSettingDumpFn) (NMSetting *setting);
typedef void (*NMSettingDestroyFn) (NMSetting *setting);
struct _NMSetting {
char *name;
NMSettingVerifyFn verify_fn;
NMSettingToHashFn hash_fn;
NMSettingDumpFn dump_fn;
NMSettingDestroyFn destroy_fn;
};
gboolean nm_settings_verify (GHashTable *all_settings);
GHashTable *nm_setting_to_hash (NMSetting *setting);
void nm_setting_destroy (NMSetting *setting);
/* Default, built-in settings */
typedef struct {
NMSetting parent;
char *name;
char *devtype;
gboolean autoconnect;
} NMSettingInfo;
NMSetting *nm_setting_info_new (void);
NMSetting *nm_setting_info_new_from_hash (GHashTable *settings);
typedef struct {
NMSetting parent;
int mtu;
} NMSettingWired;
NMSetting *nm_setting_wired_new (void);
NMSetting *nm_setting_wired_new_from_hash (GHashTable *settings);
typedef struct {
NMSetting parent;
char *ssid;
int mode;
} NMSettingWireless;
NMSetting *nm_setting_wireless_new (void);
NMSetting *nm_setting_wireless_new_from_hash (GHashTable *settings);
#endif /* NM_SETTING_H */