Commit b9ddbe58 authored by Dan Williams's avatar Dan Williams

libnm-util: add functions for getting valid bond options and defaults

Since the options are a hash table now there wasn't any way to
determine what options were allowed and what their default values
are.  Add some functions to do that.
parent 33d0cff3
......@@ -184,7 +184,9 @@ global:
nm_setting_bond_get_num_options;
nm_setting_bond_get_option;
nm_setting_bond_get_option_by_name;
nm_setting_bond_get_option_default;
nm_setting_bond_get_type;
nm_setting_bond_get_valid_options;
nm_setting_bond_new;
nm_setting_bond_remove_option;
nm_setting_cdma_error_get_type;
......
......@@ -63,7 +63,7 @@ G_DEFINE_TYPE (NMSettingBond, nm_setting_bond, NM_TYPE_SETTING)
#define NM_SETTING_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BOND, NMSettingBondPrivate))
typedef struct {
char * interface_name;
char *interface_name;
GHashTable *options;
} NMSettingBondPrivate;
......@@ -74,14 +74,18 @@ enum {
LAST_PROP
};
static const char *valid_opts[] = {
NM_SETTING_BOND_OPTION_MODE,
NM_SETTING_BOND_OPTION_MIIMON,
NM_SETTING_BOND_OPTION_DOWNDELAY,
NM_SETTING_BOND_OPTION_UPDELAY,
NM_SETTING_BOND_OPTION_ARP_INTERVAL,
NM_SETTING_BOND_OPTION_ARP_IP_TARGET,
NULL
typedef struct {
const char *opt;
const char *val;
} BondDefault;
static const BondDefault defaults[] = {
{ NM_SETTING_BOND_OPTION_MODE, "balance-rr" },
{ NM_SETTING_BOND_OPTION_MIIMON, "100" },
{ NM_SETTING_BOND_OPTION_DOWNDELAY, "0" },
{ NM_SETTING_BOND_OPTION_UPDELAY, "0" },
{ NM_SETTING_BOND_OPTION_ARP_INTERVAL, "0" },
{ NM_SETTING_BOND_OPTION_ARP_IP_TARGET, "" },
};
/**
......@@ -180,16 +184,18 @@ nm_setting_bond_get_option (NMSettingBond *setting,
}
static gboolean
validate_option_name (const char *name)
validate_option (const char *name)
{
const char *p = name;
guint32 i = 0;
guint i;
while (p && *p) {
if (isalnum (*p++) == FALSE || i++ > 200)
return FALSE;
g_return_val_if_fail (name != NULL, FALSE);
g_return_val_if_fail (name[0] != '\0', FALSE);
for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
if (g_strcmp0 (defaults[i].opt, name) == 0)
return TRUE;
}
return i > 0 ? TRUE : FALSE; /* catch empty strings */
return FALSE;
}
/**
......@@ -208,7 +214,7 @@ nm_setting_bond_get_option_by_name (NMSettingBond *setting,
const char *name)
{
g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
g_return_val_if_fail (validate_option_name (name), NULL);
g_return_val_if_fail (validate_option (name), NULL);
return g_hash_table_lookup (NM_SETTING_BOND_GET_PRIVATE (setting)->options, name);
}
......@@ -234,8 +240,7 @@ gboolean nm_setting_bond_add_option (NMSettingBond *setting,
size_t value_len;
g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);
g_return_val_if_fail (validate_option_name (name), FALSE);
g_return_val_if_fail (_nm_utils_string_in_list (name, valid_opts), FALSE);
g_return_val_if_fail (validate_option (name), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
value_len = strlen (value);
......@@ -262,11 +267,58 @@ nm_setting_bond_remove_option (NMSettingBond *setting,
const char *name)
{
g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);
g_return_val_if_fail (validate_option_name (name), FALSE);
g_return_val_if_fail (validate_option (name), FALSE);
return g_hash_table_remove (NM_SETTING_BOND_GET_PRIVATE (setting)->options, name);
}
/**
* nm_setting_bond_get_valid_options:
* @setting: the #NMSettingBond
*
* Returns a list of valid bond options.
*
* Returns: a NULL-terminated array of strings of valid bond options.
**/
const char **
nm_setting_bond_get_valid_options (NMSettingBond *setting)
{
static const char *array[G_N_ELEMENTS (defaults) + 1] = { NULL };
int i;
/* initialize the array once */
if (G_UNLIKELY (array[0] == NULL)) {
for (i = 0; i < G_N_ELEMENTS (defaults); i++)
array[i] = defaults[i].opt;
array[i] = NULL;
}
return array;
}
/**
* nm_setting_bond_get_option_default:
* @setting: the #NMSettingBond
* @name: the name of the option
*
* Returns: the value of the bond option if not overridden by an entry in
* the #NMSettingBond:options property.
**/
const char *
nm_setting_bond_get_option_default (NMSettingBond *setting, const char *name)
{
guint i;
g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
g_return_val_if_fail (validate_option (name), NULL);
for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
if (g_strcmp0 (defaults[i].opt, name) == 0)
return defaults[i].val;
}
/* Any option that passes validate_option() should also be found in defaults */
g_assert_not_reached ();
}
/*
* This function is a 1:1 copy of the kernel's
* dev_valid_name() in net/core/dev.c
......@@ -325,8 +377,8 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
g_hash_table_iter_init (&iter, priv->options);
while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
if ( !_nm_utils_string_in_list (key, valid_opts)
|| !strlen (value)
if ( !validate_option (key)
|| !value[0]
|| (strlen (value) > 200)) {
g_set_error (error,
NM_SETTING_BOND_ERROR,
......@@ -335,7 +387,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
return FALSE;
}
if (!g_strcmp0 (key, "mode")
if ( (g_strcmp0 (key, "mode") == 0)
&& !_nm_utils_string_in_list (value, valid_modes)) {
g_set_error (error,
NM_SETTING_BOND_ERROR,
......
......@@ -95,6 +95,11 @@ gboolean nm_setting_bond_add_option (NMSettingBond *setting,
gboolean nm_setting_bond_remove_option (NMSettingBond *setting,
const char *name);
const char **nm_setting_bond_get_valid_options (NMSettingBond *setting);
const char * nm_setting_bond_get_option_default (NMSettingBond *setting,
const char *name);
G_END_DECLS
#endif /* NM_SETTING_BOND_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment