cli: use nm_device_connection_valid() function from libnm-glib

to verify whether device and connection match and don't duplicate the code
in nmcli.
This commit is contained in:
Jiří Klimeš
2012-03-14 14:38:54 +01:00
parent cfa1536fa8
commit 366ac3c5a2

View File

@@ -1122,464 +1122,6 @@ error:
return nmc->return_value;
}
/* --------------------
* These function should be moved to libnm-glib in the end.
*/
static gboolean
check_ethernet_compatible (NMDeviceEthernet *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingWired *s_wired;
const char *connection_type;
gboolean is_pppoe = FALSE;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
connection_type = nm_setting_connection_get_connection_type (s_con);
if ( strcmp (connection_type, NM_SETTING_WIRED_SETTING_NAME)
&& strcmp (connection_type, NM_SETTING_PPPOE_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a wired or PPPoE connection.");
return FALSE;
}
if (!strcmp (connection_type, NM_SETTING_PPPOE_SETTING_NAME))
is_pppoe = TRUE;
s_wired = nm_connection_get_setting_wired (connection);
/* Wired setting is optional for PPPoE */
if (!is_pppoe && !s_wired) {
g_set_error (error, 0, 0,
"The connection was not a valid wired connection.");
return FALSE;
}
if (s_wired) {
const GByteArray *mac;
const char *device_mac_str;
struct ether_addr *device_mac = NULL;
device_mac_str = nm_device_ethernet_get_permanent_hw_address (device);
if (device_mac_str)
device_mac = ether_aton (device_mac_str);
if (!device_mac) {
g_set_error (error, 0, 0, "Invalid device MAC address.");
return FALSE;
}
mac = nm_setting_wired_get_mac_address (s_wired);
if (mac && memcmp (mac->data, device_mac->ether_addr_octet, ETH_ALEN)) {
g_set_error (error, 0, 0,
"The connection's MAC address did not match this device.");
return FALSE;
}
}
// FIXME: check bitrate against device capabilities
return TRUE;
}
static gboolean
check_wifi_compatible (NMDeviceWifi *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingWireless *s_wireless;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_WIRELESS_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a WiFi connection.");
return FALSE;
}
s_wireless = nm_connection_get_setting_wireless (connection);
if (!s_wireless) {
g_set_error (error, 0, 0,
"The connection was not a valid WiFi connection.");
return FALSE;
}
if (s_wireless) {
const GByteArray *mac;
const char *device_mac_str;
struct ether_addr *device_mac = NULL;
device_mac_str = nm_device_wifi_get_permanent_hw_address (device);
if (device_mac_str)
device_mac = ether_aton (device_mac_str);
if (!device_mac) {
g_set_error (error, 0, 0, "Invalid device MAC address.");
return FALSE;
}
mac = nm_setting_wireless_get_mac_address (s_wireless);
if (mac && memcmp (mac->data, device_mac->ether_addr_octet, ETH_ALEN)) {
g_set_error (error, 0, 0,
"The connection's MAC address did not match this device.");
return FALSE;
}
}
// FIXME: check channel/freq/band against bands the hardware supports
// FIXME: check encryption against device capabilities
// FIXME: check bitrate against device capabilities
return TRUE;
}
static gboolean
check_bt_compatible (NMDeviceBt *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingBluetooth *s_bt;
const GByteArray *array;
char *str;
const char *device_hw_str;
int addr_match = FALSE;
const char *bt_type_str;
guint32 bt_type, bt_capab;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_BLUETOOTH_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a Bluetooth connection.");
return FALSE;
}
s_bt = nm_connection_get_setting_bluetooth (connection);
if (!s_bt) {
g_set_error (error, 0, 0,
"The connection was not a valid Bluetooth connection.");
return FALSE;
}
array = nm_setting_bluetooth_get_bdaddr (s_bt);
if (!array || (array->len != ETH_ALEN)) {
g_set_error (error, 0, 0,
"The connection did not contain a valid Bluetooth address.");
return FALSE;
}
bt_type_str = nm_setting_bluetooth_get_connection_type (s_bt);
g_assert (bt_type_str);
bt_type = NM_BT_CAPABILITY_NONE;
if (!strcmp (bt_type_str, NM_SETTING_BLUETOOTH_TYPE_DUN))
bt_type = NM_BT_CAPABILITY_DUN;
else if (!strcmp (bt_type_str, NM_SETTING_BLUETOOTH_TYPE_PANU))
bt_type = NM_BT_CAPABILITY_NAP;
bt_capab = nm_device_bt_get_capabilities (device);
if (!(bt_type & bt_capab)) {
g_set_error (error, 0, 0,
"The connection was not compatible with the device's capabilities.");
return FALSE;
}
device_hw_str = nm_device_bt_get_hw_address (device);
str = nm_utils_hwaddr_ntoa (array->data, ARPHRD_ETHER);
addr_match = !strcmp (device_hw_str, str);
g_free (str);
return addr_match;
}
static gboolean
check_olpc_mesh_compatible (NMDeviceOlpcMesh *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingOlpcMesh *s_mesh;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_OLPC_MESH_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a Mesh connection.");
return FALSE;
}
s_mesh = nm_connection_get_setting_olpc_mesh (connection);
if (!s_mesh) {
g_set_error (error, 0, 0,
"The connection was not a valid Mesh connection.");
return FALSE;
}
return TRUE;
}
#if WITH_WIMAX
static gboolean
check_wimax_compatible (NMDeviceWimax *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingWimax *s_wimax;
const GByteArray *mac;
const char *device_mac_str;
struct ether_addr *device_mac = NULL;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_WIMAX_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a WiMAX connection.");
return FALSE;
}
s_wimax = nm_connection_get_setting_wimax (connection);
if (!s_wimax) {
g_set_error (error, 0, 0,
"The connection was not a valid WiMAX connection.");
return FALSE;
}
device_mac_str = nm_device_wimax_get_hw_address (device);
if (device_mac_str)
device_mac = ether_aton (device_mac_str);
if (!device_mac) {
g_set_error (error, 0, 0, "Invalid device MAC address.");
return FALSE;
}
mac = nm_setting_wimax_get_mac_address (s_wimax);
if (mac && memcmp (mac->data, device_mac->ether_addr_octet, ETH_ALEN)) {
g_set_error (error, 0, 0,
"The connection's MAC address did not match this device.");
return FALSE;
}
return TRUE;
}
#endif
static gboolean
check_modem_compatible (NMDeviceModem *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingGsm *s_gsm;
NMSettingCdma *s_cdma;
NMDeviceModemCapabilities caps = NM_DEVICE_MODEM_CAPABILITY_NONE;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
/* Figure out what the modem supports */
caps = nm_device_modem_get_current_capabilities (device);
if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_GSM_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a GSM connection.");
return FALSE;
}
s_gsm = nm_connection_get_setting_gsm (connection);
if (!s_gsm) {
g_set_error (error, 0, 0,
"The connection was not a valid GSM connection.");
return FALSE;
}
} else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_CDMA_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not a CDMA connection.");
return FALSE;
}
s_cdma = nm_connection_get_setting_cdma (connection);
if (!s_cdma) {
g_set_error (error, 0, 0,
"The connection was not a valid CDMA connection.");
return FALSE;
}
}
return TRUE;
}
static gboolean
check_infiniband_compatible (NMDeviceInfiniband *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingInfiniband *s_infiniband;
const char *connection_type;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
connection_type = nm_setting_connection_get_connection_type (s_con);
if (strcmp (connection_type, NM_SETTING_INFINIBAND_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not an InfiniBand connection.");
return FALSE;
}
s_infiniband = nm_connection_get_setting_infiniband (connection);
if (!s_infiniband) {
g_set_error (error, 0, 0,
"The connection was not a valid InfiniBand connection.");
return FALSE;
}
if (s_infiniband) {
const GByteArray *mac;
const char *device_mac_str;
GByteArray *device_mac;
device_mac_str = nm_device_infiniband_get_hw_address (device);
device_mac = nm_utils_hwaddr_atoba (device_mac_str, ARPHRD_INFINIBAND);
if (!device_mac) {
g_set_error (error, 0, 0, "Invalid device MAC address.");
return FALSE;
}
mac = nm_setting_infiniband_get_mac_address (s_infiniband);
if (mac && memcmp (mac->data, device_mac->data, mac->len)) {
g_byte_array_unref (device_mac);
g_set_error (error, 0, 0,
"The connection's MAC address did not match this device.");
return FALSE;
}
g_byte_array_unref (device_mac);
}
return TRUE;
}
static gboolean
check_bond_compatible (NMDeviceBond *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingBond *s_bond;
const char *connection_type;
const char *dev_iface_name, *bond_iface_name;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
connection_type = nm_setting_connection_get_connection_type (s_con);
if (strcmp (connection_type, NM_SETTING_BOND_SETTING_NAME)) {
g_set_error (error, 0, 0,
"The connection was not an Bond connection.");
return FALSE;
}
s_bond = nm_connection_get_setting_bond (connection);
if (!s_bond) {
g_set_error (error, 0, 0,
"The connection was not a valid Bond connection.");
return FALSE;
}
dev_iface_name = nm_device_get_iface (NM_DEVICE (device));
bond_iface_name = nm_setting_bond_get_interface_name (s_bond);
if (g_strcmp0 (dev_iface_name, bond_iface_name) != 0) {
g_set_error (error, 0, 0,
"The connection's and device's interface names did not match.");
return FALSE;
}
return TRUE;
}
static gboolean
check_vlan_compatible (NMDeviceVlan *device, NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
NMSettingVlan *s_vlan;
const char *ctype, *dev_iface_name, *vlan_iface_name;
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
ctype = nm_setting_connection_get_connection_type (s_con);
if (strcmp (ctype, NM_SETTING_VLAN_SETTING_NAME) != 0) {
g_set_error (error, 0, 0,
"The connection was not an VLAN connection.");
return FALSE;
}
s_vlan = nm_connection_get_setting_vlan (connection);
if (!s_vlan) {
g_set_error (error, 0, 0,
"The connection was not a valid VLAN connection.");
return FALSE;
}
if (nm_setting_vlan_get_id (s_vlan) != nm_device_vlan_get_vlan_id (NM_DEVICE_VLAN (device))) {
g_set_error (error, 0, 0,
"The connection did not match the device's VLAN ID.");
return FALSE;
}
dev_iface_name = nm_device_get_iface (NM_DEVICE (device));
vlan_iface_name = nm_setting_vlan_get_interface_name (s_vlan);
if (vlan_iface_name && g_strcmp0 (dev_iface_name, vlan_iface_name) != 0) {
g_set_error (error, 0, 0,
"The connection's and device's interface names did not match.");
return FALSE;
}
return TRUE;
}
static gboolean
nm_device_is_connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
{
g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
if (NM_IS_DEVICE_ETHERNET (device))
return check_ethernet_compatible (NM_DEVICE_ETHERNET (device), connection, error);
else if (NM_IS_DEVICE_WIFI (device))
return check_wifi_compatible (NM_DEVICE_WIFI (device), connection, error);
else if (NM_IS_DEVICE_BT (device))
return check_bt_compatible (NM_DEVICE_BT (device), connection, error);
else if (NM_IS_DEVICE_OLPC_MESH (device))
return check_olpc_mesh_compatible (NM_DEVICE_OLPC_MESH (device), connection, error);
#if WITH_WIMAX
else if (NM_IS_DEVICE_WIMAX (device))
return check_wimax_compatible (NM_DEVICE_WIMAX (device), connection, error);
#endif
else if (NM_IS_DEVICE_MODEM (device))
return check_modem_compatible (NM_DEVICE_MODEM (device), connection, error);
else if (NM_IS_DEVICE_INFINIBAND (device))
return check_infiniband_compatible (NM_DEVICE_INFINIBAND (device), connection, error);
else if (NM_IS_DEVICE_BOND (device))
return check_bond_compatible (NM_DEVICE_BOND (device), connection, error);
else if (NM_IS_DEVICE_VLAN (device))
return check_vlan_compatible (NM_DEVICE_VLAN (device), connection, error);
g_set_error (error, 0, 0, "unhandled device type '%s'", G_OBJECT_TYPE_NAME (device));
return FALSE;
}
/* -------------------- */
static NMActiveConnection *
get_default_active_connection (NmCli *nmc, NMDevice **device)
{
@@ -1691,11 +1233,11 @@ find_device_for_connection (NmCli *nmc,
if (iface) {
const char *dev_iface = nm_device_get_iface (dev);
if ( !strcmp (dev_iface, iface)
&& nm_device_is_connection_compatible (dev, connection, NULL)) {
&& nm_device_connection_valid (dev, connection)) {
found_device = dev;
}
} else {
if (nm_device_is_connection_compatible (dev, connection, NULL)) {
if (nm_device_connection_valid (dev, connection)) {
found_device = dev;
}
}