Skip to content

Commit 1959311

Browse files
authored
UdpContext::setMulticastInterface(): fix for IPv6 (#5743)
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 e3bc3c2#r32235572
1 parent 82487e5 commit 1959311

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cores/esp8266/AddrList.h

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct netifWrapper
118118
String toString() const { return addr().toString(); }
119119

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

libraries/ESP8266WiFi/src/include/UdpContext.h

+31
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ void esp_schedule();
3030
#include <assert.h>
3131
}
3232

33+
#include <AddrList.h>
34+
3335
#define GET_UDP_HDR(pb) (reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN))
3436

3537
class UdpContext
@@ -107,6 +109,33 @@ class UdpContext
107109
udp_disconnect(_pcb);
108110
}
109111

112+
#if LWIP_IPV6
113+
114+
void setMulticastInterface(IPAddress addr)
115+
{
116+
// Per 'udp_set_multicast_netif_addr()' signature and comments
117+
// in lwIP sources:
118+
// An IPv4 address designating a specific interface must be used.
119+
// When an IPv6 address is given, the matching IPv4 in the same
120+
// interface must be selected.
121+
122+
if (!addr.isV4())
123+
{
124+
for (auto a: addrList)
125+
if (a.addr() == addr)
126+
{
127+
// found the IPv6 address,
128+
// redirect parameter to IPv4 address in this interface
129+
addr = a.ipv4();
130+
break;
131+
}
132+
assert(addr.isV4());
133+
}
134+
udp_set_multicast_netif_addr(_pcb, ip_2_ip4((const ip_addr_t*)addr));
135+
}
136+
137+
#else // !LWIP_IPV6
138+
110139
void setMulticastInterface(const IPAddress& addr)
111140
{
112141
#if LWIP_VERSION_MAJOR == 1
@@ -116,6 +145,8 @@ class UdpContext
116145
#endif
117146
}
118147

148+
#endif // !LWIP_IPV6
149+
119150
void setMulticastTTL(int ttl)
120151
{
121152
#ifdef LWIP_MAYBE_XCC

0 commit comments

Comments
 (0)