From 7a64e19ba8d539889a8e27ec2bd2c541d6bb2f80 Mon Sep 17 00:00:00 2001 From: XiaXiaotian Date: Fri, 30 Jun 2017 16:18:29 +0800 Subject: [PATCH] Broadcast IP route based on source IP address. If destination IP address of the packet is broadcast address, firstly compare source IP address with the that of each network interface. If it matches, packet is forwarded from the interface. --- components/lwip/core/ipv4/ip4.c | 29 +++++++++++++++++++- components/lwip/include/lwip/port/lwipopts.h | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/components/lwip/core/ipv4/ip4.c b/components/lwip/core/ipv4/ip4.c index 3877e20609..cc4393af78 100755 --- a/components/lwip/core/ipv4/ip4.c +++ b/components/lwip/core/ipv4/ip4.c @@ -115,9 +115,36 @@ ip4_set_default_multicast_netif(struct netif* default_multicast_netif) #endif /* LWIP_MULTICAST_TX_OPTIONS */ #ifdef LWIP_HOOK_IP4_ROUTE_SRC +/** + * Source based IPv4 routing hook function. This function works only + * when destination IP is broadcast IP. + */ +struct netif * +ip4_route_src_hook(const ip4_addr_t *dest, const ip4_addr_t *src) +{ + struct netif *netif = NULL; + + /* destination IP is broadcast IP? */ + if ((src != NULL) && (dest->addr == IPADDR_BROADCAST)) { + /* iterate through netifs */ + for (netif = netif_list; netif != NULL; netif = netif->next) { + /* is the netif up, does it have a link and a valid address? */ + if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { + /* source IP matches? */ + if (ip4_addr_cmp(src, netif_ip4_addr(netif))) { + /* return netif on which to forward IP packet */ + return netif; + } + } + } + } + + return netif; +} + /** * Source based IPv4 routing must be fully implemented in - * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides he parameters. + * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters. */ struct netif * ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src) diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index a4b07006e7..0741db925d 100644 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -596,6 +596,7 @@ ---------- Hook options --------------- --------------------------------------- */ +#define LWIP_HOOK_IP4_ROUTE_SRC ip4_route_src_hook /* ---------------------------------------