Skip to content

Commit 2fee252

Browse files
committed
fix(net): Allow to compile without IPv6 enabled
cc: @Jason2866
1 parent 1ecbbae commit 2fee252

27 files changed

+194
-6
lines changed

Diff for: cores/esp32/IPAddress.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "lwip/netif.h"
2323
#include "StreamString.h"
2424

25+
#ifndef CONFIG_LWIP_IPV6
26+
#define IP6_NO_ZONE 0
27+
#endif
28+
2529
IPAddress::IPAddress() : IPAddress(IPv4) {}
2630

2731
IPAddress::IPAddress(IPType ip_type) {
@@ -387,6 +391,7 @@ IPAddress::IPAddress(const ip_addr_t *addr) {
387391
}
388392

389393
void IPAddress::to_ip_addr_t(ip_addr_t *addr) const {
394+
#if CONFIG_LWIP_IPV6
390395
if (_type == IPv6) {
391396
addr->type = IPADDR_TYPE_V6;
392397
addr->u_addr.ip6.addr[0] = _address.dword[0];
@@ -400,9 +405,13 @@ void IPAddress::to_ip_addr_t(ip_addr_t *addr) const {
400405
addr->type = IPADDR_TYPE_V4;
401406
addr->u_addr.ip4.addr = _address.dword[IPADDRESS_V4_DWORD_INDEX];
402407
}
408+
#else
409+
addr->addr = _address.dword[IPADDRESS_V4_DWORD_INDEX];
410+
#endif
403411
}
404412

405413
IPAddress &IPAddress::from_ip_addr_t(const ip_addr_t *addr) {
414+
#if CONFIG_LWIP_IPV6
406415
if (addr->type == IPADDR_TYPE_V6) {
407416
_type = IPv6;
408417
_address.dword[0] = addr->u_addr.ip6.addr[0];
@@ -413,13 +422,21 @@ IPAddress &IPAddress::from_ip_addr_t(const ip_addr_t *addr) {
413422
_zone = addr->u_addr.ip6.zone;
414423
#endif /* LWIP_IPV6_SCOPES */
415424
} else {
425+
#endif
416426
_type = IPv4;
417427
memset(_address.bytes, 0, sizeof(_address.bytes));
428+
#if CONFIG_LWIP_IPV6
418429
_address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->u_addr.ip4.addr;
430+
#else
431+
_address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->addr;
432+
#endif
433+
#if CONFIG_LWIP_IPV6
419434
}
435+
#endif
420436
return *this;
421437
}
422438

439+
#if CONFIG_LWIP_IPV6
423440
esp_ip6_addr_type_t IPAddress::addr_type() const {
424441
if (_type != IPv6) {
425442
return ESP_IP6_ADDR_IS_UNKNOWN;
@@ -428,6 +445,9 @@ esp_ip6_addr_type_t IPAddress::addr_type() const {
428445
to_ip_addr_t(&addr);
429446
return esp_netif_ip6_get_addr_type((esp_ip6_addr_t *)(&(addr.u_addr.ip6)));
430447
}
448+
#endif
431449

450+
#if CONFIG_LWIP_IPV6
432451
const IPAddress IN6ADDR_ANY(IPv6);
452+
#endif
433453
const IPAddress INADDR_NONE(0, 0, 0, 0);

Diff for: cores/esp32/IPAddress.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "WString.h"
2525
#include "lwip/ip_addr.h"
2626
#include "esp_netif_ip_addr.h"
27+
#include "sdkconfig.h"
2728

2829
#define IPADDRESS_V4_BYTES_INDEX 12
2930
#define IPADDRESS_V4_DWORD_INDEX 3
@@ -115,7 +116,9 @@ class IPAddress : public Printable {
115116
IPAddress(const ip_addr_t *addr);
116117
void to_ip_addr_t(ip_addr_t *addr) const;
117118
IPAddress &from_ip_addr_t(const ip_addr_t *addr);
119+
#if CONFIG_LWIP_IPV6
118120
esp_ip6_addr_type_t addr_type() const;
121+
#endif
119122
uint8_t zone() const {
120123
return (type() == IPv6) ? _zone : 0;
121124
}

Diff for: libraries/AsyncUDP/src/AsyncUDP.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -328,25 +328,36 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr,
328328
pbuf_ref(_pb);
329329

330330
//memcpy(&_remoteIp, raddr, sizeof(ip_addr_t));
331+
#if CONFIG_LWIP_IPV6
331332
_remoteIp.type = raddr->type;
332333
_localIp.type = _remoteIp.type;
334+
#endif
333335

334336
eth_hdr *eth = NULL;
335337
udp_hdr *udphdr = (udp_hdr *)(_data - UDP_HLEN);
336338
_localPort = ntohs(udphdr->dest);
337339
_remotePort = ntohs(udphdr->src);
338340

341+
#if CONFIG_LWIP_IPV6
339342
if (_remoteIp.type == IPADDR_TYPE_V4) {
343+
#endif
340344
eth = (eth_hdr *)(_data - UDP_HLEN - IP_HLEN - SIZEOF_ETH_HDR);
341345
struct ip_hdr *iphdr = (struct ip_hdr *)(_data - UDP_HLEN - IP_HLEN);
346+
#if CONFIG_LWIP_IPV6
342347
_localIp.u_addr.ip4.addr = iphdr->dest.addr;
343348
_remoteIp.u_addr.ip4.addr = iphdr->src.addr;
349+
#else
350+
_localIp.addr = iphdr->dest.addr;
351+
_remoteIp.addr = iphdr->src.addr;
352+
#endif
353+
#if CONFIG_LWIP_IPV6
344354
} else {
345355
eth = (eth_hdr *)(_data - UDP_HLEN - IP6_HLEN - SIZEOF_ETH_HDR);
346356
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(_data - UDP_HLEN - IP6_HLEN);
347357
memcpy(&_localIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
348358
memcpy(&_remoteIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->src.addr, 16);
349359
}
360+
#endif
350361
memcpy(_remoteMac, eth->src.addr, 6);
351362

352363
struct netif *netif = NULL;
@@ -413,36 +424,48 @@ tcpip_adapter_if_t AsyncUDPPacket::interface() {
413424
}
414425

415426
IPAddress AsyncUDPPacket::localIP() {
427+
#if CONFIG_LWIP_IPV6
416428
if (_localIp.type != IPADDR_TYPE_V4) {
417429
return IPAddress();
418430
}
419431
return IPAddress(_localIp.u_addr.ip4.addr);
432+
#else
433+
return IPAddress(_localIp.addr);
434+
#endif
420435
}
421436

437+
#if CONFIG_LWIP_IPV6
422438
IPAddress AsyncUDPPacket::localIPv6() {
423439
if (_localIp.type != IPADDR_TYPE_V6) {
424440
return IPAddress(IPv6);
425441
}
426442
return IPAddress(IPv6, (const uint8_t *)_localIp.u_addr.ip6.addr, _localIp.u_addr.ip6.zone);
427443
}
444+
#endif
428445

429446
uint16_t AsyncUDPPacket::localPort() {
430447
return _localPort;
431448
}
432449

433450
IPAddress AsyncUDPPacket::remoteIP() {
451+
#if CONFIG_LWIP_IPV6
434452
if (_remoteIp.type != IPADDR_TYPE_V4) {
435453
return IPAddress();
436454
}
437455
return IPAddress(_remoteIp.u_addr.ip4.addr);
456+
#else
457+
return IPAddress(_remoteIp.addr);
458+
#endif
438459
}
439460

461+
#if CONFIG_LWIP_IPV6
440462
IPAddress AsyncUDPPacket::remoteIPv6() {
441463
if (_remoteIp.type != IPADDR_TYPE_V6) {
442464
return IPAddress(IPv6);
443465
}
444466
return IPAddress(IPv6, (const uint8_t *)_remoteIp.u_addr.ip6.addr, _remoteIp.u_addr.ip6.zone);
445467
}
468+
#endif
446469

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

455478
bool AsyncUDPPacket::isIPv6() {
479+
#if CONFIG_LWIP_IPV6
456480
return _localIp.type == IPADDR_TYPE_V6;
481+
#else
482+
return false;
483+
#endif
457484
}
458485

459486
bool AsyncUDPPacket::isBroadcast() {
487+
#if CONFIG_LWIP_IPV6
460488
if (_localIp.type == IPADDR_TYPE_V6) {
461489
return false;
462490
}
463491
uint32_t ip = _localIp.u_addr.ip4.addr;
492+
#else
493+
uint32_t ip = _localIp.addr;
494+
#endif
464495
return ip == 0xFFFFFFFF || ip == 0 || (ip & 0xFF000000) == 0xFF000000;
465496
}
466497

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

605+
#if CONFIG_LWIP_IPV6
574606
if (addr->type == IPADDR_TYPE_V4) {
575607
if (join) {
576608
if (igmp_joingroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) {
@@ -592,7 +624,19 @@ static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adap
592624
}
593625
}
594626
}
627+
#else
628+
if (join) {
629+
if (igmp_joingroup_netif(netif, (const ip4_addr *)(addr))) {
630+
return ESP_ERR_INVALID_STATE;
631+
}
632+
} else {
633+
if (igmp_leavegroup_netif(netif, (const ip4_addr *)(addr))) {
634+
return ESP_ERR_INVALID_STATE;
635+
}
636+
}
637+
#endif
595638
} else {
639+
#if CONFIG_LWIP_IPV6
596640
if (addr->type == IPADDR_TYPE_V4) {
597641
if (join) {
598642
if (igmp_joingroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)&(addr->u_addr.ip4))) {
@@ -614,6 +658,17 @@ static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adap
614658
}
615659
}
616660
}
661+
#else
662+
if (join) {
663+
if (igmp_joingroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)(addr))) {
664+
return ESP_ERR_INVALID_STATE;
665+
}
666+
} else {
667+
if (igmp_leavegroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)(addr))) {
668+
return ESP_ERR_INVALID_STATE;
669+
}
670+
}
671+
#endif
617672
}
618673
return ESP_OK;
619674
}
@@ -722,18 +777,24 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const IPAddress addr,
722777
}
723778

724779
IPAddress AsyncUDP::listenIP() {
780+
#if CONFIG_LWIP_IPV6
725781
if (!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V4) {
726782
return IPAddress();
727783
}
728784
return IPAddress(_pcb->remote_ip.u_addr.ip4.addr);
785+
#else
786+
return IPAddress(_pcb->remote_ip.addr);
787+
#endif
729788
}
730789

790+
#if CONFIG_LWIP_IPV6
731791
IPAddress AsyncUDP::listenIPv6() {
732792
if (!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V6) {
733793
return IPAddress(IPv6);
734794
}
735795
return IPAddress(IPv6, (const uint8_t *)_pcb->remote_ip.u_addr.ip6.addr, _pcb->remote_ip.u_addr.ip6.zone);
736796
}
797+
#endif
737798

738799
size_t AsyncUDP::write(const uint8_t *data, size_t len) {
739800
return writeTo(data, len, &(_pcb->remote_ip), _pcb->remote_port);

Diff for: libraries/AsyncUDP/src/AsyncUDP.h

+6
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ class AsyncUDPPacket : public Stream {
7979
tcpip_adapter_if_t interface();
8080

8181
IPAddress localIP();
82+
#if CONFIG_LWIP_IPV6
8283
IPAddress localIPv6();
84+
#endif
8385
uint16_t localPort();
8486
IPAddress remoteIP();
87+
#if CONFIG_LWIP_IPV6
8588
IPAddress remoteIPv6();
89+
#endif
8690
uint16_t remotePort();
8791
void remoteMac(uint8_t *mac);
8892

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

148152
IPAddress listenIP();
153+
#if CONFIG_LWIP_IPV6
149154
IPAddress listenIPv6();
155+
#endif
150156
bool connected();
151157
esp_err_t lastErr();
152158
operator bool();

Diff for: libraries/Ethernet/src/ETH.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
7474
log_e("Could not find ETH interface with that handle!");
7575
return;
7676
}
77+
#if CONFIG_LWIP_IPV6
7778
if (_ethernets[index]->getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
7879
esp_err_t err = esp_netif_create_ip6_linklocal(_ethernets[index]->netif());
7980
if (err != ESP_OK) {
@@ -82,6 +83,7 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
8283
log_v("Enabled IPv6 Link Local on %s", _ethernets[index]->desc());
8384
}
8485
}
86+
#endif
8587
}
8688
}
8789

Diff for: libraries/Network/src/NetworkClient.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ int NetworkClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms) {
210210
_timeout = timeout_ms;
211211
int sockfd = -1;
212212

213+
#if CONFIG_LWIP_IPV6
213214
if (ip.type() == IPv6) {
214215
struct sockaddr_in6 *tmpaddr = (struct sockaddr_in6 *)&serveraddr;
215216
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
@@ -218,12 +219,15 @@ int NetworkClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms) {
218219
tmpaddr->sin6_port = htons(port);
219220
tmpaddr->sin6_scope_id = ip.zone();
220221
} else {
222+
#endif
221223
struct sockaddr_in *tmpaddr = (struct sockaddr_in *)&serveraddr;
222224
sockfd = socket(AF_INET, SOCK_STREAM, 0);
223225
tmpaddr->sin_family = AF_INET;
224226
tmpaddr->sin_addr.s_addr = ip;
225227
tmpaddr->sin_port = htons(port);
228+
#if CONFIG_LWIP_IPV6
226229
}
230+
#endif
227231
if (sockfd < 0) {
228232
log_e("socket: %d", errno);
229233
return 0;
@@ -590,6 +594,7 @@ IPAddress NetworkClient::remoteIP(int fd) const {
590594
return IPAddress((uint32_t)(s->sin_addr.s_addr));
591595
}
592596

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

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

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

Diff for: libraries/Network/src/NetworkEvents.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
#if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED
2525
#include "esp_wifi_types.h"
2626
#include "esp_smartconfig.h"
27+
#if CONFIG_NETWORK_PROV_NETWORK_TYPE_WIFI
2728
#include "network_provisioning/network_config.h"
2829
#endif
30+
#endif
2931

3032
#if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED
3133
constexpr int WIFI_SCANNING_BIT = BIT0;
@@ -111,7 +113,9 @@ typedef union {
111113
#endif
112114
#if SOC_WIFI_SUPPORTED
113115
wifi_sta_config_t prov_cred_recv;
116+
#if CONFIG_NETWORK_PROV_NETWORK_TYPE_WIFI
114117
network_prov_wifi_sta_fail_reason_t prov_fail_reason;
118+
#endif
115119
smartconfig_event_got_ssid_pswd_t sc_got_ssid_pswd;
116120
#endif
117121
} arduino_event_info_t;

0 commit comments

Comments
 (0)