diff options
author | Dan Williams <dcbw@redhat.com> | 2014-09-18 12:16:11 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2015-05-06 16:14:25 -0500 |
commit | fa74ed7ca1bf014b774d1e537e12df80a9c22af4 (patch) | |
tree | 34397f7e66f4ba9804771d8385f741ddf4d924e6 | |
parent | 29ccb8851ce09855d227cdc5e58ebc31e25cb4c5 (diff) | |
download | NetworkManager-fa74ed7ca1bf014b774d1e537e12df80a9c22af4.tar.gz |
platform: add nm_platform_link_get_by_address()
-rw-r--r-- | src/platform/nm-fake-platform.c | 23 | ||||
-rw-r--r-- | src/platform/nm-linux-platform.c | 25 | ||||
-rw-r--r-- | src/platform/nm-platform.c | 29 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 2 |
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 bd8fb185db..fecbdf001c 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 0b51614e2f..ff81ec01cc 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -542,6 +542,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 ea8d82994f..df03f80c5b 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, @@ -531,6 +532,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); |