Skip to content

Commit 056a3d3

Browse files
jukkarnashif
authored andcommitted
net: if: Add helper to return the first IPv4 address for iface
This is helper is only needed in socket multicast interface selection where we need to get one address from the interface so that it will tell (when getsockopt() is used), the interface IPv4 address where multicast packets will be sent. This is private function which is not needed in public headers so place the prototype to net_private.h file. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 0bd6f3b commit 056a3d3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

subsys/net/ip/net_if.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,40 @@ const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *dst_iface,
37283728
return src;
37293729
}
37303730

3731+
/* Internal function to get the first IPv4 address of the interface */
3732+
struct net_if_addr *net_if_ipv4_addr_get_first_by_index(int ifindex)
3733+
{
3734+
struct net_if *iface = net_if_get_by_index(ifindex);
3735+
struct net_if_addr *ifaddr = NULL;
3736+
struct net_if_ipv4 *ipv4;
3737+
3738+
if (!iface) {
3739+
return NULL;
3740+
}
3741+
3742+
net_if_lock(iface);
3743+
3744+
ipv4 = iface->config.ip.ipv4;
3745+
if (!ipv4) {
3746+
goto out;
3747+
}
3748+
3749+
ARRAY_FOR_EACH(ipv4->unicast, i) {
3750+
if (!ipv4->unicast[i].ipv4.is_used ||
3751+
ipv4->unicast[i].ipv4.address.family != AF_INET) {
3752+
continue;
3753+
}
3754+
3755+
ifaddr = &ipv4->unicast[i].ipv4;
3756+
break;
3757+
}
3758+
3759+
out:
3760+
net_if_unlock(iface);
3761+
3762+
return ifaddr;
3763+
}
3764+
37313765
struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
37323766
struct net_if **ret)
37333767
{

subsys/net/ip/net_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ extern void net_if_stats_reset_all(void);
6262
extern void net_process_rx_packet(struct net_pkt *pkt);
6363
extern void net_process_tx_packet(struct net_pkt *pkt);
6464

65+
extern struct net_if_addr *net_if_ipv4_addr_get_first_by_index(int ifindex);
66+
6567
extern int net_icmp_call_ipv4_handlers(struct net_pkt *pkt,
6668
struct net_ipv4_hdr *ipv4_hdr,
6769
struct net_icmp_hdr *icmp_hdr);

0 commit comments

Comments
 (0)