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
Fixes incorrect handling of inet_gethostbyname() return values in Cellular.resolve() and WiFi.resolve() #1375
Conversation
…or. Correctly handle that
wiring/inc/spark_wiring_wifi.h
Outdated
@@ -201,7 +201,7 @@ class WiFiClass : public NetworkClass | |||
IPAddress resolve(const char* name) | |||
{ | |||
HAL_IPAddress ip; | |||
return (inet_gethostbyname(name, strlen(name), &ip, *this, NULL)<0) ? | |||
return (inet_gethostbyname(name, strlen(name), &ip, *this, NULL) != 0) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please explain this? I thought the default comparison is not equal to 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Electron's inet_gethostbyname
returns a positive value in case of an error, which fails the <0
check. It sounds also reasonable to make sure that the return value is negative in all the HALs, but the check for != 0
sounds good as well, as that mimics the behavior of gethostbyname_r
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I didn't see the <0 when I originally looked (text wrapping.) If we change it to != 0 then we should check no other HALs are using >0 codes for success. (I have a feeling the CC3000 does this...) so it might be better to make the Electron HAL conform to the established <0 convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's the case on the Core either: see https://github.com/spark/firmware/blob/develop/hal/src/core/inet_hal.c#L39
cellularint inet_gethostbyname(const char* hostname, uint16_t hostnameLen, HAL_IPAddress* out_ip_addr,
network_interface_t nif, void* reserved)
{
uint32_t result = electronMDM.gethostbyname(hostname);
if (result > 0) {
out_ip_addr->ipv4 = result;
return 0;
}
return 1;
} wifiint inet_gethostbyname(const char* hostname, uint16_t hostnameLen, HAL_IPAddress* out_ip_addr, network_interface_t nif, void* reserved)
{
wiced_ip_address_t address;
wiced_result_t result = wiced_hostname_lookup (hostname, &address, 5000);
if (result == WICED_SUCCESS) {
HAL_IPV4_SET(out_ip_addr, GET_IPV4_ADDRESS(address));
}
return -result;
} |
How about we do both then? :) Core also returns positive error: int inet_gethostbyname(const char* hostname, uint16_t hostnameLen, HAL_IPAddress* out_ip_addr,
network_interface_t nif, void* reserved)
{
int attempts = 5;
out_ip_addr->ipv4 = 0;
int result = 0;
while (!out_ip_addr->ipv4 && attempts --> 0) {
HAL_Delay_Milliseconds(1);
result = gethostbyname(hostname, hostnameLen, &out_ip_addr->ipv4)<0;
}
return result;
}
|
where are you referring to for the
For cellular, we should be setting the ip with |
Cherry-picked to 0.7.0-rc.7 branch, and that merged to develop for 0.8.0-rc.2 release. (I probably should have rebased instead so Github maintained the tracking, but that's hindsight.) |
Problem
See issue #1304
Cellular.resolve()
andWiFi.resolve()
functions that internally useinet_gethostbyname()
incorrectly handleinet_gethostbyname()
return values.Solution
inet_gethostbyname()
return value other than0
as an error.Steps to Test
wiring/no_fixture
wiring/wifi
References
Completeness