net/sock_dtls: do not put msg in mbox on timeout

This commit is contained in:
Aiman Ismail 2020-02-27 13:52:56 +01:00
parent 5e9733645f
commit aa3cbacceb
2 changed files with 14 additions and 43 deletions

View File

@ -24,8 +24,6 @@
#include "debug.h"
#include "dtls_debug.h"
#define DTLS_EVENT_TIMEOUT (0x01E1)
#define DTLS_HANDSHAKE_BUFSIZE (256) /**< Size buffer used in handshake
to hold credentials */
/* ECC handshake takes more time */
@ -35,8 +33,6 @@
#define DTLS_HANDSHAKE_TIMEOUT (1 * US_PER_SEC)
#endif /* CONFIG_DTLS_ECC */
static void _timeout_callback(void *arg);
#ifdef CONFIG_DTLS_PSK
static int _get_psk_info(struct dtls_context_t *ctx, const session_t *session,
dtls_credentials_type_t type,
@ -348,18 +344,13 @@ ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote,
}
else if (res > 0) {
/* handshake initiated, wait until connected or timed out */
xtimer_t timeout_timer;
timeout_timer.callback = _timeout_callback;
timeout_timer.arg = sock;
xtimer_set(&timeout_timer, DTLS_HANDSHAKE_TIMEOUT);
msg_t msg;
do {
mbox_get(&sock->mbox, &msg);
} while ((msg.type != DTLS_EVENT_CONNECTED) &&
(msg.type != DTLS_EVENT_TIMEOUT));
if (msg.type == DTLS_EVENT_TIMEOUT) {
res = xtimer_msg_receive_timeout(&msg, 3 * DTLS_HANDSHAKE_TIMEOUT);
}
while ((res != -1) && (msg.type != DTLS_EVENT_CONNECTED));
if (res == -1) {
DEBUG("sock_dtls: handshake process timed out\n");
/* deletes peer created in dtls_connect() before */
@ -367,12 +358,10 @@ ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote,
dtls_reset_peer(sock->dtls_ctx, peer);
return -EHOSTUNREACH;
}
xtimer_remove(&timeout_timer);
}
}
return dtls_write(sock->dtls_ctx, &remote->dtls_session, (uint8_t *)data,
len);
return dtls_write(sock->dtls_ctx, &remote->dtls_session, (uint8_t *)data, len);
}
static ssize_t _copy_buffer(sock_dtls_t *sock, void *data, size_t max_len)
@ -393,8 +382,6 @@ static ssize_t _copy_buffer(sock_dtls_t *sock, void *data, size_t max_len)
ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
void *data, size_t max_len, uint32_t timeout)
{
xtimer_t timeout_timer;
assert(sock);
assert(data);
assert(remote);
@ -403,11 +390,6 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
/* there is already decrypted data available */
return _copy_buffer(sock, data, max_len);
}
if ((timeout != SOCK_NO_TIMEOUT) && (timeout != 0)) {
timeout_timer.callback = _timeout_callback;
timeout_timer.arg = sock;
xtimer_set(&timeout_timer, timeout);
}
/* loop breaks when timeout or application data read */
while(1) {
@ -416,10 +398,13 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
&remote->ep);
if (res <= 0) {
DEBUG("sock_dtls: error receiving UDP packet: %zd\n", res);
xtimer_remove(&timeout_timer);
return res;
}
_ep_to_session(&remote->ep, &remote->dtls_session);
res = dtls_handle_message(sock->dtls_ctx, &remote->dtls_session,
(uint8_t *)data, res);
if ((timeout != SOCK_NO_TIMEOUT) && (timeout != 0)) {
uint32_t time_passed = (xtimer_now_usec() - start_recv);
timeout = (time_passed > timeout) ? 0: timeout - time_passed;
@ -430,21 +415,10 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
(uint8_t *)data, res);
if (sock->buf != NULL) {
xtimer_remove(&timeout_timer);
return _copy_buffer(sock, data, max_len);
}
/* reset msg type */
msg_t msg;
if (mbox_try_get(&sock->mbox, &msg)) {
switch(msg.type) {
case DTLS_EVENT_TIMEOUT:
DEBUG("sock_dtls: timed out while decrypting message\n");
return -ETIMEDOUT;
default:
break;
}
}
else if (timeout == 0) {
DEBUG("sock_dtls: timed out while decrypting message\n");
return -ETIMEDOUT;
}
}
@ -478,11 +452,4 @@ static void _session_to_ep(const session_t *session, sock_udp_ep_t *ep)
memcpy(&ep->addr.ipv6, &session->addr, sizeof(ipv6_addr_t));
}
static void _timeout_callback(void *arg)
{
msg_t timeout_msg = { .type = DTLS_EVENT_TIMEOUT };
sock_dtls_t *sock = arg;
mbox_try_put(&sock->mbox, &timeout_msg);
}
/** @} */

View File

@ -618,6 +618,10 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
* @note Function may block until a session is created if there is no
* existing session with @p remote.
*
* @note Initiating a session through this function will require
* @ref sock_dtls_recv() called from another thread to receive the handshake
* messages.
*
* @return The number of bytes sent on success
* @return -EADDRINUSE, if sock_dtls_t::udp_sock has no local end-point.
* @return -EAFNOSUPPORT, if `remote->ep != NULL` and