Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions features/net/network-socket/SocketAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ SocketAddress::operator bool() const
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 {
return false;
}

return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
}

bool operator!=(const SocketAddress &a, const SocketAddress &b)
{
return !(a == b);
}

void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
{
_ip_address[0] = '\0';
Expand Down
12 changes: 12 additions & 0 deletions features/net/network-socket/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ class SocketAddress {
*/
operator bool() const;

/** Compare two addresses for equality
*
* @return True if both addresses are equal
*/
friend bool operator==(const SocketAddress &a, const SocketAddress &b);

/** Compare two addresses for equality
*
* @return True if both addresses are not equal
*/
friend bool operator!=(const SocketAddress &a, const SocketAddress &b);

private:
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);

Expand Down