mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-16 00:08:07 +02:00
Historically, the NMSetting types were in public headers. Theoretically,
that allowed users to subtype our classes. However in practice that was
impossible because they lacked required access to internal functions to
fully create an NMSetting class outside of libnm. And it also was not
useful, because you simply cannot extend libnm by subtyping a libnm
class. And supporting such a use case would be hard and limit what we can
do in libnm.
Having GObject structs in public headers also require that we don't
change it's layout. The ABI of those structs must not change, if anybody
out there was actually subclassing our GObjects.
In libnm 1.34 (commit e46d484fae ('libnm: hide NMSetting types from
public headers')) we moved the structs from headers to internal.
This would have caused a compiler error if anybody was using those
struct definitions. However, we still didn't change the ABI/layout so
that we didn't break users who relied on it (for whatever reason).
It doesn't seem there were any affected user. We waited long enough.
Change internal ABI.
No longer use g_type_class_add_private(). Instead, embed the private
structs directly (_NM_GET_PRIVATE()) or indirectly
(_NM_GET_PRIVATE_PTR()) in the object.
The main benefit is for debugging in the debugger, where we can now
easily find the private data. Previously that was so cumbersome to be
effectively impossible.
It's also the fastest possible way, since NM_SETTING_*_GET_PRIVATE()
literally resolves to "&self->_priv" (plus static asserts and
nm_assert() for type checking).
_NM_GET_PRIVATE() also propagates constness and requires that the
argument is a compatible pointer type (at compile time).
Note that g_type_class_add_private() is also deprecated in glib 2.58 and
replaced by G_ADD_PRIVATE(). For one, we still don't rely on 2.58. Also,
G_ADD_PRIVATE() is a worse solution as it supports a usecase that we
don't care for (public structs in headers). _NM_GET_PRIVATE() is still
faster, works with older glib and most importantly: is better for
debugging as you can find the private data from an object pointer.
For NMSettingIPConfig this is rather awkward, because all direct
properties require a common "klass->private_offset". This was however
the case before this change. Nothing new here. And if you ever touch
this and do something wrong, many unit tests will fail. It's almost
impossible to get wrong, albeit it can be confusing to understand.
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1773
337 lines
11 KiB
C
337 lines
11 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
/*
|
|
* Copyright (C) 2018 Lubomir Rintel <lkundrak@v3.sk>
|
|
*/
|
|
|
|
#include "libnm-core-impl/nm-default-libnm-core.h"
|
|
|
|
#include "nm-setting-wpan.h"
|
|
|
|
#include "nm-connection-private.h"
|
|
#include "nm-setting-connection.h"
|
|
#include "nm-setting-private.h"
|
|
#include "nm-utils-private.h"
|
|
|
|
/**
|
|
* SECTION:nm-setting-wpan
|
|
* @short_description: Describes connection properties for IEEE 802.15.4 (WPAN) MAC
|
|
*
|
|
* The #NMSettingWpan object is a #NMSetting subclass that describes properties
|
|
* necessary for configuring IEEE 802.15.4 (WPAN) MAC layer devices.
|
|
**/
|
|
|
|
/* Ideally we'll be able to get these from a public header. */
|
|
#ifndef IEEE802154_ADDR_LEN
|
|
#define IEEE802154_ADDR_LEN 8
|
|
#endif
|
|
|
|
#ifndef IEEE802154_MAX_PAGE
|
|
#define IEEE802154_MAX_PAGE 31
|
|
#endif
|
|
|
|
#ifndef IEEE802154_MAX_CHANNEL
|
|
#define IEEE802154_MAX_CHANNEL 26
|
|
#endif
|
|
|
|
/*****************************************************************************/
|
|
|
|
NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_MAC_ADDRESS,
|
|
PROP_PAN_ID,
|
|
PROP_SHORT_ADDRESS,
|
|
PROP_PAGE,
|
|
PROP_CHANNEL, );
|
|
|
|
typedef struct {
|
|
char *mac_address;
|
|
guint32 pan_id;
|
|
guint32 short_address;
|
|
gint32 page;
|
|
gint32 channel;
|
|
} NMSettingWpanPrivate;
|
|
|
|
/**
|
|
* NMSettingWpan:
|
|
*
|
|
* IEEE 802.15.4 (WPAN) MAC Settings
|
|
*
|
|
* Since: 1.14
|
|
*/
|
|
struct _NMSettingWpan {
|
|
NMSetting parent;
|
|
NMSettingWpanPrivate _priv;
|
|
};
|
|
|
|
struct _NMSettingWpanClass {
|
|
NMSettingClass parent;
|
|
};
|
|
|
|
G_DEFINE_TYPE(NMSettingWpan, nm_setting_wpan, NM_TYPE_SETTING)
|
|
|
|
#define NM_SETTING_WPAN_GET_PRIVATE(o) \
|
|
_NM_GET_PRIVATE(o, NMSettingWpan, NM_IS_SETTING_WPAN, NMSetting)
|
|
|
|
/*****************************************************************************/
|
|
|
|
/**
|
|
* nm_setting_wpan_get_mac_address:
|
|
* @setting: the #NMSettingWpan
|
|
*
|
|
* Returns: the #NMSettingWpan:mac-address property of the setting
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
const char *
|
|
nm_setting_wpan_get_mac_address(NMSettingWpan *setting)
|
|
{
|
|
g_return_val_if_fail(NM_IS_SETTING_WPAN(setting), NULL);
|
|
|
|
return NM_SETTING_WPAN_GET_PRIVATE(setting)->mac_address;
|
|
}
|
|
|
|
/**
|
|
* nm_setting_wpan_get_pan_id:
|
|
* @setting: the #NMSettingWpan
|
|
*
|
|
* Returns: the #NMSettingWpan:pan-id property of the setting
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
guint16
|
|
nm_setting_wpan_get_pan_id(NMSettingWpan *setting)
|
|
{
|
|
g_return_val_if_fail(NM_IS_SETTING_WPAN(setting), G_MAXUINT16);
|
|
|
|
return NM_SETTING_WPAN_GET_PRIVATE(setting)->pan_id;
|
|
}
|
|
|
|
/**
|
|
* nm_setting_wpan_get_short_address:
|
|
* @setting: the #NMSettingWpan
|
|
*
|
|
* Returns: the #NMSettingWpan:short-address property of the setting
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
guint16
|
|
nm_setting_wpan_get_short_address(NMSettingWpan *setting)
|
|
{
|
|
g_return_val_if_fail(NM_IS_SETTING_WPAN(setting), G_MAXUINT16);
|
|
|
|
return NM_SETTING_WPAN_GET_PRIVATE(setting)->short_address;
|
|
}
|
|
|
|
/**
|
|
* nm_setting_wpan_get_page:
|
|
* @setting: the #NMSettingWpan
|
|
*
|
|
* Returns: the #NMSettingWpan:page property of the setting
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
gint16
|
|
nm_setting_wpan_get_page(NMSettingWpan *setting)
|
|
{
|
|
g_return_val_if_fail(NM_IS_SETTING_WPAN(setting), NM_SETTING_WPAN_PAGE_DEFAULT);
|
|
|
|
return NM_SETTING_WPAN_GET_PRIVATE(setting)->page;
|
|
}
|
|
|
|
/**
|
|
* nm_setting_wpan_get_channel:
|
|
* @setting: the #NMSettingWpan
|
|
*
|
|
* Returns: the #NMSettingWpan:channel property of the setting
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
gint16
|
|
nm_setting_wpan_get_channel(NMSettingWpan *setting)
|
|
{
|
|
g_return_val_if_fail(NM_IS_SETTING_WPAN(setting), NM_SETTING_WPAN_CHANNEL_DEFAULT);
|
|
|
|
return NM_SETTING_WPAN_GET_PRIVATE(setting)->channel;
|
|
}
|
|
|
|
static gboolean
|
|
verify(NMSetting *setting, NMConnection *connection, GError **error)
|
|
{
|
|
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE(setting);
|
|
|
|
if (priv->mac_address && !nm_utils_hwaddr_valid(priv->mac_address, IEEE802154_ADDR_LEN)) {
|
|
g_set_error_literal(error,
|
|
NM_CONNECTION_ERROR,
|
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
_("property is invalid"));
|
|
g_prefix_error(error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_MAC_ADDRESS);
|
|
return FALSE;
|
|
}
|
|
|
|
if ((priv->page == NM_SETTING_WPAN_PAGE_DEFAULT)
|
|
!= (priv->channel == NM_SETTING_WPAN_CHANNEL_DEFAULT)) {
|
|
g_set_error_literal(error,
|
|
NM_CONNECTION_ERROR,
|
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
_("page must be defined along with a channel"));
|
|
g_prefix_error(error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_PAGE);
|
|
return FALSE;
|
|
}
|
|
|
|
if (priv->page < NM_SETTING_WPAN_PAGE_DEFAULT || priv->page > IEEE802154_MAX_PAGE) {
|
|
g_set_error(error,
|
|
NM_CONNECTION_ERROR,
|
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
_("page must be between %d and %d"),
|
|
NM_SETTING_WPAN_PAGE_DEFAULT,
|
|
IEEE802154_MAX_PAGE);
|
|
g_prefix_error(error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_PAGE);
|
|
return FALSE;
|
|
}
|
|
|
|
if (priv->channel < NM_SETTING_WPAN_CHANNEL_DEFAULT || priv->channel > IEEE802154_MAX_CHANNEL) {
|
|
g_set_error(error,
|
|
NM_CONNECTION_ERROR,
|
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
_("channel must not be between %d and %d"),
|
|
NM_SETTING_WPAN_CHANNEL_DEFAULT,
|
|
IEEE802154_MAX_CHANNEL);
|
|
g_prefix_error(error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_CHANNEL);
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
/*****************************************************************************/
|
|
|
|
static void
|
|
nm_setting_wpan_init(NMSettingWpan *setting)
|
|
{}
|
|
|
|
/**
|
|
* nm_setting_wpan_new:
|
|
*
|
|
* Creates a new #NMSettingWpan object with default values.
|
|
*
|
|
* Returns: (transfer full): the new empty #NMSettingWpan object
|
|
*
|
|
* Since: 1.42
|
|
**/
|
|
NMSetting *
|
|
nm_setting_wpan_new(void)
|
|
{
|
|
return g_object_new(NM_TYPE_SETTING_WPAN, NULL);
|
|
}
|
|
|
|
static void
|
|
nm_setting_wpan_class_init(NMSettingWpanClass *klass)
|
|
{
|
|
GObjectClass *object_class = G_OBJECT_CLASS(klass);
|
|
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
|
|
GArray *properties_override = _nm_sett_info_property_override_create_array();
|
|
|
|
object_class->get_property = _nm_setting_property_get_property_direct;
|
|
object_class->set_property = _nm_setting_property_set_property_direct;
|
|
|
|
setting_class->verify = verify;
|
|
|
|
/**
|
|
* NMSettingWpan:mac-address:
|
|
*
|
|
* If specified, this connection will only apply to the IEEE 802.15.4 (WPAN)
|
|
* MAC layer device whose permanent MAC address matches.
|
|
**/
|
|
/* ---keyfile---
|
|
* property: mac-address
|
|
* format: usual hex-digits-and-colons notation
|
|
* description: MAC address in hex-digits-and-colons notation
|
|
* (e.g. 76:d8:9b:87:66:60:84:ee).
|
|
* ---end---
|
|
*/
|
|
_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,
|
|
.direct_set_string_mac_address_len =
|
|
IEEE802154_ADDR_LEN);
|
|
|
|
/**
|
|
* NMSettingWpan:pan-id:
|
|
*
|
|
* IEEE 802.15.4 Personal Area Network (PAN) identifier.
|
|
**/
|
|
_nm_setting_property_define_direct_uint32(properties_override,
|
|
obj_properties,
|
|
NM_SETTING_WPAN_PAN_ID,
|
|
PROP_PAN_ID,
|
|
0,
|
|
G_MAXUINT16,
|
|
G_MAXUINT16,
|
|
NM_SETTING_PARAM_NONE,
|
|
NMSettingWpanPrivate,
|
|
pan_id);
|
|
|
|
/**
|
|
* NMSettingWpan:short-address:
|
|
*
|
|
* Short IEEE 802.15.4 address to be used within a restricted environment.
|
|
**/
|
|
_nm_setting_property_define_direct_uint32(properties_override,
|
|
obj_properties,
|
|
NM_SETTING_WPAN_SHORT_ADDRESS,
|
|
PROP_SHORT_ADDRESS,
|
|
0,
|
|
G_MAXUINT16,
|
|
G_MAXUINT16,
|
|
NM_SETTING_PARAM_NONE,
|
|
NMSettingWpanPrivate,
|
|
short_address);
|
|
|
|
/**
|
|
* NMSettingWpan:page:
|
|
*
|
|
* IEEE 802.15.4 channel page. A positive integer or -1, meaning "do not
|
|
* set, use whatever the device is already set to".
|
|
*
|
|
* Since: 1.16
|
|
**/
|
|
_nm_setting_property_define_direct_int32(properties_override,
|
|
obj_properties,
|
|
NM_SETTING_WPAN_PAGE,
|
|
PROP_PAGE,
|
|
G_MININT16,
|
|
G_MAXINT16,
|
|
NM_SETTING_WPAN_PAGE_DEFAULT,
|
|
NM_SETTING_PARAM_NONE,
|
|
NMSettingWpanPrivate,
|
|
page);
|
|
|
|
/**
|
|
* NMSettingWpan:channel:
|
|
*
|
|
* IEEE 802.15.4 channel. A positive integer or -1, meaning "do not
|
|
* set, use whatever the device is already set to".
|
|
*
|
|
* Since: 1.16
|
|
**/
|
|
_nm_setting_property_define_direct_int32(properties_override,
|
|
obj_properties,
|
|
NM_SETTING_WPAN_CHANNEL,
|
|
PROP_CHANNEL,
|
|
G_MININT16,
|
|
G_MAXINT16,
|
|
NM_SETTING_WPAN_CHANNEL_DEFAULT,
|
|
NM_SETTING_PARAM_NONE,
|
|
NMSettingWpanPrivate,
|
|
channel);
|
|
|
|
g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
|
|
|
|
_nm_setting_class_commit(setting_class,
|
|
NM_META_SETTING_TYPE_WPAN,
|
|
NULL,
|
|
properties_override,
|
|
G_STRUCT_OFFSET(NMSettingWpan, _priv));
|
|
}
|