2007-10-16 Tambet Ingo <tambet@gmail.com>

Implement a generic NMSetting creator from setting name.
        While at it, get rid of all nm_setting_foo_new_from_hash()
functions and
        add a virtual function 'populate_fn'.

        * libnm-util/nm-connection.c (nm_connection_create_setting):
        * Implement.
        (register_default_creators): Register setting creators instead
of functions
        that create and then populate.
        (parse_one_setting): Use the common setting creator and then
setting specific
        poplulation function.

        * libnm-util/nm-setting.c: Get rid of
        * nm_setting_foo_new_from_hash() functions,
        they all looked exactly the same.
        Add a 'populate_fn' virtual function to NMSetting.
        Use default virtual functions in case they are not overriden.
        (nm_setting_populate_from_hash): Implement.

        * src/nm-device.c (real_act_stage3_ip_config_start): Don't hard
        * code the setting
        name, use a defined string.
        (real_act_stage4_get_ip4_config): Ditto.



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2978 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Tambet Ingo
2007-10-16 15:18:01 +00:00
parent b85dd0a01c
commit 850f441ea5
6 changed files with 89 additions and 175 deletions

View File

@@ -1,3 +1,25 @@
2007-10-16 Tambet Ingo <tambet@gmail.com>
Implement a generic NMSetting creator from setting name.
While at it, get rid of all nm_setting_foo_new_from_hash() functions and
add a virtual function 'populate_fn'.
* libnm-util/nm-connection.c (nm_connection_create_setting): Implement.
(register_default_creators): Register setting creators instead of functions
that create and then populate.
(parse_one_setting): Use the common setting creator and then setting specific
poplulation function.
* libnm-util/nm-setting.c: Get rid of nm_setting_foo_new_from_hash() functions,
they all looked exactly the same.
Add a 'populate_fn' virtual function to NMSetting.
Use default virtual functions in case they are not overriden.
(nm_setting_populate_from_hash): Implement.
* src/nm-device.c (real_act_stage3_ip_config_start): Don't hard code the setting
name, use a defined string.
(real_act_stage4_get_ip4_config): Ditto.
2007-10-16 Tambet Ingo <tambet@gmail.com>
* src/nm-hal-manager.c (killswitch_getpower_reply): The type returned from

View File

@@ -1,3 +1,5 @@
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <string.h>
@@ -30,14 +32,14 @@ register_default_creators (void)
const char *name;
NMSettingCreateFn fn;
} default_map[] = {
{ NM_SETTING_CONNECTION, nm_setting_connection_new_from_hash },
{ NM_SETTING_WIRED, nm_setting_wired_new_from_hash },
{ NM_SETTING_WIRELESS, nm_setting_wireless_new_from_hash },
{ NM_SETTING_IP4_CONFIG, nm_setting_ip4_config_new_from_hash },
{ NM_SETTING_WIRELESS_SECURITY, nm_setting_wireless_security_new_from_hash },
{ NM_SETTING_PPP, nm_setting_ppp_new_from_hash },
{ NM_SETTING_VPN, nm_setting_vpn_new_from_hash },
{ NM_SETTING_VPN_PROPERTIES, nm_setting_vpn_properties_new_from_hash },
{ NM_SETTING_CONNECTION, nm_setting_connection_new },
{ NM_SETTING_WIRED, nm_setting_wired_new },
{ NM_SETTING_WIRELESS, nm_setting_wireless_new },
{ NM_SETTING_IP4_CONFIG, nm_setting_ip4_config_new },
{ NM_SETTING_WIRELESS_SECURITY, nm_setting_wireless_security_new },
{ NM_SETTING_PPP, nm_setting_ppp_new },
{ NM_SETTING_VPN, nm_setting_vpn_new },
{ NM_SETTING_VPN_PROPERTIES, nm_setting_vpn_properties_new },
{ NULL, NULL }
};
@@ -68,20 +70,38 @@ nm_setting_parser_unregister (const char *name)
g_hash_table_remove (registered_setting_creators, name);
}
NMSetting *
nm_connection_create_setting (const char *name)
{
NMSettingCreateFn fn;
NMSetting *setting;
g_return_val_if_fail (name != NULL, NULL);
fn = (NMSettingCreateFn) g_hash_table_lookup (registered_setting_creators, name);
if (fn)
setting = fn ();
else {
g_warning ("Unknown setting '%s'", name);
setting = NULL;
}
return setting;
}
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)
setting = nm_connection_create_setting ((char *) key);
if (setting) {
if (nm_setting_populate_from_hash (setting, (GHashTable *) value))
nm_connection_add_setting (connection, setting);
} else
g_warning ("Unknown setting '%s'", (char *) key);
else
nm_setting_destroy (setting);
}
}
void

View File

@@ -58,6 +58,7 @@ void nm_connection_for_each_setting_value (NMConnection *connection,
GHashTable *nm_connection_to_hash (NMConnection *connection);
void nm_connection_dump (NMConnection *connection);
NMSetting *nm_connection_create_setting (const char *name);
void nm_setting_parser_register (const char *name,
NMSettingCreateFn creator);

View File

@@ -10,7 +10,19 @@
#include "nm-utils.h"
static GHashTable * nm_setting_hash (NMSetting *setting);
static gboolean nm_setting_populate_from_hash_default (NMSetting *setting, GHashTable *table);
static void default_setting_clear_secrets (NMSetting *setting);
gboolean
nm_setting_populate_from_hash (NMSetting *setting, GHashTable *hash)
{
g_return_val_if_fail (setting != NULL, FALSE);
if (setting->populate_fn)
return setting->populate_fn (setting, hash);
else
return nm_setting_populate_from_hash_default (setting, hash);
}
typedef struct {
gboolean success;
@@ -59,9 +71,11 @@ 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);
if (setting->hash_fn)
return setting->hash_fn (setting);
else
return default_setting_hash (setting);
}
gboolean
@@ -82,7 +96,9 @@ nm_setting_clear_secrets (NMSetting *setting)
g_return_if_fail (setting != NULL);
if (setting->clear_secrets_fn)
return setting->clear_secrets_fn (setting);
setting->clear_secrets_fn (setting);
else
default_setting_clear_secrets (setting);
}
GPtrArray *
@@ -299,7 +315,7 @@ string_slist_validate (GSList *list, const char **valid_values)
/***********************************************************************/
static gboolean
nm_setting_populate_from_hash (NMSetting *setting, GHashTable *table)
nm_setting_populate_from_hash_default (NMSetting *setting, GHashTable *table)
{
SettingMember *m;
@@ -505,29 +521,11 @@ nm_setting_connection_new (void)
setting->name = g_strdup (NM_SETTING_CONNECTION);
setting->_members = con_table;
setting->verify_fn = setting_connection_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_connection_destroy;
return setting;
}
NMSetting *
nm_setting_connection_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_connection_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* IP4 config */
static gboolean
@@ -574,29 +572,11 @@ nm_setting_ip4_config_new (void)
setting->name = g_strdup (NM_SETTING_IP4_CONFIG);
setting->_members = ip4_config_table;
setting->verify_fn = setting_ip4_config_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_ip4_config_destroy;
return setting;
}
NMSetting *
nm_setting_ip4_config_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_ip4_config_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wired device */
static gboolean
@@ -660,8 +640,6 @@ nm_setting_wired_new (void)
setting->name = g_strdup (NM_SETTING_WIRED);
setting->_members = wired_table;
setting->verify_fn = setting_wired_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wired_destroy;
s_wired->auto_negotiate = TRUE;
@@ -669,22 +647,6 @@ nm_setting_wired_new (void)
return setting;
}
NMSetting *
nm_setting_wired_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wired_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wireless device */
static gboolean
@@ -813,29 +775,11 @@ nm_setting_wireless_new (void)
setting->name = g_strdup (NM_SETTING_WIRELESS);
setting->_members = wireless_table;
setting->verify_fn = setting_wireless_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wireless_destroy;
return setting;
}
NMSetting *
nm_setting_wireless_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wireless_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wireless security */
static gboolean
@@ -1182,31 +1126,13 @@ nm_setting_wireless_security_new (void)
setting->name = g_strdup (NM_SETTING_WIRELESS_SECURITY);
setting->_members = wireless_sec_table;
setting->verify_fn = setting_wireless_security_verify;
setting->hash_fn = default_setting_hash;
setting->update_secrets_fn = setting_wireless_security_update_secrets;
setting->need_secrets_fn = setting_wireless_security_need_secrets;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wireless_security_destroy;
return setting;
}
NMSetting *
nm_setting_wireless_security_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wireless_security_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* PPP */
static gboolean
@@ -1255,29 +1181,11 @@ nm_setting_ppp_new (void)
setting->name = g_strdup (NM_SETTING_PPP);
setting->_members = ppp_table;
setting->verify_fn = setting_ppp_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_ppp_destroy;
return setting;
}
NMSetting *
nm_setting_ppp_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_ppp_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* vpn setting */
static gboolean
@@ -1328,29 +1236,11 @@ nm_setting_vpn_new (void)
setting->name = g_strdup (NM_SETTING_VPN);
setting->_members = vpn_table;
setting->verify_fn = setting_vpn_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_vpn_destroy;
return setting;
}
NMSetting *
nm_setting_vpn_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_vpn_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* vpn-properties setting */
static gboolean
@@ -1443,7 +1333,6 @@ nm_setting_vpn_properties_new (void)
setting->verify_fn = setting_vpn_properties_verify;
setting->hash_fn = setting_vpn_properties_hash;
setting->update_secrets_fn = setting_vpn_properties_update_secrets;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_vpn_properties_destroy;
s_vpn_props = (NMSettingVPNProperties *) setting;
@@ -1453,19 +1342,3 @@ nm_setting_vpn_properties_new (void)
return setting;
}
NMSetting *
nm_setting_vpn_properties_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_vpn_properties_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}

View File

@@ -9,7 +9,9 @@ G_BEGIN_DECLS
typedef struct _NMSetting NMSetting;
typedef NMSetting *(*NMSettingCreateFn) (GHashTable *settings);
typedef NMSetting *(*NMSettingCreateFn) (void);
typedef gboolean (*NMSettingPopulateFn) (NMSetting *setting,
GHashTable *hash);
typedef gboolean (*NMSettingVerifyFn) (NMSetting *setting,
GHashTable *all_settings);
@@ -51,6 +53,7 @@ struct _NMSetting {
char *name;
SettingMember *_members; /* Private */
NMSettingPopulateFn populate_fn;
NMSettingVerifyFn verify_fn;
NMSettingToHashFn hash_fn;
NMSettingUpdateSecretsFn update_secrets_fn;
@@ -59,6 +62,7 @@ struct _NMSetting {
NMSettingDestroyFn destroy_fn;
};
gboolean nm_setting_populate_from_hash (NMSetting *setting, GHashTable *hash);
gboolean nm_settings_verify (GHashTable *all_settings);
GHashTable *nm_setting_to_hash (NMSetting *setting);
gboolean nm_setting_update_secrets (NMSetting *setting, GHashTable *secrets);
@@ -85,7 +89,6 @@ typedef struct {
} NMSettingConnection;
NMSetting *nm_setting_connection_new (void);
NMSetting *nm_setting_connection_new_from_hash (GHashTable *settings);
/* IP4 config */
@@ -101,7 +104,6 @@ typedef struct {
} NMSettingIP4Config;
NMSetting *nm_setting_ip4_config_new (void);
NMSetting *nm_setting_ip4_config_new_from_hash (GHashTable *settings);
/* Wired device */
@@ -119,7 +121,6 @@ typedef struct {
} NMSettingWired;
NMSetting *nm_setting_wired_new (void);
NMSetting *nm_setting_wired_new_from_hash (GHashTable *settings);
/* Wireless device */
@@ -142,7 +143,6 @@ typedef struct {
} NMSettingWireless;
NMSetting *nm_setting_wireless_new (void);
NMSetting *nm_setting_wireless_new_from_hash (GHashTable *settings);
/* Wireless security */
@@ -187,7 +187,6 @@ typedef struct {
} NMSettingWirelessSecurity;
NMSetting *nm_setting_wireless_security_new (void);
NMSetting *nm_setting_wireless_security_new_from_hash (GHashTable *settings);
/* PPP */
@@ -217,7 +216,6 @@ typedef struct {
} NMSettingPPP;
NMSetting *nm_setting_ppp_new (void);
NMSetting *nm_setting_ppp_new_from_hash (GHashTable *settings);
/* VPN */
@@ -232,7 +230,6 @@ typedef struct {
} NMSettingVPN;
NMSetting *nm_setting_vpn_new (void);
NMSetting *nm_setting_vpn_new_from_hash (GHashTable *hash);
/* VPN properties */
@@ -245,7 +242,6 @@ typedef struct {
} NMSettingVPNProperties;
NMSetting *nm_setting_vpn_properties_new (void);
NMSetting *nm_setting_vpn_properties_new_from_hash (GHashTable *hash);
G_END_DECLS

View File

@@ -555,7 +555,8 @@ real_act_stage3_ip_config_start (NMDevice *self)
NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
req = nm_device_get_act_request (self);
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req), "ipv4");
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req),
NM_SETTING_IP4_CONFIG);
/* If we did not receive IP4 configuration information, default to DHCP */
if (!setting || setting->manual == FALSE) {
@@ -702,7 +703,8 @@ real_act_stage4_get_ip4_config (NMDevice *self,
}
req = nm_device_get_act_request (self);
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req), "ipv4");
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req),
NM_SETTING_IP4_CONFIG);
if (real_config && setting) {
/* If settings are provided, use them, even if it means overriding the values we got from DHCP */