Skip to content

Commit 46c4601

Browse files
author
Seppo Takalo
committed
Refactor DTLSSocket to use Socket::getpeername()
Also, let DTLSSocket to be a friend of InternetSocket so it can do the name resolution from its _stack. + Some whitespace fixes
1 parent 695db63 commit 46c4601

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

features/netsocket/DTLSSocket.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@
2525

2626
nsapi_error_t DTLSSocket::connect(const char *host, uint16_t port)
2727
{
28-
if (!_remote_address) {
29-
nsapi_error_t err = _stack->gethostbyname(host, &_remote_address);
28+
SocketAddress addr;
29+
nsapi_error_t ret;
30+
31+
ret = _udp_socket.getpeername(&addr);
32+
if (ret != NSAPI_ERROR_NO_CONNECTION) {
33+
return ret;
34+
}
35+
36+
if (!addr || ret == NSAPI_ERROR_NO_CONNECTION) {
37+
nsapi_error_t err = _udp_socket._stack->gethostbyname(host, &addr);
3038
if (err) {
3139
return NSAPI_ERROR_DNS_FAILURE;
3240
}
3341

34-
_remote_address.set_port(port);
42+
addr.set_port(port);
3543

3644
set_hostname(host);
37-
_udp_socket.connect(_remote_address); // UDPSocket::connect() cannot fail
45+
_udp_socket.connect(addr); // UDPSocket::connect() cannot fail
3846
}
3947

40-
return connect(_remote_address);
48+
return connect(addr);
4149
}
4250

4351
DTLSSocket::~DTLSSocket()
@@ -48,4 +56,4 @@ DTLSSocket::~DTLSSocket()
4856
close();
4957
}
5058

51-
#endif // MBEDTLS_SSL_CLI_C
59+
#endif // MBEDTLS_SSL_CLI_C

features/netsocket/DTLSSocket.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
#ifndef DTLSSOCKET_H
219
#define DTLSSOCKET_H
320

@@ -46,7 +63,6 @@ class DTLSSocket : public DTLSSocketWrapper {
4663
* @return 0 on success, negative error code on failure
4764
*/
4865
virtual nsapi_error_t open(NetworkStack *stack) {
49-
_stack = stack;
5066
return _udp_socket.open(stack);
5167
}
5268

@@ -70,9 +86,7 @@ class DTLSSocket : public DTLSSocketWrapper {
7086

7187
private:
7288
UDPSocket _udp_socket;
73-
NetworkStack *_stack;
74-
SocketAddress _remote_address;
7589
};
7690

7791
#endif
78-
#endif
92+
#endif

features/netsocket/DTLSSocketWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ void DTLSSocketWrapper::timer_event(void)
5252
event();
5353
}
5454

55-
#endif /* MBEDTLS_SSL_CLI_C */
55+
#endif /* MBEDTLS_SSL_CLI_C */

features/netsocket/DTLSSocketWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ class DTLSSocketWrapper : public TLSSocketWrapper {
1818
};
1919

2020
#endif
21-
#endif
21+
#endif

features/netsocket/InternetSocket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class InternetSocket : public Socket {
175175
static const int WRITE_FLAG = 0x2u;
176176
static const int FINISHED_FLAG = 0x3u;
177177

178+
friend class DTLSSocket; // Allow DTLSSocket::connect() to do name resolution on the _stack
179+
178180
#endif //!defined(DOXYGEN_ONLY)
179181
};
180182

features/netsocket/TLSSocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ TLSSocket::~TLSSocket()
4747
close();
4848
}
4949

50-
#endif // MBEDTLS_SSL_CLI_C
50+
#endif // MBEDTLS_SSL_CLI_C

0 commit comments

Comments
 (0)