Blame libnm-core/nm-setting-olpc-mesh.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2007 - 2013 Red Hat, Inc.
Packit 5756e2
 * Copyright (C) 2007 - 2008 Novell, Inc.
Packit 5756e2
 * Copyright (C) 2009 One Laptop per Child
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include "nm-setting-olpc-mesh.h"
Packit 5756e2
Packit Service d0b836
#include <linux/if_ether.h>
Packit Service d0b836
Packit 5756e2
#include "nm-utils.h"
Packit 5756e2
#include "nm-utils-private.h"
Packit 5756e2
#include "nm-setting-private.h"
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * SECTION:nm-setting-olpc-mesh
Packit 5756e2
 * @short_description: Describes connection properties for OLPC-Mesh devices
Packit 5756e2
 *
Packit 5756e2
 * The #NMSettingOlpcMesh object is a #NMSetting subclass that describes properties
Packit 5756e2
 * necessary for connection to OLPC-Mesh devices.
Packit 5756e2
 **/
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_SSID, PROP_CHANNEL, PROP_DHCP_ANYCAST_ADDRESS, );
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    GBytes *ssid;
Packit Service a1bd4f
    char *  dhcp_anycast_addr;
Packit Service a1bd4f
    guint32 channel;
Packit 5756e2
} NMSettingOlpcMeshPrivate;
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NMSettingOlpcMesh, nm_setting_olpc_mesh, NM_TYPE_SETTING)
Packit 5756e2
Packit Service a1bd4f
#define NM_SETTING_OLPC_MESH_GET_PRIVATE(o) \
Packit Service a1bd4f
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NM_TYPE_SETTING_OLPC_MESH, NMSettingOlpcMeshPrivate))
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_setting_olpc_mesh_get_ssid:
Packit 5756e2
 * @setting: the #NMSettingOlpcMesh
Packit 5756e2
 *
Packit 5756e2
 * Returns: (transfer none):
Packit 5756e2
 */
Packit 5756e2
GBytes *
Packit Service a1bd4f
nm_setting_olpc_mesh_get_ssid(NMSettingOlpcMesh *setting)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_SETTING_OLPC_MESH(setting), NULL);
Packit 5756e2
Packit Service a1bd4f
    return NM_SETTING_OLPC_MESH_GET_PRIVATE(setting)->ssid;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
guint32
Packit Service a1bd4f
nm_setting_olpc_mesh_get_channel(NMSettingOlpcMesh *setting)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_SETTING_OLPC_MESH(setting), 0);
Packit 5756e2
Packit Service a1bd4f
    return NM_SETTING_OLPC_MESH_GET_PRIVATE(setting)->channel;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
const char *
Packit Service a1bd4f
nm_setting_olpc_mesh_get_dhcp_anycast_address(NMSettingOlpcMesh *setting)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_SETTING_OLPC_MESH(setting), NULL);
Packit 5756e2
Packit Service a1bd4f
    return NM_SETTING_OLPC_MESH_GET_PRIVATE(setting)->dhcp_anycast_addr;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
verify(NMSetting *setting, NMConnection *connection, GError **error)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingOlpcMeshPrivate *priv = NM_SETTING_OLPC_MESH_GET_PRIVATE(setting);
Packit Service a1bd4f
    gsize                     length;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!priv->ssid) {
Packit Service a1bd4f
        g_set_error_literal(error,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR_MISSING_PROPERTY,
Packit Service a1bd4f
                            _("property is missing"));
Packit Service a1bd4f
        g_prefix_error(error,
Packit Service a1bd4f
                       "%s.%s: ",
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SETTING_NAME,
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SSID);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    length = g_bytes_get_size(priv->ssid);
Packit Service a1bd4f
    if (length == 0 || length > 32) {
Packit Service a1bd4f
        g_set_error_literal(error,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR_INVALID_PROPERTY,
Packit Service a1bd4f
                            _("SSID length is out of range <1-32> bytes"));
Packit Service a1bd4f
        g_prefix_error(error,
Packit Service a1bd4f
                       "%s.%s: ",
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SETTING_NAME,
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SSID);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (priv->channel == 0 || priv->channel > 13) {
Packit Service a1bd4f
        g_set_error(error,
Packit Service a1bd4f
                    NM_CONNECTION_ERROR,
Packit Service a1bd4f
                    NM_CONNECTION_ERROR_INVALID_PROPERTY,
Packit Service a1bd4f
                    _("'%d' is not a valid channel"),
Packit Service a1bd4f
                    priv->channel);
Packit Service a1bd4f
        g_prefix_error(error,
Packit Service a1bd4f
                       "%s.%s: ",
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SETTING_NAME,
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_CHANNEL);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (priv->dhcp_anycast_addr && !nm_utils_hwaddr_valid(priv->dhcp_anycast_addr, ETH_ALEN)) {
Packit Service a1bd4f
        g_set_error_literal(error,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR,
Packit Service a1bd4f
                            NM_CONNECTION_ERROR_INVALID_PROPERTY,
Packit Service a1bd4f
                            _("property is invalid"));
Packit Service a1bd4f
        g_prefix_error(error,
Packit Service a1bd4f
                       "%s.%s: ",
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_SETTING_NAME,
Packit Service a1bd4f
                       NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingOlpcMesh *setting = NM_SETTING_OLPC_MESH(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_SSID:
Packit Service a1bd4f
        g_value_set_boxed(value, nm_setting_olpc_mesh_get_ssid(setting));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_CHANNEL:
Packit Service a1bd4f
        g_value_set_uint(value, nm_setting_olpc_mesh_get_channel(setting));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_DHCP_ANYCAST_ADDRESS:
Packit Service a1bd4f
        g_value_set_string(value, nm_setting_olpc_mesh_get_dhcp_anycast_address(setting));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingOlpcMeshPrivate *priv = NM_SETTING_OLPC_MESH_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_SSID:
Packit Service a1bd4f
        if (priv->ssid)
Packit Service a1bd4f
            g_bytes_unref(priv->ssid);
Packit Service a1bd4f
        priv->ssid = g_value_dup_boxed(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_CHANNEL:
Packit Service a1bd4f
        priv->channel = g_value_get_uint(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_DHCP_ANYCAST_ADDRESS:
Packit Service a1bd4f
        g_free(priv->dhcp_anycast_addr);
Packit Service a1bd4f
        priv->dhcp_anycast_addr = g_value_dup_string(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_setting_olpc_mesh_init(NMSettingOlpcMesh *setting)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_setting_olpc_mesh_new:
Packit 5756e2
 *
Packit 5756e2
 * Creates a new #NMSettingOlpcMesh object with default values.
Packit 5756e2
 *
Packit 5756e2
 * Returns: the new empty #NMSettingOlpcMesh object
Packit 5756e2
 **/
Packit Service a1bd4f
NMSetting *
Packit Service a1bd4f
nm_setting_olpc_mesh_new(void)
Packit 5756e2
{
Packit Service a1bd4f
    return g_object_new(NM_TYPE_SETTING_OLPC_MESH, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
finalize(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingOlpcMeshPrivate *priv = NM_SETTING_OLPC_MESH_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    if (priv->ssid)
Packit Service a1bd4f
        g_bytes_unref(priv->ssid);
Packit Service a1bd4f
    g_free(priv->dhcp_anycast_addr);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_setting_olpc_mesh_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_setting_olpc_mesh_class_init(NMSettingOlpcMeshClass *klass)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *  object_class        = G_OBJECT_CLASS(klass);
Packit Service a1bd4f
    NMSettingClass *setting_class       = NM_SETTING_CLASS(klass);
Packit Service a1bd4f
    GArray *        properties_override = _nm_sett_info_property_override_create_array();
Packit Service a1bd4f
Packit Service a1bd4f
    g_type_class_add_private(klass, sizeof(NMSettingOlpcMeshPrivate));
Packit Service a1bd4f
Packit Service a1bd4f
    object_class->get_property = get_property;
Packit Service a1bd4f
    object_class->set_property = set_property;
Packit Service a1bd4f
    object_class->finalize     = finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    setting_class->verify = verify;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMSettingOlpcMesh:ssid:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * SSID of the mesh network to join.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_SSID] = g_param_spec_boxed(NM_SETTING_OLPC_MESH_SSID,
Packit Service a1bd4f
                                                   "",
Packit Service a1bd4f
                                                   "",
Packit Service a1bd4f
                                                   G_TYPE_BYTES,
Packit Service a1bd4f
                                                   G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE
Packit Service a1bd4f
                                                       | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMSettingOlpcMesh:channel:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Channel on which the mesh network to join is located.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_CHANNEL] =
Packit Service a1bd4f
        g_param_spec_uint(NM_SETTING_OLPC_MESH_CHANNEL,
Packit Service a1bd4f
                          "",
Packit Service a1bd4f
                          "",
Packit Service a1bd4f
                          0,
Packit Service a1bd4f
                          G_MAXUINT32,
Packit Service a1bd4f
                          0,
Packit Service a1bd4f
                          G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMSettingOlpcMesh:dhcp-anycast-address:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Anycast DHCP MAC address used when requesting an IP address via DHCP.
Packit Service a1bd4f
     * The specific anycast address used determines which DHCP server class
Packit Service a1bd4f
     * answers the request.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_DHCP_ANYCAST_ADDRESS] =
Packit Service a1bd4f
        g_param_spec_string(NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS,
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            NULL,
Packit Service a1bd4f
                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
    _nm_properties_override_gobj(properties_override,
Packit Service a1bd4f
                                 obj_properties[PROP_DHCP_ANYCAST_ADDRESS],
Packit Service a1bd4f
                                 &nm_sett_info_propert_type_mac_address);
Packit Service a1bd4f
Packit Service a1bd4f
    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
Packit Service a1bd4f
Packit Service a1bd4f
    _nm_setting_class_commit_full(setting_class,
Packit Service a1bd4f
                                  NM_META_SETTING_TYPE_OLPC_MESH,
Packit Service a1bd4f
                                  NULL,
Packit Service a1bd4f
                                  properties_override);
Packit 5756e2
}