From 46c46019bc59fddd158f5f09a12914ab442e3fe0 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Wed, 14 Nov 2018 15:33:38 +0200 Subject: [PATCH] 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 --- features/netsocket/DTLSSocket.cpp | 20 ++++++++++++++------ features/netsocket/DTLSSocket.h | 22 ++++++++++++++++++---- features/netsocket/DTLSSocketWrapper.cpp | 2 +- features/netsocket/DTLSSocketWrapper.h | 2 +- features/netsocket/InternetSocket.h | 2 ++ features/netsocket/TLSSocket.cpp | 2 +- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/features/netsocket/DTLSSocket.cpp b/features/netsocket/DTLSSocket.cpp index fa67a37509..cbe1b79763 100644 --- a/features/netsocket/DTLSSocket.cpp +++ b/features/netsocket/DTLSSocket.cpp @@ -25,19 +25,27 @@ nsapi_error_t DTLSSocket::connect(const char *host, uint16_t port) { - if (!_remote_address) { - nsapi_error_t err = _stack->gethostbyname(host, &_remote_address); + SocketAddress addr; + nsapi_error_t ret; + + ret = _udp_socket.getpeername(&addr); + if (ret != NSAPI_ERROR_NO_CONNECTION) { + return ret; + } + + if (!addr || ret == NSAPI_ERROR_NO_CONNECTION) { + nsapi_error_t err = _udp_socket._stack->gethostbyname(host, &addr); if (err) { return NSAPI_ERROR_DNS_FAILURE; } - _remote_address.set_port(port); + addr.set_port(port); set_hostname(host); - _udp_socket.connect(_remote_address); // UDPSocket::connect() cannot fail + _udp_socket.connect(addr); // UDPSocket::connect() cannot fail } - return connect(_remote_address); + return connect(addr); } DTLSSocket::~DTLSSocket() @@ -48,4 +56,4 @@ DTLSSocket::~DTLSSocket() close(); } -#endif // MBEDTLS_SSL_CLI_C \ No newline at end of file +#endif // MBEDTLS_SSL_CLI_C diff --git a/features/netsocket/DTLSSocket.h b/features/netsocket/DTLSSocket.h index 5e4486c54a..2a57635806 100644 --- a/features/netsocket/DTLSSocket.h +++ b/features/netsocket/DTLSSocket.h @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2018 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef DTLSSOCKET_H #define DTLSSOCKET_H @@ -46,7 +63,6 @@ public: * @return 0 on success, negative error code on failure */ virtual nsapi_error_t open(NetworkStack *stack) { - _stack = stack; return _udp_socket.open(stack); } @@ -70,9 +86,7 @@ public: private: UDPSocket _udp_socket; - NetworkStack *_stack; - SocketAddress _remote_address; }; #endif -#endif \ No newline at end of file +#endif diff --git a/features/netsocket/DTLSSocketWrapper.cpp b/features/netsocket/DTLSSocketWrapper.cpp index 3289b4575c..08399af085 100644 --- a/features/netsocket/DTLSSocketWrapper.cpp +++ b/features/netsocket/DTLSSocketWrapper.cpp @@ -52,4 +52,4 @@ void DTLSSocketWrapper::timer_event(void) event(); } -#endif /* MBEDTLS_SSL_CLI_C */ \ No newline at end of file +#endif /* MBEDTLS_SSL_CLI_C */ diff --git a/features/netsocket/DTLSSocketWrapper.h b/features/netsocket/DTLSSocketWrapper.h index 457729139d..fadab108fe 100644 --- a/features/netsocket/DTLSSocketWrapper.h +++ b/features/netsocket/DTLSSocketWrapper.h @@ -18,4 +18,4 @@ private: }; #endif -#endif \ No newline at end of file +#endif diff --git a/features/netsocket/InternetSocket.h b/features/netsocket/InternetSocket.h index 168a10c21e..c08d90a3b8 100644 --- a/features/netsocket/InternetSocket.h +++ b/features/netsocket/InternetSocket.h @@ -175,6 +175,8 @@ protected: static const int WRITE_FLAG = 0x2u; static const int FINISHED_FLAG = 0x3u; + friend class DTLSSocket; // Allow DTLSSocket::connect() to do name resolution on the _stack + #endif //!defined(DOXYGEN_ONLY) }; diff --git a/features/netsocket/TLSSocket.cpp b/features/netsocket/TLSSocket.cpp index 281a6f1db9..bb0c7eb9e4 100644 --- a/features/netsocket/TLSSocket.cpp +++ b/features/netsocket/TLSSocket.cpp @@ -47,4 +47,4 @@ TLSSocket::~TLSSocket() close(); } -#endif // MBEDTLS_SSL_CLI_C \ No newline at end of file +#endif // MBEDTLS_SSL_CLI_C