diff --git a/UNITTESTS/stubs/SocketAddress_stub.cpp b/UNITTESTS/stubs/SocketAddress_stub.cpp index 874e5ce667..034fe9de99 100644 --- a/UNITTESTS/stubs/SocketAddress_stub.cpp +++ b/UNITTESTS/stubs/SocketAddress_stub.cpp @@ -16,24 +16,9 @@ */ #include "SocketAddress.h" -#include "NetworkInterface.h" -#include "NetworkStack.h" -#include -#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) { } @@ -49,10 +34,6 @@ SocketAddress::SocketAddress(const SocketAddress &addr) { } -SocketAddress::~SocketAddress() -{ -} - bool SocketAddress::set_ip_address(const char *addr) { return false; @@ -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) { } @@ -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; diff --git a/features/netsocket/SocketAddress.cpp b/features/netsocket/SocketAddress.cpp index deb7affe88..50bea62e6c 100644 --- a/features/netsocket/SocketAddress.cpp +++ b/features/netsocket/SocketAddress.cpp @@ -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) +SocketAddress::SocketAddress(const SocketAddress &addr) : _addr(addr._addr), _port(addr._port) { - mem_init(); - _ip_address = NULL; - set_addr(addr.get_addr()); - set_port(addr.get_port()); -} - -void SocketAddress::mem_init(void) -{ - _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; @@ -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); @@ -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 @@ -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; } @@ -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; -} diff --git a/features/netsocket/SocketAddress.h b/features/netsocket/SocketAddress.h index 5310ba4d64..3d25dadacd 100644 --- a/features/netsocket/SocketAddress.h +++ b/features/netsocket/SocketAddress.h @@ -21,6 +21,7 @@ #ifndef SOCKET_ADDRESS_H #define SOCKET_ADDRESS_H +#include #include "nsapi_types.h" #include "mbed_toolchain.h" @@ -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. + /** Create an unspecified SocketAddress */ - template - 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); - } + 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 * @@ -89,7 +70,7 @@ public: SocketAddress(const SocketAddress &addr); /** Destructor */ - ~SocketAddress(); + ~SocketAddress() = default; /** Set the IP address * @@ -110,13 +91,16 @@ public: * * @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 * @@ -131,31 +115,43 @@ public: * * @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 * @@ -178,14 +174,9 @@ public: 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 _ip_address; + nsapi_addr_t _addr{}; + uint16_t _port = 0; }; diff --git a/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws b/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws index 7535d003fb..dbda7de87e 100644 --- a/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws +++ b/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws @@ -88,6 +88,7 @@ emacs json noncopyable sendto +gethostbyname multicast multicasts singleshot