diff --git a/README.md b/README.md index 81762e4..25face9 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Transports * Ethernet: unicast and multicast on ESP8266 and ESP32 * UDP/IPv4: unicast and multicast on ESP8266 and ESP32 -* UDP/IPv6: unicast on ESP8266 +* UDP/IPv6: unicast on ESP8266 and ESP32 * [Bluetooth Low Energy](https://github.com/yoursunny/NDNts/tree/main/pkg/web-bluetooth-transport): server/peripheral only on ESP32 and nRF52 KeyChain diff --git a/src/transport/udp-transport.cpp b/src/transport/udp-transport.cpp index 4f24c3d..06313ac 100644 --- a/src/transport/udp-transport.cpp +++ b/src/transport/udp-transport.cpp @@ -31,7 +31,9 @@ UdpTransport::beginListen(uint16_t localPort, IPAddress localIp) { bool ok = m_udp.begin(localPort); #elif defined(ARDUINO_ARCH_ESP32) LOG(F("listening on ") << localIp << ':' << _DEC(localPort)); - bool ok = m_udp.begin(localIp, localPort); + bool ok = localIp.type() == IPType::IPv4 && uint32_t(localIp) == 0 + ? m_udp.begin(localPort) + : m_udp.begin(localIp, localPort); #endif if (ok) { m_mode = Mode::LISTEN; @@ -43,7 +45,11 @@ bool UdpTransport::beginTunnel(IPAddress remoteIp, uint16_t remotePort, uint16_t localPort) { end(); LOG(F("connecting to ") << remoteIp << ':' << remotePort << F(" from :") << _DEC(localPort)); - bool ok = m_udp.begin(localPort); + bool ok = +#if defined(ARDUINO_ARCH_ESP32) && LWIP_IPV6 + remoteIp.type() == IPType::IPv6 ? m_udp.begin(IN6ADDR_ANY, localPort) : +#endif + m_udp.begin(localPort); if (ok) { m_mode = Mode::TUNNEL; m_ip = remoteIp; @@ -98,10 +104,20 @@ UdpTransport::doLoop() { } else { IPAddress ip = m_udp.remoteIP(); uint16_t port = m_udp.remotePort(); -#if defined(ARDUINO_ARCH_ESP8266) && LWIP_IPV6 +#if LWIP_IPV6 +#if defined(ARDUINO_ARCH_ESP8266) if (ip.isV6()) { endpointId = m_endpoints.encode(reinterpret_cast(ip.raw6()), 16, port); } else +#elif defined(ARDUINO_ARCH_ESP32) + if (ip.type() == IPType::IPv6) { + uint8_t addr[16]; + for (int i = 0; i < 16; ++i) { + addr[i] = ip[i]; + } + endpointId = m_endpoints.encode(addr, 16, port); + } else +#endif #endif { uint32_t ip4 = ip; @@ -155,9 +171,13 @@ UdpTransport::doSend(const uint8_t* pkt, size_t pktLen, uint64_t endpointId) { case 4: ip = addr; break; -#if defined(ARDUINO_ARCH_ESP8266) && LWIP_IPV6 +#if LWIP_IPV6 case 16: +#if defined(ARDUINO_ARCH_ESP8266) std::copy_n(addr, addrLen, reinterpret_cast(ip.raw6())); +#elif defined(ARDUINO_ARCH_ESP32) + ip = IPAddress(IPType::IPv6, addr); +#endif break; #endif default: diff --git a/src/transport/udp-transport.hpp b/src/transport/udp-transport.hpp index de18514..82925a1 100644 --- a/src/transport/udp-transport.hpp +++ b/src/transport/udp-transport.hpp @@ -43,17 +43,15 @@ class UdpTransport : public virtual ndnph::Transport { * @param localPort local port. * @param localIp local interface address (ESP32 only). * - * IPv6 is supported on ESP8266 only. Up to four simultaneous IPv6 EndpointIds can be tracked. + * Up to four simultaneous IPv6 EndpointIds can be tracked. */ - bool beginListen(uint16_t localPort = 6363, IPAddress localIp = IPAddress(0, 0, 0, 0)); + bool beginListen(uint16_t localPort = 6363, IPAddress localIp = IPAddress()); /** * @brief Establish a UDP tunnel to a remote endpoint. * @param remoteIp remote host address. * @param remotePort remote port. * @param localPort local port. - * - * IPv6 is supported on ESP8266 only. */ bool beginTunnel(IPAddress remoteIp, uint16_t remotePort = 6363, uint16_t localPort = 6363); @@ -62,7 +60,7 @@ class UdpTransport : public virtual ndnph::Transport { * @param localIp local interface address (ESP8266 only). * @param groupPort group port. */ - bool beginMulticast(IPAddress localIp = IPAddress(0, 0, 0, 0), uint16_t groupPort = 56363); + bool beginMulticast(IPAddress localIp = IPAddress(), uint16_t groupPort = 56363); /** @brief Disable the transport. */ void end(); @@ -96,7 +94,7 @@ class UdpTransport : public virtual ndnph::Transport { std::unique_ptr m_ownBuf; WiFiUDP m_udp; -#if defined(ARDUINO_ARCH_ESP8266) && LWIP_IPV6 +#if LWIP_IPV6 using EndpointIdHelper = ndnph::port_transport_socket::Ipv6EndpointIdHelper<4>; #else using EndpointIdHelper = ndnph::port_transport_socket::Ipv6EndpointIdHelper<1>;