Skip to content

Commit

Permalink
UdpContext::setMulticastInterface(): fix for IPv6 (esp8266#5743)
Browse files Browse the repository at this point in the history
Per 'udp_set_multicast_netif_addr()' signature and comments in lwIP sources:
An IPv4 address designating a specific interface must be used.
When an IPv6 address is given, the matching IPv4 in the same interface must be selected.

fix esp8266@e3bc3c2#r32235572
  • Loading branch information
d-a-v authored Feb 8, 2019
1 parent 82487e5 commit 1959311
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions cores/esp8266/AddrList.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct netifWrapper
String toString() const { return addr().toString(); }

// related to legacy address (_num=0, ipv4)
IPAddress ipv4 () const { return _netif->ip_addr; }
IPAddress netmask () const { return _netif->netmask; }
IPAddress gw () const { return _netif->gw; }

Expand Down
31 changes: 31 additions & 0 deletions libraries/ESP8266WiFi/src/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void esp_schedule();
#include <assert.h>
}

#include <AddrList.h>

#define GET_UDP_HDR(pb) (reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN))

class UdpContext
Expand Down Expand Up @@ -107,6 +109,33 @@ class UdpContext
udp_disconnect(_pcb);
}

#if LWIP_IPV6

void setMulticastInterface(IPAddress addr)
{
// Per 'udp_set_multicast_netif_addr()' signature and comments
// in lwIP sources:
// An IPv4 address designating a specific interface must be used.
// When an IPv6 address is given, the matching IPv4 in the same
// interface must be selected.

if (!addr.isV4())
{
for (auto a: addrList)
if (a.addr() == addr)
{
// found the IPv6 address,
// redirect parameter to IPv4 address in this interface
addr = a.ipv4();
break;
}
assert(addr.isV4());
}
udp_set_multicast_netif_addr(_pcb, ip_2_ip4((const ip_addr_t*)addr));
}

#else // !LWIP_IPV6

void setMulticastInterface(const IPAddress& addr)
{
#if LWIP_VERSION_MAJOR == 1
Expand All @@ -116,6 +145,8 @@ class UdpContext
#endif
}

#endif // !LWIP_IPV6

void setMulticastTTL(int ttl)
{
#ifdef LWIP_MAYBE_XCC
Expand Down

0 comments on commit 1959311

Please sign in to comment.