@@ -30,12 +30,11 @@ void esp_schedule();
30
30
#include < assert.h>
31
31
}
32
32
33
+ #if !LWIP_IPV6
34
+ #define GET_IP_HDR (pb ) (reinterpret_cast <ip_hdr*>(((uint8_t *)((pb)->payload)) - UDP_HLEN - IP_HLEN))
35
+ #endif
33
36
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))
39
38
40
39
class UdpContext
41
40
{
@@ -162,14 +161,17 @@ class UdpContext
162
161
return (pos <= _rx_buf->len );
163
162
}
164
163
165
- const ip_addr_t * getRemoteAddress ()
164
+ const IPAddress getRemoteAddress ()
166
165
{
166
+ #if LWIP_IPV6
167
+ return _src_addr;
168
+ #else // IPv4:
167
169
if (!_rx_buf)
168
- return 0 ;
170
+ return IPAddress () ;
169
171
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
173
175
}
174
176
175
177
uint16_t getRemotePort ()
@@ -181,21 +183,23 @@ class UdpContext
181
183
return ntohs (udphdr->src );
182
184
}
183
185
184
- const ip_addr_t * getDestAddress ()
186
+ const IPAddress getDestAddress ()
185
187
{
188
+ #if LWIP_IPV6
189
+ return _dst_addr;
190
+ #else // IPv4 only
186
191
if (!_rx_buf)
187
- return 0 ;
192
+ return IPAddress () ;
188
193
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
192
197
}
193
198
194
199
uint16_t getLocalPort ()
195
200
{
196
201
if (!_pcb)
197
202
return 0 ;
198
-
199
203
return _pcb->local_port ;
200
204
}
201
205
@@ -263,7 +267,6 @@ class UdpContext
263
267
_consume (_rx_buf->len - _rx_buf_offset);
264
268
}
265
269
266
-
267
270
size_t append (const char * data, size_t size)
268
271
{
269
272
if (!_tx_buf_head || _tx_buf_head->tot_len < _tx_buf_offset + size)
@@ -407,6 +410,22 @@ class UdpContext
407
410
_rx_buf = pb;
408
411
_rx_buf_offset = 0 ;
409
412
}
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
+
410
429
if (_on_rx) {
411
430
_on_rx ();
412
431
}
@@ -439,6 +458,9 @@ class UdpContext
439
458
#ifdef LWIP_MAYBE_XCC
440
459
uint16_t _mcast_ttl;
441
460
#endif
461
+ #if LWIP_IPV6
462
+ ip_addr_t _src_addr, _dst_addr;
463
+ #endif
442
464
};
443
465
444
466
0 commit comments