diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f003d7562..85d044fa3 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -24,6 +24,8 @@ nng_sources( device.h dialer.c dialer.h + fdconn.c + fdconn.h file.c file.h idhash.c diff --git a/src/core/fdconn.c b/src/core/fdconn.c new file mode 100644 index 000000000..d2fb6227d --- /dev/null +++ b/src/core/fdconn.c @@ -0,0 +1,217 @@ +// +// Copyright 2023 Staysail Systems, Inc. +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include +#include + +#include + +#include "core/fdconn.h" +#include "core/nng_impl.h" + +// We will accept up to this many FDs to be queued up for +// accept, before we start rejecting with NNG_ENOSPC. Once +// accept is performed, then another slot is available. +#define NNG_FDC_LISTEN_QUEUE 16 + +int +nni_fdc_dialer_alloc(nng_stream_dialer **dp, const nng_url *url) +{ + NNI_ARG_UNUSED(dp); + NNI_ARG_UNUSED(url); + // No dialer support for this. + return (NNG_ENOTSUP); +} + +typedef struct { + nng_stream_listener ops; + int listen_cnt; // how many FDs are waiting + int listen_q[NNG_FDC_LISTEN_QUEUE]; + bool closed; + nni_list accept_q; + nni_mtx mtx; +} fdc_listener; + +static void +fdc_listener_free(void *arg) +{ + fdc_listener *l = arg; + nni_mtx_fini(&l->mtx); + NNI_FREE_STRUCT(l); +} + +static void +fdc_listener_close(void *arg) +{ + nni_aio *aio; + fdc_listener *l = arg; + nni_mtx_lock(&l->mtx); + l->closed = true; + while ((aio = nni_list_first(&l->accept_q)) != NULL) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, NNG_ECLOSED); + } + for (int i = 0; i < l->listen_cnt; i++) { + nni_fdc_close_fd(l->listen_q[i]); + } + nni_mtx_unlock(&l->mtx); +} + +static int +fdc_listener_listen(void *arg) +{ + NNI_ARG_UNUSED(arg); + // nothing really for us to do + return (0); +} + +static void +fdc_start_conn(fdc_listener *l, nni_aio *aio) +{ + int fd; + int rv; + nni_fdc_conn *c; + NNI_ASSERT(l->listen_cnt > 0); + fd = l->listen_q[0]; + for (int i = 1; i < l->listen_cnt; i++) { + l->listen_q[i] = l->listen_q[i + 1]; + } + l->listen_cnt--; + if ((rv = nni_fdc_conn_alloc(&c, fd)) == 0) { + nni_aio_finish_error(aio, rv); + nni_fdc_close_fd(fd); + } + nni_aio_set_output(aio, 0, c); + nni_aio_finish(aio, 0, 0); +} + +static void +fdc_cancel_accept(nni_aio *aio, void *arg, int rv) +{ + fdc_listener *l = arg; + + nni_mtx_lock(&l->mtx); + if (nni_aio_list_active(aio)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, rv); + } + nni_mtx_unlock(&l->mtx); +} + +static void +fdc_listener_accept(void *arg, nng_aio *aio) +{ + fdc_listener *l = arg; + + if (nni_aio_begin(aio) != 0) { + return; + } + nni_mtx_lock(&l->mtx); + if (l->closed) { + nni_mtx_unlock(&l->mtx); + nni_aio_finish_error(aio, NNG_ECLOSED); + return; + } + + if (l->listen_cnt) { + fdc_start_conn(l, aio); + } else { + nni_aio_schedule(aio, fdc_cancel_accept, l); + nni_aio_list_append(&l->accept_q, aio); + } + nni_mtx_unlock(&l->mtx); +} + +static int +fdc_listener_set_fd(void *arg, const void *buf, size_t sz, nni_type t) +{ + fdc_listener *l = arg; + nni_aio *aio; + int fd; + int rv; + + if ((rv = nni_copyin_int(&fd, buf, sz, NNI_MININT, NNI_MAXINT, t)) != + 0) { + return (rv); + } + + nni_mtx_lock(&l->mtx); + if (l->closed) { + nni_mtx_unlock(&l->mtx); + return (NNG_ECLOSED); + } + + if (l->listen_cnt == NNG_FDC_LISTEN_QUEUE) { + nni_mtx_unlock(&l->mtx); + return (NNG_ENOSPC); + } + + l->listen_q[l->listen_cnt++] = fd; + + // if someone was waiting in accept, give it to them now + if ((aio = nni_list_first(&l->accept_q)) != NULL) { + nni_aio_list_remove(aio); + fdc_start_conn(l, aio); + } + + nni_mtx_unlock(&l->mtx); + return (0); +} + +static const nni_option fdc_listener_options[] = { + { + .o_name = NNG_OPT_FDC_FD, + .o_set = fdc_listener_set_fd, + }, + { + .o_name = NULL, + }, +}; + +int +fdc_listener_get( + void *arg, const char *name, void *buf, size_t *szp, nni_type t) +{ + fdc_listener *l = arg; + return (nni_getopt(fdc_listener_options, name, l, buf, szp, t)); +} + +int +fdc_listener_set( + void *arg, const char *name, const void *buf, size_t sz, nni_type t) +{ + fdc_listener *l = arg; + return (nni_setopt(fdc_listener_options, name, l, buf, sz, t)); +} + +int +nni_fdc_listener_alloc(nng_stream_listener **lp, const nng_url *url) +{ + fdc_listener *l; + + NNI_ARG_UNUSED(url); + + if ((l = NNI_ALLOC_STRUCT(l)) == NULL) { + return (NNG_ENOMEM); + } + memset(l->listen_q, 0, sizeof(l->listen_q)); + l->listen_cnt = 0; + nni_aio_list_init(&l->accept_q); + nni_mtx_init(&l->mtx); + + l->ops.sl_free = fdc_listener_free; + l->ops.sl_close = fdc_listener_close; + l->ops.sl_listen = fdc_listener_listen; + l->ops.sl_accept = fdc_listener_accept; + l->ops.sl_get = fdc_listener_get; + l->ops.sl_set = fdc_listener_set; + + *lp = (void *) l; + return (0); +} diff --git a/src/core/fdconn.h b/src/core/fdconn.h new file mode 100644 index 000000000..3e5c49648 --- /dev/null +++ b/src/core/fdconn.h @@ -0,0 +1,28 @@ +// +// Copyright 2023 Staysail Systems, Inc. +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#ifndef CORE_FDC_H +#define CORE_FDC_H + +#include "core/nng_impl.h" + +#define NNG_OPT_FDC_FD "fdc:fd" + +// the nni_fdc_conn struct is provided by platform code to wrap +// an arbitrary byte stream file descriptor (UNIX) or handle (Windows) +// with a nng_stream. +typedef struct nni_fdc_conn nni_fdc_conn; +extern int nni_fdc_conn_alloc(nni_fdc_conn **cp, int fd); + +// this is used to close a file descriptor, in case we cannot +// create a connection (or if the listener is closed before the +// connection is accepted.) +extern int nni_fdc_close_fd(int fd); + +#endif // CORE_FDC_H diff --git a/src/platform/posix/CMakeLists.txt b/src/platform/posix/CMakeLists.txt index dcfea2211..793c1a97d 100644 --- a/src/platform/posix/CMakeLists.txt +++ b/src/platform/posix/CMakeLists.txt @@ -75,6 +75,7 @@ if (NNG_PLATFORM_POSIX) posix_clock.c posix_debug.c posix_file.c + posix_fdconn.c posix_ipcconn.c posix_ipcdial.c posix_ipclisten.c diff --git a/src/platform/posix/posix_fdconn.c b/src/platform/posix/posix_fdconn.c new file mode 100644 index 000000000..0f93da8ed --- /dev/null +++ b/src/platform/posix/posix_fdconn.c @@ -0,0 +1,382 @@ +// +// Copyright 2023 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV +// Copyright 2019 Devolutions +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "core/nng_impl.h" + +#include +#include +#include +#include + +#include "core/fdconn.h" +#include "platform/posix/posix_aio.h" + +struct nni_fdc_conn { + nng_stream stream; + nni_posix_pfd * pfd; + nni_list readq; + nni_list writeq; + bool closed; + nni_mtx mtx; + nni_reap_node reap; +}; + +static void +fdc_dowrite(nni_fdc_conn *c) +{ + nni_aio *aio; + int fd; + + if (c->closed || ((fd = nni_posix_pfd_fd(c->pfd)) < 0)) { + return; + } + + while ((aio = nni_list_first(&c->writeq)) != NULL) { + unsigned i; + int n; + int niov; + unsigned naiov; + nni_iov * aiov; + struct iovec iovec[16]; + + nni_aio_get_iov(aio, &naiov, &aiov); + + if (naiov > NNI_NUM_ELEMENTS(iovec)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, NNG_EINVAL); + continue; + } + + for (niov = 0, i = 0; i < naiov; i++) { + if (aiov[i].iov_len > 0) { + iovec[niov].iov_len = aiov[i].iov_len; + iovec[niov].iov_base = aiov[i].iov_buf; + niov++; + } + } + + if ((n = writev(fd, iovec, niov)) < 0) { + switch (errno) { + case EINTR: + continue; + case EAGAIN: +#ifdef EWOULDBLOCK +#if EWOULDBLOCK != EAGAIN + case EWOULDBLOCK: +#endif +#endif + return; + default: + nni_aio_list_remove(aio); + nni_aio_finish_error( + aio, nni_plat_errno(errno)); + return; + } + } + + // If we didn't send all the data, the caller will + // resubmit. As a corollary, callers should probably + // only have one message on the write queue at a time. + nni_aio_bump_count(aio, n); + nni_aio_list_remove(aio); + nni_aio_finish(aio, 0, nni_aio_count(aio)); + + // Go back to start of loop to see if there is another + // aio ready for us to process. + } +} + +static void +fdc_doread(nni_fdc_conn *c) +{ + nni_aio *aio; + int fd; + + if (c->closed || ((fd = nni_posix_pfd_fd(c->pfd)) < 0)) { + return; + } + + while ((aio = nni_list_first(&c->readq)) != NULL) { + unsigned i; + int n; + int niov; + unsigned naiov; + nni_iov * aiov; + struct iovec iovec[16]; + + nni_aio_get_iov(aio, &naiov, &aiov); + if (naiov > NNI_NUM_ELEMENTS(iovec)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, NNG_EINVAL); + continue; + } + for (niov = 0, i = 0; i < naiov; i++) { + if (aiov[i].iov_len != 0) { + iovec[niov].iov_len = aiov[i].iov_len; + iovec[niov].iov_base = aiov[i].iov_buf; + niov++; + } + } + + if ((n = readv(fd, iovec, niov)) < 0) { + switch (errno) { + case EINTR: + continue; + case EAGAIN: + return; + default: + nni_aio_list_remove(aio); + nni_aio_finish_error( + aio, nni_plat_errno(errno)); + return; + } + } + + if (n == 0) { + // Zero indicates a closed descriptor. + // This implicitly completes this (all!) aio. + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, NNG_ECONNSHUT); + continue; + } + + nni_aio_bump_count(aio, n); + + // We completed the entire operation on this aio. + nni_aio_list_remove(aio); + nni_aio_finish(aio, 0, nni_aio_count(aio)); + + // Go back to start of loop to see if there is another + // aio ready for us to process. + } +} + +static void +fdc_error(void *arg, int err) +{ + nni_fdc_conn *c = arg; + nni_aio * aio; + + nni_mtx_lock(&c->mtx); + while (((aio = nni_list_first(&c->readq)) != NULL) || + ((aio = nni_list_first(&c->writeq)) != NULL)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, err); + } + if (c->pfd != NULL) { + nni_posix_pfd_close(c->pfd); + } + nni_mtx_unlock(&c->mtx); +} + +static void +fdc_close(void *arg) +{ + nni_fdc_conn *c = arg; + nni_mtx_lock(&c->mtx); + if (!c->closed) { + nni_aio *aio; + c->closed = true; + while (((aio = nni_list_first(&c->readq)) != NULL) || + ((aio = nni_list_first(&c->writeq)) != NULL)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, NNG_ECLOSED); + } + if (c->pfd != NULL) { + nni_posix_pfd_close(c->pfd); + } + } + nni_mtx_unlock(&c->mtx); +} + +// fdc_fini may block briefly waiting for the pollq thread. +// To get that out of our context, we simply reap this. +static void +fdc_fini(void *arg) +{ + nni_fdc_conn *c = arg; + fdc_close(c); + if (c->pfd != NULL) { + nni_posix_pfd_fini(c->pfd); + } + nni_mtx_fini(&c->mtx); + + NNI_FREE_STRUCT(c); +} + +static nni_reap_list fdc_reap_list = { + .rl_offset = offsetof(nni_fdc_conn , reap), + .rl_func = fdc_fini, +}; +static void +fdc_free(void *arg) +{ + struct nni_fdc_conn *c = arg; + nni_reap(&fdc_reap_list, c); +} + +static void +fdc_cb(nni_posix_pfd *pfd, unsigned events, void *arg) +{ + struct nni_fdc_conn *c = arg; + + if (events & (NNI_POLL_HUP | NNI_POLL_ERR | NNI_POLL_INVAL)) { + fdc_error(c, NNG_ECONNSHUT); + return; + } + nni_mtx_lock(&c->mtx); + if ((events & NNI_POLL_IN) != 0) { + fdc_doread(c); + } + if ((events & NNI_POLL_OUT) != 0) { + fdc_dowrite(c); + } + events = 0; + if (!nni_list_empty(&c->writeq)) { + events |= NNI_POLL_OUT; + } + if (!nni_list_empty(&c->readq)) { + events |= NNI_POLL_IN; + } + if ((!c->closed) && (events != 0)) { + nni_posix_pfd_arm(pfd, events); + } + nni_mtx_unlock(&c->mtx); +} + +static void +fdc_cancel(nni_aio *aio, void *arg, int rv) +{ + nni_fdc_conn *c = arg; + + nni_mtx_lock(&c->mtx); + if (nni_aio_list_active(aio)) { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, rv); + } + nni_mtx_unlock(&c->mtx); +} + +static void +fdc_send(void *arg, nni_aio *aio) +{ + nni_fdc_conn *c = arg; + int rv; + + if (nni_aio_begin(aio) != 0) { + return; + } + nni_mtx_lock(&c->mtx); + + if ((rv = nni_aio_schedule(aio, fdc_cancel, c)) != 0) { + nni_mtx_unlock(&c->mtx); + nni_aio_finish_error(aio, rv); + return; + } + nni_aio_list_append(&c->writeq, aio); + + if (nni_list_first(&c->writeq) == aio) { + fdc_dowrite(c); + // If we are still the first thing on the list, that + // means we didn't finish the job, so arm the poller to + // complete us. + if (nni_list_first(&c->writeq) == aio) { + nni_posix_pfd_arm(c->pfd, POLLOUT); + } + } + nni_mtx_unlock(&c->mtx); +} + +static void +fdc_recv(void *arg, nni_aio *aio) +{ + nni_fdc_conn *c = arg; + int rv; + + if (nni_aio_begin(aio) != 0) { + return; + } + nni_mtx_lock(&c->mtx); + + if ((rv = nni_aio_schedule(aio, fdc_cancel, c)) != 0) { + nni_mtx_unlock(&c->mtx); + nni_aio_finish_error(aio, rv); + return; + } + nni_aio_list_append(&c->readq, aio); + + // If we are only job on the list, go ahead and try to do an + // immediate transfer. This allows for faster completions in + // many cases. We also need not arm a list if it was already + // armed. + if (nni_list_first(&c->readq) == aio) { + fdc_doread(c); + // If we are still the first thing on the list, that + // means we didn't finish the job, so arm the poller to + // complete us. + if (nni_list_first(&c->readq) == aio) { + nni_posix_pfd_arm(c->pfd, POLLIN); + } + } + nni_mtx_unlock(&c->mtx); +} + +static const nni_option fdc_options[] = { + { + .o_name = NULL, + }, +}; + +static int +fdc_get(void *arg, const char *name, void *buf, size_t *szp, nni_type t) +{ + nni_fdc_conn *c = arg; + return (nni_getopt(fdc_options, name, c, buf, szp, t)); +} + +static int +fdc_set(void *arg, const char *name, const void *buf, size_t sz, nni_type t) +{ + nni_fdc_conn *c = arg; + return (nni_setopt(fdc_options, name, c, buf, sz, t)); +} + +int +nni_fdc_conn_alloc(nni_fdc_conn **cp, int fd) +{ + nni_fdc_conn *c; + int rv; + if ((c = NNI_ALLOC_STRUCT(c)) == NULL) { + return (NNG_ENOMEM); + } + if ((rv = nni_posix_pfd_init(&c->pfd, fd)) != 0) { + NNI_FREE_STRUCT(c); + return (rv); + } + + c->closed = false; + + nni_mtx_init(&c->mtx); + nni_aio_list_init(&c->readq); + nni_aio_list_init(&c->writeq); + + c->stream.s_free = fdc_free; + c->stream.s_close = fdc_close; + c->stream.s_recv = fdc_recv; + c->stream.s_send = fdc_send; + c->stream.s_get = fdc_get; + c->stream.s_set = fdc_set; + + nni_posix_pfd_set_cb(c->pfd, fdc_cb, c); + + *cp = c; + return (0); +} diff --git a/src/sp/transport/CMakeLists.txt b/src/sp/transport/CMakeLists.txt index add8a9c95..95d67101f 100644 --- a/src/sp/transport/CMakeLists.txt +++ b/src/sp/transport/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright 2020 Staysail Systems, Inc. +# Copyright 2023 Staysail Systems, Inc. # # This software is supplied under the terms of the MIT License, a # copy of which should be located in the distribution where this @@ -10,6 +10,7 @@ # Transports. nng_directory(transport) +add_subdirectory(fdconn) add_subdirectory(inproc) add_subdirectory(ipc) add_subdirectory(tcp) diff --git a/src/sp/transport/fdconn/CMakeLists.txt b/src/sp/transport/fdconn/CMakeLists.txt new file mode 100644 index 000000000..dbeb3abb9 --- /dev/null +++ b/src/sp/transport/fdconn/CMakeLists.txt @@ -0,0 +1,16 @@ +# +# Copyright 2023 Staysail Systems, Inc. +# +# This software is supplied under the terms of the MIT License, a +# copy of which should be located in the distribution where this +# file was obtained (LICENSE.txt). A copy of the license may also be +# found online at https://opensource.org/licenses/MIT. +# + +# File Descriptor (or Handle) based connections +nng_directory(fdconn) + +nng_sources(fdconn.c) +# nng_headers_if(NNG_TRANSPORT_FDCONN nng/transport/fdconn/fdconn.h) +nng_defines(NNG_TRANSPORT_FDCONN) +nng_test(fdc_test) \ No newline at end of file diff --git a/src/sp/transport/fdconn/fdc_test.c b/src/sp/transport/fdconn/fdc_test.c new file mode 100644 index 000000000..daccf89e0 --- /dev/null +++ b/src/sp/transport/fdconn/fdc_test.c @@ -0,0 +1,112 @@ +// +// Copyright 2020 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV +// Copyright 2018 Devolutions +// Copyright 2018 Cody Piersall +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + + +#include + +// FDC tests. +static void +test_fdc_connect_fail(void) +{ + nng_socket s; + + NUTS_OPEN(s); + NUTS_FAIL(nng_dial(s, "fdconn://", NULL, 0), NNG_ENOTSUP); + NUTS_CLOSE(s); +} + +void +test_tcp_local_address_connect(void) +{ + + nng_socket s1; + nng_socket s2; + char addr[NNG_MAXADDRLEN]; + uint16_t port; + + NUTS_OPEN(s1); + NUTS_OPEN(s2); + port = nuts_next_port(); + (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); + NUTS_PASS(nng_listen(s1, addr, NULL, 0)); + (void) snprintf( + addr, sizeof(addr), "tcp://127.0.0.1;127.0.0.1:%u", port); + NUTS_PASS(nng_dial(s2, addr, NULL, 0)); + NUTS_CLOSE(s2); + NUTS_CLOSE(s1); +} + +void +test_tcp_non_local_address(void) +{ + nng_socket s1; + + NUTS_OPEN(s1); + NUTS_FAIL(nng_dial(s1, "tcp://8.8.8.8;127.0.0.1:80", NULL, 0), + NNG_EADDRINVAL); + NUTS_CLOSE(s1); +} + +void +test_fdc_malformed_address(void) +{ + nng_socket s1; + + NUTS_OPEN(s1); + NUTS_FAIL( + nng_dial(s1, "fdconn://junk", NULL, 0), NNG_EADDRINVAL); + NUTS_CLOSE(s1); +} + +void +test_tcp_recv_max(void) +{ + char msg[256]; + char buf[256]; + nng_socket s0; + nng_socket s1; + nng_listener l; + size_t sz; + char *addr; + + NUTS_ADDR(addr, "tcp"); + + NUTS_OPEN(s0); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_size(s0, NNG_OPT_RECVMAXSZ, 200)); + NUTS_PASS(nng_listener_create(&l, s0, addr)); + NUTS_PASS(nng_socket_get_size(s0, NNG_OPT_RECVMAXSZ, &sz)); + NUTS_TRUE(sz == 200); + NUTS_PASS(nng_listener_set_size(l, NNG_OPT_RECVMAXSZ, 100)); + NUTS_PASS(nng_listener_start(l, 0)); + + NUTS_OPEN(s1); + NUTS_PASS(nng_dial(s1, addr, NULL, 0)); + NUTS_PASS(nng_send(s1, msg, 95, 0)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_SENDTIMEO, 100)); + NUTS_PASS(nng_recv(s0, buf, &sz, 0)); + NUTS_TRUE(sz == 95); + NUTS_PASS(nng_send(s1, msg, 150, 0)); + NUTS_FAIL(nng_recv(s0, buf, &sz, 0), NNG_ETIMEDOUT); + NUTS_PASS(nng_close(s0)); + NUTS_CLOSE(s1); +} + +NUTS_TESTS = { + + { "tcp fdc connect fail", test_fdc_connect_fail }, + { "tcp local address connect", test_tcp_local_address_connect }, + // { "tcp non-local address", test_tcp_non_local_address }, + { "fdc malformed address", test_fdc_malformed_address }, + // { "fdc recv max", test_tcp_recv_max }, + { NULL, NULL }, +}; \ No newline at end of file diff --git a/src/sp/transport/fdconn/fdconn.c b/src/sp/transport/fdconn/fdconn.c new file mode 100644 index 000000000..1c4c70333 --- /dev/null +++ b/src/sp/transport/fdconn/fdconn.c @@ -0,0 +1,1019 @@ +// +// Copyright 2023 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV +// Copyright 2019 Devolutions +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include +#include + +#include "core/nng_impl.h" + +// TCP transport. Platform specific TCP operations must be +// supplied as well. + +typedef struct fdc_tran_pipe fdc_tran_pipe; +typedef struct fdc_tran_ep fdc_tran_ep; + +// fdc_tran_pipe wraps an open file descriptor +struct fdc_tran_pipe { + nng_stream *conn; + nni_pipe *npipe; + uint16_t peer; + uint16_t proto; + size_t rcvmax; + bool closed; + nni_list_node node; + fdc_tran_ep *ep; + nni_atomic_flag reaped; + nni_reap_node reap; + uint8_t txlen[sizeof(uint64_t)]; + uint8_t rxlen[sizeof(uint64_t)]; + size_t gottxhead; + size_t gotrxhead; + size_t wanttxhead; + size_t wantrxhead; + nni_list recvq; + nni_list sendq; + nni_aio *txaio; + nni_aio *rxaio; + nni_aio *negoaio; + nni_msg *rxmsg; + nni_mtx mtx; +}; + +struct fdc_tran_ep { + nni_mtx mtx; + uint16_t proto; + size_t rcvmax; + bool fini; + bool started; + bool closed; + nng_sockaddr src; + int refcnt; // active pipes + nni_aio *useraio; + nni_aio *connaio; + nni_aio *timeaio; + nni_list busypipes; // busy pipes -- ones passed to socket + nni_list waitpipes; // pipes waiting to match to socket + nni_list negopipes; // pipes busy negotiating + nni_reap_node reap; + nng_stream_listener *listener; + +#ifdef NNG_ENABLE_STATS + nni_stat_item st_rcv_max; +#endif +}; + +static void fdc_tran_pipe_send_start(fdc_tran_pipe *); +static void fdc_tran_pipe_recv_start(fdc_tran_pipe *); +static void fdc_tran_pipe_send_cb(void *); +static void fdc_tran_pipe_recv_cb(void *); +static void fdc_tran_pipe_nego_cb(void *); +static void fdc_tran_ep_fini(void *); +static void fdc_tran_pipe_fini(void *); + +static nni_reap_list fdc_tran_ep_reap_list = { + .rl_offset = offsetof(fdc_tran_ep, reap), + .rl_func = fdc_tran_ep_fini, +}; + +static nni_reap_list fdc_tran_pipe_reap_list = { + .rl_offset = offsetof(fdc_tran_pipe, reap), + .rl_func = fdc_tran_pipe_fini, +}; + +static void +fdc_tran_init(void) +{ +} + +static void +fdc_tran_fini(void) +{ +} + +static void +fdc_tran_pipe_close(void *arg) +{ + fdc_tran_pipe *p = arg; + + nni_mtx_lock(&p->mtx); + p->closed = true; + nni_mtx_unlock(&p->mtx); + + nni_aio_close(p->rxaio); + nni_aio_close(p->txaio); + nni_aio_close(p->negoaio); + + nng_stream_close(p->conn); +} + +static void +fdc_tran_pipe_stop(void *arg) +{ + fdc_tran_pipe *p = arg; + + nni_aio_stop(p->rxaio); + nni_aio_stop(p->txaio); + nni_aio_stop(p->negoaio); +} + +static int +fdc_tran_pipe_init(void *arg, nni_pipe *npipe) +{ + fdc_tran_pipe *p = arg; + p->npipe = npipe; + + return (0); +} + +static void +fdc_tran_pipe_fini(void *arg) +{ + fdc_tran_pipe *p = arg; + fdc_tran_ep *ep; + + fdc_tran_pipe_stop(p); + if ((ep = p->ep) != NULL) { + nni_mtx_lock(&ep->mtx); + nni_list_node_remove(&p->node); + ep->refcnt--; + if (ep->fini && (ep->refcnt == 0)) { + nni_reap(&fdc_tran_ep_reap_list, ep); + } + nni_mtx_unlock(&ep->mtx); + } + + nni_aio_free(p->rxaio); + nni_aio_free(p->txaio); + nni_aio_free(p->negoaio); + nng_stream_free(p->conn); + nni_msg_free(p->rxmsg); + nni_mtx_fini(&p->mtx); + NNI_FREE_STRUCT(p); +} + +static void +fdc_tran_pipe_reap(fdc_tran_pipe *p) +{ + if (!nni_atomic_flag_test_and_set(&p->reaped)) { + if (p->conn != NULL) { + nng_stream_close(p->conn); + } + nni_reap(&fdc_tran_pipe_reap_list, p); + } +} + +static int +fdc_tran_pipe_alloc(fdc_tran_pipe **pipep) +{ + fdc_tran_pipe *p; + int rv; + + if ((p = NNI_ALLOC_STRUCT(p)) == NULL) { + return (NNG_ENOMEM); + } + nni_mtx_init(&p->mtx); + if (((rv = nni_aio_alloc(&p->txaio, fdc_tran_pipe_send_cb, p)) != 0) || + ((rv = nni_aio_alloc(&p->rxaio, fdc_tran_pipe_recv_cb, p)) != 0) || + ((rv = nni_aio_alloc(&p->negoaio, fdc_tran_pipe_nego_cb, p)) != + 0)) { + fdc_tran_pipe_fini(p); + return (rv); + } + nni_aio_list_init(&p->recvq); + nni_aio_list_init(&p->sendq); + nni_atomic_flag_reset(&p->reaped); + + *pipep = p; + + return (0); +} + +static void +fdc_tran_ep_match(fdc_tran_ep *ep) +{ + nni_aio *aio; + fdc_tran_pipe *p; + + if (((aio = ep->useraio) == NULL) || + ((p = nni_list_first(&ep->waitpipes)) == NULL)) { + return; + } + nni_list_remove(&ep->waitpipes, p); + nni_list_append(&ep->busypipes, p); + ep->useraio = NULL; + p->rcvmax = ep->rcvmax; + nni_aio_set_output(aio, 0, p); + nni_aio_finish(aio, 0, 0); +} + +static void +fdc_tran_pipe_nego_cb(void *arg) +{ + fdc_tran_pipe *p = arg; + fdc_tran_ep *ep = p->ep; + nni_aio *aio = p->negoaio; + nni_aio *uaio; + int rv; + + nni_mtx_lock(&ep->mtx); + + if ((rv = nni_aio_result(aio)) != 0) { + goto error; + } + + // We start transmitting before we receive. + if (p->gottxhead < p->wanttxhead) { + p->gottxhead += nni_aio_count(aio); + } else if (p->gotrxhead < p->wantrxhead) { + p->gotrxhead += nni_aio_count(aio); + } + + if (p->gottxhead < p->wanttxhead) { + nni_iov iov; + iov.iov_len = p->wanttxhead - p->gottxhead; + iov.iov_buf = &p->txlen[p->gottxhead]; + // send it down... + nni_aio_set_iov(aio, 1, &iov); + nng_stream_send(p->conn, aio); + nni_mtx_unlock(&ep->mtx); + return; + } + if (p->gotrxhead < p->wantrxhead) { + nni_iov iov; + iov.iov_len = p->wantrxhead - p->gotrxhead; + iov.iov_buf = &p->rxlen[p->gotrxhead]; + nni_aio_set_iov(aio, 1, &iov); + nng_stream_recv(p->conn, aio); + nni_mtx_unlock(&ep->mtx); + return; + } + // We have both sent and received the headers. Let's check the + // receiver. + if ((p->rxlen[0] != 0) || (p->rxlen[1] != 'S') || + (p->rxlen[2] != 'P') || (p->rxlen[3] != 0) || (p->rxlen[6] != 0) || + (p->rxlen[7] != 0)) { + rv = NNG_EPROTO; + goto error; + } + + NNI_GET16(&p->rxlen[4], p->peer); + + // We are ready now. We put this in the wait list, and + // then try to run the matcher. + nni_list_remove(&ep->negopipes, p); + nni_list_append(&ep->waitpipes, p); + + fdc_tran_ep_match(ep); + nni_mtx_unlock(&ep->mtx); + + return; + +error: + // If the connection is closed, we need to pass back a different + // error code. This is necessary to avoid a problem where the + // closed status is confused with the accept file descriptor + // being closed. + if (rv == NNG_ECLOSED) { + rv = NNG_ECONNSHUT; + } + nng_stream_close(p->conn); + + if ((uaio = ep->useraio) != NULL) { + ep->useraio = NULL; + nni_aio_finish_error(uaio, rv); + } + nni_mtx_unlock(&ep->mtx); + fdc_tran_pipe_reap(p); +} + +static void +fdc_tran_pipe_send_cb(void *arg) +{ + fdc_tran_pipe *p = arg; + int rv; + nni_aio *aio; + size_t n; + nni_msg *msg; + nni_aio *txaio = p->txaio; + + nni_mtx_lock(&p->mtx); + aio = nni_list_first(&p->sendq); + + if ((rv = nni_aio_result(txaio)) != 0) { + nni_pipe_bump_error(p->npipe, rv); + // Intentionally we do not queue up another transfer. + // There's an excellent chance that the pipe is no longer + // usable, with a partial transfer. + // The protocol should see this error, and close the + // pipe itself, we hope. + nni_aio_list_remove(aio); + nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); + return; + } + + n = nni_aio_count(txaio); + nni_aio_iov_advance(txaio, n); + if (nni_aio_iov_count(txaio) > 0) { + nng_stream_send(p->conn, txaio); + nni_mtx_unlock(&p->mtx); + return; + } + + nni_aio_list_remove(aio); + fdc_tran_pipe_send_start(p); + + msg = nni_aio_get_msg(aio); + n = nni_msg_len(msg); + nni_pipe_bump_tx(p->npipe, n); + nni_mtx_unlock(&p->mtx); + + nni_aio_set_msg(aio, NULL); + nni_msg_free(msg); + nni_aio_finish_sync(aio, 0, n); +} + +static void +fdc_tran_pipe_recv_cb(void *arg) +{ + fdc_tran_pipe *p = arg; + nni_aio *aio; + int rv; + size_t n; + nni_msg *msg; + nni_aio *rxaio = p->rxaio; + + nni_mtx_lock(&p->mtx); + aio = nni_list_first(&p->recvq); + + if ((rv = nni_aio_result(rxaio)) != 0) { + goto recv_error; + } + + if (p->closed) { + rv = NNG_ECLOSED; + goto recv_error; + } + + n = nni_aio_count(rxaio); + nni_aio_iov_advance(rxaio, n); + if (nni_aio_iov_count(rxaio) > 0) { + nng_stream_recv(p->conn, rxaio); + nni_mtx_unlock(&p->mtx); + return; + } + + // If we don't have a message yet, we were reading the message + // header, which is just the length. This tells us the size of the + // message to allocate and how much more to expect. + if (p->rxmsg == NULL) { + uint64_t len; + // We should have gotten a message header. + NNI_GET64(p->rxlen, len); + + // Make sure the message payload is not too big. If it is + // the caller will shut down the pipe. + if ((len > p->rcvmax) && (p->rcvmax > 0)) { + rv = NNG_EMSGSIZE; + goto recv_error; + } + + if ((rv = nni_msg_alloc(&p->rxmsg, (size_t) len)) != 0) { + goto recv_error; + } + + // Submit the rest of the data for a read -- we want to + // read the entire message now. + if (len != 0) { + nni_iov iov; + iov.iov_buf = nni_msg_body(p->rxmsg); + iov.iov_len = (size_t) len; + + nni_aio_set_iov(rxaio, 1, &iov); + nng_stream_recv(p->conn, rxaio); + nni_mtx_unlock(&p->mtx); + return; + } + } + + // We read a message completely. Let the user know the good news. + nni_aio_list_remove(aio); + msg = p->rxmsg; + p->rxmsg = NULL; + n = nni_msg_len(msg); + + nni_pipe_bump_rx(p->npipe, n); + fdc_tran_pipe_recv_start(p); + nni_mtx_unlock(&p->mtx); + + nni_aio_set_msg(aio, msg); + nni_aio_finish_sync(aio, 0, n); + return; + +recv_error: + nni_aio_list_remove(aio); + msg = p->rxmsg; + p->rxmsg = NULL; + nni_pipe_bump_error(p->npipe, rv); + // Intentionally, we do not queue up another receive. + // The protocol should notice this error and close the pipe. + nni_mtx_unlock(&p->mtx); + + nni_msg_free(msg); + nni_aio_finish_error(aio, rv); +} + +static void +fdc_tran_pipe_send_cancel(nni_aio *aio, void *arg, int rv) +{ + fdc_tran_pipe *p = arg; + + nni_mtx_lock(&p->mtx); + if (!nni_aio_list_active(aio)) { + nni_mtx_unlock(&p->mtx); + return; + } + // If this is being sent, then cancel the pending transfer. + // The callback on the txaio will cause the user aio to + // be canceled too. + if (nni_list_first(&p->sendq) == aio) { + nni_aio_abort(p->txaio, rv); + nni_mtx_unlock(&p->mtx); + return; + } + nni_aio_list_remove(aio); + nni_mtx_unlock(&p->mtx); + + nni_aio_finish_error(aio, rv); +} + +static void +fdc_tran_pipe_send_start(fdc_tran_pipe *p) +{ + nni_aio *aio; + nni_aio *txaio; + nni_msg *msg; + int niov; + nni_iov iov[3]; + uint64_t len; + + if (p->closed) { + while ((aio = nni_list_first(&p->sendq)) != NULL) { + nni_list_remove(&p->sendq, aio); + nni_aio_finish_error(aio, NNG_ECLOSED); + } + return; + } + + if ((aio = nni_list_first(&p->sendq)) == NULL) { + return; + } + + // This runs to send the message. + msg = nni_aio_get_msg(aio); + len = nni_msg_len(msg) + nni_msg_header_len(msg); + + NNI_PUT64(p->txlen, len); + + txaio = p->txaio; + niov = 0; + iov[0].iov_buf = p->txlen; + iov[0].iov_len = sizeof(p->txlen); + niov++; + if (nni_msg_header_len(msg) > 0) { + iov[niov].iov_buf = nni_msg_header(msg); + iov[niov].iov_len = nni_msg_header_len(msg); + niov++; + } + if (nni_msg_len(msg) > 0) { + iov[niov].iov_buf = nni_msg_body(msg); + iov[niov].iov_len = nni_msg_len(msg); + niov++; + } + nni_aio_set_iov(txaio, niov, iov); + nng_stream_send(p->conn, txaio); +} + +static void +fdc_tran_pipe_send(void *arg, nni_aio *aio) +{ + fdc_tran_pipe *p = arg; + int rv; + + if (nni_aio_begin(aio) != 0) { + // No way to give the message back to the protocol, so + // we just discard it silently to prevent it from leaking. + nni_msg_free(nni_aio_get_msg(aio)); + nni_aio_set_msg(aio, NULL); + return; + } + nni_mtx_lock(&p->mtx); + if ((rv = nni_aio_schedule(aio, fdc_tran_pipe_send_cancel, p)) != 0) { + nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); + return; + } + nni_list_append(&p->sendq, aio); + if (nni_list_first(&p->sendq) == aio) { + fdc_tran_pipe_send_start(p); + } + nni_mtx_unlock(&p->mtx); +} + +static void +fdc_tran_pipe_recv_cancel(nni_aio *aio, void *arg, int rv) +{ + fdc_tran_pipe *p = arg; + + nni_mtx_lock(&p->mtx); + if (!nni_aio_list_active(aio)) { + nni_mtx_unlock(&p->mtx); + return; + } + // If receive in progress, then cancel the pending transfer. + // The callback on the rxaio will cause the user aio to + // be canceled too. + if (nni_list_first(&p->recvq) == aio) { + nni_aio_abort(p->rxaio, rv); + nni_mtx_unlock(&p->mtx); + return; + } + nni_aio_list_remove(aio); + nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); +} + +static void +fdc_tran_pipe_recv_start(fdc_tran_pipe *p) +{ + nni_aio *rxaio; + nni_iov iov; + NNI_ASSERT(p->rxmsg == NULL); + + if (p->closed) { + nni_aio *aio; + while ((aio = nni_list_first(&p->recvq)) != NULL) { + nni_list_remove(&p->recvq, aio); + nni_aio_finish_error(aio, NNG_ECLOSED); + } + return; + } + if (nni_list_empty(&p->recvq)) { + return; + } + + // Schedule a read of the header. + rxaio = p->rxaio; + iov.iov_buf = p->rxlen; + iov.iov_len = sizeof(p->rxlen); + nni_aio_set_iov(rxaio, 1, &iov); + + nng_stream_recv(p->conn, rxaio); +} + +static void +fdc_tran_pipe_recv(void *arg, nni_aio *aio) +{ + fdc_tran_pipe *p = arg; + int rv; + + if (nni_aio_begin(aio) != 0) { + return; + } + nni_mtx_lock(&p->mtx); + if ((rv = nni_aio_schedule(aio, fdc_tran_pipe_recv_cancel, p)) != 0) { + nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); + return; + } + + nni_list_append(&p->recvq, aio); + if (nni_list_first(&p->recvq) == aio) { + fdc_tran_pipe_recv_start(p); + } + nni_mtx_unlock(&p->mtx); +} + +static uint16_t +fdc_tran_pipe_peer(void *arg) +{ + fdc_tran_pipe *p = arg; + + return (p->peer); +} + +static int +fdc_tran_pipe_getopt( + void *arg, const char *name, void *buf, size_t *szp, nni_type t) +{ + fdc_tran_pipe *p = arg; + return (nni_stream_get(p->conn, name, buf, szp, t)); +} + +static void +fdc_tran_pipe_start(fdc_tran_pipe *p, nng_stream *conn, fdc_tran_ep *ep) +{ + nni_iov iov; + + ep->refcnt++; + + p->conn = conn; + p->ep = ep; + p->proto = ep->proto; + + p->txlen[0] = 0; + p->txlen[1] = 'S'; + p->txlen[2] = 'P'; + p->txlen[3] = 0; + NNI_PUT16(&p->txlen[4], p->proto); + NNI_PUT16(&p->txlen[6], 0); + + p->gotrxhead = 0; + p->gottxhead = 0; + p->wantrxhead = 8; + p->wanttxhead = 8; + iov.iov_len = 8; + iov.iov_buf = &p->txlen[0]; + nni_aio_set_iov(p->negoaio, 1, &iov); + nni_list_append(&ep->negopipes, p); + + nni_aio_set_timeout(p->negoaio, 10000); // 10 sec timeout to negotiate + nng_stream_send(p->conn, p->negoaio); +} + +static void +fdc_tran_ep_fini(void *arg) +{ + fdc_tran_ep *ep = arg; + + nni_mtx_lock(&ep->mtx); + ep->fini = true; + if (ep->refcnt != 0) { + nni_mtx_unlock(&ep->mtx); + return; + } + nni_mtx_unlock(&ep->mtx); + nni_aio_stop(ep->timeaio); + nni_aio_stop(ep->connaio); + nng_stream_listener_free(ep->listener); + nni_aio_free(ep->timeaio); + nni_aio_free(ep->connaio); + + nni_mtx_fini(&ep->mtx); + NNI_FREE_STRUCT(ep); +} + +static void +fdc_tran_ep_close(void *arg) +{ + fdc_tran_ep *ep = arg; + fdc_tran_pipe *p; + + nni_mtx_lock(&ep->mtx); + + ep->closed = true; + nni_aio_close(ep->timeaio); + if (ep->listener != NULL) { + nng_stream_listener_close(ep->listener); + } + NNI_LIST_FOREACH (&ep->negopipes, p) { + fdc_tran_pipe_close(p); + } + NNI_LIST_FOREACH (&ep->waitpipes, p) { + fdc_tran_pipe_close(p); + } + NNI_LIST_FOREACH (&ep->busypipes, p) { + fdc_tran_pipe_close(p); + } + if (ep->useraio != NULL) { + nni_aio_finish_error(ep->useraio, NNG_ECLOSED); + ep->useraio = NULL; + } + + nni_mtx_unlock(&ep->mtx); +} + +static void +fdc_tran_timer_cb(void *arg) +{ + fdc_tran_ep *ep = arg; + if (nni_aio_result(ep->timeaio) == 0) { + nng_stream_listener_accept(ep->listener, ep->connaio); + } +} + +static void +fdc_tran_accept_cb(void *arg) +{ + fdc_tran_ep *ep = arg; + nni_aio *aio = ep->connaio; + fdc_tran_pipe *p; + int rv; + nng_stream *conn; + + nni_mtx_lock(&ep->mtx); + + if ((rv = nni_aio_result(aio)) != 0) { + goto error; + } + + conn = nni_aio_get_output(aio, 0); + if ((rv = fdc_tran_pipe_alloc(&p)) != 0) { + nng_stream_free(conn); + goto error; + } + + if (ep->closed) { + fdc_tran_pipe_fini(p); + nng_stream_free(conn); + rv = NNG_ECLOSED; + goto error; + } + fdc_tran_pipe_start(p, conn, ep); + nng_stream_listener_accept(ep->listener, ep->connaio); + nni_mtx_unlock(&ep->mtx); + return; + +error: + // When an error here occurs, let's send a notice up to the consumer. + // That way it can be reported properly. + if ((aio = ep->useraio) != NULL) { + ep->useraio = NULL; + nni_aio_finish_error(aio, rv); + } + switch (rv) { + + case NNG_ENOMEM: + case NNG_ENOFILES: + nng_sleep_aio(10, ep->timeaio); + break; + + default: + if (!ep->closed) { + nng_stream_listener_accept(ep->listener, ep->connaio); + } + break; + } + nni_mtx_unlock(&ep->mtx); +} + +static int +fdc_tran_ep_init(fdc_tran_ep **epp, nng_url *url, nni_sock *sock) +{ + fdc_tran_ep *ep; + NNI_ARG_UNUSED(url); + + if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { + return (NNG_ENOMEM); + } + nni_mtx_init(&ep->mtx); + NNI_LIST_INIT(&ep->busypipes, fdc_tran_pipe, node); + NNI_LIST_INIT(&ep->waitpipes, fdc_tran_pipe, node); + NNI_LIST_INIT(&ep->negopipes, fdc_tran_pipe, node); + + ep->proto = nni_sock_proto_id(sock); + +#ifdef NNG_ENABLE_STATS + static const nni_stat_info rcv_max_info = { + .si_name = "rcv_max", + .si_desc = "maximum receive size", + .si_type = NNG_STAT_LEVEL, + .si_unit = NNG_UNIT_BYTES, + .si_atomic = true, + }; + nni_stat_init(&ep->st_rcv_max, &rcv_max_info); +#endif + + *epp = ep; + return (0); +} + +static int +fdc_tran_dialer_init(void **dp, nng_url *url, nni_dialer *ndialer) +{ + NNI_ARG_UNUSED(dp); + NNI_ARG_UNUSED(url); + NNI_ARG_UNUSED(ndialer); + return (NNG_ENOTSUP); +} + +static int +fdc_tran_listener_init(void **lp, nng_url *url, nni_listener *nlistener) +{ + fdc_tran_ep *ep; + int rv; + nni_sock *sock = nni_listener_sock(nlistener); + + // Check for invalid URL components. + if ((strlen(url->u_path) != 0) && (strcmp(url->u_path, "/") != 0)) { + return (NNG_EADDRINVAL); + } + if ((url->u_fragment != NULL) || (url->u_userinfo != NULL) || + (url->u_query != NULL)) { + return (NNG_EADDRINVAL); + } + + if ((rv = fdc_tran_ep_init(&ep, url, sock)) != 0) { + return (rv); + } + + if (((rv = nni_aio_alloc(&ep->connaio, fdc_tran_accept_cb, ep)) != + 0) || + ((rv = nni_aio_alloc(&ep->timeaio, fdc_tran_timer_cb, ep)) != 0) || + ((rv = nng_stream_listener_alloc_url(&ep->listener, url)) != 0)) { + fdc_tran_ep_fini(ep); + return (rv); + } +#ifdef NNG_ENABLE_STATS + nni_listener_add_stat(nlistener, &ep->st_rcv_max); +#endif + + *lp = ep; + return (0); +} + +static void +fdc_tran_ep_cancel(nni_aio *aio, void *arg, int rv) +{ + fdc_tran_ep *ep = arg; + nni_mtx_lock(&ep->mtx); + if (ep->useraio == aio) { + ep->useraio = NULL; + nni_aio_finish_error(aio, rv); + } + nni_mtx_unlock(&ep->mtx); +} + +static int +fdc_tran_ep_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t) +{ + fdc_tran_ep *ep = arg; + int rv; + + nni_mtx_lock(&ep->mtx); + rv = nni_copyout_size(ep->rcvmax, v, szp, t); + nni_mtx_unlock(&ep->mtx); + return (rv); +} + +static int +fdc_tran_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) +{ + fdc_tran_ep *ep = arg; + size_t val; + int rv; + if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + fdc_tran_pipe *p; + nni_mtx_lock(&ep->mtx); + ep->rcvmax = val; + NNI_LIST_FOREACH (&ep->waitpipes, p) { + p->rcvmax = val; + } + NNI_LIST_FOREACH (&ep->negopipes, p) { + p->rcvmax = val; + } + NNI_LIST_FOREACH (&ep->busypipes, p) { + p->rcvmax = val; + } + nni_mtx_unlock(&ep->mtx); +#ifdef NNG_ENABLE_STATS + nni_stat_set_value(&ep->st_rcv_max, val); +#endif + } + return (rv); +} + +static int +fdc_tran_ep_bind(void *arg) +{ + NNI_ARG_UNUSED(arg); + return (0); +} + +static void +fdc_tran_ep_accept(void *arg, nni_aio *aio) +{ + fdc_tran_ep *ep = arg; + int rv; + + if (nni_aio_begin(aio) != 0) { + return; + } + nni_mtx_lock(&ep->mtx); + if (ep->closed) { + nni_mtx_unlock(&ep->mtx); + nni_aio_finish_error(aio, NNG_ECLOSED); + return; + } + if (ep->useraio != NULL) { + nni_mtx_unlock(&ep->mtx); + nni_aio_finish_error(aio, NNG_EBUSY); + return; + } + if ((rv = nni_aio_schedule(aio, fdc_tran_ep_cancel, ep)) != 0) { + nni_mtx_unlock(&ep->mtx); + nni_aio_finish_error(aio, rv); + return; + } + ep->useraio = aio; + if (!ep->started) { + ep->started = true; + nng_stream_listener_accept(ep->listener, ep->connaio); + } else { + fdc_tran_ep_match(ep); + } + nni_mtx_unlock(&ep->mtx); +} + +static nni_sp_pipe_ops fdc_tran_pipe_ops = { + .p_init = fdc_tran_pipe_init, + .p_fini = fdc_tran_pipe_fini, + .p_stop = fdc_tran_pipe_stop, + .p_send = fdc_tran_pipe_send, + .p_recv = fdc_tran_pipe_recv, + .p_close = fdc_tran_pipe_close, + .p_peer = fdc_tran_pipe_peer, + .p_getopt = fdc_tran_pipe_getopt, +}; + +static const nni_option fdc_tran_ep_opts[] = { + { + .o_name = NNG_OPT_RECVMAXSZ, + .o_get = fdc_tran_ep_get_recvmaxsz, + .o_set = fdc_tran_ep_set_recvmaxsz, + }, + // terminate list + { + .o_name = NULL, + }, +}; + +static int +fdc_tran_listener_getopt( + void *arg, const char *name, void *buf, size_t *szp, nni_type t) +{ + fdc_tran_ep *ep = arg; + int rv; + + rv = nni_stream_listener_get(ep->listener, name, buf, szp, t); + if (rv == NNG_ENOTSUP) { + rv = nni_getopt(fdc_tran_ep_opts, name, ep, buf, szp, t); + } + return (rv); +} + +static int +fdc_tran_listener_setopt( + void *arg, const char *name, const void *buf, size_t sz, nni_type t) +{ + fdc_tran_ep *ep = arg; + int rv; + + rv = nni_stream_listener_set(ep->listener, name, buf, sz, t); + if (rv == NNG_ENOTSUP) { + rv = nni_setopt(fdc_tran_ep_opts, name, ep, buf, sz, t); + } + return (rv); +} + +static nni_sp_dialer_ops fdc_tran_dialer_ops = { + .d_init = fdc_tran_dialer_init, + .d_fini = fdc_tran_ep_fini, + .d_connect = NULL, + .d_close = NULL, + .d_getopt = NULL, + .d_setopt = NULL, +}; + +static nni_sp_listener_ops fdc_tran_listener_ops = { + .l_init = fdc_tran_listener_init, + .l_fini = fdc_tran_ep_fini, + .l_bind = fdc_tran_ep_bind, + .l_accept = fdc_tran_ep_accept, + .l_close = fdc_tran_ep_close, + .l_getopt = fdc_tran_listener_getopt, + .l_setopt = fdc_tran_listener_setopt, +}; + +static nni_sp_tran fdc_tran = { + .tran_scheme = "fdconn", + .tran_dialer = &fdc_tran_dialer_ops, + .tran_listener = &fdc_tran_listener_ops, + .tran_pipe = &fdc_tran_pipe_ops, + .tran_init = fdc_tran_init, + .tran_fini = fdc_tran_fini, +}; + +void +nni_sp_fdc_register(void) +{ + nni_sp_tran_register(&fdc_tran); +}