summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2014-09-18 12:16:11 -0500
committerThomas Haller <thaller@redhat.com>2015-06-17 16:19:19 +0200
commit081aeaf3e6faa48824fdae9a6ee90253f3e34336 (patch)
treede9a04938ed3fbd07b473d8b8cf923e8ba86342b
parentf8a9574f7e2a0c0cff839c2d3cd8fe7c9b7f81b9 (diff)
downloadNetworkManager-081aeaf3e6faa48824fdae9a6ee90253f3e34336.tar.gz
platform: add nm_platform_link_get_by_address()
(cherry picked from commit fa74ed7ca1bf014b774d1e537e12df80a9c22af4)
-rw-r--r--src/platform/nm-fake-platform.c23
-rw-r--r--src/platform/nm-linux-platform.c25
-rw-r--r--src/platform/nm-platform.c29
-rw-r--r--src/platform/nm-platform.h2
4 files changed, 79 insertions, 0 deletions
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c
index 2969e37b6b..1d74a3c5e7 100644
--- a/src/platform/nm-fake-platform.c
+++ b/src/platform/nm-fake-platform.c
@@ -168,6 +168,28 @@ _nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *l)
}
static gboolean
+_nm_platform_link_get_by_address (NMPlatform *platform,
+ gconstpointer address,
+ size_t length,
+ NMPlatformLink *l)
+{
+ NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
+ guint i;
+
+ for (i = 0; i < priv->links->len; i++) {
+ NMFakePlatformLink *device = &g_array_index (priv->links, NMFakePlatformLink, i);
+
+ if ( device->address
+ && g_bytes_get_size (device->address) == length
+ && memcmp (g_bytes_get_data (device->address, NULL), address, length) == 0) {
+ *l = device->link;
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+static gboolean
link_add (NMPlatform *platform,
const char *name,
NMLinkType type,
@@ -1370,6 +1392,7 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
platform_class->sysctl_get = sysctl_get;
platform_class->link_get = _nm_platform_link_get;
+ platform_class->link_get_by_address = _nm_platform_link_get_by_address;
platform_class->link_get_all = link_get_all;
platform_class->link_add = link_add;
platform_class->link_delete = link_delete;
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 24f69a84ea..67e93c001f 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -2318,6 +2318,30 @@ _nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *l)
return (rtnllink && init_link (platform, l ? l : &tmp, rtnllink));
}
+static gboolean
+_nm_platform_link_get_by_address (NMPlatform *platform,
+ gconstpointer address,
+ size_t length,
+ NMPlatformLink *l)
+{
+ NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
+ struct nl_object *object;
+
+ for (object = nl_cache_get_first (priv->link_cache); object; object = nl_cache_get_next (object)) {
+ struct rtnl_link *rtnl_link = (struct rtnl_link *) object;
+ struct nl_addr *nladdr;
+ gconstpointer hwaddr;
+
+ nladdr = rtnl_link_get_addr (rtnl_link);
+ if (nladdr && (nl_addr_get_len (nladdr) == length)) {
+ hwaddr = nl_addr_get_binary_addr (nladdr);
+ if (hwaddr && memcmp (hwaddr, address, length) == 0)
+ return init_link (platform, l, rtnl_link);
+ }
+ }
+ return FALSE;
+}
+
static struct nl_object *
build_rtnl_link (int ifindex, const char *name, NMLinkType type)
{
@@ -4705,6 +4729,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
platform_class->sysctl_get = sysctl_get;
platform_class->link_get = _nm_platform_link_get;
+ platform_class->link_get_by_address = _nm_platform_link_get_by_address;
platform_class->link_get_all = link_get_all;
platform_class->link_add = link_add;
platform_class->link_delete = link_delete;
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 71b1046802..9ce677ee18 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -544,6 +544,35 @@ nm_platform_link_get (NMPlatform *self, int ifindex, NMPlatformLink *link)
}
/**
+ * nm_platform_link_get_by_address:
+ * @self: platform instance
+ * @address: a pointer to the binary hardware address
+ * @length: the size of @address in bytes
+ * @link: (out): output NMPlatformLink structure.
+ *
+ * If a link with given @address exists, fill the given NMPlatformLink
+ * structure.
+ *
+ * Returns: %TRUE, if such a link exists, %FALSE otherwise.
+ * If the link does not exist, the content of @link is undefined.
+ **/
+gboolean
+nm_platform_link_get_by_address (NMPlatform *self,
+ gconstpointer address,
+ size_t length,
+ NMPlatformLink *link)
+{
+ _CHECK_SELF (self, klass, FALSE);
+
+ g_return_val_if_fail (address != NULL, FALSE);
+ g_return_val_if_fail (length > 0, FALSE);
+ g_return_val_if_fail (link, FALSE);
+
+ g_return_val_if_fail (klass->link_get_by_address, FALSE);
+ return !!klass->link_get_by_address (self, address, length, link);
+}
+
+/**
* nm_platform_link_add:
* @self: platform instance
* @name: Interface name
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index 05f4686c0a..6d8d1cfede 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -374,6 +374,7 @@ typedef struct {
char * (*sysctl_get) (NMPlatform *, const char *path);
gboolean (*link_get) (NMPlatform *platform, int ifindex, NMPlatformLink *link);
+ gboolean (*link_get_by_address) (NMPlatform *platform, gconstpointer address, size_t length, NMPlatformLink *link);
GArray *(*link_get_all) (NMPlatform *);
gboolean (*link_add) (NMPlatform *,
const char *name,
@@ -530,6 +531,7 @@ gboolean nm_platform_sysctl_set_ip6_hop_limit_safe (NMPlatform *self, const char
gboolean nm_platform_link_get (NMPlatform *self, int ifindex, NMPlatformLink *link);
GArray *nm_platform_link_get_all (NMPlatform *self);
+gboolean nm_platform_link_get_by_address (NMPlatform *self, gconstpointer address, size_t length, NMPlatformLink *link);
gboolean nm_platform_dummy_add (NMPlatform *self, const char *name, NMPlatformLink *out_link);
gboolean nm_platform_bridge_add (NMPlatform *self, const char *name, const void *address, size_t address_len, NMPlatformLink *out_link);
gboolean nm_platform_bond_add (NMPlatform *self, const char *name, NMPlatformLink *out_link);