libnm-util, libnm-glib: Add master/slave-matching utils (rh #1045203)

Add functions for figuring out what device/connection types can be
slaves of other device/connection types.
This commit is contained in:
Dan Winship
2014-03-06 09:41:59 -05:00
21 changed files with 207 additions and 11 deletions

View File

@@ -143,6 +143,7 @@ global:
nm_device_get_mtu;
nm_device_get_physical_port_id;
nm_device_get_product;
nm_device_get_setting_type;
nm_device_get_state;
nm_device_get_state_reason;
nm_device_get_type;

View File

@@ -133,6 +133,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_adsl_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_ADSL;
}
/******************************************************************/
static void
@@ -221,6 +227,7 @@ nm_device_adsl_class_init (NMDeviceAdslClass *adsl_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
/* properties */
/**

View File

@@ -191,6 +191,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_BOND;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -298,6 +304,7 @@ nm_device_bond_class_init (NMDeviceBondClass *eth_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -199,6 +199,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_bridge_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_BRIDGE;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -306,6 +312,7 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *bridge_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -223,6 +223,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_BLUETOOTH;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -325,6 +331,7 @@ nm_device_bt_class_init (NMDeviceBtClass *bt_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -226,6 +226,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_WIRED;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -332,6 +338,7 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *eth_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -157,6 +157,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_generic_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_GENERIC;
}
/***********************************************************/
static void
@@ -251,6 +257,7 @@ nm_device_generic_class_init (NMDeviceGenericClass *klass)
device_class->get_type_description = get_type_description;
device_class->get_hw_address = get_hw_address;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
/**
* NMDeviceGeneric:hw-address:

View File

@@ -178,6 +178,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_INFINIBAND;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -275,6 +281,7 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *eth_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -167,6 +167,20 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_modem_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
NMDeviceModemCapabilities caps;
caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
if (caps & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS | NM_DEVICE_MODEM_CAPABILITY_LTE))
return NM_TYPE_SETTING_GSM;
else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
return NM_TYPE_SETTING_CDMA;
else
return G_TYPE_INVALID;
}
/*******************************************************************/
static void
@@ -249,6 +263,7 @@ nm_device_modem_class_init (NMDeviceModemClass *modem_class)
device_class->get_type_description = get_type_description;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
/**
* NMDeviceModem:modem-capabilities:

View File

@@ -183,6 +183,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_olpc_mesh_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_OLPC_MESH;
}
/**************************************************************/
static void
@@ -279,6 +285,7 @@ nm_device_olpc_mesh_class_init (NMDeviceOlpcMeshClass *olpc_mesh_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -203,6 +203,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_team_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_TEAM;
}
/***********************************************************/
static void
@@ -304,6 +310,7 @@ nm_device_team_class_init (NMDeviceTeamClass *eth_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -205,6 +205,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_vlan_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_VLAN;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -306,6 +312,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *eth_class)
object_class->finalize = finalize;
object_class->get_property = get_property;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */

View File

@@ -523,6 +523,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_wifi_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_WIRELESS;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -704,6 +710,7 @@ nm_device_wifi_class_init (NMDeviceWifiClass *wifi_class)
object_class->dispose = dispose;
object_class->finalize = finalize;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
wifi_class->access_point_removed = access_point_removed;

View File

@@ -397,6 +397,12 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro
return NM_DEVICE_CLASS (nm_device_wimax_parent_class)->connection_compatible (device, connection, error);
}
static GType
get_setting_type (NMDevice *device)
{
return NM_TYPE_SETTING_WIMAX;
}
static const char *
get_hw_address (NMDevice *device)
{
@@ -599,6 +605,7 @@ nm_device_wimax_class_init (NMDeviceWimaxClass *wimax_class)
object_class->get_property = get_property;
object_class->dispose = dispose;
device_class->connection_compatible = connection_compatible;
device_class->get_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
wimax_class->nsp_removed = nsp_removed;

View File

@@ -2260,3 +2260,22 @@ nm_device_filter_connections (NMDevice *device, const GSList *connections)
return g_slist_reverse (filtered);
}
/**
* nm_device_get_setting_type:
* @device: an #NMDevice
*
* Gets the (primary) #NMSetting subtype associated with connections
* that can be used on @device.
*
* Returns: @device's associated #NMSetting type
*
* Since: 0.9.10
*/
GType
nm_device_get_setting_type (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), G_TYPE_INVALID);
g_return_val_if_fail (NM_DEVICE_GET_CLASS (device)->get_setting_type != NULL, G_TYPE_INVALID);
return NM_DEVICE_GET_CLASS (device)->get_setting_type (device);
}

View File

@@ -104,11 +104,12 @@ typedef struct {
const char * (*get_type_description) (NMDevice *device);
const char * (*get_hw_address) (NMDevice *device);
GType (*get_setting_type) (NMDevice *device);
/* Padding for future expansion */
void (*_reserved1) (void);
void (*_reserved2) (void);
void (*_reserved3) (void);
void (*_reserved4) (void);
} NMDeviceClass;
GType nm_device_get_type (void);
@@ -168,6 +169,9 @@ gboolean nm_device_connection_compatible (NMDevice *device,
NMConnection *connection,
GError **error);
NM_AVAILABLE_IN_0_9_10
GType nm_device_get_setting_type (NMDevice *device);
G_END_DECLS
#endif /* NM_DEVICE_H */

View File

@@ -603,6 +603,7 @@ global:
nm_utils_ap_mode_security_valid;
nm_utils_bin2hexstr;
nm_utils_deinit;
nm_utils_check_virtual_device_compatibility;
nm_utils_escape_ssid;
nm_utils_file_is_pkcs12;
nm_utils_gvalue_hash_dup;

View File

@@ -38,6 +38,7 @@ void _nm_register_setting (const char *name,
#define _nm_register_setting(name, type, priority, error_quark) _nm_register_setting ((name ""), type, priority, error_quark)
gboolean _nm_setting_is_base_type (NMSetting *setting);
gboolean _nm_setting_type_is_base_type (GType type);
GType _nm_setting_lookup_setting_type (const char *name);
GType _nm_setting_lookup_setting_type_by_quark (GQuark error_quark);
gint _nm_setting_compare_priority (gconstpointer a, gconstpointer b);

View File

@@ -199,25 +199,31 @@ _nm_setting_lookup_setting_by_type (GType type)
}
static guint32
_get_setting_priority (NMSetting *setting)
_get_setting_type_priority (GType type)
{
NMSettingPrivate *priv;
const SettingInfo *info;
g_return_val_if_fail (NM_IS_SETTING (setting), G_MAXUINT32);
priv = NM_SETTING_GET_PRIVATE (setting);
_ensure_setting_info (setting, priv);
return priv->info->priority;
g_return_val_if_fail (g_type_is_a (type, NM_TYPE_SETTING), G_MAXUINT32);
info = _nm_setting_lookup_setting_by_type (type);
return info->priority;
}
gboolean
_nm_setting_is_base_type (NMSetting *setting)
_nm_setting_type_is_base_type (GType type)
{
/* Historical oddity: PPPoE is a base-type even though it's not
* priority 1. It needs to be sorted *after* lower-level stuff like
* WiFi security or 802.1x for secrets, but it's still allowed as a
* base type.
*/
return _get_setting_priority (setting) == 1 || NM_IS_SETTING_PPPOE (setting);
return _get_setting_type_priority (type) == 1 || (type == NM_TYPE_SETTING_PPPOE);
}
gboolean
_nm_setting_is_base_type (NMSetting *setting)
{
return _nm_setting_type_is_base_type (G_OBJECT_TYPE (setting));
}
GType
@@ -254,8 +260,8 @@ _nm_setting_compare_priority (gconstpointer a, gconstpointer b)
{
guint32 prio_a, prio_b;
prio_a = _get_setting_priority (NM_SETTING (a));
prio_b = _get_setting_priority (NM_SETTING (b));
prio_a = _get_setting_type_priority (G_OBJECT_TYPE (a));
prio_b = _get_setting_type_priority (G_OBJECT_TYPE (b));
if (prio_a < prio_b)
return -1;

View File

@@ -36,6 +36,7 @@
#include "nm-utils-private.h"
#include "nm-glib-compat.h"
#include "nm-dbus-glib-types.h"
#include "nm-setting-private.h"
#include "crypto.h"
/**
@@ -2327,3 +2328,64 @@ nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst)
INET6_ADDRSTRLEN);
}
/**
* nm_utils_check_virtual_device_compatibility:
* @virtual_type: a virtual connection type
* @other_type: a connection type to test against @virtual_type
*
* Determines if a connection of type @virtual_type can (in the
* general case) work with connections of type @other_type.
*
* If @virtual_type is %NM_TYPE_SETTING_VLAN, then this checks if
* @other_type is a valid type for the parent of a VLAN.
*
* If @virtual_type is a "master" type (eg, %NM_TYPE_SETTING_BRIDGE),
* then this checks if @other_type is a valid type for a slave of that
* master.
*
* Note that even if this returns %TRUE it is not guaranteed that
* <emphasis>every</emphasis> connection of type @other_type is
* compatible with @virtual_type; it may depend on the exact
* configuration of the two connections, or on the capabilities of an
* underlying device driver.
*
* Returns: %TRUE or %FALSE
*
* Since: 0.9.10
*/
gboolean
nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type)
{
g_return_val_if_fail (_nm_setting_type_is_base_type (virtual_type), FALSE);
g_return_val_if_fail (_nm_setting_type_is_base_type (other_type), FALSE);
if (virtual_type == NM_TYPE_SETTING_BOND) {
return ( other_type == NM_TYPE_SETTING_INFINIBAND
|| other_type == NM_TYPE_SETTING_WIRED
|| other_type == NM_TYPE_SETTING_BRIDGE
|| other_type == NM_TYPE_SETTING_BOND
|| other_type == NM_TYPE_SETTING_TEAM
|| other_type == NM_TYPE_SETTING_VLAN);
} else if (virtual_type == NM_TYPE_SETTING_BRIDGE) {
return ( other_type == NM_TYPE_SETTING_WIRED
|| other_type == NM_TYPE_SETTING_BOND
|| other_type == NM_TYPE_SETTING_TEAM
|| other_type == NM_TYPE_SETTING_VLAN);
} else if (virtual_type == NM_TYPE_SETTING_TEAM) {
return ( other_type == NM_TYPE_SETTING_WIRED
|| other_type == NM_TYPE_SETTING_BRIDGE
|| other_type == NM_TYPE_SETTING_BOND
|| other_type == NM_TYPE_SETTING_TEAM
|| other_type == NM_TYPE_SETTING_VLAN);
} else if (virtual_type == NM_TYPE_SETTING_VLAN) {
return ( other_type == NM_TYPE_SETTING_WIRED
|| other_type == NM_TYPE_SETTING_WIRELESS
|| other_type == NM_TYPE_SETTING_BRIDGE
|| other_type == NM_TYPE_SETTING_BOND
|| other_type == NM_TYPE_SETTING_TEAM
|| other_type == NM_TYPE_SETTING_VLAN);
} else {
g_warn_if_reached ();
return FALSE;
}
}

View File

@@ -174,6 +174,9 @@ const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst);
NM_AVAILABLE_IN_0_9_10
const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst);
NM_AVAILABLE_IN_0_9_10
gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type);
G_END_DECLS
#endif /* NM_UTILS_H */