1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-29 05:21:37 +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

@ -201,10 +201,19 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
}
bool ret = true;
if ( !local_ip.isV4()
|| !subnet.isV4()
#if LWIP_IPV6
// uninitialized gateway is valid
|| gateway.isV6()
#endif
) {
return false;
}
struct ip_info info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
info.ip.addr = local_ip.v4();
info.gw.addr = gateway.v4();
info.netmask.addr = subnet.v4();
if(!wifi_softap_dhcps_stop()) {
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
@ -218,11 +227,11 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
struct dhcps_lease dhcp_lease;
IPAddress ip = local_ip;
ip[3] += 99;
dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
dhcp_lease.start_ip.addr = ip.v4();
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
ip[3] += 100;
dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
dhcp_lease.end_ip.addr = ip.v4();
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
@ -252,7 +261,7 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
if(info.ip.addr == 0x00000000) {
DEBUG_WIFI("[APConfig] IP config Invalid?!\n");
ret = false;
} else if(local_ip != info.ip.addr) {
} else if(local_ip.v4() != info.ip.addr) {
ip = info.ip.addr;
DEBUG_WIFI("[APConfig] IP config not set correct?! new IP: %s\n", ip.toString().c_str());
ret = false;

View File

@ -510,11 +510,7 @@ bool ESP8266WiFiGenericClass::isSleepLevelMax () {
// ------------------------------------------------ Generic Network function ---------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
#if LWIP_VERSION_MAJOR == 1
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg);
#else
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
#endif
void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg);
static bool _dns_lookup_pending = false;
@ -545,13 +541,13 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
if(err == ERR_OK) {
aResult = addr.addr;
aResult = IPAddress(&addr);
} else if(err == ERR_INPROGRESS) {
_dns_lookup_pending = true;
delay(timeout_ms);
_dns_lookup_pending = false;
// will return here when dns_found_callback fires
if(aResult != 0) {
if(aResult.isSet()) {
err = ERR_OK;
}
}
@ -571,18 +567,14 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
* @param ipaddr
* @param callback_arg
*/
#if LWIP_VERSION_MAJOR == 1
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg)
#else
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
#endif
void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg)
{
(void) name;
if (!_dns_lookup_pending) {
return;
}
if(ipaddr) {
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
(*reinterpret_cast<IPAddress*>(callback_arg)) = IPAddress(ipaddr);
}
esp_schedule(); // resume the hostByName function
}

View File

@ -38,6 +38,9 @@ extern "C" {
#include "lwip/err.h"
#include "lwip/dns.h"
#include "lwip/init.h" // LWIP_VERSION_
#if LWIP_IPV6
#include "lwip/netif.h" // struct netif
#endif
}
#include "debug.h"
@ -117,7 +120,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
struct station_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
conf.threshold.authmode = AUTH_OPEN;
if(passphrase) {
@ -244,15 +247,20 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
dns1 = arg1;
}
// check whether all is IPv4 (or gateway not set)
if (!(local_ip.isV4() && subnet.isV4() && (!gateway.isSet() || gateway.isV4()))) {
return false;
}
//ip and gateway must be in the same subnet
if((local_ip & subnet) != (gateway & subnet)) {
if((local_ip.v4() & subnet.v4()) != (gateway.v4() & subnet.v4())) {
return false;
}
struct ip_info info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
info.ip.addr = local_ip.v4();
info.gw.addr = gateway.v4();
info.netmask.addr = subnet.v4();
wifi_station_dhcpc_stop();
if(wifi_set_ip_info(STATION_IF, &info)) {
@ -260,18 +268,15 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
} else {
return false;
}
ip_addr_t d;
if(dns1 != (uint32_t)0x00000000) {
if(dns1.isSet()) {
// Set DNS1-Server
d.addr = static_cast<uint32_t>(dns1);
dns_setserver(0, &d);
dns_setserver(0, dns1);
}
if(dns2 != (uint32_t)0x00000000) {
if(dns2.isSet()) {
// Set DNS2-Server
d.addr = static_cast<uint32_t>(dns2);
dns_setserver(1, &d);
dns_setserver(1, dns2);
}
return true;
@ -392,7 +397,6 @@ IPAddress ESP8266WiFiSTAClass::localIP() {
return IPAddress(ip.ip.addr);
}
/**
* Get the station interface MAC address.
* @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
@ -446,8 +450,7 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
ip_addr_t dns_ip = dns_getserver(dns_no);
return IPAddress(dns_ip.addr);
#else
const ip_addr_t* dns_ip = dns_getserver(dns_no);
return IPAddress(dns_ip->addr);
return IPAddress(dns_getserver(dns_no));
#endif
}

View File

@ -137,22 +137,19 @@ int WiFiClient::connect(const String& host, uint16_t port)
return connect(host.c_str(), port);
}
int WiFiClient::connect(IPAddress ip, uint16_t port)
int WiFiClient::connect(CONST IPAddress& ip, uint16_t port)
{
ip_addr_t addr;
addr.addr = ip;
if (_client) {
stop();
_client->unref();
_client = nullptr;
}
#if LWIP_VERSION_MAJOR == 1
// if the default interface is down, tcp_connect exits early without
// ever calling tcp_err
// http://lists.gnu.org/archive/html/lwip-devel/2010-05/msg00001.html
#if LWIP_VERSION_MAJOR == 1
netif* interface = ip_route(&addr);
netif* interface = ip_route(ip);
if (!interface) {
DEBUGV("no route to host\r\n");
return 0;
@ -170,7 +167,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
_client = new ClientContext(pcb, nullptr, nullptr);
_client->ref();
_client->setTimeout(_timeout);
int res = _client->connect(&addr, port);
int res = _client->connect(ip, port);
if (res == 0) {
_client->unref();
_client = nullptr;

View File

@ -53,7 +53,7 @@ public:
WiFiClient& operator=(const WiFiClient&);
uint8_t status();
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(CONST IPAddress& ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual int connect(const String& host, uint16_t port);
virtual size_t write(uint8_t);

View File

@ -95,7 +95,7 @@ WiFiClientSecure::WiFiClientSecure(ClientContext* client, bool usePMEM,
_ssl->connectServer(client, _timeout);
}
int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
int WiFiClientSecure::connect(CONST IPAddress& ip, uint16_t port)
{
if (!WiFiClient::connect(ip, port)) {
return 0;

View File

@ -35,7 +35,7 @@ public:
WiFiClientSecure();
~WiFiClientSecure() override;
int connect(IPAddress ip, uint16_t port) override;
int connect(CONST IPAddress& ip, uint16_t port) override;
int connect(const String& host, uint16_t port) override;
int connect(const char* name, uint16_t port) override;

View File

@ -200,7 +200,7 @@ bool WiFiClientSecure::flush(unsigned int maxWaitMs) {
return WiFiClient::flush(maxWaitMs);
}
int WiFiClientSecure::connect(IPAddress ip, uint16_t port) {
int WiFiClientSecure::connect(CONST IPAddress& ip, uint16_t port) {
if (!WiFiClient::connect(ip, port)) {
return 0;
}

View File

@ -36,7 +36,7 @@ class WiFiClientSecure : public WiFiClient {
WiFiClientSecure();
~WiFiClientSecure() override;
int connect(IPAddress ip, uint16_t port) override;
int connect(CONST IPAddress& ip, uint16_t port) override;
int connect(const String& host, uint16_t port) override;
int connect(const char* name, uint16_t port) override;

View File

@ -37,7 +37,7 @@ extern "C" {
#include "lwip/inet.h"
#include <include/ClientContext.h>
WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
WiFiServer::WiFiServer(const IPAddress& addr, uint16_t port)
: _port(port)
, _addr(addr)
, _pcb(nullptr)
@ -48,7 +48,7 @@ WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
WiFiServer::WiFiServer(uint16_t port)
: _port(port)
, _addr((uint32_t) IPADDR_ANY)
, _addr(IP_ANY_TYPE)
, _pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
@ -61,16 +61,16 @@ void WiFiServer::begin() {
void WiFiServer::begin(uint16_t port) {
close();
_port = port;
_port = port;
err_t err;
tcp_pcb* pcb = tcp_new();
if (!pcb)
return;
ip_addr_t local_addr;
local_addr.addr = (uint32_t) _addr;
pcb->so_options |= SOF_REUSEADDR;
err = tcp_bind(pcb, &local_addr, _port);
// (IPAddress _addr) operator-converted to (const ip_addr_t*)
err = tcp_bind(pcb, _addr, _port);
if (err != ERR_OK) {
tcp_close(pcb);

View File

@ -46,7 +46,7 @@ protected:
enum { _ndDefault, _ndFalse, _ndTrue } _noDelay = _ndDefault;
public:
WiFiServer(IPAddress addr, uint16_t port);
WiFiServer(const IPAddress& addr, uint16_t port);
WiFiServer(uint16_t port);
virtual ~WiFiServer() {}
WiFiClient available(uint8_t* status = NULL);

View File

@ -40,7 +40,6 @@ extern "C"
#include "lwip/mem.h"
#include <include/UdpContext.h>
template<>
WiFiUDP* SList<WiFiUDP>::_s_first = 0;
@ -83,9 +82,7 @@ uint8_t WiFiUDP::begin(uint16_t port)
_ctx = new UdpContext;
_ctx->ref();
ip_addr_t addr;
addr.addr = INADDR_ANY;
return (_ctx->listen(addr, port)) ? 1 : 0;
return (_ctx->listen(IPAddress(), port)) ? 1 : 0;
}
uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port)
@ -95,20 +92,14 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
_ctx = 0;
}
ip_addr_t ifaddr;
ifaddr.addr = (uint32_t) interfaceAddr;
ip_addr_t multicast_addr;
multicast_addr.addr = (uint32_t) multicast;
if (igmp_joingroup(&ifaddr, &multicast_addr)!= ERR_OK) {
if (igmp_joingroup(interfaceAddr, multicast)!= ERR_OK) {
return 0;
}
_ctx = new UdpContext;
_ctx->ref();
ip_addr_t addr;
addr.addr = INADDR_ANY;
if (!_ctx->listen(addr, port)) {
ip_addr_t addr = IPADDR4_INIT(INADDR_ANY);
if (!_ctx->listen(&addr, port)) {
return 0;
}
@ -155,32 +146,24 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port)
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
ip_addr_t addr;
addr.addr = ip;
if (!_ctx) {
_ctx = new UdpContext;
_ctx->ref();
}
return (_ctx->connect(addr, port)) ? 1 : 0;
return (_ctx->connect(ip, port)) ? 1 : 0;
}
int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,
IPAddress interfaceAddress, int ttl)
{
ip_addr_t mcastAddr;
mcastAddr.addr = multicastAddress;
ip_addr_t ifaceAddr;
ifaceAddr.addr = interfaceAddress;
if (!_ctx) {
_ctx = new UdpContext;
_ctx->ref();
}
if (!_ctx->connect(mcastAddr, port)) {
if (!_ctx->connect(multicastAddress, port)) {
return 0;
}
_ctx->setMulticastInterface(ifaceAddr);
_ctx->setMulticastInterface(interfaceAddress);
_ctx->setMulticastTTL(ttl);
return 1;
}
@ -248,15 +231,15 @@ void WiFiUDP::flush()
endPacket();
}
IPAddress WiFiUDP::remoteIP()
IPAddress WiFiUDP::remoteIP() const
{
if (!_ctx)
return IPAddress(0U);
return IPNoAddress;
return IPAddress(_ctx->getRemoteAddress());
return _ctx->getRemoteAddress();
}
uint16_t WiFiUDP::remotePort()
uint16_t WiFiUDP::remotePort() const
{
if (!_ctx)
return 0;
@ -264,18 +247,15 @@ uint16_t WiFiUDP::remotePort()
return _ctx->getRemotePort();
}
IPAddress WiFiUDP::destinationIP()
IPAddress WiFiUDP::destinationIP() const
{
IPAddress addr;
if (!_ctx)
return addr;
return IPNoAddress;
addr = _ctx->getDestAddress();
return addr;
return _ctx->getDestAddress();
}
uint16_t WiFiUDP::localPort()
uint16_t WiFiUDP::localPort() const
{
if (!_ctx)
return 0;

View File

@ -47,7 +47,7 @@ public:
// Finish with the UDP connetion
virtual void stop();
// join a multicast group and listen on the given port
uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port);
uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port);
// Sending UDP packets
@ -63,9 +63,9 @@ public:
// use WiFi.localIP() or WiFi.softAPIP() depending on the interface you need
// ttl - multicast packet TTL (default is 1)
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacketMulticast(IPAddress multicastAddress,
virtual int beginPacketMulticast(IPAddress multicastAddress,
uint16_t port,
IPAddress interfaceAddress,
IPAddress interfaceAddress,
int ttl = 1);
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
@ -95,14 +95,14 @@ public:
virtual void flush(); // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP();
virtual IPAddress remoteIP() const;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort();
virtual uint16_t remotePort() const;
// Return the destination address for incoming packets,
// useful to distinguish multicast and ordinary packets
IPAddress destinationIP();
IPAddress destinationIP() const;
// Return the local port for outgoing packets
uint16_t localPort();
uint16_t localPort() const;
static void stopAll();
static void stopAllExcept(WiFiUDP * exC);

View File

@ -122,7 +122,7 @@ public:
}
}
int connect(ip_addr_t* addr, uint16_t port)
int connect(CONST ip_addr_t* addr, uint16_t port)
{
err_t err = tcp_connect(_pcb, addr, port, &ClientContext::_s_connected);
if (err != ERR_OK) {
@ -145,7 +145,7 @@ public:
return 1;
}
size_t availableForWrite()
size_t availableForWrite() const
{
return _pcb? tcp_sndbuf(_pcb): 0;
}
@ -180,13 +180,13 @@ public:
return _timeout_ms;
}
uint32_t getRemoteAddress() const
const ip_addr_t* getRemoteAddress() const
{
if(!_pcb) {
return 0;
}
return _pcb->remote_ip.addr;
return &_pcb->remote_ip;
}
uint16_t getRemotePort() const
@ -198,13 +198,13 @@ public:
return _pcb->remote_port;
}
uint32_t getLocalAddress() const
const ip_addr_t* getLocalAddress() const
{
if(!_pcb) {
return 0;
}
return _pcb->local_ip.addr;
return &_pcb->local_ip;
}
uint16_t getLocalPort() const

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;
};