Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/dtls-sock/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ static int client_send(char *addr_str, char *data, size_t datalen)
res = credman_add(&credential);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %zd\n", res);
printf("Error cannot add credential to system: %d\n", (int)res);
return -1;
}

res = sock_dtls_session_create(&dtls_sock, &remote, &session);
if (res < 0) {
printf("Error creating session: %zd\n", res);
printf("Error creating session: %d\n", (int)res);
sock_dtls_close(&dtls_sock);
sock_udp_close(&udp_sock);
return -1;
Expand Down
8 changes: 4 additions & 4 deletions examples/dtls-sock/dtls-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void *dtls_server_wrapper(void *arg)
res = credman_add(&credential);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %zd\n", res);
printf("Error cannot add credential to system: %d\n", (int)res);
return NULL;
}

Expand All @@ -116,14 +116,14 @@ void *dtls_server_wrapper(void *arg)
10 * US_PER_SEC);
if (res < 0) {
if (res != -ETIMEDOUT) {
printf("Error receiving UDP over DTLS %zd", res);
printf("Error receiving UDP over DTLS %d", (int)res);
}
continue;
}
printf("Received %zd bytes -- (echo!)\n", res);
printf("Received %d bytes -- (echo!)\n", (int)res);
res = sock_dtls_send(&sock, &session, rcv, (size_t)res);
if (res < 0) {
printf("Error resending DTLS message: %zd", res);
printf("Error resending DTLS message: %d", (int)res);
}
}
}
Expand Down
61 changes: 14 additions & 47 deletions pkg/tinydtls/contrib/sock_dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
Expand Down Expand Up @@ -102,7 +98,7 @@ static int _write(struct dtls_context_t *ctx, session_t *session, uint8_t *buf,

ssize_t res = sock_udp_send(sock->udp_sock, buf, len, &remote);
if (res < 0) {
DEBUG("sock_dtls: failed to send DTLS record: %zd\n", res);
DEBUG("sock_dtls: failed to send DTLS record: %d\n", (int)res);
}
return res;
}
Expand Down Expand Up @@ -291,7 +287,7 @@ int sock_dtls_session_create(sock_dtls_t *sock, const sock_udp_ep_t *ep,
DEBUG("sock_dtls: starting handshake\n");
res = dtls_connect(sock->dtls_ctx, &remote->dtls_session);
if (res < 0) {
DEBUG("sock_dtls: error establishing a session: %zd\n", res);
DEBUG("sock_dtls: error establishing a session: %d\n", (int)res);
return -ENOMEM;
}
else if (res == 0) {
Expand All @@ -305,7 +301,7 @@ int sock_dtls_session_create(sock_dtls_t *sock, const sock_udp_ep_t *ep,
res = sock_udp_recv(sock->udp_sock, rcv_buffer, sizeof(rcv_buffer),
DTLS_HANDSHAKE_TIMEOUT, &remote->ep);
if (res <= 0) {
DEBUG("sock_dtls: error receiving handshake messages: %zd\n", res);
DEBUG("sock_dtls: error receiving handshake messages: %d\n", (int)res);
/* deletes peer created in dtls_connect() */
dtls_peer_t *peer = dtls_get_peer(sock->dtls_ctx,
&remote->dtls_session);
Expand Down Expand Up @@ -348,31 +344,24 @@ 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 */
dtls_peer_t *peer = dtls_get_peer(sock->dtls_ctx, &remote->dtls_session);
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)
Expand All @@ -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);
Expand All @@ -403,23 +390,21 @@ 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) {
uint32_t start_recv = xtimer_now_usec();
ssize_t res = sock_udp_recv(sock->udp_sock, data, max_len, timeout,
&remote->ep);
if (res <= 0) {
DEBUG("sock_dtls: error receiving UDP packet: %zd\n", res);
xtimer_remove(&timeout_timer);
DEBUG("sock_dtls: error receiving UDP packet: %d\n", (int)res);
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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

/** @} */
4 changes: 4 additions & 0 deletions sys/include/net/sock/dtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down