From 8bcb82d245a5fce1bd519e2f99250dedf11e763d Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 27 Apr 2025 18:40:40 -0700 Subject: Introduce DTLS transport for NNG. This introduces a new experimental transport for DTLS, that provides encryption over UDP. It has a simpler protocol than the current UDP SP protocol (but we intend to fix that by making the UDP transport simpler in a follow up!) There are a few other fixes in the TLS layer itself, and in the build, that were needed to accomplish this work. Also there was an endianness bug in the UDP protocol handling, which is fixed here. --- src/platform/posix/posix_udp.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/platform/posix') diff --git a/src/platform/posix/posix_udp.c b/src/platform/posix/posix_udp.c index b643d0b2..99460721 100644 --- a/src/platform/posix/posix_udp.c +++ b/src/platform/posix/posix_udp.c @@ -58,6 +58,7 @@ struct nni_plat_udp { nni_list udp_recvq; nni_list udp_sendq; nni_mtx udp_mtx; + bool udp_stopped; }; static void @@ -361,15 +362,20 @@ nni_plat_udp_open(nni_plat_udp **upp, const nni_sockaddr *bindaddr) } void -nni_plat_udp_close(nni_plat_udp *udp) +nni_plat_udp_stop(nni_plat_udp *udp) { - nni_posix_pfd_stop(&udp->udp_pfd); - nni_mtx_lock(&udp->udp_mtx); + udp->udp_stopped = true; nni_posix_udp_doclose(udp); nni_mtx_unlock(&udp->udp_mtx); nni_posix_pfd_stop(&udp->udp_pfd); +} + +void +nni_plat_udp_close(nni_plat_udp *udp) +{ + nni_plat_udp_stop(udp); nni_posix_pfd_fini(&udp->udp_pfd); (void) close(udp->udp_fd); nni_mtx_fini(&udp->udp_mtx); @@ -399,6 +405,11 @@ nni_plat_udp_recv(nni_plat_udp *udp, nni_aio *aio) nni_mtx_unlock(&udp->udp_mtx); return; } + if (udp->udp_stopped) { + nni_mtx_unlock(&udp->udp_mtx); + nni_aio_finish_error(aio, NNG_ECLOSED); + return; + } nni_list_append(&udp->udp_recvq, aio); if (nni_list_first(&udp->udp_recvq) == aio) { if ((rv = nni_posix_pfd_arm(&udp->udp_pfd, NNI_POLL_IN)) != @@ -420,6 +431,11 @@ nni_plat_udp_send(nni_plat_udp *udp, nni_aio *aio) nni_mtx_unlock(&udp->udp_mtx); return; } + if (udp->udp_stopped) { + nni_mtx_unlock(&udp->udp_mtx); + nni_aio_finish_error(aio, NNG_ECLOSED); + return; + } nni_list_append(&udp->udp_sendq, aio); if (nni_list_first(&udp->udp_sendq) == aio) { if ((rv = nni_posix_pfd_arm(&udp->udp_pfd, NNI_POLL_OUT)) != -- cgit v1.2.3-70-g09d2