1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

sys/net/netif: add netif_get_by_name_buffer function

This commit is contained in:
Leandro Lanzieri 2021-03-24 09:26:56 +01:00
parent 5c2b599efa
commit d8cf157b26
No known key found for this signature in database
GPG Key ID: F4E9A721761C7593
2 changed files with 26 additions and 3 deletions

View File

@ -34,6 +34,7 @@
#define NET_NETIF_H
#include <stdint.h>
#include <string.h>
#include "list.h"
#include "net/netopt.h"
@ -113,6 +114,19 @@ int netif_get_name(netif_t *netif, char *name);
*/
int16_t netif_get_id(const netif_t *netif);
/**
* @brief Gets interface by name, from a buffer
*
* @pre `name != NULL`
*
* @param[in] name The name of an interface as an array of chars. Must not be `NULL`.
* @param[in] name_len Number of characters in @p name.
*
* @return Pointer to the interface that matches the name
* @retval NULL if no interface is named @p name.
*/
netif_t *netif_get_by_name_buffer(const char *name, size_t name_len);
/**
* @brief Gets interface by name
*
@ -125,7 +139,10 @@ int16_t netif_get_id(const netif_t *netif);
* @return The interface on success.
* @return NULL if no interface is named @p name.
*/
netif_t *netif_get_by_name(const char *name);
static inline netif_t *netif_get_by_name(const char *name)
{
return netif_get_by_name_buffer(name, strlen(name));
}
/**
* @brief Gets interface by a numeric identifier.

View File

@ -56,16 +56,22 @@ __attribute__((weak)) int16_t netif_get_id(const netif_t *netif)
return -1;
}
netif_t *netif_get_by_name(const char *name)
netif_t *netif_get_by_name_buffer(const char *name, size_t name_len)
{
assert(name);
if (name_len > CONFIG_NETIF_NAMELENMAX) {
return NULL;
}
list_node_t *node = netif_list.next;
char tmp[CONFIG_NETIF_NAMELENMAX];
while (node) {
netif_get_name((netif_t *)node, tmp);
if (strncmp(name, tmp, CONFIG_NETIF_NAMELENMAX) == 0) {
size_t len = strlen(tmp);
if ((len == name_len) && (strncmp(name, tmp, name_len) == 0)) {
return (netif_t *)node;
}
node = node->next;