Browse Source

net: if: Add helper to get src interface and address from dst address

Instead of calling various network interface API functions to get
the network interface and related source IP address, have a single
function that can return both data.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
pull/87596/head
Jukka Rissanen 5 months ago committed by Benjamin Cabé
parent
commit
663867dbb0
  1. 52
      include/zephyr/net/net_if.h
  2. 24
      subsys/net/ip/net_if.c

52
include/zephyr/net/net_if.h

@ -2207,6 +2207,32 @@ static inline struct net_if *net_if_ipv6_select_src_iface( @@ -2207,6 +2207,32 @@ static inline struct net_if *net_if_ipv6_select_src_iface(
}
#endif
/**
* @brief Get a network interface that should be used when sending
* IPv6 network data to destination. Also return the source IPv6 address from
* that network interface.
*
* @param dst IPv6 destination address
* @param src_addr IPv6 source address. This can be set to NULL if the source
* address is not needed.
*
* @return Pointer to network interface to use, NULL if no suitable interface
* could be found.
*/
#if defined(CONFIG_NET_IPV6)
struct net_if *net_if_ipv6_select_src_iface_addr(const struct in6_addr *dst,
const struct in6_addr **src_addr);
#else
static inline struct net_if *net_if_ipv6_select_src_iface_addr(
const struct in6_addr *dst, const struct in6_addr **src_addr)
{
ARG_UNUSED(dst);
ARG_UNUSED(src_addr);
return NULL;
}
#endif /* CONFIG_NET_IPV6 */
/**
* @brief Get a IPv6 link local address in a given state.
*
@ -2588,6 +2614,32 @@ static inline struct net_if *net_if_ipv4_select_src_iface( @@ -2588,6 +2614,32 @@ static inline struct net_if *net_if_ipv4_select_src_iface(
}
#endif
/**
* @brief Get a network interface that should be used when sending
* IPv4 network data to destination. Also return the source IPv4 address from
* that network interface.
*
* @param dst IPv4 destination address
* @param src_addr IPv4 source address. This can be set to NULL if the source
* address is not needed.
*
* @return Pointer to network interface to use, NULL if no suitable interface
* could be found.
*/
#if defined(CONFIG_NET_IPV4)
struct net_if *net_if_ipv4_select_src_iface_addr(const struct in_addr *dst,
const struct in_addr **src_addr);
#else
static inline struct net_if *net_if_ipv4_select_src_iface_addr(
const struct in_addr *dst, const struct in_addr **src_addr)
{
ARG_UNUSED(dst);
ARG_UNUSED(src_addr);
return NULL;
}
#endif /* CONFIG_NET_IPV4 */
/**
* @brief Get a IPv4 source address that should be used when sending
* network data to destination.

24
subsys/net/ip/net_if.c

@ -3275,7 +3275,8 @@ const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *dst_iface, @@ -3275,7 +3275,8 @@ const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *dst_iface,
IPV6_PREFER_SRC_PUBTMP_DEFAULT);
}
struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
struct net_if *net_if_ipv6_select_src_iface_addr(const struct in6_addr *dst,
const struct in6_addr **src_addr)
{
struct net_if *iface = NULL;
const struct in6_addr *src;
@ -3285,6 +3286,10 @@ struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst) @@ -3285,6 +3286,10 @@ struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
net_if_ipv6_addr_lookup(src, &iface);
}
if (src_addr != NULL) {
*src_addr = src;
}
if (iface == NULL) {
iface = net_if_get_default();
}
@ -3292,6 +3297,11 @@ struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst) @@ -3292,6 +3297,11 @@ struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
return iface;
}
struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
{
return net_if_ipv6_select_src_iface_addr(dst, NULL);
}
#if defined(CONFIG_NET_NATIVE_IPV6)
uint32_t net_if_ipv6_calc_reachable_time(struct net_if_ipv6 *ipv6)
@ -3600,7 +3610,8 @@ out: @@ -3600,7 +3610,8 @@ out:
return ret;
}
struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
struct net_if *net_if_ipv4_select_src_iface_addr(const struct in_addr *dst,
const struct in_addr **src_addr)
{
struct net_if *selected = NULL;
const struct in_addr *src;
@ -3614,9 +3625,18 @@ struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst) @@ -3614,9 +3625,18 @@ struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
selected = net_if_get_default();
}
if (src_addr != NULL) {
*src_addr = src;
}
return selected;
}
struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
{
return net_if_ipv4_select_src_iface_addr(dst, NULL);
}
static uint8_t get_diff_ipv4(const struct in_addr *src,
const struct in_addr *dst)
{

Loading…
Cancel
Save