config: add NMConfigDevice

Add NMConfigDevice, which is mostly just a wrapper around
nm_device_get_hw_address() and nm_device_spec_match_list(), and
implement it in NMDevice. This will be used for config options that
match devices. (We can't use NMDevice directly for dependency
reasons.)
This commit is contained in:
Dan Winship
2013-03-20 09:48:24 -04:00
parent 29ed892de7
commit 0b815ca166
6 changed files with 154 additions and 6 deletions

View File

@@ -363,7 +363,7 @@ nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting)
nm_ip6_config_set_never_default (ip6_config, TRUE);
}
static gboolean
gboolean
nm_match_spec_string (const GSList *specs, const char *match)
{
const GSList *iter;

View File

@@ -39,6 +39,7 @@ int nm_spawn_process (const char *args);
void nm_utils_merge_ip4_config (NMIP4Config *ip4_config, NMSettingIP4Config *setting);
void nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting);
gboolean nm_match_spec_string (const GSList *specs, const char *string);
gboolean nm_match_spec_hwaddr (const GSList *specs, const char *hwaddr);
gboolean nm_match_spec_s390_subchannels (const GSList *specs, const char *subchannels);
gboolean nm_match_spec_interface_name (const GSList *specs, const char *interface_name);

View File

@@ -4,13 +4,19 @@ noinst_LTLIBRARIES = libnm-config.la
libnm_config_la_SOURCES = \
nm-config.c \
nm-config.h
nm-config.h \
nm-config-device.c \
nm-config-device.h
libnm_config_la_CPPFLAGS = \
$(GLIB_CFLAGS) \
-I$(top_srcdir)/include \
-I$(top_srcdir)/libnm-util \
-I$(top_builddir)/libnm-util \
-I$(top_srcdir)/src/logging \
-DNMCONFDIR=\"$(nmconfdir)\"
libnm_config_la_LIBADD = \
$(top_builddir)/libnm-util/libnm-util.la \
$(GLIB_LIBS)

View File

@@ -0,0 +1,80 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2013 Red Hat, Inc.
*/
#include "config.h"
#include "nm-config-device.h"
#include <net/if_arp.h>
#include <nm-utils.h>
G_DEFINE_INTERFACE (NMConfigDevice, nm_config_device, G_TYPE_OBJECT)
static void
nm_config_device_default_init (NMConfigDeviceInterface *iface)
{
}
gboolean
nm_config_device_spec_match_list (NMConfigDevice *self, const char **config_specs)
{
GSList *specs = NULL;
gboolean match;
char buf[NM_UTILS_HWADDR_LEN_MAX + 1], *tmp;
int i;
g_return_val_if_fail (NM_IS_CONFIG_DEVICE (self), FALSE);
if (!config_specs)
return FALSE;
/* For compatibility, we allow an untagged MAC address, and for convenience,
* we allow untagged interface names as well.
*/
for (i = 0; config_specs[i]; i++) {
if (nm_utils_iface_valid_name (config_specs[i]))
specs = g_slist_prepend (specs, g_strdup_printf ("interface-name:%s", config_specs[i]));
else if ( nm_utils_hwaddr_aton (config_specs[i], ARPHRD_ETHER, buf)
|| nm_utils_hwaddr_aton (config_specs[i], ARPHRD_INFINIBAND, buf)) {
tmp = g_ascii_strdown (config_specs[i], -1);
specs = g_slist_prepend (specs, g_strdup_printf ("mac:%s", tmp));
g_free (tmp);
} else
specs = g_slist_prepend (specs, g_strdup (config_specs[i]));
}
specs = g_slist_reverse (specs);
match = NM_CONFIG_DEVICE_GET_INTERFACE (self)->spec_match_list (self, specs);
g_slist_free_full (specs, g_free);
return match;
}
char *
nm_config_device_get_hwaddr (NMConfigDevice *self)
{
const guint8 *bytes;
guint len;
bytes = NM_CONFIG_DEVICE_GET_INTERFACE (self)->get_hw_address (self, &len);
return nm_utils_hwaddr_ntoa (bytes, nm_utils_hwaddr_type (len));
}

View File

@@ -0,0 +1,47 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2013 Red Hat, Inc.
*/
#ifndef NM_CONFIG_DEVICE_H
#define NM_CONFIG_DEVICE_H
#include <glib-object.h>
#define NM_TYPE_CONFIG_DEVICE (nm_config_device_get_type ())
#define NM_CONFIG_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_CONFIG_DEVICE, NMConfigDevice))
#define NM_IS_CONFIG_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_CONFIG_DEVICE))
#define NM_CONFIG_DEVICE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_CONFIG_DEVICE, NMConfigDeviceInterface))
typedef struct _NMConfigDevice NMConfigDevice;
typedef struct _NMConfigDeviceInterface NMConfigDeviceInterface;
struct _NMConfigDeviceInterface {
GTypeInterface g_iface;
/* Methods */
gboolean (*spec_match_list) (NMConfigDevice *device, const GSList *specs);
const guint8 * (* get_hw_address) (NMConfigDevice *device, guint *out_len);
};
GType nm_config_device_get_type (void);
gboolean nm_config_device_spec_match_list (NMConfigDevice *device, const char **config_specs);
char *nm_config_device_get_hwaddr (NMConfigDevice *device);
#endif /* NM_CONFIG_DEVICE_H */

View File

@@ -66,6 +66,7 @@
#include "nm-manager-auth.h"
#include "nm-dbus-glib-types.h"
#include "nm-dispatcher.h"
#include "nm-config-device.h"
static void impl_device_disconnect (NMDevice *device, DBusGMethodInvocation *context);
@@ -133,7 +134,10 @@ enum {
/***********************************************************/
G_DEFINE_ABSTRACT_TYPE (NMDevice, nm_device, G_TYPE_OBJECT)
static void nm_device_config_device_interface_init (NMConfigDeviceInterface *iface);
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (NMDevice, nm_device, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (NM_TYPE_CONFIG_DEVICE, nm_device_config_device_interface_init))
#define NM_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE, NMDevicePrivate))
@@ -5067,6 +5071,13 @@ nm_device_class_init (NMDeviceClass *klass)
dbus_g_error_domain_register (NM_DEVICE_ERROR, NULL, NM_TYPE_DEVICE_ERROR);
}
static void
nm_device_config_device_interface_init (NMConfigDeviceInterface *iface)
{
iface->spec_match_list = (gboolean (*) (NMConfigDevice *, const GSList *)) nm_device_spec_match_list;
iface->get_hw_address = (const guint8 * (*) (NMConfigDevice *, guint *)) nm_device_get_hw_address;
}
void
nm_device_set_firmware_missing (NMDevice *self, gboolean new_missing)
{
@@ -5549,6 +5560,8 @@ nm_device_set_managed (NMDevice *device,
* "s390-subchannels:00.11.22" - matches a device with the given
* z/VM / s390 subchannels.
*
* "*" - matches any device
*
* Returns: #TRUE if @device matches one of the specs in @specs
*/
gboolean
@@ -5556,9 +5569,7 @@ nm_device_spec_match_list (NMDevice *device, const GSList *specs)
{
g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
if (NM_DEVICE_GET_CLASS (device)->spec_match_list)
return NM_DEVICE_GET_CLASS (device)->spec_match_list (device, specs);
return FALSE;
}
static gboolean
@@ -5569,6 +5580,9 @@ spec_match_list (NMDevice *device, const GSList *specs)
char *hwaddr_str;
gboolean matched = FALSE;
if (nm_match_spec_string (specs, "*"))
return TRUE;
hwaddr = nm_device_get_hw_address (device, &hwaddr_len);
if (hwaddr && hwaddr_len) {
hwaddr_str = nm_utils_hwaddr_ntoa (hwaddr, nm_utils_hwaddr_type (hwaddr_len));