Skip to content

Commit

Permalink
transport: UDP/IPv6 on ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jun 19, 2024
1 parent da3e7dc commit 19380ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions src/transport/udp-transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<const uint8_t*>(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;
Expand Down Expand Up @@ -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<uint8_t*>(ip.raw6()));
#elif defined(ARDUINO_ARCH_ESP32)
ip = IPAddress(IPType::IPv6, addr);
#endif
break;
#endif
default:
Expand Down
10 changes: 4 additions & 6 deletions src/transport/udp-transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -96,7 +94,7 @@ class UdpTransport : public virtual ndnph::Transport {
std::unique_ptr<uint8_t[]> 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>;
Expand Down

0 comments on commit 19380ba

Please sign in to comment.