aboutsummaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2025-04-27 18:40:40 -0700
committerGarrett D'Amore <garrett@damore.org>2025-06-01 22:49:00 -0700
commit8bcb82d245a5fce1bd519e2f99250dedf11e763d (patch)
tree4d663bedbb043b9d599f061d7f2b5f9509c8f390 /src/platform
parent08400bd437149c4fb31af9b2abece2ae44041283 (diff)
downloadnng-8bcb82d245a5fce1bd519e2f99250dedf11e763d.tar.gz
nng-8bcb82d245a5fce1bd519e2f99250dedf11e763d.tar.bz2
nng-8bcb82d245a5fce1bd519e2f99250dedf11e763d.zip
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.
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/posix/posix_udp.c22
-rw-r--r--src/platform/windows/win_udp.c10
2 files changed, 27 insertions, 5 deletions
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)) !=
diff --git a/src/platform/windows/win_udp.c b/src/platform/windows/win_udp.c
index 709ef82e..79720223 100644
--- a/src/platform/windows/win_udp.c
+++ b/src/platform/windows/win_udp.c
@@ -85,9 +85,8 @@ nni_plat_udp_open(nni_plat_udp **udpp, const nni_sockaddr *sa)
return (rv);
}
-// nni_plat_udp_close closes the underlying UDP socket.
void
-nni_plat_udp_close(nni_plat_udp *u)
+nni_plat_udp_stop(nni_plat_udp *u)
{
nni_mtx_lock(&u->lk);
u->closed = true;
@@ -98,6 +97,13 @@ nni_plat_udp_close(nni_plat_udp *u)
nni_cv_wait(&u->cv);
}
nni_mtx_unlock(&u->lk);
+}
+
+// nni_plat_udp_close closes the underlying UDP socket.
+void
+nni_plat_udp_close(nni_plat_udp *u)
+{
+ nni_plat_udp_stop(u);
if (u->s != INVALID_SOCKET) {
closesocket(u->s);