Skip to content

SocketAddress rework #12683

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

Merged
merged 2 commits into from
Mar 27, 2020
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
50 changes: 2 additions & 48 deletions UNITTESTS/stubs/SocketAddress_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,9 @@
*/

#include "SocketAddress.h"
#include "NetworkInterface.h"
#include "NetworkStack.h"
#include <string.h>
#include "mbed.h"


static bool ipv6_is_valid(const char *addr)
{
return false;
}

static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
{
return 0;
}


SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port)
{
}

Expand All @@ -49,10 +34,6 @@ SocketAddress::SocketAddress(const SocketAddress &addr)
{
}

SocketAddress::~SocketAddress()
{
}

bool SocketAddress::set_ip_address(const char *addr)
{
return false;
Expand All @@ -62,11 +43,7 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
{
}

void SocketAddress::set_addr(nsapi_addr_t addr)
{
}

void SocketAddress::set_port(uint16_t port)
void SocketAddress::set_addr(const nsapi_addr_t &addr)
{
}

Expand All @@ -75,29 +52,6 @@ const char *SocketAddress::get_ip_address() const
return NULL;
}

const void *SocketAddress::get_ip_bytes() const
{
return NULL;
}

nsapi_version_t SocketAddress::get_ip_version() const
{
nsapi_version_t ver = NSAPI_IPv6;
return ver;
}

nsapi_addr_t SocketAddress::get_addr() const
{
nsapi_addr_t addr;
addr.version = NSAPI_IPv6;
return _addr;
}

uint16_t SocketAddress::get_port() const
{
return 0;
}

SocketAddress::operator bool() const
{
return false;
Expand Down
99 changes: 15 additions & 84 deletions features/netsocket/SocketAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,27 @@



SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port) : _addr(addr), _port(port)
{
mem_init();
_ip_address = NULL;
set_addr(addr);
set_port(port);
}

SocketAddress::SocketAddress(const char *addr, uint16_t port)
SocketAddress::SocketAddress(const char *addr, uint16_t port) : _port(port)
{
mem_init();
_ip_address = NULL;
set_ip_address(addr);
set_port(port);
}

SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port) : _port(port)
{
mem_init();
_ip_address = NULL;
set_ip_bytes(bytes, version);
set_port(port);
}

SocketAddress::SocketAddress(const SocketAddress &addr)
{
mem_init();
_ip_address = NULL;
set_addr(addr.get_addr());
set_port(addr.get_port());
}

void SocketAddress::mem_init(void)
SocketAddress::SocketAddress(const SocketAddress &addr) : _addr(addr._addr), _port(addr._port)
{
_addr.version = NSAPI_UNSPEC;
memset(_addr.bytes, 0, NSAPI_IP_BYTES);
_port = 0;
}

bool SocketAddress::set_ip_address(const char *addr)
{
delete[] _ip_address;
_ip_address = NULL;
_ip_address.reset();

if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
_addr.version = NSAPI_IPv4;
Expand All @@ -82,9 +60,8 @@ bool SocketAddress::set_ip_address(const char *addr)

void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
{
nsapi_addr_t addr;
nsapi_addr_t addr{};

addr = nsapi_addr_t();
addr.version = version;
if (version == NSAPI_IPv6) {
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
Expand All @@ -94,54 +71,28 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
set_addr(addr);
}

void SocketAddress::set_addr(nsapi_addr_t addr)
void SocketAddress::set_addr(const nsapi_addr_t &addr)
{
delete[] _ip_address;
_ip_address = NULL;
_ip_address.reset();
_addr = addr;
}

void SocketAddress::set_port(uint16_t port)
{
_port = port;
}

const char *SocketAddress::get_ip_address() const
{
if (_addr.version == NSAPI_UNSPEC) {
return NULL;
return nullptr;
}

if (!_ip_address) {
_ip_address = new char[NSAPI_IP_SIZE];
_ip_address.reset(new char[NSAPI_IP_SIZE]);
if (_addr.version == NSAPI_IPv4) {
ip4tos(_addr.bytes, _ip_address);
ip4tos(_addr.bytes, _ip_address.get());
} else if (_addr.version == NSAPI_IPv6) {
ip6tos(_addr.bytes, _ip_address);
ip6tos(_addr.bytes, _ip_address.get());
}
}

return _ip_address;
}

const void *SocketAddress::get_ip_bytes() const
{
return _addr.bytes;
}

nsapi_version_t SocketAddress::get_ip_version() const
{
return _addr.version;
}

nsapi_addr_t SocketAddress::get_addr() const
{
return _addr;
}

uint16_t SocketAddress::get_port() const
{
return _port;
return _ip_address.get();
}

SocketAddress::operator bool() const
Expand Down Expand Up @@ -169,10 +120,8 @@ SocketAddress::operator bool() const

SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
{
delete[] _ip_address;
_ip_address = NULL;
set_addr(addr.get_addr());
set_port(addr.get_port());
set_addr(addr._addr);
set_port(addr._port);
return *this;
}

Expand All @@ -197,21 +146,3 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
{
return !(a == b);
}

void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
{
_ip_address = NULL;

// gethostbyname must check for literals, so can call it directly
int err = iface->gethostbyname(host, this);
_port = port;
if (err) {
_addr = nsapi_addr_t();
_port = 0;
}
}

SocketAddress::~SocketAddress()
{
delete[] _ip_address;
}
73 changes: 32 additions & 41 deletions features/netsocket/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef SOCKET_ADDRESS_H
#define SOCKET_ADDRESS_H

#include <memory>
#include "nsapi_types.h"
#include "mbed_toolchain.h"

Expand All @@ -34,38 +35,18 @@ class NetworkInterface;
*/
class SocketAddress {
public:
/** Create a SocketAddress from a hostname and port
*
* The hostname may be either a domain name or an IP address. If the
* hostname is an IP address, no network transactions will be performed.
*
* On failure, the IP address and port will be set to zero
*
* @tparam S Type of the Network stack
* @param stack Network stack to use for DNS resolution
* @param host Hostname to resolve
* @param port Optional 16-bit port, defaults to 0
* @deprecated
* Constructors hide possible errors. Replaced by
* NetworkInterface::gethostbyname.
*/
template <typename S>
MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
"Constructors hide possible errors. Replaced by "
"NetworkInterface::gethostbyname.")
SocketAddress(S *stack, const char *host, uint16_t port = 0)
{
_SocketAddress(nsapi_create_stack(stack), host, port);
}
/** Create an unspecified SocketAddress
*/
constexpr SocketAddress() = default;

/** Create a SocketAddress from a raw IP address and port
*
* @note To construct from a host name, use NetworkInterface::gethostbyname
* @note To construct from a host name, use @ref NetworkInterface::gethostbyname
*
* @param addr Raw IP address
* @param port Optional 16-bit port, defaults to 0
*/
SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);

/** Create a SocketAddress from an IP address and port
*
Expand All @@ -89,7 +70,7 @@ class SocketAddress {
SocketAddress(const SocketAddress &addr);

/** Destructor */
~SocketAddress();
~SocketAddress() = default;

/** Set the IP address
*
Expand All @@ -110,13 +91,16 @@ class SocketAddress {
*
* @param addr Raw IP address
*/
void set_addr(nsapi_addr_t addr);
void set_addr(const nsapi_addr_t &addr);

/** Set the port
*
* @param port 16-bit port
*/
void set_port(uint16_t port);
void set_port(uint16_t port)
{
_port = port;
}

/** Get the human-readable IP address
*
Expand All @@ -131,31 +115,43 @@ class SocketAddress {
*
* @return Raw IP address in big-endian order
*/
const void *get_ip_bytes() const;
const void *get_ip_bytes() const
{
return _addr.bytes;
}

/** Get the IP address version
*
* @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
*/
nsapi_version_t get_ip_version() const;
nsapi_version_t get_ip_version() const
{
return _addr.version;
}

/** Get the raw IP address
*
* @return Raw IP address
*/
nsapi_addr_t get_addr() const;
nsapi_addr_t get_addr() const
{
return _addr;
}

/** Get the port
*
* @return The 16-bit port
*/
uint16_t get_port() const;
uint16_t get_port() const
{
return _port;
}

/** Test if address is zero
*
* @return True if address is not zero
*/
operator bool() const;
explicit operator bool() const;

/** Copy address from another SocketAddress
*
Expand All @@ -178,14 +174,9 @@ class SocketAddress {
friend bool operator!=(const SocketAddress &a, const SocketAddress &b);

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

/** Initialize memory */
void mem_init(void);

mutable char *_ip_address;
nsapi_addr_t _addr;
uint16_t _port;
mutable std::unique_ptr<char[]> _ip_address;
nsapi_addr_t _addr{};
uint16_t _port = 0;
};


Expand Down
1 change: 1 addition & 0 deletions tools/test/travis-ci/doxy-spellchecker/ignore.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ emacs
json
noncopyable
sendto
gethostbyname
multicast
multicasts
singleshot
Expand Down