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
pull/8659/head
Seppo Takalo 2018-11-14 15:33:38 +02:00
parent 695db63df8
commit 46c46019bc
6 changed files with 37 additions and 13 deletions

View File

@ -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()

View File

@ -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,8 +86,6 @@ public:
private:
UDPSocket _udp_socket;
NetworkStack *_stack;
SocketAddress _remote_address;
};
#endif

View File

@ -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)
};