From 0e12a5d1c33032902bf22d1c933d7fb2bfa335c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Brodbeck?= Date: Sun, 13 Dec 2020 06:08:23 +0100 Subject: [PATCH 1/5] sock/dtls: introduce function to get the udp endpoint from a session --- sys/include/net/sock/dtls.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sys/include/net/sock/dtls.h b/sys/include/net/sock/dtls.h index efc0c49773..5f29540f3e 100644 --- a/sys/include/net/sock/dtls.h +++ b/sys/include/net/sock/dtls.h @@ -664,6 +664,17 @@ int sock_dtls_session_init(sock_dtls_t *sock, const sock_udp_ep_t *ep, */ void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote); +/** + * @brief Get the remote UDP endpoint from a session. + * + * @pre `(session != NULL) && (ep != NULL)` + * + * @param[in] session DTLS session + * @param[out] ep UDP endpoint + */ +void sock_dtls_session_get_udp_ep(const sock_dtls_session_t *session, + sock_udp_ep_t *ep); + /** * @brief Receive handshake messages and application data from remote peer. * From 4be161c3e76aeea07abb11ba6c0ce34b64fdf8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Brodbeck?= Date: Sun, 13 Dec 2020 06:09:17 +0100 Subject: [PATCH 2/5] pkg/tinydtls: implement sock_dtls_session_get_udp_ep --- pkg/tinydtls/contrib/sock_dtls.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/tinydtls/contrib/sock_dtls.c b/pkg/tinydtls/contrib/sock_dtls.c index 3885f786ab..6c9f745d76 100644 --- a/pkg/tinydtls/contrib/sock_dtls.c +++ b/pkg/tinydtls/contrib/sock_dtls.c @@ -345,6 +345,15 @@ void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote) dtls_close(sock->dtls_ctx, &remote->dtls_session); } +void sock_dtls_session_get_udp_ep(const sock_dtls_session_t *session, + sock_udp_ep_t *ep) +{ + assert(session); + assert(ep); + + _session_to_ep(&session->dtls_session, ep); +} + ssize_t sock_dtls_send_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, const void *data, size_t len, uint32_t timeout, sock_dtls_aux_tx_t *aux) From a3030811019244edc6d7938b3ea446cc14eb2727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Brodbeck?= Date: Fri, 18 Dec 2020 15:07:30 +0100 Subject: [PATCH 3/5] sock/dtls: introduce function to set the udp endpoint from a session --- sys/include/net/sock/dtls.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sys/include/net/sock/dtls.h b/sys/include/net/sock/dtls.h index 5f29540f3e..5788ae06ca 100644 --- a/sys/include/net/sock/dtls.h +++ b/sys/include/net/sock/dtls.h @@ -675,6 +675,20 @@ void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote); void sock_dtls_session_get_udp_ep(const sock_dtls_session_t *session, sock_udp_ep_t *ep); +/** + * @brief Set the remote UDP endpoint from a session. + * + * @pre `(session != NULL) && (ep != NULL)` + * + * @param[in] session DTLS session + * @param[in] ep UDP endpoint + * + * @note Function should only be needed when doing a blocking handshake with + * @ref sock_dtls_send() to set the remote UDP endpoint. + */ +void sock_dtls_session_set_udp_ep(sock_dtls_session_t *session, + const sock_udp_ep_t *ep); + /** * @brief Receive handshake messages and application data from remote peer. * From 34a15b654036c7bb3d3e3e66c62e8a734cf683d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Brodbeck?= Date: Fri, 18 Dec 2020 15:09:04 +0100 Subject: [PATCH 4/5] pkg/tinydtls: implement sock_dtls_session_set_udp_ep --- pkg/tinydtls/contrib/sock_dtls.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/tinydtls/contrib/sock_dtls.c b/pkg/tinydtls/contrib/sock_dtls.c index 6c9f745d76..8aa00d3de1 100644 --- a/pkg/tinydtls/contrib/sock_dtls.c +++ b/pkg/tinydtls/contrib/sock_dtls.c @@ -354,6 +354,15 @@ void sock_dtls_session_get_udp_ep(const sock_dtls_session_t *session, _session_to_ep(&session->dtls_session, ep); } +void sock_dtls_session_set_udp_ep(sock_dtls_session_t *session, + const sock_udp_ep_t *ep) +{ + assert(session); + assert(ep); + + _ep_to_session(ep, &session->dtls_session); +} + ssize_t sock_dtls_send_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, const void *data, size_t len, uint32_t timeout, sock_dtls_aux_tx_t *aux) From a72e053d32b4f3a191ee36ec394b003e83012b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Brodbeck?= Date: Mon, 4 Jan 2021 20:21:22 +0100 Subject: [PATCH 5/5] sock/dtls: add example of a blocking handshake to the doc --- sys/include/net/sock/dtls.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sys/include/net/sock/dtls.h b/sys/include/net/sock/dtls.h index 5788ae06ca..a1a129d488 100644 --- a/sys/include/net/sock/dtls.h +++ b/sys/include/net/sock/dtls.h @@ -907,6 +907,14 @@ ssize_t sock_dtls_send_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, * @note When blocking, we will need an extra thread to call * @ref sock_dtls_recv() function to handle the incoming handshake * messages. + * An example for a blocking handshake is: + * 1. Create an empty @ref sock_dtls_session_t object. + * 2. Set the UDP endpoint of the peer you want to connect to in the + * session object with @ref sock_dtls_session_set_udp_ep(). + * 3. Call @ref sock_dtls_send() with a timeout greater than 0. + * The send function blocks until the handshake completes or the + * timeout expires. If the handshake was successful the data has + * been sent. * * @return The number of bytes sent on success * @return -ENOTCONN, if `timeout == 0` and no existing session exists with