Skip to content

Commit 1bbcaec

Browse files
authored
Merge pull request #12683 from kivaisan/socketaddress_refactor
SocketAddress rework
2 parents a34333f + 62170dd commit 1bbcaec

File tree

4 files changed

+50
-173
lines changed

4 files changed

+50
-173
lines changed

UNITTESTS/stubs/SocketAddress_stub.cpp

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,9 @@
1616
*/
1717

1818
#include "SocketAddress.h"
19-
#include "NetworkInterface.h"
20-
#include "NetworkStack.h"
21-
#include <string.h>
22-
#include "mbed.h"
2319

2420

25-
static bool ipv6_is_valid(const char *addr)
26-
{
27-
return false;
28-
}
29-
30-
static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
31-
{
32-
return 0;
33-
}
34-
35-
36-
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
21+
SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port)
3722
{
3823
}
3924

@@ -49,10 +34,6 @@ SocketAddress::SocketAddress(const SocketAddress &addr)
4934
{
5035
}
5136

52-
SocketAddress::~SocketAddress()
53-
{
54-
}
55-
5637
bool SocketAddress::set_ip_address(const char *addr)
5738
{
5839
return false;
@@ -62,11 +43,7 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
6243
{
6344
}
6445

65-
void SocketAddress::set_addr(nsapi_addr_t addr)
66-
{
67-
}
68-
69-
void SocketAddress::set_port(uint16_t port)
46+
void SocketAddress::set_addr(const nsapi_addr_t &addr)
7047
{
7148
}
7249

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

78-
const void *SocketAddress::get_ip_bytes() const
79-
{
80-
return NULL;
81-
}
82-
83-
nsapi_version_t SocketAddress::get_ip_version() const
84-
{
85-
nsapi_version_t ver = NSAPI_IPv6;
86-
return ver;
87-
}
88-
89-
nsapi_addr_t SocketAddress::get_addr() const
90-
{
91-
nsapi_addr_t addr;
92-
addr.version = NSAPI_IPv6;
93-
return _addr;
94-
}
95-
96-
uint16_t SocketAddress::get_port() const
97-
{
98-
return 0;
99-
}
100-
10155
SocketAddress::operator bool() const
10256
{
10357
return false;

features/netsocket/SocketAddress.cpp

Lines changed: 15 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,27 @@
2424

2525

2626

27-
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
27+
SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port) : _addr(addr), _port(port)
2828
{
29-
mem_init();
30-
_ip_address = NULL;
31-
set_addr(addr);
32-
set_port(port);
3329
}
3430

35-
SocketAddress::SocketAddress(const char *addr, uint16_t port)
31+
SocketAddress::SocketAddress(const char *addr, uint16_t port) : _port(port)
3632
{
37-
mem_init();
38-
_ip_address = NULL;
3933
set_ip_address(addr);
40-
set_port(port);
4134
}
4235

43-
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
36+
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port) : _port(port)
4437
{
45-
mem_init();
46-
_ip_address = NULL;
4738
set_ip_bytes(bytes, version);
48-
set_port(port);
49-
}
50-
51-
SocketAddress::SocketAddress(const SocketAddress &addr)
52-
{
53-
mem_init();
54-
_ip_address = NULL;
55-
set_addr(addr.get_addr());
56-
set_port(addr.get_port());
5739
}
5840

59-
void SocketAddress::mem_init(void)
41+
SocketAddress::SocketAddress(const SocketAddress &addr) : _addr(addr._addr), _port(addr._port)
6042
{
61-
_addr.version = NSAPI_UNSPEC;
62-
memset(_addr.bytes, 0, NSAPI_IP_BYTES);
63-
_port = 0;
6443
}
6544

6645
bool SocketAddress::set_ip_address(const char *addr)
6746
{
68-
delete[] _ip_address;
69-
_ip_address = NULL;
47+
_ip_address.reset();
7048

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

8361
void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
8462
{
85-
nsapi_addr_t addr;
63+
nsapi_addr_t addr{};
8664

87-
addr = nsapi_addr_t();
8865
addr.version = version;
8966
if (version == NSAPI_IPv6) {
9067
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
@@ -94,54 +71,28 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
9471
set_addr(addr);
9572
}
9673

97-
void SocketAddress::set_addr(nsapi_addr_t addr)
74+
void SocketAddress::set_addr(const nsapi_addr_t &addr)
9875
{
99-
delete[] _ip_address;
100-
_ip_address = NULL;
76+
_ip_address.reset();
10177
_addr = addr;
10278
}
10379

104-
void SocketAddress::set_port(uint16_t port)
105-
{
106-
_port = port;
107-
}
108-
10980
const char *SocketAddress::get_ip_address() const
11081
{
11182
if (_addr.version == NSAPI_UNSPEC) {
112-
return NULL;
83+
return nullptr;
11384
}
11485

11586
if (!_ip_address) {
116-
_ip_address = new char[NSAPI_IP_SIZE];
87+
_ip_address.reset(new char[NSAPI_IP_SIZE]);
11788
if (_addr.version == NSAPI_IPv4) {
118-
ip4tos(_addr.bytes, _ip_address);
89+
ip4tos(_addr.bytes, _ip_address.get());
11990
} else if (_addr.version == NSAPI_IPv6) {
120-
ip6tos(_addr.bytes, _ip_address);
91+
ip6tos(_addr.bytes, _ip_address.get());
12192
}
12293
}
12394

124-
return _ip_address;
125-
}
126-
127-
const void *SocketAddress::get_ip_bytes() const
128-
{
129-
return _addr.bytes;
130-
}
131-
132-
nsapi_version_t SocketAddress::get_ip_version() const
133-
{
134-
return _addr.version;
135-
}
136-
137-
nsapi_addr_t SocketAddress::get_addr() const
138-
{
139-
return _addr;
140-
}
141-
142-
uint16_t SocketAddress::get_port() const
143-
{
144-
return _port;
95+
return _ip_address.get();
14596
}
14697

14798
SocketAddress::operator bool() const
@@ -169,10 +120,8 @@ SocketAddress::operator bool() const
169120

170121
SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
171122
{
172-
delete[] _ip_address;
173-
_ip_address = NULL;
174-
set_addr(addr.get_addr());
175-
set_port(addr.get_port());
123+
set_addr(addr._addr);
124+
set_port(addr._port);
176125
return *this;
177126
}
178127

@@ -197,21 +146,3 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
197146
{
198147
return !(a == b);
199148
}
200-
201-
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
202-
{
203-
_ip_address = NULL;
204-
205-
// gethostbyname must check for literals, so can call it directly
206-
int err = iface->gethostbyname(host, this);
207-
_port = port;
208-
if (err) {
209-
_addr = nsapi_addr_t();
210-
_port = 0;
211-
}
212-
}
213-
214-
SocketAddress::~SocketAddress()
215-
{
216-
delete[] _ip_address;
217-
}

features/netsocket/SocketAddress.h

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef SOCKET_ADDRESS_H
2222
#define SOCKET_ADDRESS_H
2323

24+
#include <memory>
2425
#include "nsapi_types.h"
2526
#include "mbed_toolchain.h"
2627

@@ -34,38 +35,18 @@ class NetworkInterface;
3435
*/
3536
class SocketAddress {
3637
public:
37-
/** Create a SocketAddress from a hostname and port
38-
*
39-
* The hostname may be either a domain name or an IP address. If the
40-
* hostname is an IP address, no network transactions will be performed.
41-
*
42-
* On failure, the IP address and port will be set to zero
43-
*
44-
* @tparam S Type of the Network stack
45-
* @param stack Network stack to use for DNS resolution
46-
* @param host Hostname to resolve
47-
* @param port Optional 16-bit port, defaults to 0
48-
* @deprecated
49-
* Constructors hide possible errors. Replaced by
50-
* NetworkInterface::gethostbyname.
51-
*/
52-
template <typename S>
53-
MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
54-
"Constructors hide possible errors. Replaced by "
55-
"NetworkInterface::gethostbyname.")
56-
SocketAddress(S *stack, const char *host, uint16_t port = 0)
57-
{
58-
_SocketAddress(nsapi_create_stack(stack), host, port);
59-
}
38+
/** Create an unspecified SocketAddress
39+
*/
40+
constexpr SocketAddress() = default;
6041

6142
/** Create a SocketAddress from a raw IP address and port
6243
*
63-
* @note To construct from a host name, use NetworkInterface::gethostbyname
44+
* @note To construct from a host name, use @ref NetworkInterface::gethostbyname
6445
*
6546
* @param addr Raw IP address
6647
* @param port Optional 16-bit port, defaults to 0
6748
*/
68-
SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
49+
SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);
6950

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

9172
/** Destructor */
92-
~SocketAddress();
73+
~SocketAddress() = default;
9374

9475
/** Set the IP address
9576
*
@@ -110,13 +91,16 @@ class SocketAddress {
11091
*
11192
* @param addr Raw IP address
11293
*/
113-
void set_addr(nsapi_addr_t addr);
94+
void set_addr(const nsapi_addr_t &addr);
11495

11596
/** Set the port
11697
*
11798
* @param port 16-bit port
11899
*/
119-
void set_port(uint16_t port);
100+
void set_port(uint16_t port)
101+
{
102+
_port = port;
103+
}
120104

121105
/** Get the human-readable IP address
122106
*
@@ -131,31 +115,43 @@ class SocketAddress {
131115
*
132116
* @return Raw IP address in big-endian order
133117
*/
134-
const void *get_ip_bytes() const;
118+
const void *get_ip_bytes() const
119+
{
120+
return _addr.bytes;
121+
}
135122

136123
/** Get the IP address version
137124
*
138125
* @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
139126
*/
140-
nsapi_version_t get_ip_version() const;
127+
nsapi_version_t get_ip_version() const
128+
{
129+
return _addr.version;
130+
}
141131

142132
/** Get the raw IP address
143133
*
144134
* @return Raw IP address
145135
*/
146-
nsapi_addr_t get_addr() const;
136+
nsapi_addr_t get_addr() const
137+
{
138+
return _addr;
139+
}
147140

148141
/** Get the port
149142
*
150143
* @return The 16-bit port
151144
*/
152-
uint16_t get_port() const;
145+
uint16_t get_port() const
146+
{
147+
return _port;
148+
}
153149

154150
/** Test if address is zero
155151
*
156152
* @return True if address is not zero
157153
*/
158-
operator bool() const;
154+
explicit operator bool() const;
159155

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

180176
private:
181-
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
182-
183-
/** Initialize memory */
184-
void mem_init(void);
185-
186-
mutable char *_ip_address;
187-
nsapi_addr_t _addr;
188-
uint16_t _port;
177+
mutable std::unique_ptr<char[]> _ip_address;
178+
nsapi_addr_t _addr{};
179+
uint16_t _port = 0;
189180
};
190181

191182

tools/test/travis-ci/doxy-spellchecker/ignore.en.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ emacs
8888
json
8989
noncopyable
9090
sendto
91+
gethostbyname
9192
multicast
9293
multicasts
9394
singleshot

0 commit comments

Comments
 (0)