Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net): Allow to compile without IPv6 enabled #10582

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cores/esp32/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "lwip/netif.h"
#include "StreamString.h"

#ifndef CONFIG_LWIP_IPV6
#define IP6_NO_ZONE 0
#endif

IPAddress::IPAddress() : IPAddress(IPv4) {}

IPAddress::IPAddress(IPType ip_type) {
Expand Down Expand Up @@ -387,6 +391,7 @@ IPAddress::IPAddress(const ip_addr_t *addr) {
}

void IPAddress::to_ip_addr_t(ip_addr_t *addr) const {
#if CONFIG_LWIP_IPV6
if (_type == IPv6) {
addr->type = IPADDR_TYPE_V6;
addr->u_addr.ip6.addr[0] = _address.dword[0];
Expand All @@ -400,9 +405,13 @@ void IPAddress::to_ip_addr_t(ip_addr_t *addr) const {
addr->type = IPADDR_TYPE_V4;
addr->u_addr.ip4.addr = _address.dword[IPADDRESS_V4_DWORD_INDEX];
}
#else
addr->addr = _address.dword[IPADDRESS_V4_DWORD_INDEX];
#endif
}

IPAddress &IPAddress::from_ip_addr_t(const ip_addr_t *addr) {
#if CONFIG_LWIP_IPV6
if (addr->type == IPADDR_TYPE_V6) {
_type = IPv6;
_address.dword[0] = addr->u_addr.ip6.addr[0];
Expand All @@ -413,13 +422,21 @@ IPAddress &IPAddress::from_ip_addr_t(const ip_addr_t *addr) {
_zone = addr->u_addr.ip6.zone;
#endif /* LWIP_IPV6_SCOPES */
} else {
#endif
_type = IPv4;
memset(_address.bytes, 0, sizeof(_address.bytes));
#if CONFIG_LWIP_IPV6
_address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->u_addr.ip4.addr;
#else
_address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->addr;
#endif
#if CONFIG_LWIP_IPV6
}
#endif
return *this;
}

#if CONFIG_LWIP_IPV6
esp_ip6_addr_type_t IPAddress::addr_type() const {
if (_type != IPv6) {
return ESP_IP6_ADDR_IS_UNKNOWN;
Expand All @@ -428,6 +445,9 @@ esp_ip6_addr_type_t IPAddress::addr_type() const {
to_ip_addr_t(&addr);
return esp_netif_ip6_get_addr_type((esp_ip6_addr_t *)(&(addr.u_addr.ip6)));
}
#endif

#if CONFIG_LWIP_IPV6
const IPAddress IN6ADDR_ANY(IPv6);
#endif
const IPAddress INADDR_NONE(0, 0, 0, 0);
3 changes: 3 additions & 0 deletions cores/esp32/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "WString.h"
#include "lwip/ip_addr.h"
#include "esp_netif_ip_addr.h"
#include "sdkconfig.h"

#define IPADDRESS_V4_BYTES_INDEX 12
#define IPADDRESS_V4_DWORD_INDEX 3
Expand Down Expand Up @@ -115,7 +116,9 @@ class IPAddress : public Printable {
IPAddress(const ip_addr_t *addr);
void to_ip_addr_t(ip_addr_t *addr) const;
IPAddress &from_ip_addr_t(const ip_addr_t *addr);
#if CONFIG_LWIP_IPV6
esp_ip6_addr_type_t addr_type() const;
#endif
uint8_t zone() const {
return (type() == IPv6) ? _zone : 0;
}
Expand Down
61 changes: 61 additions & 0 deletions libraries/AsyncUDP/src/AsyncUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,25 +328,36 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr,
pbuf_ref(_pb);

//memcpy(&_remoteIp, raddr, sizeof(ip_addr_t));
#if CONFIG_LWIP_IPV6
_remoteIp.type = raddr->type;
_localIp.type = _remoteIp.type;
#endif

eth_hdr *eth = NULL;
udp_hdr *udphdr = (udp_hdr *)(_data - UDP_HLEN);
_localPort = ntohs(udphdr->dest);
_remotePort = ntohs(udphdr->src);

#if CONFIG_LWIP_IPV6
if (_remoteIp.type == IPADDR_TYPE_V4) {
#endif
eth = (eth_hdr *)(_data - UDP_HLEN - IP_HLEN - SIZEOF_ETH_HDR);
struct ip_hdr *iphdr = (struct ip_hdr *)(_data - UDP_HLEN - IP_HLEN);
#if CONFIG_LWIP_IPV6
_localIp.u_addr.ip4.addr = iphdr->dest.addr;
_remoteIp.u_addr.ip4.addr = iphdr->src.addr;
#else
_localIp.addr = iphdr->dest.addr;
_remoteIp.addr = iphdr->src.addr;
#endif
#if CONFIG_LWIP_IPV6
} else {
eth = (eth_hdr *)(_data - UDP_HLEN - IP6_HLEN - SIZEOF_ETH_HDR);
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(_data - UDP_HLEN - IP6_HLEN);
memcpy(&_localIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
memcpy(&_remoteIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->src.addr, 16);
}
#endif
memcpy(_remoteMac, eth->src.addr, 6);

struct netif *netif = NULL;
Expand Down Expand Up @@ -413,36 +424,48 @@ tcpip_adapter_if_t AsyncUDPPacket::interface() {
}

IPAddress AsyncUDPPacket::localIP() {
#if CONFIG_LWIP_IPV6
if (_localIp.type != IPADDR_TYPE_V4) {
return IPAddress();
}
return IPAddress(_localIp.u_addr.ip4.addr);
#else
return IPAddress(_localIp.addr);
#endif
}

#if CONFIG_LWIP_IPV6
IPAddress AsyncUDPPacket::localIPv6() {
if (_localIp.type != IPADDR_TYPE_V6) {
return IPAddress(IPv6);
}
return IPAddress(IPv6, (const uint8_t *)_localIp.u_addr.ip6.addr, _localIp.u_addr.ip6.zone);
}
#endif

uint16_t AsyncUDPPacket::localPort() {
return _localPort;
}

IPAddress AsyncUDPPacket::remoteIP() {
#if CONFIG_LWIP_IPV6
if (_remoteIp.type != IPADDR_TYPE_V4) {
return IPAddress();
}
return IPAddress(_remoteIp.u_addr.ip4.addr);
#else
return IPAddress(_remoteIp.addr);
#endif
}

#if CONFIG_LWIP_IPV6
IPAddress AsyncUDPPacket::remoteIPv6() {
if (_remoteIp.type != IPADDR_TYPE_V6) {
return IPAddress(IPv6);
}
return IPAddress(IPv6, (const uint8_t *)_remoteIp.u_addr.ip6.addr, _remoteIp.u_addr.ip6.zone);
}
#endif

uint16_t AsyncUDPPacket::remotePort() {
return _remotePort;
Expand All @@ -453,14 +476,22 @@ void AsyncUDPPacket::remoteMac(uint8_t *mac) {
}

bool AsyncUDPPacket::isIPv6() {
#if CONFIG_LWIP_IPV6
return _localIp.type == IPADDR_TYPE_V6;
#else
return false;
#endif
}

bool AsyncUDPPacket::isBroadcast() {
#if CONFIG_LWIP_IPV6
if (_localIp.type == IPADDR_TYPE_V6) {
return false;
}
uint32_t ip = _localIp.u_addr.ip4.addr;
#else
uint32_t ip = _localIp.addr;
#endif
return ip == 0xFFFFFFFF || ip == 0 || (ip & 0xFF000000) == 0xFF000000;
}

Expand Down Expand Up @@ -571,6 +602,7 @@ static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adap
}
netif = (struct netif *)nif;

#if CONFIG_LWIP_IPV6
if (addr->type == IPADDR_TYPE_V4) {
if (join) {
if (igmp_joingroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) {
Expand All @@ -592,7 +624,19 @@ static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adap
}
}
}
#else
if (join) {
if (igmp_joingroup_netif(netif, (const ip4_addr *)(addr))) {
return ESP_ERR_INVALID_STATE;
}
} else {
if (igmp_leavegroup_netif(netif, (const ip4_addr *)(addr))) {
return ESP_ERR_INVALID_STATE;
}
}
#endif
} else {
#if CONFIG_LWIP_IPV6
if (addr->type == IPADDR_TYPE_V4) {
if (join) {
if (igmp_joingroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)&(addr->u_addr.ip4))) {
Expand All @@ -614,6 +658,17 @@ static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adap
}
}
}
#else
if (join) {
if (igmp_joingroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)(addr))) {
return ESP_ERR_INVALID_STATE;
}
} else {
if (igmp_leavegroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)(addr))) {
return ESP_ERR_INVALID_STATE;
}
}
#endif
}
return ESP_OK;
}
Expand Down Expand Up @@ -722,18 +777,24 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const IPAddress addr,
}

IPAddress AsyncUDP::listenIP() {
#if CONFIG_LWIP_IPV6
if (!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V4) {
return IPAddress();
}
return IPAddress(_pcb->remote_ip.u_addr.ip4.addr);
#else
return IPAddress(_pcb->remote_ip.addr);
#endif
}

#if CONFIG_LWIP_IPV6
IPAddress AsyncUDP::listenIPv6() {
if (!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V6) {
return IPAddress(IPv6);
}
return IPAddress(IPv6, (const uint8_t *)_pcb->remote_ip.u_addr.ip6.addr, _pcb->remote_ip.u_addr.ip6.zone);
}
#endif

size_t AsyncUDP::write(const uint8_t *data, size_t len) {
return writeTo(data, len, &(_pcb->remote_ip), _pcb->remote_port);
Expand Down
6 changes: 6 additions & 0 deletions libraries/AsyncUDP/src/AsyncUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ class AsyncUDPPacket : public Stream {
tcpip_adapter_if_t interface();

IPAddress localIP();
#if CONFIG_LWIP_IPV6
IPAddress localIPv6();
#endif
uint16_t localPort();
IPAddress remoteIP();
#if CONFIG_LWIP_IPV6
IPAddress remoteIPv6();
#endif
uint16_t remotePort();
void remoteMac(uint8_t *mac);

Expand Down Expand Up @@ -146,7 +150,9 @@ class AsyncUDP : public Print {
size_t broadcast(AsyncUDPMessage &message);

IPAddress listenIP();
#if CONFIG_LWIP_IPV6
IPAddress listenIPv6();
#endif
bool connected();
esp_err_t lastErr();
operator bool();
Expand Down
2 changes: 2 additions & 0 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
log_e("Could not find ETH interface with that handle!");
return;
}
#if CONFIG_LWIP_IPV6
if (_ethernets[index]->getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
esp_err_t err = esp_netif_create_ip6_linklocal(_ethernets[index]->netif());
if (err != ESP_OK) {
Expand All @@ -82,6 +83,7 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
log_v("Enabled IPv6 Link Local on %s", _ethernets[index]->desc());
}
}
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions libraries/Network/src/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ int NetworkClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms) {
_timeout = timeout_ms;
int sockfd = -1;

#if CONFIG_LWIP_IPV6
if (ip.type() == IPv6) {
struct sockaddr_in6 *tmpaddr = (struct sockaddr_in6 *)&serveraddr;
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
Expand All @@ -218,12 +219,15 @@ int NetworkClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms) {
tmpaddr->sin6_port = htons(port);
tmpaddr->sin6_scope_id = ip.zone();
} else {
#endif
struct sockaddr_in *tmpaddr = (struct sockaddr_in *)&serveraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
tmpaddr->sin_family = AF_INET;
tmpaddr->sin_addr.s_addr = ip;
tmpaddr->sin_port = htons(port);
#if CONFIG_LWIP_IPV6
}
#endif
if (sockfd < 0) {
log_e("socket: %d", errno);
return 0;
Expand Down Expand Up @@ -590,6 +594,7 @@ IPAddress NetworkClient::remoteIP(int fd) const {
return IPAddress((uint32_t)(s->sin_addr.s_addr));
}

#if CONFIG_LWIP_IPV6
// IPv6, but it might be IPv4 mapped address
if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)&addr;
Expand All @@ -600,6 +605,7 @@ IPAddress NetworkClient::remoteIP(int fd) const {
}
}
log_e("NetworkClient::remoteIP Not AF_INET or AF_INET6?");
#endif
return (IPAddress(0, 0, 0, 0));
}

Expand Down Expand Up @@ -630,6 +636,7 @@ IPAddress NetworkClient::localIP(int fd) const {
return IPAddress((uint32_t)(s->sin_addr.s_addr));
}

#if CONFIG_LWIP_IPV6
// IPv6, but it might be IPv4 mapped address
if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)&addr;
Expand All @@ -640,6 +647,7 @@ IPAddress NetworkClient::localIP(int fd) const {
}
}
log_e("NetworkClient::localIP Not AF_INET or AF_INET6?");
#endif
return (IPAddress(0, 0, 0, 0));
}

Expand Down
4 changes: 4 additions & 0 deletions libraries/Network/src/NetworkEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED
#include "esp_wifi_types.h"
#include "esp_smartconfig.h"
#if CONFIG_NETWORK_PROV_NETWORK_TYPE_WIFI
#include "network_provisioning/network_config.h"
#endif
#endif

#if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED
constexpr int WIFI_SCANNING_BIT = BIT0;
Expand Down Expand Up @@ -111,7 +113,9 @@ typedef union {
#endif
#if SOC_WIFI_SUPPORTED
wifi_sta_config_t prov_cred_recv;
#if CONFIG_NETWORK_PROV_NETWORK_TYPE_WIFI
network_prov_wifi_sta_fail_reason_t prov_fail_reason;
#endif
smartconfig_event_got_ssid_pswd_t sc_got_ssid_pswd;
#endif
} arduino_event_info_t;
Expand Down
Loading
Loading