Skip to content

Commit 30044de

Browse files
committed
WIP: get unicoap to work with dtls
1 parent a854553 commit 30044de

2 files changed

Lines changed: 38 additions & 37 deletions

File tree

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ static void _dtls_session_triage(unicoap_scheduled_event_t* event)
5858
}
5959

6060
static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg)
61-
{DTLS_DEBUG("received event from network backend %x\n", type);
61+
{
6262
(void)arg;
6363
sock_dtls_session_t session = { 0 };
6464

6565
if (type & SOCK_ASYNC_CONN_RECV) {
6666
DTLS_DEBUG("establishing session\n");
67-
ssize_t res = sock_dtls_recv(sock, &session, unicoap_receiver_buffer,
68-
sizeof(unicoap_receiver_buffer),
67+
68+
uint8_t buf[1];
69+
ssize_t res = sock_dtls_recv(sock, &session, buf, sizeof(buf),
6970
CONFIG_UNICOAP_DTLS_HANDSHAKE_TIMEOUT_MS * US_PER_MS);
7071

7172
if (res != -SOCK_DTLS_HANDSHAKE) {
@@ -102,27 +103,6 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
102103
}
103104
}
104105

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-
126106
if (type & SOCK_ASYNC_CONN_RDY) {
127107
DTLS_DEBUG("connection ready\n");
128108
}
@@ -141,11 +121,6 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
141121
DTLS_DEBUG("recv failure: %" PRIdSIZE "\n", received);
142122
return;
143123
}
144-
/* FIXME: sock_dtls_recv_buf_aux fails on second read due to sock->buf_ctx not being NULL */
145-
// sock->buf_ctx = NULL;
146-
if (received == 0) {
147-
return;
148-
}
149124

150125
assert(pdu);
151126

@@ -165,6 +140,29 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
165140

166141
/* Truncated DTLS messages would already have gotten lost at verification */
167142
unicoap_messaging_process_rfc7252((uint8_t*)pdu, received, false, &packet);
143+
144+
received = sock_dtls_recv_buf_aux(sock, &session, &pdu, &buffer_ctx, 0, &aux_rx);
145+
/* If the networking backends holds its zero-copy guarantee, then trying to read
146+
* another chunk must not yield any more data. */
147+
assert(received == 0);
148+
}
149+
150+
if (type & SOCK_ASYNC_CONN_FIN) {
151+
if (sock_dtls_get_event_session(sock, &session)) {
152+
/* Session is already destroyed, only remove it from dsm */
153+
dsm_remove(sock, &session);
154+
}
155+
else {
156+
DTLS_DEBUG("session was closed, but the corresponding session "
157+
"could not be retrieved from the socket\n");
158+
return;
159+
}
160+
161+
DTLS_DEBUG("session ended, removing associated endpoint state\n");
162+
163+
unicoap_endpoint_t endpoint = { .proto = UNICOAP_PROTO_DTLS };
164+
sock_dtls_session_get_udp_ep(&session, unicoap_endpoint_get_dtls(&endpoint));
165+
unicoap_exchange_release_endpoint_state(&endpoint);
168166
}
169167

170168
return;
@@ -236,18 +234,19 @@ int unicoap_transport_sendv_dtls(iolist_t* iolist, const sock_udp_ep_t* remote,
236234
return res;
237235
}
238236
}
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-
}
237+
// todo: not needed, already done in _dtls_authenticate, or session is already established?!
238+
// /* prepare session */
239+
// sock_dtls_session_set_udp_ep(session, remote);
240+
// dsm_state_t session_state = dsm_store(&_dtls_socket, session, SESSION_STATE_HANDSHAKE, true);
241+
// if (session_state == NO_SPACE) {
242+
// return -1;
243+
// }
245244

246245
DTLS_DEBUG("started sending\n");
247246

248247
if (unlikely(local)) {
249248
sock_dtls_aux_tx_t aux_tx = { .flags = SOCK_AUX_SET_LOCAL, .local = *local };
250-
res = sock_dtls_sendv_aux(&_dtls_socket, session, iolist, SOCK_NO_TIMEOUT, &aux_tx);
249+
res = sock_dtls_sendv_aux(&_dtls_socket, session, iolist, 500000, &aux_tx);
251250
}
252251
else {
253252
res = sock_dtls_sendv_aux(&_dtls_socket, session, iolist,
@@ -256,8 +255,9 @@ int unicoap_transport_sendv_dtls(iolist_t* iolist, const sock_udp_ep_t* remote,
256255
DTLS_DEBUG("done sending\n");
257256

258257
switch (res) {
259-
case -EHOSTUNREACH:
258+
case -EHOSTUNREACH: // todo: not one of the return values
260259
case -ENOTCONN:
260+
case -ETIMEDOUT:
261261
case 0:
262262
DTLS_DEBUG("DTLS sock not connected or remote unreachable. "
263263
"Destroying session.\n");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static void _udp_on_event(sock_udp_t* sock, sock_async_flags_t type, void* arg)
6969
}
7070
else {
7171
pdu = unicoap_receiver_buffer;
72+
// todo: shouldn't this check truncated somewhere and bail out?
7273
while (true) {
7374
ssize_t chunk_size = sock_udp_recv_buf_aux(
7475
sock, &stackbuf, &buffer_ctx, 0, unicoap_endpoint_get_udp(&remote), &aux_rx);

0 commit comments

Comments
 (0)