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
5 changes: 2 additions & 3 deletions examples/dtls-sock/dtls-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ void *dtls_server_wrapper(void *arg)
/* Prepare (thread) messages reception */
msg_init_queue(_reader_queue, READER_QUEUE_SIZE);

sock_dtls_session_t session;
sock_dtls_t sock;
sock_udp_t udp_sock;
sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
Expand All @@ -113,6 +112,7 @@ void *dtls_server_wrapper(void *arg)
active = false;
}
else {
sock_dtls_session_t session = { 0 };
res = sock_dtls_recv(&sock, &session, rcv, sizeof(rcv),
10 * US_PER_SEC);
if (res >= 0) {
Expand All @@ -121,14 +121,13 @@ void *dtls_server_wrapper(void *arg)
if (res < 0) {
printf("Error resending DTLS message: %d", (int)res);
}
sock_dtls_session_destroy(&sock, &session);
}
else if (res == -SOCK_DTLS_HANDSHAKE) {
printf("New client connected\n");
}
}
}

sock_dtls_session_destroy(&sock, &session);
sock_dtls_close(&sock);
sock_udp_close(&udp_sock);
puts("Terminating");
Expand Down
31 changes: 19 additions & 12 deletions pkg/tinydtls/contrib/sock_dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ static int _read(struct dtls_context_t *ctx, session_t *session, uint8_t *buf,
sock_dtls_t *sock = dtls_get_app_data(ctx);

DEBUG("sock_dtls: decrypted message arrived\n");
sock->buf = buf;
sock->buflen = len;
sock->buffer.data = buf;
sock->buffer.datalen = len;
sock->buffer.session = session;
return len;
}

Expand Down Expand Up @@ -116,7 +117,9 @@ static int _event(struct dtls_context_t *ctx, session_t *session,
break;
}
#endif /* ENABLE_DEBUG */
mbox_put(&sock->mbox, &msg);
if (!level && (code != DTLS_EVENT_CONNECT)) {
mbox_put(&sock->mbox, &msg);
}
return 0;
}

Expand Down Expand Up @@ -246,7 +249,7 @@ int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock,
}

sock->udp_sock = udp_sock;
sock->buf = NULL;
sock->buffer.data = NULL;
sock->role = role;
sock->tag = tag;
sock->dtls_ctx = dtls_new_context(sock);
Expand Down Expand Up @@ -367,18 +370,22 @@ ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote,
(uint8_t *)data, len);
}

static ssize_t _copy_buffer(sock_dtls_t *sock, void *data, size_t max_len)
static ssize_t _copy_buffer(sock_dtls_t *sock, sock_dtls_session_t *remote,
void *data, size_t max_len)
{
uint8_t *buf = sock->buf;
size_t buflen = sock->buflen;
uint8_t *buf = sock->buffer.data;
size_t buflen = sock->buffer.datalen;

sock->buf = NULL;
sock->buffer.data = NULL;
if (buflen > max_len) {
return -ENOBUFS;
}
/* use `memmove()` as tinydtls reuses `data` to store decrypted data with an
* offset in `buf`. This prevents problems with overlapping buffers. */
memmove(data, buf, buflen);
memcpy(&remote->dtls_session, sock->buffer.session,
sizeof(remote->dtls_session));
_session_to_ep(&remote->dtls_session, &remote->ep);
return buflen;
}

Expand All @@ -389,9 +396,9 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
assert(data);
assert(remote);

if (sock->buf != NULL) {
if (sock->buffer.data != NULL) {
/* there is already decrypted data available */
return _copy_buffer(sock, data, max_len);
return _copy_buffer(sock, remote, data, max_len);
}

/* loop breaks when timeout or application data read */
Expand All @@ -413,8 +420,8 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
}

msg_t msg;
if (sock->buf != NULL) {
return _copy_buffer(sock, data, max_len);
if (sock->buffer.data != NULL) {
return _copy_buffer(sock, remote, data, max_len);
}
else if (mbox_try_get(&sock->mbox, &msg) &&
msg.type == DTLS_EVENT_CONNECTED) {
Expand Down
11 changes: 8 additions & 3 deletions pkg/tinydtls/include/sock_dtls_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ struct sock_dtls {
handling */
msg_t mbox_queue[SOCK_DTLS_MBOX_SIZE]; /**< Queue for struct
sock_dtls::mbox */
uint8_t *buf; /**< Buffer to pass decrypted data
back to user */
size_t buflen; /**< Size of buffer */
/**
* @brief Buffer used to pass decrypted data and its session information.
*/
struct {
uint8_t *data; /**< Pointer to the decrypted data */
size_t datalen; /**< data length */
session_t *session; /**< Session information */
} buffer;
credman_tag_t tag; /**< Credential tag of a registered
(D)TLS credential */
dtls_peer_type role; /**< DTLS role of the socket */
Expand Down