mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
tinydtls_sock_dtls: save session information after data decrypted
This commit is contained in:
parent
2bdf9b16ee
commit
2e16b90b38
@ -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");
|
||||
|
@ -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) {
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user