diff --git a/examples/dtls-sock/dtls-server.c b/examples/dtls-sock/dtls-server.c index aa43f8956e..31e0c908fb 100644 --- a/examples/dtls-sock/dtls-server.c +++ b/examples/dtls-sock/dtls-server.c @@ -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; @@ -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) { @@ -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"); diff --git a/pkg/tinydtls/contrib/sock_dtls.c b/pkg/tinydtls/contrib/sock_dtls.c index c9c2603182..1e85ee4b28 100644 --- a/pkg/tinydtls/contrib/sock_dtls.c +++ b/pkg/tinydtls/contrib/sock_dtls.c @@ -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; } @@ -246,7 +247,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); @@ -367,18 +368,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; } @@ -389,9 +394,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 */ @@ -413,8 +418,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) { diff --git a/pkg/tinydtls/include/sock_dtls_types.h b/pkg/tinydtls/include/sock_dtls_types.h index 653b75c3c8..a62c04e552 100644 --- a/pkg/tinydtls/include/sock_dtls_types.h +++ b/pkg/tinydtls/include/sock_dtls_types.h @@ -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 */