Skip to content

IPADDR_NONE conflicting defines #6760

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

Closed
1 task done
mrengineer7777 opened this issue May 16, 2022 · 12 comments
Closed
1 task done

IPADDR_NONE conflicting defines #6760

mrengineer7777 opened this issue May 16, 2022 · 12 comments
Assignees
Labels
IDE: PlaformIO Issue relates to PlatformIO IDE Status: Solved Type: For reference Common questions & problems

Comments

@mrengineer7777
Copy link
Collaborator

mrengineer7777 commented May 16, 2022

Board

Adafruit Feather ESP32

Device Description

ESP32-C3-DevKitC-02

Hardware Configuration

None

Version

v2.0.3

IDE Name

VSCODE w/ PIO

Operating System

Win10

Flash frequency

40Mhz

PSRAM enabled

no

Upload speed

115200

Description

I have run into an issue while creating a new "PingClass" library to replace the deprecated ping.h and esp_ping.h libraries.

This issue is mentioned in issue #6247, #6610. Even when applying the fix in #6659 to my copy of IPAddress.cpp/h, I still get the error seen below.

I have resolved the issue in my class by dropping all references to String and IPAddress, and just using C strings. However, I believe the definitions still conflict. Definitions:

IPAddress.cpp: IPAddress INADDR_NONE(0, 0, 0, 0);
IPAddress.h: extern IPAddress INADDR_NONE;
inet.h: #define INADDR_NONE IPADDR_NONE
ip4_addr.h: #define IPADDR_NONE ((u32_t)0xffffffffUL)

I believe INADDR_NONE expands in IPAddress.h as: IPAddress(0,0,0,0), which works with the constructor. But if inet.h is included it may expand as:
((u32_t)0xffffffffUL)(0, 0, 0, 0).

Note also that 0.0.0.0 is a completely different IP than IPADDR_NONE=255.255.255.255.

Sketch

#include <stdio.h>
#include "lwip/netdb.h"
#include "ping/ping_sock.h"
#include "IPAddress.h"

extern "C" {
  #include "esp32-hal.h"
  #include "lwip\sockets.h"
}

#include <Arduino.h>

//-----Prototypes-----
ip_addr_t _url_to_ipaddr(const char *ip_cstr);
//-----Prototypes-----

//Convert passed URL or IP cstr into ip address
//Uses new ip_addr_t format that combines IP4 and IP6 addresses
//Based on https://github.com/espressif/esp-idf/blob/master/examples/protocols/icmp_echo/main/echo_example_main.c
ip_addr_t _url_to_ipaddr(const char *ip_cstr) {
    struct sockaddr_in6 sock_addr6;
    ip_addr_t target_addr;
    memset(&target_addr, 0, sizeof(target_addr));

    if (inet_pton(AF_INET6, ip_cstr, &sock_addr6.sin6_addr) == 1) {
        /* convert ip6 string to ip6 address */
        ipaddr_aton(ip_cstr, &target_addr);
    } else {
        struct addrinfo hint;
        struct addrinfo *res = NULL;
        memset(&hint, 0, sizeof(hint));
        /* convert ip4 string or hostname to ip4 or ip6 address */
        if (getaddrinfo(ip_cstr, NULL, &hint, &res) != 0) {
            log_v("Unknown host %s\n", ip_cstr);
            return target_addr;
        }
        if (res->ai_family == AF_INET) {
            struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
            inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
        } else {
            struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr;
            inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6);
        }
        freeaddrinfo(res);
    }
    return target_addr;
}

void setup() {
    char buf[64];
    
    Serial.begin(115200);
    delay(2000);
    
    sprintf(buf, "%s", "www.google.com");
    ip_addr_t ip = _url_to_ipaddr(buf);

    log_v("%s = %s", buf, IPAddress(ip.u_addr.ip4.addr).toString().c_str());
}

void loop() {
    delay(1000);                        // Allow other tasks to run
}

Debug Message

No dependencies
Building in release mode
Compiling .pio\build\featheresp32ard\src\main.cpp.o
In file included from C:/Users/David/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/ip_addr.h:43,
                 from C:/Users/David/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/inet.h:45,
                 from C:/Users/David/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/netdb.h:42,
                 from src/main.cpp:2:
C:/Users/David/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/ip4_addr.h:63:37: error: expected ')' before numeric constant
 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
                             ~       ^~~~~~~~~~~~
C:/Users/David/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/inet.h:71:29: note: in expansion of macro 'IPADDR_NONE'
 #define INADDR_NONE         IPADDR_NONE
                             ^~~~~~~~~~~
C:/Users/David/.platformio/packages/framework-arduinoespressif32/cores/esp32/IPAddress.h:95:18: note: in expansion of macro 'INADDR_NONE'
 extern IPAddress INADDR_NONE;
                  ^~~~~~~~~~~
*** [.pio\build\featheresp32ard\src\main.cpp.o] Error 1

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@mrengineer7777 mrengineer7777 added the Status: Awaiting triage Issue is waiting for triage label May 16, 2022
@mrengineer7777
Copy link
Collaborator Author

One possible way to correct this issue is to delete the declaration of INADDR_NONE from IPAddress completely. Searching through the Arduino project, the only file affected is ETH.cpp:393: if(local_ip != (uint32_t)0x00000000 && local_ip != INADDR_NONE){.

@mrengineer7777
Copy link
Collaborator Author

PR 6659 notes:
This PR only works when <wifi-provisioning/wifi_config.h> has a modification to include <lwip/ip4_addr.h> instead of <lwip/inet.h>. This will be done directly to the sdk folder in the github structure and it has been fixed in IDF by a separated Merge Request. This will be reflected in the future, for good.

Hopefully this will be fixed in the IDF.

@SuGlider SuGlider self-assigned this May 16, 2022
@SuGlider
Copy link
Collaborator

SuGlider commented May 16, 2022

@mrengineer7777 -
The fix actually isn't limited to just apply #6659 to your copy of IPAddress.cpp/h.
It is necessary to change the all header files as listed in the PR:

  • libraries/AsyncUDP/src/AsyncUDP.h
  • libraries/WiFi/src/WiFiServer.h
  • tools/sdk/esp32c3/include/wifi_provisioning/include/wifi_provisioning/wifi_config.h

Please check https://github.com/espressif/arduino-esp32/pull/6659/files and you will see that the PR does all you have commented in your issue report.

@SuGlider
Copy link
Collaborator

SuGlider commented May 16, 2022

Hopefully this will be fixed in the IDF.

It is already fixed in IDF, but it has not been backported to the 4.4. yet.

@SuGlider
Copy link
Collaborator

You can manually apply all the necessary changes to your local files - it seems you use PIO.
I guess that PIO will launch an official version with the Core 2.0.3 in the near future and the issue will be solved.

Using Arduino IDE and updating it to 2.0.3 works fine for this issue, because it already has the all those header files changes commited to the main GitHub repository.

@SuGlider SuGlider added Type: For reference Common questions & problems Status: Solved IDE: PlaformIO Issue relates to PlatformIO IDE and removed Status: Awaiting triage Issue is waiting for triage labels May 16, 2022
@mrengineer7777
Copy link
Collaborator Author

@SuGlider Thanks for pointing out the modifications to the other files. I just made those changes and I'm still getting a compile error if I include "IPAddress.h". I'll do some more digging and see if I can create a simple sketch to repro the issue.

I will note "lwip/inet.h" is still referenced in multiple files, including "netdb.h", which I use to convert an IP string to ip_addr_t.

@mrengineer7777
Copy link
Collaborator Author

See updated sketch above which reproduces the issue. Note that it doesn't happen if `#include "Arduino.h" is called first, but the library I'm compiling doesn't include Arduino.h (by design), so it exhibits the same issue.

Also note the IP lookup likely won't work without WiFi being up. We are just testing for a successful compile here.

@SuGlider
Copy link
Collaborator

SuGlider commented May 16, 2022

The PR#6659 solves the INADDR_NONE for the ESP32 Arduino framework.
But if you create a sketch that includes and uses IDF directly into it, then you must take care for which files you include and the order it is done...

There is nothing that we can do to fix any combination of Arduino/IDF users may want to create.
We have already an internal discussion about INADDR_NONE and it won't be removed as it is part of UpStream Arduino.

@SuGlider
Copy link
Collaborator

See updated sketch above which reproduces the issue. Note that it doesn't happen if `#include "Arduino.h" is called first, but the library I'm compiling doesn't include Arduino.h (by design), so it exhibits the same issue.

Also note the IP lookup likely won't work without WiFi being up. We are just testing for a successful compile here.

This is not a pure Arduino Sketch... it is a mix of IDF LWIP and Arduino... which only needs IPAddress Class for a log_v().
As I said, I see no real Arduino issue here.

@mrengineer7777
Copy link
Collaborator Author

The PR#6659 solves the INADDR_NONE for the ESP32 Arduino framework. But if you create a sketch that includes and uses IDF directly into it, then you must take care for which files you include and the order it is done...

Fair point. I'm creating an Arduino library but it does need some IDF calls. That makes me wonder how esp_ping.c avoided the URL lookup issue. Will look into it tomorrow.

INADDR_NONE ... won't be removed as it is part of UpStream Arduino.

Accepted. I will close out this issue. Thanks for the clarifications!

@SuGlider
Copy link
Collaborator

avillacis added a commit to yubox-node-org/yubox-now that referenced this issue Jul 6, 2022
…tro default

Según espressif/arduino-esp32#6760 la
biblioteca LwIP y Arduino requieren valores distintos para el valor
INADDR_NONE. Dependiendo de el orden de inclusión de librerías, la
declaración de WiFiAPClass::softAPConfig() puede ver el valor 0xFFFFFFFF
en lugar de 0 para lease_start. El código interno asume 0 para valor por
omisión, y por lo tanto la inicialización de softAP falla y el valor
recibido es 0xFFFFFFFF. Para lidiar con esto, se pasa explícitamente 0
como lease_start en lugar de depender del default.
@Tianbawen
Copy link

See updated sketch above which reproduces the issue. Note that it doesn't happen if `#include "Arduino.h" is called first, but the library I'm compiling doesn't include Arduino.h (by design), so it exhibits the same issue.

Also note the IP lookup likely won't work without WiFi being up. We are just testing for a successful compile here.

It really helps to call the #include "Arduino. h" first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDE: PlaformIO Issue relates to PlatformIO IDE Status: Solved Type: For reference Common questions & problems
Projects
None yet
Development

No branches or pull requests

3 participants