Skip to content

Commit fe1f908

Browse files
author
Seppo Takalo
committed
Implement DTLSSocket helper
This is equivalent of TLSSocket class but for UDP and DTLS.
1 parent d0ed62c commit fe1f908

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

features/netsocket/DTLSSocket.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
18+
#include "DTLSSocket.h"
19+
20+
#define TRACE_GROUP "TLSS"
21+
#include "mbed-trace/mbed_trace.h"
22+
23+
// This class requires Mbed TLS SSL/TLS client code
24+
#if defined(MBEDTLS_SSL_CLI_C)
25+
26+
nsapi_error_t DTLSSocket::connect(const char *host, uint16_t port)
27+
{
28+
if (!_remote_address) {
29+
nsapi_error_t err = _stack->gethostbyname(host, &_remote_address);
30+
if (err) {
31+
return NSAPI_ERROR_DNS_FAILURE;
32+
}
33+
34+
_remote_address.set_port(port);
35+
36+
set_hostname(host);
37+
_udp_socket.connect(_remote_address); // UDPSocket::connect() cannot fail
38+
}
39+
40+
return connect(_remote_address);
41+
}
42+
43+
DTLSSocket::~DTLSSocket()
44+
{
45+
/* Make sure that DTLSSocketWrapper::close() is called before the transport is
46+
* destroyed.
47+
*/
48+
close();
49+
}
50+
51+
#endif // MBEDTLS_SSL_CLI_C

features/netsocket/DTLSSocket.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#ifndef DTLSSOCKET_H
2+
#define DTLSSOCKET_H
3+
4+
#include "DTLSSocketWrapper.h"
5+
#include "SocketAddress.h"
6+
#include "UDPSocket.h"
7+
8+
// This class requires Mbed TLS SSL/TLS client code
9+
#if defined(MBEDTLS_SSL_CLI_C)
10+
11+
class DTLSSocket : public DTLSSocketWrapper {
12+
public:
13+
/** Create an uninitialized DTLS socket
14+
*
15+
* Must call open to initialize the socket on a network stack.
16+
*/
17+
DTLSSocket() : DTLSSocketWrapper(&_udp_socket) {}
18+
19+
/** Destroy the DTLSSocket and closes the transport.
20+
*/
21+
virtual ~DTLSSocket();
22+
23+
/** Create a socket on a network interface
24+
*
25+
* Creates and opens a socket on the network stack of the given
26+
* network interface.
27+
* If hostname is also given, user is not required to call set_hostname() later.
28+
*
29+
* @param stack Network stack as target for socket
30+
* @param hostname Hostname used for certificate verification
31+
*/
32+
template <typename S>
33+
DTLSSocket(S *stack, const char *hostname = NULL) : DTLSSocketWrapper(&_udp_socket, hostname)
34+
{
35+
nsapi_error_t ret = _udp_socket.open(stack);
36+
MBED_ASSERT(ret == NSAPI_ERROR_OK);
37+
}
38+
39+
/** Opens a socket
40+
*
41+
* Creates a network socket on the network stack of the given
42+
* network interface. Not needed if stack is passed to the
43+
* socket's constructor.
44+
*
45+
* @param stack Network stack as target for socket
46+
* @return 0 on success, negative error code on failure
47+
*/
48+
virtual nsapi_error_t open(NetworkStack *stack) {
49+
_stack = stack;
50+
return _udp_socket.open(stack);
51+
}
52+
53+
template <typename S>
54+
nsapi_error_t open(S *stack) {
55+
return open(nsapi_create_stack(stack));
56+
}
57+
58+
using DTLSSocketWrapper::connect;
59+
60+
/** Connects TCP socket to a remote host
61+
*
62+
* Initiates a connection to a remote server specified by either
63+
* a domain name or an IP address and a port.
64+
*
65+
* @param host Hostname of the remote host
66+
* @param port Port of the remote host
67+
* @return 0 on success, negative error code on failure
68+
*/
69+
nsapi_error_t connect(const char *host, uint16_t port);
70+
71+
private:
72+
UDPSocket _udp_socket;
73+
NetworkStack *_stack;
74+
SocketAddress _remote_address;
75+
};
76+
77+
#endif
78+
#endif

0 commit comments

Comments
 (0)