@@ -58,14 +58,14 @@ static void _dtls_session_triage(unicoap_scheduled_event_t* event)
5858}
5959
6060static 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+ uint8_t buf [ 1 ];
68+ ssize_t res = sock_dtls_recv ( sock , & session , buf , sizeof (buf ),
6969 CONFIG_UNICOAP_DTLS_HANDSHAKE_TIMEOUT_MS * US_PER_MS );
7070
7171 if (res != - SOCK_DTLS_HANDSHAKE ) {
@@ -102,27 +102,6 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
102102 }
103103 }
104104
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-
126105 if (type & SOCK_ASYNC_CONN_RDY ) {
127106 _DTLS_DEBUG ("connection ready\n" );
128107 }
@@ -141,11 +120,6 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
141120 _DTLS_DEBUG ("recv failure: %" PRIdSIZE "\n" , received );
142121 return ;
143122 }
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- }
149123
150124 assert (pdu );
151125
@@ -165,6 +139,29 @@ static void _dtls_on_event(sock_dtls_t* sock, sock_async_flags_t type, void* arg
165139
166140 /* Truncated DTLS messages would already have gotten lost at verification */
167141 unicoap_messaging_process_rfc7252 ((uint8_t * )pdu , received , false, & packet );
142+
143+ received = sock_dtls_recv_buf_aux (sock , & session , & pdu , & buffer_ctx , 0 , & aux_rx );
144+ /* If the networking backends holds its zero-copy guarantee, then trying to read
145+ * another chunk must not yield any more data. */
146+ assert (received == 0 );
147+ }
148+
149+ if (type & SOCK_ASYNC_CONN_FIN ) {
150+ if (sock_dtls_get_event_session (sock , & session )) {
151+ /* Session is already destroyed, only remove it from dsm */
152+ dsm_remove (sock , & session );
153+ }
154+ else {
155+ _DTLS_DEBUG ("session was closed, but the corresponding session "
156+ "could not be retrieved from the socket\n" );
157+ return ;
158+ }
159+
160+ _DTLS_DEBUG ("session ended, removing associated endpoint state\n" );
161+
162+ unicoap_endpoint_t endpoint = { .proto = UNICOAP_PROTO_DTLS };
163+ sock_dtls_session_get_udp_ep (& session , unicoap_endpoint_get_dtls (& endpoint ));
164+ unicoap_exchange_release_endpoint_state (& endpoint );
168165 }
169166
170167 return ;
@@ -236,18 +233,20 @@ int unicoap_transport_sendv_dtls(iolist_t* iolist, const sock_udp_ep_t* remote,
236233 return res ;
237234 }
238235 }
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- }
236+
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 ,
0 commit comments