@@ -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
415426IPAddress 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
422438IPAddress 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
429446uint16_t AsyncUDPPacket::localPort () {
430447 return _localPort;
431448}
432449
433450IPAddress 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
440462IPAddress 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
447470uint16_t AsyncUDPPacket::remotePort () {
448471 return _remotePort;
@@ -453,14 +476,22 @@ void AsyncUDPPacket::remoteMac(uint8_t *mac) {
453476}
454477
455478bool AsyncUDPPacket::isIPv6 () {
479+ #if CONFIG_LWIP_IPV6
456480 return _localIp.type == IPADDR_TYPE_V6;
481+ #else
482+ return false ;
483+ #endif
457484}
458485
459486bool 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
724779IPAddress 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
731791IPAddress 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
738799size_t AsyncUDP::write (const uint8_t *data, size_t len) {
739800 return writeTo (data, len, &(_pcb->remote_ip ), _pcb->remote_port );
0 commit comments