From d4d8fd819d9afb7a4053d9c07a32c6ab40dc2cb4 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Mon, 13 Feb 2017 13:21:45 +0100 Subject: [PATCH] Some code cleanup related to netif index handling Made the macro "netif_index_to_num" private, if someone needs it externally, please complain. --- src/core/netif.c | 25 ++++++++++++++++++++++++- src/core/raw.c | 8 ++------ src/core/udp.c | 6 +----- src/include/lwip/ip6_zone.h | 4 ++-- src/include/lwip/netif.h | 5 ++--- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/core/netif.c b/src/core/netif.c index baac4dc5..6fc7f5ee 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -104,6 +104,7 @@ struct netif *netif_list; struct netif *netif_default; +#define netif_index_to_num(index) ((index) - 1) static u8_t netif_num; #if LWIP_NUM_NETIF_CLIENT_DATA > 0 @@ -1309,7 +1310,7 @@ netif_name_to_index(const char *name) { struct netif *netif = netif_find(name); if (netif != NULL) { - return netif_num_to_index(netif); + return netif_get_index(netif); } /* No name found, return invalid index */ return 0; @@ -1345,3 +1346,25 @@ netif_index_to_name(u8_t idx, char *name) } return NULL; } + +/** +* @ingroup netif +* Return the interface for the netif index +* +* @param index index of netif to find +*/ +struct netif* +netif_get_by_index(u8_t index) +{ + struct netif* netif; + + if (index != NETIF_NO_INDEX) { + for (netif = netif_list; netif != NULL; netif = netif->next) { + if (index == netif_get_index(netif)) { + return netif; /* found! */ + } + } + } + + return NULL; +} diff --git a/src/core/raw.c b/src/core/raw.c index d357051d..78e78c77 100644 --- a/src/core/raw.c +++ b/src/core/raw.c @@ -327,15 +327,11 @@ raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) #if LWIP_MULTICAST_TX_OPTIONS netif = NULL; - if (ip_addr_ismulticast(ipaddr) && (pcb->mcast_ifindex != NETIF_NO_INDEX)) { + if (ip_addr_ismulticast(ipaddr)) { /* For multicast-destined packets, use the user-provided interface index to * determine the outgoing interface, if an interface index is set and a * matching netif can be found. Otherwise, fall back to regular routing. */ - for (netif = netif_list; netif != NULL; netif = netif->next) { - if (pcb->mcast_ifindex == netif_num_to_index(netif)) { - break; /* found! */ - } - } + netif = netif_get_by_index(pcb->mcast_ifindex); } if (netif == NULL) diff --git a/src/core/udp.c b/src/core/udp.c index 54ee35b4..ec4b25f6 100644 --- a/src/core/udp.c +++ b/src/core/udp.c @@ -540,11 +540,7 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, * list, but by doing so we skip a route lookup. If the interface index has * gone stale, we fall through and do the regular route lookup after all. */ if (pcb->mcast_ifindex != NETIF_NO_INDEX) { - for (netif = netif_list; netif != NULL; netif = netif->next) { - if (pcb->mcast_ifindex == netif_num_to_index(netif)) { - break; /* found! */ - } - } + netif = netif_get_by_index(pcb->mcast_ifindex); } #if LWIP_IPV4 else diff --git a/src/include/lwip/ip6_zone.h b/src/include/lwip/ip6_zone.h index 0d3bc8fe..a757575e 100644 --- a/src/include/lwip/ip6_zone.h +++ b/src/include/lwip/ip6_zone.h @@ -193,7 +193,7 @@ enum lwip_ipv6_scope_type */ #define ip6_addr_assign_zone(ip6addr, type, netif) \ (ip6_addr_set_zone((ip6addr), \ - ip6_addr_has_scope((ip6addr), (type)) ? netif_num_to_index(netif) : 0)) + ip6_addr_has_scope((ip6addr), (type)) ? netif_get_index(netif) : 0)) /** * Test whether an IPv6 address is "zone-compatible" with a network interface. @@ -215,7 +215,7 @@ enum lwip_ipv6_scope_type * @return 1 if the address is scope-compatible with the netif, 0 if not. */ #define ip6_addr_test_zone(ip6addr, netif) \ - (ip6_addr_equals_zone((ip6addr), netif_num_to_index(netif))) + (ip6_addr_equals_zone((ip6addr), netif_get_index(netif))) #endif /* !IPV6_CUSTOM_SCOPES */ diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h index 0b47ec06..4792a12c 100644 --- a/src/include/lwip/netif.h +++ b/src/include/lwip/netif.h @@ -497,13 +497,12 @@ err_t netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t #define NETIF_SET_HWADDRHINT(netif, hint) #endif /* LWIP_NETIF_HWADDRHINT */ -/* @ingroup netif */ u8_t netif_name_to_index(const char *name); char * netif_index_to_name(u8_t idx, char *name); +struct netif* netif_get_by_index(u8_t index); /* Interface indexes always start at 1 per RFC 3493, section 4, num starts at 0 */ -#define netif_num_to_index(netif) ((netif)->num + 1) -#define netif_index_to_num(index) ((index) - 1) +#define netif_get_index(netif) ((netif)->num + 1) #define NETIF_NO_INDEX (0) #ifdef __cplusplus