From 3206568e438dd97182bde66b8dafe4fbbffb330c Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 14 Feb 2019 11:37:23 +0200 Subject: [PATCH] net: if: Start index numbering from 1 In order to follow the BSD socket numbering of the network interfaces, start numbering from 1. The index 0 is reserved to mean any interface in BSD socket code. Fixes #13084 Signed-off-by: Jukka Rissanen --- include/net/net_if.h | 4 ++-- subsys/net/ip/net_if.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/net/net_if.h b/include/net/net_if.h index 97f3bd45a12..5e8506546f4 100644 --- a/include/net/net_if.h +++ b/include/net/net_if.h @@ -1632,7 +1632,7 @@ bool net_if_need_calc_tx_checksum(struct net_if *iface); * * @return Pointer to interface or NULL if not found. */ -struct net_if *net_if_get_by_index(u8_t index); +struct net_if *net_if_get_by_index(int index); /** * @brief Get interface index according to pointer @@ -1641,7 +1641,7 @@ struct net_if *net_if_get_by_index(u8_t index); * * @return Interface index */ -u8_t net_if_get_by_iface(struct net_if *iface); +int net_if_get_by_iface(struct net_if *iface); /** * @typedef net_if_cb_t diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index 73482de1936..50402d0e12a 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -2818,21 +2818,27 @@ bool net_if_need_calc_rx_checksum(struct net_if *iface) return need_calc_checksum(iface, ETHERNET_HW_RX_CHKSUM_OFFLOAD); } -struct net_if *net_if_get_by_index(u8_t index) +struct net_if *net_if_get_by_index(int index) { - if (&__net_if_start[index] >= __net_if_end) { + if (index <= 0) { + return NULL; + } + + if (&__net_if_start[index - 1] >= __net_if_end) { NET_DBG("Index %d is too large", index); return NULL; } - return &__net_if_start[index]; + return &__net_if_start[index - 1]; } -u8_t net_if_get_by_iface(struct net_if *iface) +int net_if_get_by_iface(struct net_if *iface) { - NET_ASSERT(iface >= __net_if_start && iface < __net_if_end); + if (!(iface >= __net_if_start && iface < __net_if_end)) { + return -1; + } - return iface - __net_if_start; + return (iface - __net_if_start) + 1; } void net_if_foreach(net_if_cb_t cb, void *user_data)