libnm: implement special setter for direct string property for mac address

This commit is contained in:
Thomas Haller
2021-07-14 07:40:35 +02:00
parent 96657b1556
commit 0c7286a855
11 changed files with 113 additions and 156 deletions

View File

@@ -230,28 +230,6 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
/*****************************************************************************/
static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE(object);
switch (prop_id) {
case PROP_BDADDR:
g_free(priv->bdaddr);
priv->bdaddr = _nm_utils_hwaddr_canonical_or_invalid(g_value_get_string(value), ETH_ALEN);
break;
case PROP_TYPE:
g_free(priv->type);
priv->type = g_value_dup_string(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
static void
nm_setting_bluetooth_init(NMSettingBluetooth *setting)
{}
@@ -279,7 +257,7 @@ nm_setting_bluetooth_class_init(NMSettingBluetoothClass *klass)
g_type_class_add_private(klass, sizeof(NMSettingBluetoothPrivate));
object_class->get_property = _nm_setting_property_get_property_direct;
object_class->set_property = set_property;
object_class->set_property = _nm_setting_property_set_property_direct;
setting_class->verify = verify;
setting_class->finalize_direct = TRUE;
@@ -289,16 +267,14 @@ nm_setting_bluetooth_class_init(NMSettingBluetoothClass *klass)
*
* The Bluetooth address of the device.
**/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_BLUETOOTH_BDADDR,
PROP_BDADDR,
NM_SETTING_PARAM_INFERRABLE,
NMSettingBluetoothPrivate,
bdaddr,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingBluetooth:type:

View File

@@ -1627,16 +1627,14 @@ nm_setting_bridge_class_init(NMSettingBridgeClass *klass)
* BRIDGE_MACADDR for bridges is an NM extension.
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_BRIDGE_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_INFERRABLE,
NMSettingBridgePrivate,
mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingBridge:stp:
@@ -1936,16 +1934,14 @@ nm_setting_bridge_class_init(NMSettingBridgeClass *klass)
* example: BRIDGING_OPTS="group_address=01:80:C2:00:00:0A"
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_BRIDGE_GROUP_ADDRESS,
PROP_GROUP_ADDRESS,
NM_SETTING_PARAM_INFERRABLE,
NMSettingBridgePrivate,
group_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingBridge:vlan-protocol:

View File

@@ -426,16 +426,15 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
* example: HWADDR=01:02:03:04:05:06:07:08:09:0A:01:02:03:04:05:06:07:08:09:11
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_INFINIBAND_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_INFERRABLE,
NMSettingInfinibandPrivate,
mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len =
INFINIBAND_ALEN);
/**
* NMSettingInfiniband:mtu:

View File

@@ -275,16 +275,14 @@ nm_setting_olpc_mesh_class_init(NMSettingOlpcMeshClass *klass)
*
* This is currently only implemented by dhclient DHCP plugin.
**/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS,
PROP_DHCP_ANYCAST_ADDRESS,
NM_SETTING_PARAM_NONE,
NMSettingOlpcMeshPrivate,
dhcp_anycast_addr,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);

View File

@@ -252,16 +252,14 @@ nm_setting_wimax_class_init(NMSettingWimaxClass *klass)
*
* Deprecated: 1.2: WiMAX is no longer supported.
**/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_WIMAX_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_NONE,
NMSettingWimaxPrivate,
mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);

View File

@@ -1409,16 +1409,14 @@ nm_setting_wired_class_init(NMSettingWiredClass *klass)
* permanent MAC address exists, the MAC address initially configured on the device.
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_WIRED_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_INFERRABLE,
NMSettingWiredPrivate,
device_mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingWired:cloned-mac-address:

View File

@@ -1533,16 +1533,14 @@ nm_setting_wireless_class_init(NMSettingWirelessClass *klass)
* example: BSSID=00:1E:BD:64:83:21
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_WIRELESS_BSSID,
PROP_BSSID,
NM_SETTING_PARAM_NONE,
NMSettingWirelessPrivate,
bssid,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingWireless:rate:
@@ -1613,16 +1611,14 @@ nm_setting_wireless_class_init(NMSettingWirelessClass *klass)
* permanent MAC address exists, the MAC address initially configured on the device.
* ---end---
*/
_nm_setting_property_define_direct_mac_address(
properties_override,
_nm_setting_property_define_direct_mac_address(properties_override,
obj_properties,
NM_SETTING_WIRELESS_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_NONE,
NMSettingWirelessPrivate,
device_mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len = ETH_ALEN);
/**
* NMSettingWireless:cloned-mac-address:

View File

@@ -200,37 +200,6 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
/*****************************************************************************/
static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE(object);
switch (prop_id) {
case PROP_MAC_ADDRESS:
g_free(priv->mac_address);
priv->mac_address =
_nm_utils_hwaddr_canonical_or_invalid(g_value_get_string(value), IEEE802154_ADDR_LEN);
break;
case PROP_PAN_ID:
priv->pan_id = g_value_get_uint(value);
break;
case PROP_SHORT_ADDRESS:
priv->short_address = g_value_get_uint(value);
break;
case PROP_PAGE:
priv->page = g_value_get_int(value);
break;
case PROP_CHANNEL:
priv->channel = g_value_get_int(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
static void
nm_setting_wpan_init(NMSettingWpan *setting)
{}
@@ -260,7 +229,7 @@ nm_setting_wpan_class_init(NMSettingWpanClass *klass)
g_type_class_add_private(setting_class, sizeof(NMSettingWpanPrivate));
object_class->get_property = _nm_setting_property_get_property_direct;
object_class->set_property = set_property;
object_class->set_property = _nm_setting_property_set_property_direct;
setting_class->verify = verify;
setting_class->finalize_direct = TRUE;
@@ -278,16 +247,15 @@ nm_setting_wpan_class_init(NMSettingWpanClass *klass)
* (e.g. 76:d8:9b:87:66:60:84:ee).
* ---end---
*/
_nm_setting_property_define_direct_string(
properties_override,
_nm_setting_property_define_direct_string(properties_override,
obj_properties,
NM_SETTING_WPAN_MAC_ADDRESS,
PROP_MAC_ADDRESS,
NM_SETTING_PARAM_NONE,
NMSettingWpanPrivate,
mac_address,
/* it's special, because it uses _nm_utils_hwaddr_canonical_or_invalid(). */
.direct_has_special_setter = TRUE);
.direct_set_string_mac_address_len =
IEEE802154_ADDR_LEN);
/**
* NMSettingWpan:pan-id:

View File

@@ -679,6 +679,12 @@ _property_direct_set_string(const NMSettInfoProperty *property_info, char **dst,
{
if (property_info->direct_set_string_ascii_strdown)
return nm_utils_strdup_reset_take(dst, src ? g_ascii_strdown(src, -1) : NULL);
if (property_info->direct_set_string_mac_address_len > 0) {
return nm_utils_strdup_reset_take(dst,
_nm_utils_hwaddr_canonical_or_invalid(
src,
property_info->direct_set_string_mac_address_len));
}
return nm_utils_strdup_reset(dst, src);
}

View File

@@ -7,6 +7,8 @@
#include <linux/pkt_sched.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_infiniband.h>
#include "libnm-glib-aux/nm-uuid.h"
#include "libnm-glib-aux/nm-json-aux.h"
@@ -4493,7 +4495,10 @@ test_setting_metadata(void)
g_assert(g_variant_type_equal(sip->property_type->dbus_type, "ay"));
g_assert(sip->property_type->to_dbus_fcn
== _nm_setting_property_to_dbus_fcn_direct_mac_address);
g_assert(sip->direct_has_special_setter);
g_assert(NM_IN_SET((guint) sip->direct_set_string_mac_address_len,
ETH_ALEN,
8,
INFINIBAND_ALEN));
} else {
g_assert(g_variant_type_equal(sip->property_type->dbus_type, "s"));
g_assert(sip->property_type->to_dbus_fcn
@@ -4507,6 +4512,17 @@ test_setting_metadata(void)
if (sip->direct_set_string_ascii_strdown)
g_assert(sip->property_type->direct_type == NM_VALUE_TYPE_STRING);
if (sip->direct_set_string_mac_address_len != 0) {
g_assert(NM_IN_SET(sip->property_type,
&nm_sett_info_propert_type_direct_string,
&nm_sett_info_propert_type_direct_mac_address));
g_assert(sip->property_type->direct_type == NM_VALUE_TYPE_STRING);
}
g_assert(((sip->direct_set_string_mac_address_len != 0)
+ (!!sip->direct_set_string_ascii_strdown))
<= 1);
if (!sip->property_type->to_dbus_fcn) {
/* it's allowed to have no to_dbus_fcn(), to ignore a property. But such
* properties must not have a param_spec. */

View File

@@ -752,6 +752,12 @@ struct _NMSettInfoProperty {
* normalize the string via g_ascii_strdown(). */
bool direct_set_string_ascii_strdown : 1;
/* If non-zero, this is a NM_VALUE_TYPE_STRING direct property. Actually, it is
* a _nm_setting_property_define_direct_mac_address(), and the setter will
* call _nm_utils_hwaddr_canonical_or_invalid() on the string, with the specified
* MAC address length. */
guint8 direct_set_string_mac_address_len : 5;
/* Currently, properties that set property_type->direct_type only have to_dbus_fcn()
* implemented "the direct way". For the property setter, they still call g_object_set().
* In the future, also other operations, like from_dbus_fcn() should be implemented