Skip to content

Commit 92e63e5

Browse files
committed
WIP: mostly works, but sometimes deadlocks
1 parent f9d7483 commit 92e63e5

File tree

2 files changed

+76
-48
lines changed

2 files changed

+76
-48
lines changed

pkg/tinydtls/contrib/sock_dtls.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "net/sock/async/event.h"
3232
#endif
3333

34-
#define ENABLE_DEBUG 0
34+
#define ENABLE_DEBUG 1
3535
#include "debug.h"
3636
#include "dtls_debug.h"
3737

@@ -104,7 +104,8 @@ static int _read(struct dtls_context_t *ctx, session_t *session, uint8_t *buf, s
104104
#ifdef SOCK_HAS_ASYNC
105105
if (sock->async_cb != NULL) {
106106
/* reset retrievable event session */
107-
memset(&sock->async_cb_session, 0, sizeof(sock->async_cb_session));
107+
// todo: why would we need to do that? if message comes to fast, CONN_RDY might not have been processed yet
108+
// memset(&sock->async_cb_session, 0, sizeof(sock->async_cb_session));
108109
sock->async_cb(sock, SOCK_ASYNC_MSG_RECV, sock->async_cb_arg);
109110
}
110111
#endif
@@ -143,7 +144,8 @@ static int _event(struct dtls_context_t *ctx, session_t *session,
143144
}
144145
}
145146
if (!level && (code != DTLS_EVENT_CONNECT)) {
146-
mbox_put(&sock->mbox, &msg);
147+
// todo: do we even need mbox for async?
148+
mbox_try_put(&sock->mbox, &msg);
147149
}
148150

149151
#if IS_ACTIVE(CONFIG_DTLS_ECC)
@@ -165,11 +167,17 @@ static int _event(struct dtls_context_t *ctx, session_t *session,
165167
memcpy(&sock->async_cb_session, session, sizeof(session_t));
166168
sock->async_cb(sock, SOCK_ASYNC_CONN_FIN, sock->async_cb_arg);
167169
break;
168-
case DTLS_EVENT_CONNECTED:
170+
case DTLS_EVENT_CONNECT:
169171
/* we received a session handshake initialization */
170172
sock->async_cb(sock, SOCK_ASYNC_CONN_RECV,
171173
sock->async_cb_arg);
172174
break;
175+
case DTLS_EVENT_CONNECTED:
176+
/* session handshake complete */
177+
memcpy(&sock->async_cb_session, session, sizeof(session_t));
178+
sock->async_cb(sock, SOCK_ASYNC_CONN_RDY,
179+
sock->async_cb_arg);
180+
break;
173181
default:
174182
break;
175183
}
@@ -724,7 +732,7 @@ ssize_t sock_dtls_sendv_aux(sock_dtls_t *sock, sock_dtls_session_t *remote,
724732
#if SOCK_HAS_ASYNC
725733
/**
726734
* @brief Checks for and iterates for more data chunks within the network
727-
* stacks anternal packet buffer
735+
* stacks internal packet buffer
728736
*
729737
* When no more chunks exists, `data_ctx` assures cleaning up the internal
730738
* buffer state and `sock_udp_recv_buf()` returns 0.
@@ -1048,7 +1056,8 @@ void _udp_cb(sock_udp_t *udp_sock, sock_async_flags_t flags, void *ctx)
10481056
return;
10491057
}
10501058
_ep_to_session(&remote_ep, &remote);
1051-
sock->buf_ctx = data_ctx;
1059+
// todo: why would we even need sock->buf_ctx??
1060+
// sock->buf_ctx = data_ctx;
10521061
res = dtls_handle_message(sock->dtls_ctx, &remote,
10531062
data, res);
10541063
if (sock->buffer.data == NULL) {

sys/net/application_layer/unicoap/drivers/rfc7252/dtls/transport.c

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,33 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
6262
(void)arg;
6363
sock_dtls_session_t session = { 0 };
6464

65-
if (type & SOCK_ASYNC_CONN_RECV) {
66-
DTLS_DEBUG("establishing session\n");
67-
ssize_t res = sock_dtls_recv(sock, &session, unicoap_receiver_buffer,
68-
sizeof(unicoap_receiver_buffer),
69-
CONFIG_UNICOAP_DTLS_HANDSHAKE_TIMEOUT_MS * US_PER_MS);
70-
71-
if (res != -SOCK_DTLS_HANDSHAKE) {
72-
DTLS_DEBUG("could not establish DTLS session: %" PRIiSIZE " (%s)\n", res,
73-
strerror(-(int)res));
74-
goto error;
65+
// if (type & SOCK_ASYNC_CONN_RECV) {
66+
// DTLS_DEBUG("establishing session...\n");
67+
// ssize_t res = sock_dtls_recv(sock, &session, unicoap_receiver_buffer,
68+
// sizeof(unicoap_receiver_buffer),
69+
// CONFIG_UNICOAP_DTLS_HANDSHAKE_TIMEOUT_MS * US_PER_MS);
70+
// // _UNICOAP_DEBUG_HEX(unicoap_receiver_buffer, res);
71+
72+
// if (-res != SOCK_DTLS_HANDSHAKE) {
73+
// DTLS_DEBUG("could not establish DTLS session: %" PRIiSIZE " (%s)\n", res,
74+
// strerror(-(int)res));
75+
// goto error;
76+
// }
77+
// }
78+
79+
DTLS_DEBUG("CONN_RDY = %d, MSG_RECV = %d, CONN_FIN = %d, CONN_RECV = %d\n",
80+
(type & SOCK_ASYNC_CONN_RDY) != 0,
81+
(type & SOCK_ASYNC_MSG_RECV) != 0,
82+
(type & SOCK_ASYNC_CONN_FIN) != 0,
83+
(type & SOCK_ASYNC_CONN_RECV) != 0);
84+
85+
if (type & SOCK_ASYNC_CONN_RDY) {
86+
DTLS_DEBUG("session established\n");
87+
88+
if (!sock_dtls_get_event_session(sock, &session)) {
89+
DTLS_DEBUG("session was established, but the corresponding session "
90+
"could not be retrieved from the socket\n");
91+
return;
7592
}
7693

7794
dsm_state_t prev_state = dsm_store(sock, &session, SESSION_STATE_ESTABLISHED, false);
@@ -82,7 +99,7 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
8299
* waiting thread to inform about established session */
83100
if (prev_state == SESSION_STATE_HANDSHAKE) {
84101
msg_t msg = { .type = DTLS_EVENT_CONNECTED };
85-
msg_send(&msg, _dtls_auth_waiting_thread);
102+
msg_try_send(&msg, _dtls_auth_waiting_thread);
86103
}
87104
else if (prev_state == NO_SPACE) {
88105
/* No space in session management. Should not happen. If it occurs,
@@ -102,40 +119,17 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
102119
}
103120
}
104121

105-
if (type & SOCK_ASYNC_CONN_FIN) {
106-
DTLS_DEBUG("closing session\n");
107-
if (sock_dtls_get_event_session(sock, &session)) {
108-
/* Session is already destroyed, only remove it from session mgmt. */
109-
dsm_remove(sock, &session);
110-
}
111-
else {
112-
DTLS_DEBUG("session was closed, but the corresponding session "
113-
"could not be retrieved from the socket\n");
114-
return;
115-
}
116-
117-
DTLS_DEBUG("session ended, removing associated endpoint state\n");
118-
119-
unicoap_endpoint_t endpoint = { .proto = UNICOAP_PROTO_DTLS };
120-
sock_dtls_session_get_udp_ep(&session, unicoap_endpoint_get_dtls(&endpoint));
121-
unicoap_exchange_release_endpoint_state(&endpoint);
122-
/* It is safe to ignore the result of exchange_release_endpoint state as this logic follows
123-
* a best-effort philosophy. */
124-
}
125-
126-
if (type & SOCK_ASYNC_CONN_RDY) {
127-
DTLS_DEBUG("connection ready\n");
128-
}
129-
130122
if (type & SOCK_ASYNC_MSG_RECV) {
131123
DTLS_DEBUG("received encrypted datagram\n");
124+
132125
sock_dtls_aux_rx_t aux_rx = {
133126
.flags = IS_ACTIVE(CONFIG_UNICOAP_GET_LOCAL_ENDPOINTS) ? SOCK_AUX_GET_LOCAL : 0,
134127
};
135128

136129
void* pdu = NULL;
137130
void* buffer_ctx = NULL;
138131

132+
// todo: this should be called again with return value 0, but `buffer_ctx` is apparently ignored in sock-async case
139133
ssize_t received = sock_dtls_recv_buf_aux(sock, &session, &pdu, &buffer_ctx, 0, &aux_rx);
140134
if (received < 0) {
141135
DTLS_DEBUG("recv failure: %" PRIdSIZE "\n", received);
@@ -146,6 +140,12 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
146140
if (received == 0) {
147141
return;
148142
}
143+
// void *empty_pdu = NULL;
144+
// ssize_t empty_received = sock_dtls_recv_buf_aux(sock, &session, &empty_pdu, &buffer_ctx, , &aux_rx);
145+
// if (empty_received != 0) {
146+
// DTLS_DEBUG("failure or unexpected second buf in recv: %" PRIdSIZE "\n", empty_received);
147+
// return;
148+
// }
149149

150150
assert(pdu);
151151

@@ -167,6 +167,24 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
167167
unicoap_messaging_process_rfc7252((uint8_t*)pdu, received, false, &packet);
168168
}
169169

170+
if (type & SOCK_ASYNC_CONN_FIN) {
171+
if (sock_dtls_get_event_session(sock, &session)) {
172+
/* Session is already destroyed, only remove it from dsm */
173+
dsm_remove(sock, &session);
174+
}
175+
else {
176+
DTLS_DEBUG("session was closed, but the corresponding session "
177+
"could not be retrieved from the socket\n");
178+
return;
179+
}
180+
181+
DTLS_DEBUG("session ended, removing associated endpoint state\n");
182+
183+
unicoap_endpoint_t endpoint = { .proto = UNICOAP_PROTO_DTLS };
184+
sock_dtls_session_get_udp_ep(&session, unicoap_endpoint_get_dtls(&endpoint));
185+
unicoap_exchange_release_endpoint_state(&endpoint);
186+
}
187+
170188
return;
171189

172190
error:
@@ -236,12 +254,13 @@ int unicoap_transport_sendv_dtls(iolist_t* iolist, const sock_udp_ep_t* remote,
236254
return res;
237255
}
238256
}
239-
/* prepare session */
240-
sock_dtls_session_set_udp_ep(session, remote);
241-
dsm_state_t session_state = dsm_store(&_dtls_socket, session, SESSION_STATE_HANDSHAKE, true);
242-
if (session_state == NO_SPACE) {
243-
return -1;
244-
}
257+
// todo: not needed, already done in _dtls_authenticate, or session is already established?!
258+
// /* prepare session */
259+
// sock_dtls_session_set_udp_ep(session, remote);
260+
// dsm_state_t session_state = dsm_store(&_dtls_socket, session, SESSION_STATE_HANDSHAKE, true);
261+
// if (session_state == NO_SPACE) {
262+
// return -1;
263+
// }
245264

246265
DTLS_DEBUG("started sending\n");
247266

0 commit comments

Comments
 (0)