Merge pull request #12683 from kivaisan/socketaddress_refactor

SocketAddress rework
pull/12712/head
Anna Bridge 2020-03-27 11:30:12 +00:00 committed by GitHub
commit 1bbcaec04a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 172 deletions

View File

@ -16,24 +16,9 @@
*/ */
#include "SocketAddress.h" #include "SocketAddress.h"
#include "NetworkInterface.h"
#include "NetworkStack.h"
#include <string.h>
#include "mbed.h"
static bool ipv6_is_valid(const char *addr) SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port)
{
return false;
}
static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
{
return 0;
}
SocketAddress::SocketAddress(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) bool SocketAddress::set_ip_address(const char *addr)
{ {
return false; 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_addr(const nsapi_addr_t &addr)
{
}
void SocketAddress::set_port(uint16_t port)
{ {
} }
@ -75,29 +52,6 @@ const char *SocketAddress::get_ip_address() const
return NULL; 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 SocketAddress::operator bool() const
{ {
return false; return false;

View File

@ -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_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_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) bool SocketAddress::set_ip_address(const char *addr)
{ {
delete[] _ip_address; _ip_address.reset();
_ip_address = NULL;
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) { if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
_addr.version = NSAPI_IPv4; _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) 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; addr.version = version;
if (version == NSAPI_IPv6) { if (version == NSAPI_IPv6) {
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES); 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); 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.reset();
_ip_address = NULL;
_addr = addr; _addr = addr;
} }
void SocketAddress::set_port(uint16_t port)
{
_port = port;
}
const char *SocketAddress::get_ip_address() const const char *SocketAddress::get_ip_address() const
{ {
if (_addr.version == NSAPI_UNSPEC) { if (_addr.version == NSAPI_UNSPEC) {
return NULL; return nullptr;
} }
if (!_ip_address) { if (!_ip_address) {
_ip_address = new char[NSAPI_IP_SIZE]; _ip_address.reset(new char[NSAPI_IP_SIZE]);
if (_addr.version == NSAPI_IPv4) { if (_addr.version == NSAPI_IPv4) {
ip4tos(_addr.bytes, _ip_address); ip4tos(_addr.bytes, _ip_address.get());
} else if (_addr.version == NSAPI_IPv6) { } else if (_addr.version == NSAPI_IPv6) {
ip6tos(_addr.bytes, _ip_address); ip6tos(_addr.bytes, _ip_address.get());
} }
} }
return _ip_address; return _ip_address.get();
}
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;
} }
SocketAddress::operator bool() const SocketAddress::operator bool() const
@ -169,10 +120,8 @@ SocketAddress::operator bool() const
SocketAddress &SocketAddress::operator=(const SocketAddress &addr) SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
{ {
delete[] _ip_address; set_addr(addr._addr);
_ip_address = NULL; set_port(addr._port);
set_addr(addr.get_addr());
set_port(addr.get_port());
return *this; return *this;
} }
@ -197,21 +146,3 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
{ {
return !(a == 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;
}

View File

@ -21,6 +21,7 @@
#ifndef SOCKET_ADDRESS_H #ifndef SOCKET_ADDRESS_H
#define SOCKET_ADDRESS_H #define SOCKET_ADDRESS_H
#include <memory>
#include "nsapi_types.h" #include "nsapi_types.h"
#include "mbed_toolchain.h" #include "mbed_toolchain.h"
@ -34,38 +35,18 @@ class NetworkInterface;
*/ */
class SocketAddress { class SocketAddress {
public: public:
/** Create a SocketAddress from a hostname and port /** Create an unspecified SocketAddress
*
* 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> constexpr SocketAddress() = default;
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 a SocketAddress from a raw IP address and port /** 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 addr Raw IP address
* @param port Optional 16-bit port, defaults to 0 * @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 /** Create a SocketAddress from an IP address and port
* *
@ -89,7 +70,7 @@ public:
SocketAddress(const SocketAddress &addr); SocketAddress(const SocketAddress &addr);
/** Destructor */ /** Destructor */
~SocketAddress(); ~SocketAddress() = default;
/** Set the IP address /** Set the IP address
* *
@ -110,13 +91,16 @@ public:
* *
* @param addr Raw IP address * @param addr Raw IP address
*/ */
void set_addr(nsapi_addr_t addr); void set_addr(const nsapi_addr_t &addr);
/** Set the port /** Set the port
* *
* @param port 16-bit 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 /** Get the human-readable IP address
* *
@ -131,31 +115,43 @@ public:
* *
* @return Raw IP address in big-endian order * @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 /** Get the IP address version
* *
* @return IP address version, NSAPI_IPv4 or NSAPI_IPv6 * @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 /** Get the raw IP address
* *
* @return Raw IP address * @return Raw IP address
*/ */
nsapi_addr_t get_addr() const; nsapi_addr_t get_addr() const
{
return _addr;
}
/** Get the port /** Get the port
* *
* @return The 16-bit port * @return The 16-bit port
*/ */
uint16_t get_port() const; uint16_t get_port() const
{
return _port;
}
/** Test if address is zero /** Test if address is zero
* *
* @return True if address is not zero * @return True if address is not zero
*/ */
operator bool() const; explicit operator bool() const;
/** Copy address from another SocketAddress /** Copy address from another SocketAddress
* *
@ -178,14 +174,9 @@ public:
friend bool operator!=(const SocketAddress &a, const SocketAddress &b); friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
private: private:
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port); mutable std::unique_ptr<char[]> _ip_address;
nsapi_addr_t _addr{};
/** Initialize memory */ uint16_t _port = 0;
void mem_init(void);
mutable char *_ip_address;
nsapi_addr_t _addr;
uint16_t _port;
}; };

View File

@ -88,6 +88,7 @@ emacs
json json
noncopyable noncopyable
sendto sendto
gethostbyname
multicast multicast
multicasts multicasts
singleshot singleshot