Skip to content

Commit d0ed62c

Browse files
author
Seppo Takalo
committed
Implement DTLSSocketWrapper and fix non-blocking connections on TLSSocket
DTLSSocketWrapper is equivalent of TLSSocketWrapper but uses datagram mode and timers for handling Mbed TLS timeouts. Non-blocking connections were not working earlier, now fixed for both secure socket modes.
1 parent cb49241 commit d0ed62c

File tree

6 files changed

+269
-52
lines changed

6 files changed

+269
-52
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "DTLSSocketWrapper.h"
2+
#include "platform/Callback.h"
3+
#include "drivers/Timer.h"
4+
#include "events/mbed_events.h"
5+
6+
#if defined(MBEDTLS_SSL_CLI_C)
7+
8+
DTLSSocketWrapper::DTLSSocketWrapper(Socket *transport, const char *hostname, control_transport control)
9+
: TLSSocketWrapper(transport, hostname, control)
10+
{
11+
mbedtls_ssl_conf_transport( get_ssl_config(), MBEDTLS_SSL_TRANSPORT_DATAGRAM);
12+
mbedtls_ssl_set_timer_cb( get_ssl_context(), this, timing_set_delay, timing_get_delay);
13+
_timer_event_id = 0;
14+
_timer_expired = false;
15+
}
16+
17+
void DTLSSocketWrapper::timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms)
18+
{
19+
DTLSSocketWrapper *context = static_cast<DTLSSocketWrapper *>(ctx);
20+
21+
if (context->_timer_event_id) {
22+
mbed::mbed_event_queue()->cancel(context->_timer_event_id);
23+
context->_timer_expired = false;
24+
}
25+
26+
if (fin_ms == 0) {
27+
context->_timer_event_id = 0;
28+
return;
29+
}
30+
31+
context->_timer_event_id = mbed::mbed_event_queue()->call_in(fin_ms, context, &DTLSSocketWrapper::timer_event);
32+
}
33+
34+
int DTLSSocketWrapper::timing_get_delay(void *ctx)
35+
{
36+
DTLSSocketWrapper *context = static_cast<DTLSSocketWrapper *>(ctx);
37+
38+
/* See documentation of "typedef int mbedtls_ssl_get_timer_t( void * ctx );" from ssl.h */
39+
40+
if (context->_timer_event_id == 0) {
41+
return -1;
42+
} else if (context->_timer_expired) {
43+
return 2;
44+
} else {
45+
return 0;
46+
}
47+
}
48+
49+
void DTLSSocketWrapper::timer_event(void)
50+
{
51+
_timer_expired = true;
52+
event();
53+
}
54+
55+
#endif /* MBEDTLS_SSL_CLI_C */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef DTLSSOCKETWRAPPER_H
2+
#define DTLSSOCKETWRAPPER_H
3+
4+
#include "TLSSocketWrapper.h"
5+
6+
// This class requires Mbed TLS SSL/TLS client code
7+
#if defined(MBEDTLS_SSL_CLI_C)
8+
9+
class DTLSSocketWrapper : public TLSSocketWrapper {
10+
public:
11+
DTLSSocketWrapper(Socket *transport, const char *hostname = NULL, control_transport control = TRANSPORT_CONNECT_AND_CLOSE);
12+
private:
13+
static void timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms);
14+
static int timing_get_delay(void *ctx);
15+
void timer_event();
16+
int _timer_event_id;
17+
bool _timer_expired:1;
18+
};
19+
20+
#endif
21+
#endif

features/netsocket/TLSSocket.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525

2626
nsapi_error_t TLSSocket::connect(const char *host, uint16_t port)
2727
{
28-
set_hostname(host);
29-
30-
nsapi_error_t ret = tcp_socket.connect(host, port);
31-
if (ret) {
32-
return ret;
28+
nsapi_error_t ret = NSAPI_ERROR_OK;
29+
if (!is_handshake_started()) {
30+
ret = tcp_socket.connect(host, port);
31+
if (ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IN_PROGRESS) {
32+
set_hostname(host);
33+
}
34+
if (ret != NSAPI_ERROR_OK && ret != NSAPI_ERROR_IS_CONNECTED) {
35+
return ret;
36+
}
3337
}
34-
35-
return TLSSocketWrapper::do_handshake();
38+
return TLSSocketWrapper::start_handshake(ret == NSAPI_ERROR_OK);
3639
}
3740

3841
TLSSocket::~TLSSocket()

0 commit comments

Comments
 (0)