net: linkaddr: introduce net_linkaddr_set function
The net_linkaddr_storage structure contains an array of bytes used to store the link address. This array can be different sizes depending on the CONFIG options used when building. To facilitate consistency and error checking let's introduce a new helper function to copy the addr and len values to this structure. Also move all uses of memcpy related to net_link_storage structures to the new helper function. Change-Id: Ic547d86b07e62e5ac3bc330d4eaeb4508a143200 Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
parent
4aad767328
commit
7b674cb287
3 changed files with 39 additions and 12 deletions
|
@ -14,6 +14,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -78,6 +79,32 @@ static inline bool net_linkaddr_cmp(struct net_linkaddr *lladdr1,
|
|||
return !memcmp(lladdr1->addr, lladdr2->addr, lladdr1->len);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Set the member data of a link layer address storage structure.
|
||||
*
|
||||
* @param lladdr_store The link address storage structure to change.
|
||||
* @param new_addr Array of bytes containing the link address.
|
||||
* @param new_len Length of the link address array.
|
||||
* This value should always be <= NET_LINK_ADDR_MAX_LENGTH.
|
||||
*/
|
||||
static inline int net_linkaddr_set(struct net_linkaddr_storage *lladdr_store,
|
||||
uint8_t *new_addr, uint8_t new_len)
|
||||
{
|
||||
if (!lladdr_store || !new_addr) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (new_len > NET_LINK_ADDR_MAX_LENGTH) {
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
lladdr_store->len = new_len;
|
||||
memcpy(lladdr_store->addr, new_addr, new_len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -709,8 +709,8 @@ static inline void handle_ns_neighbor(struct net_buf *buf,
|
|||
cached_lladdr,
|
||||
&NET_IPV6_BUF(buf)->src);
|
||||
|
||||
cached_lladdr->len = lladdr.len;
|
||||
memcpy(cached_lladdr->addr, lladdr.addr, lladdr.len);
|
||||
net_linkaddr_set(cached_lladdr, lladdr.addr,
|
||||
lladdr.len);
|
||||
|
||||
net_ipv6_nbr_data(nbr)->state = NET_NBR_STALE;
|
||||
} else {
|
||||
|
@ -1173,9 +1173,9 @@ static inline bool handle_na_neighbor(struct net_buf *buf,
|
|||
cached_lladdr,
|
||||
&NET_ICMPV6_NS_BUF(buf)->tgt);
|
||||
|
||||
memcpy(cached_lladdr->addr,
|
||||
&tllao[NET_ICMPV6_OPT_DATA_OFFSET],
|
||||
cached_lladdr->len);
|
||||
net_linkaddr_set(cached_lladdr,
|
||||
&tllao[NET_ICMPV6_OPT_DATA_OFFSET],
|
||||
cached_lladdr->len);
|
||||
}
|
||||
|
||||
if (net_is_solicited(buf)) {
|
||||
|
@ -1213,9 +1213,9 @@ static inline bool handle_na_neighbor(struct net_buf *buf,
|
|||
cached_lladdr,
|
||||
&NET_ICMPV6_NS_BUF(buf)->tgt);
|
||||
|
||||
memcpy(cached_lladdr->addr,
|
||||
&tllao[NET_ICMPV6_OPT_DATA_OFFSET],
|
||||
cached_lladdr->len);
|
||||
net_linkaddr_set(cached_lladdr,
|
||||
&tllao[NET_ICMPV6_OPT_DATA_OFFSET],
|
||||
cached_lladdr->len);
|
||||
}
|
||||
|
||||
if (net_is_solicited(buf)) {
|
||||
|
@ -1644,8 +1644,8 @@ static inline struct net_buf *handle_ra_neighbor(struct net_buf *buf,
|
|||
cached_lladdr,
|
||||
&NET_IPV6_BUF(buf)->src);
|
||||
|
||||
cached_lladdr->len = lladdr.len;
|
||||
memcpy(cached_lladdr->addr, lladdr.addr, lladdr.len);
|
||||
net_linkaddr_set(cached_lladdr, lladdr.addr,
|
||||
lladdr.len);
|
||||
|
||||
net_ipv6_nbr_data(*nbr)->state = NET_NBR_STALE;
|
||||
} else {
|
||||
|
|
|
@ -124,8 +124,8 @@ int net_nbr_link(struct net_nbr *nbr, struct net_if *iface,
|
|||
net_neighbor_lladdr[avail].ref++;
|
||||
nbr->idx = avail;
|
||||
|
||||
memcpy(net_neighbor_lladdr[avail].lladdr.addr,
|
||||
lladdr->addr, lladdr->len);
|
||||
net_linkaddr_set(&net_neighbor_lladdr[avail].lladdr, lladdr->addr,
|
||||
lladdr->len);
|
||||
net_neighbor_lladdr[avail].lladdr.len = lladdr->len;
|
||||
|
||||
nbr->iface = iface;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue