8000 udp with IPV6 fixes · esp8266/Arduino@a777aa3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a777aa3

Browse files
committed
udp with IPV6 fixes
1 parent 43c5c9f commit a777aa3

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

cores/esp8266/IPAddress.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include <Print.h>
2323

2424
IPAddress::IPAddress() {
25-
setV4();
26-
v4() = 0;
25+
_ip = ip_addr_any_type;
2726
}
2827

2928
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
@@ -143,15 +142,13 @@ size_t IPAddress::printTo(Print& p) const {
143142

144143
String IPAddress::toString() const
145144
{
146-
if (isV4()) {
147-
char szRet[16];
148-
sprintf(szRet,"%u.%u.%u.%u", (*this)[0], (*this)[1], (*this)[2], (*this)[3]);
149-
return String(szRet);
150-
}
151145
#if LWIP_IPV6
152-
else
146+
if (isV6())
153147
return "(v6todo)"; // do we have stringprint? (==c++stringstream)
154148
#endif
149+
char szRet[16];
150+
sprintf(szRet,"%u.%u.%u.%u", (*this)[0], (*this)[1], (*this)[2], (*this)[3]);
151+
return String(szRet);
155152
}
156153

157154
bool IPAddress::isValid(const String& arg) {

libraries/ESP8266WiFi/src/WiFiUdp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ uint8_t WiFiUDP::begin(uint16_t port)
8383

8484
_ctx = new UdpContext;
8585
_ctx->ref();
86+
#if LWIP_VERSION_MAJOR == 1
8687
ip_addr_t addr = IPADDR4_INIT(INADDR_ANY);
87-
return (_ctx->listen(&addr, port)) ? 1 : 0;
88+
return (_ctx->listen(IP_ANY_TYPE, port)) ? 1 : 0;
89+
#else
90+
return (_ctx->listen(&ip_addr_any_type, port)) ? 1 : 0;
91+
#endif
8892
}
8993

9094
uint8_t WiFiUDP::beginMulticast(const IPAddress& interfaceAddr, const IPAddress& multicast, uint16_t port)

libraries/ESP8266WiFi/src/include/UdpContext.h

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ void esp_schedule();
3030
#include <assert.h>
3131
}
3232

33+
#if !LWIP_IPV6
34+
#define GET_IP_HDR(pb) (reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN))
35+
#endif
3336

34-
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
35-
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
36-
37-
#pragma message "FIXME"
38-
static ip_addr_t blark;
37+
#define GET_UDP_HDR(pb) (reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN))
3938

4039
class UdpContext
4140
{
@@ -162,14 +161,17 @@ class UdpContext
162161
return (pos <= _rx_buf->len);
163162
}
164163

165-
const ip_addr_t* getRemoteAddress()
164+
const IPAddress getRemoteAddress()
166165
{
166+
#if LWIP_IPV6
167+
return _src_addr;
168+
#else // IPv4:
167169
if (!_rx_buf)
168-
return 0;
170+
return IPAddress();
169171

170-
//ip_hdr* iphdr = GET_IP_HDRs(_rx_buf);
171-
//return iphdr->src;
172-
return &blark;
172+
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
173+
return IPAddress(iphdr->src.addr);
174+
#endif // IPv4
173175
}
174176

175177
uint16_t getRemotePort()
@@ -181,21 +183,23 @@ class UdpContext
181183
return ntohs(udphdr->src);
182184
}
183185

184-
const ip_addr_t* getDestAddress()
186+
const IPAddress getDestAddress()
185187
{
188+
#if LWIP_IPV6
189+
return _dst_addr;
190+
#else // IPv4 only
186191
if (!_rx_buf)
187-
return 0;
192+
return IPAddress();
188193

189-
//ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
190-
//return iphdr->dest;
191-
return &blark;
194+
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
195+
return IPAddress(iphdr->dest.addr);
196+
#endif // IPv4 only
192197
}
193198

194199
uint16_t getLocalPort()
195200
{
196201
if (!_pcb)
197202
return 0;
198-
199203
return _pcb->local_port;
200204
}
201205

@@ -263,7 +267,6 @@ class UdpContext
263267
_consume(_rx_buf->len - _rx_buf_offset);
264268
}
265269

266-
267270
size_t append(const char* data, size_t size)
268271
{
269272
if (!_tx_buf_head || _tx_buf_head->tot_len < _tx_buf_offset + size)
@@ -407,6 +410,22 @@ class UdpContext
407410
_rx_buf = pb;
408411
_rx_buf_offset = 0;
409412
}
413+
414+
#if LWIP_IPV6
415+
// --> Arduino's UDP is a stream but UDP is not <--
416+
417+
// When IPv6 is enabled, we store addresses from here
418+
// because lwIP's macro are valid only in this callback
419+
// (there's no easy way to safely guess if packet is from v4 or v6)
420+
421+
// Because of this stream-ed way this is inacurate when user does not
422+
// swallow data quickly enough. However the former way (still here
423+
// when IPv6 is not enabled) suffers from the exact same issue.
424+
425+
_src_addr = ip_data.current_iphdr_src;
426+
_dst_addr = ip_data.current_iphdr_dest;
427+
#endif
428+
410429
if (_on_rx) {
411430
_on_rx();
412431
}
@@ -439,6 +458,9 @@ class UdpContext
439458
#ifdef LWIP_MAYBE_XCC
440459
uint16_t _mcast_ttl;
441460
#endif
461+
#if LWIP_IPV6
462+
ip_addr_t _src_addr, _dst_addr;
463+
#endif
442464
};
443465

444466

0 commit comments

Comments
 (0)
0