-
Notifications
You must be signed in to change notification settings - Fork 3k
nsapi - Standardize support of NSAPI_UNSPEC #2897
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,17 +215,19 @@ void SocketAddress::set_port(uint16_t port) | |
|
||
const char *SocketAddress::get_ip_address() const | ||
{ | ||
char *ip_address = (char *)_ip_address; | ||
if (_addr.version == NSAPI_UNSPEC) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So UNSPEC SocketAddress round-trips via NULL string pointer, if I read correctly. Seems fine. But would still vaguely prefer a real string, so users don't have to take care when doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does have the benefit of matching the behaviour of |
||
return NULL; | ||
} | ||
|
||
if (!ip_address[0]) { | ||
if (!_ip_address[0]) { | ||
if (_addr.version == NSAPI_IPv4) { | ||
ipv4_to_address(ip_address, _addr.bytes); | ||
ipv4_to_address(_ip_address, _addr.bytes); | ||
} else if (_addr.version == NSAPI_IPv6) { | ||
ipv6_to_address(ip_address, _addr.bytes); | ||
ipv6_to_address(_ip_address, _addr.bytes); | ||
} | ||
} | ||
|
||
return ip_address; | ||
return _ip_address; | ||
} | ||
|
||
const void *SocketAddress::get_ip_bytes() const | ||
|
@@ -250,34 +252,38 @@ uint16_t SocketAddress::get_port() const | |
|
||
SocketAddress::operator bool() const | ||
{ | ||
int count = 0; | ||
if (_addr.version == NSAPI_IPv4) { | ||
count = NSAPI_IPv4_BYTES; | ||
} else if (_addr.version == NSAPI_IPv6) { | ||
count = NSAPI_IPv6_BYTES; | ||
} | ||
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) { | ||
if (_addr.bytes[i]) { | ||
return true; | ||
} | ||
} | ||
|
||
for (int i = 0; i < count; i++) { | ||
if (_addr.bytes[i]) { | ||
return true; | ||
return false; | ||
} else if (_addr.version == NSAPI_IPv6) { | ||
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) { | ||
if (_addr.bytes[i]) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
return false; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool operator==(const SocketAddress &a, const SocketAddress &b) | ||
{ | ||
int count = 0; | ||
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) { | ||
count = NSAPI_IPv4_BYTES; | ||
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) { | ||
count = NSAPI_IPv6_BYTES; | ||
} else { | ||
if (!a && !b) { | ||
return true; | ||
} else if (a._addr.version != b._addr.version) { | ||
return false; | ||
} else if (a._addr.version == NSAPI_IPv4) { | ||
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0; | ||
} else if (a._addr.version == NSAPI_IPv6) { | ||
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0; | ||
} | ||
|
||
return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0); | ||
} | ||
|
||
bool operator!=(const SocketAddress &a, const SocketAddress &b) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Not sure about this flopping towards IPV4. Could it at least follow the IPv4/IPv6 preference? Ideally we'd convert to lwip's own IPADDR_TYPE_ANY, but that isn't fully supported in their ip_addr_t either - it's used internally, but I don't believe it's valid for external users.
Jesus, this ifdeffing is confusing. Not sure we can do better though.