1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-12-13 20:03:19 +03:00

IPv6 on esp8266-nonos-sdk and arduino (#5136)

This commit is contained in:
david gauchard
2018-11-27 23:07:47 +01:00
committed by GitHub
parent a501d3ca3b
commit 5c4db3acf4
59 changed files with 1270 additions and 809 deletions

View File

@@ -30,9 +30,7 @@ void esp_schedule();
#include <assert.h>
}
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
#define GET_UDP_HDR(pb) (reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN))
class UdpContext
{
@@ -90,17 +88,17 @@ public:
}
}
bool connect(ip_addr_t addr, uint16_t port)
bool connect(const ip_addr_t* addr, uint16_t port)
{
ip_addr_copy(_pcb->remote_ip, addr);
_pcb->remote_ip = *addr;
_pcb->remote_port = port;
return true;
}
bool listen(ip_addr_t addr, uint16_t port)
bool listen(CONST ip_addr_t* addr, uint16_t port)
{
udp_recv(_pcb, &_s_recv, (void *) this);
err_t err = udp_bind(_pcb, &addr, port);
err_t err = udp_bind(_pcb, addr, port);
return err == ERR_OK;
}
@@ -110,14 +108,14 @@ public:
}
#if LWIP_VERSION_MAJOR == 1
void setMulticastInterface(ip_addr_t addr)
void setMulticastInterface(const ip_addr_t addr)
{
udp_set_multicast_netif_addr(_pcb, addr);
}
#else
void setMulticastInterface(const ip_addr_t& addr)
void setMulticastInterface(const ip_addr_t* addr)
{
udp_set_multicast_netif_addr(_pcb, &addr);
udp_set_multicast_netif_addr(_pcb, ip_2_ip4(addr));
}
#endif
@@ -159,16 +157,12 @@ public:
return (pos <= _rx_buf->len);
}
uint32_t getRemoteAddress()
CONST IPAddress& getRemoteAddress() CONST
{
if (!_rx_buf)
return 0;
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
return iphdr->src.addr;
return _src_addr;
}
uint16_t getRemotePort()
uint16_t getRemotePort() const
{
if (!_rx_buf)
return 0;
@@ -177,20 +171,15 @@ public:
return lwip_ntohs(udphdr->src);
}
uint32_t getDestAddress()
const IPAddress& getDestAddress() const
{
if (!_rx_buf)
return 0;
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
return iphdr->dest.addr;
return _dst_addr;
}
uint16_t getLocalPort()
uint16_t getLocalPort() const
{
if (!_pcb)
return 0;
return _pcb->local_port;
}
@@ -242,7 +231,7 @@ public:
return size;
}
int peek()
int peek() const
{
if (!_rx_buf || _rx_buf_offset == _rx_buf->len)
return -1;
@@ -259,7 +248,6 @@ public:
_consume(_rx_buf->len - _rx_buf_offset);
}
size_t append(const char* data, size_t size)
{
if (!_tx_buf_head || _tx_buf_head->tot_len < _tx_buf_offset + size)
@@ -292,7 +280,7 @@ public:
return size;
}
bool send(ip_addr_t* addr = 0, uint16_t port = 0)
bool send(CONST ip_addr_t* addr = 0, uint16_t port = 0)
{
size_t data_size = _tx_buf_offset;
pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM);
@@ -403,21 +391,33 @@ private:
_rx_buf = pb;
_rx_buf_offset = 0;
}
// --> Arduino's UDP is a stream but UDP is not <--
// When IPv6 is enabled, we store addresses from here
// because lwIP's macro are valid only in this callback
// (there's no easy way to safely guess whether packet
// is from v4 or v6 when we have only access to payload)
// Because of this stream-ed way this is inacurate when
// user does not swallow data quickly enough (the former
// IPv4-only way suffers from the exact same issue.
#if LWIP_VERSION_MAJOR == 1
_src_addr = current_iphdr_src;
_dst_addr = current_iphdr_dest;
#else
_src_addr = ip_data.current_iphdr_src;
_dst_addr = ip_data.current_iphdr_dest;
#endif
if (_on_rx) {
_on_rx();
}
}
#if LWIP_VERSION_MAJOR == 1
static void _s_recv(void *arg,
udp_pcb *upcb, pbuf *p,
ip_addr_t *addr, u16_t port)
#else
static void _s_recv(void *arg,
udp_pcb *upcb, pbuf *p,
const ip_addr_t *addr, u16_t port)
#endif
CONST ip_addr_t *addr, u16_t port)
{
reinterpret_cast<UdpContext*>(arg)->_recv(upcb, p, addr, port);
}
@@ -435,6 +435,7 @@ private:
#ifdef LWIP_MAYBE_XCC
uint16_t _mcast_ttl;
#endif
IPAddress _src_addr, _dst_addr;
};