diff options
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | docs/man/nng_compat.3compat.adoc | 17 | ||||
| -rw-r--r-- | src/compat/nanomsg/nn.c | 120 | ||||
| -rw-r--r-- | src/core/socket.c | 11 | ||||
| -rw-r--r-- | src/platform/posix/posix_epdesc.c | 10 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | tests/compat_tcp.c | 238 | ||||
| -rw-r--r-- | tests/tcp.c | 7 | ||||
| -rw-r--r-- | tests/tls.c | 7 |
9 files changed, 407 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bf08ca90..96eed693 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,6 +274,11 @@ if (CMAKE_SYSTEM_NAME MATCHES "Linux") add_definitions (-DNNG_PLATFORM_POSIX) add_definitions (-DNNG_PLATFORM_LINUX) add_definitions (-DNNG_USE_EVENTFD) + # Windows subsystem for Linux -- smells like Linux, but it has + # some differences (SO_REUSEADDR for one). + if (CMAKE_SYSTEM_VERSION MATCHES "Microsoft") + add_definitions (-DNNG_PLATFORM_WSL) + endif() set(NNG_PLATFORM_POSIX ON) elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin") diff --git a/docs/man/nng_compat.3compat.adoc b/docs/man/nng_compat.3compat.adoc index f8472c3c..148f09d1 100644 --- a/docs/man/nng_compat.3compat.adoc +++ b/docs/man/nng_compat.3compat.adoc @@ -190,6 +190,23 @@ The following caveats apply when using the legacy API with _nng_. Specifically, there is no `nn_symbol()` function yet. (This may be addressed later if there is a need.) +* The TCP transport (`tcp://` URLs) does not support specifying the local + address or interface when binding. (This could be fixed in the future, + but most likely this will be available only using the new API.) + +* The values of `NN_RCVMAXSIZE` are constrained. + Specifically, values set larger than 2GB using the new API will be reported + as unlimited (`-1`) in the new API, and the value `0` will disable any + enforcement, just like `-1`. + (There is no practical reason to ever want to limit the receive size to + zero.) + +* This implementation counts buffers in terms of messages rather than bytes. + As a result, the buffer sizes accessed with `NN_SNDBUF` and `NN_RCVBUF` are + rounded up to a whole number of kilobytes, then divided by 1024, in order + to approximate buffering assuming 1 KB messages. + Few applications should need to adjust the default values. + == SEE ALSO <<libnng.3#,libnng(3)>>, diff --git a/src/compat/nanomsg/nn.c b/src/compat/nanomsg/nn.c index 6755a8ea..564e60d8 100644 --- a/src/compat/nanomsg/nn.c +++ b/src/compat/nanomsg/nn.c @@ -64,7 +64,7 @@ static const struct { { NNG_ENOENT, ENOENT }, { NNG_EPROTO, EPROTO }, { NNG_EUNREACHABLE, EHOSTUNREACH }, - { NNG_EADDRINVAL, EADDRNOTAVAIL }, + { NNG_EADDRINVAL, EINVAL }, { NNG_EPERM, EACCES }, { NNG_EMSGSIZE, EMSGSIZE }, { NNG_ECONNABORTED, ECONNABORTED }, @@ -237,6 +237,8 @@ nn_socket(int domain, int protocol) return (-1); } + // Legacy sockets have nodelay disabled. + (void) nng_setopt_bool(sock, NNG_OPT_TCP_NODELAY, false); return ((int) sock.id); } @@ -747,6 +749,54 @@ nn_setignore(nng_socket s, const void *valp, size_t sz) } static int +nn_settcpnodelay(nng_socket s, const void *valp, size_t sz) +{ + bool val; + int ival; + int rv; + + if (sz != sizeof(ival)) { + errno = EINVAL; + return (-1); + } + memcpy(&ival, valp, sizeof(ival)); + switch (ival) { + case 0: + val = false; + break; + case 1: + val = true; + break; + default: + nn_seterror(NNG_EINVAL); + return (-1); + } + + if ((rv = nng_setopt_bool(s, NNG_OPT_TCP_NODELAY, val)) != 0) { + nn_seterror(rv); + return (-1); + } + return (0); +} + +static int +nn_gettcpnodelay(nng_socket s, void *valp, size_t *szp) +{ + bool val; + int ival; + int rv; + + if ((rv = nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &val)) != 0) { + nn_seterror(rv); + return (-1); + } + ival = val ? 1 : 0; + memcpy(valp, &ival, *szp < sizeof(ival) ? *szp : sizeof(ival)); + *szp = sizeof(ival); + return (0); +} + +static int nn_getrcvbuf(nng_socket s, void *valp, size_t *szp) { int cnt; @@ -769,7 +819,7 @@ nn_setrcvbuf(nng_socket s, const void *valp, size_t sz) int rv; if (sz != sizeof(cnt)) { - nn_seterror(NNG_EINVAL); + errno = EINVAL; return (-1); } memcpy(&cnt, valp, sizeof(cnt)); @@ -808,7 +858,7 @@ nn_setsndbuf(nng_socket s, const void *valp, size_t sz) int rv; if (sz != sizeof(cnt)) { - nn_seterror(NNG_EINVAL); + errno = EINVAL; return (-1); } memcpy(&cnt, valp, sizeof(cnt)); @@ -824,6 +874,61 @@ nn_setsndbuf(nng_socket s, const void *valp, size_t sz) return (0); } +static int +nn_setrcvmaxsz(nng_socket s, const void *valp, size_t sz) +{ + int ival; + size_t val; + int rv; + + if (sz != sizeof(ival)) { + errno = EINVAL; + return (-1); + } + memcpy(&ival, valp, sizeof(ival)); + if (ival == -1) { + val = 0; + } else if (ival >= 0) { + // Note that if the user sets 0, it disables the limit. + // This is a different semantic. + val = (size_t) ival; + } else { + errno = EINVAL; + return (-1); + } + if ((rv = nng_setopt_size(s, NNG_OPT_RECVMAXSZ, val)) != 0) { + nn_seterror(rv); + return (-1); + } + return (0); +} + +static int +nn_getrcvmaxsz(nng_socket s, void *valp, size_t *szp) +{ + int ival; + int rv; + size_t val; + + if ((rv = nng_getopt_size(s, NNG_OPT_RECVMAXSZ, &val)) != 0) { + nn_seterror(rv); + return (-1); + } + // Legacy uses -1 to mean unlimited. New code uses 0. Note that + // as a consequence, we can't set a message limit of zero. + // We report any size beyond 2GB as effectively unlimited. + // There is an implicit assumption here that ints are 32-bits, + // but that's generally true of any platform we support. + if ((val == 0) || (val > 0x7FFFFFFF)) { + ival = -1; + } else { + ival = (int) val; + } + memcpy(valp, &ival, *szp < sizeof(ival) ? *szp : sizeof(ival)); + *szp = sizeof(ival); + return (0); +} + // options which we convert -- most of the array is initialized at run time. static const struct { int nnlevel; @@ -881,7 +986,8 @@ static const struct { { .nnlevel = NN_SOL_SOCKET, .nnopt = NN_RCVMAXSIZE, - .opt = NNG_OPT_RECVMAXSZ, + .get = nn_getrcvmaxsz, + .set = nn_setrcvmaxsz, }, { .nnlevel = NN_SOL_SOCKET, @@ -928,6 +1034,12 @@ static const struct { .nnopt = NN_SURVEYOR_DEADLINE, .opt = NNG_OPT_SURVEYOR_SURVEYTIME, }, + { + .nnlevel = NN_TCP, + .nnopt = NN_TCP_NODELAY, + .get = nn_gettcpnodelay, + .set = nn_settcpnodelay, + } // XXX: IPV4ONLY, SNDPRIO, RCVPRIO }; diff --git a/src/core/socket.c b/src/core/socket.c index 3a209441..faca1b06 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -532,6 +532,7 @@ nni_sock_create(nni_sock **sp, const nni_proto *proto) { int rv; nni_sock *s; + bool on; if ((s = NNI_ALLOC_STRUCT(s)) == NULL) { return (NNG_ENOMEM); @@ -587,6 +588,16 @@ nni_sock_create(nni_sock **sp, const nni_proto *proto) return (rv); } + // These we *attempt* to call so that we are likely to have initial + // values loaded. They should not fail, but if they do we don't + // worry about it. + on = true; + (void) nni_sock_setopt( + s, NNG_OPT_TCP_NODELAY, &on, sizeof(on), NNI_TYPE_BOOL); + on = false; + (void) nni_sock_setopt( + s, NNG_OPT_TCP_KEEPALIVE, &on, sizeof(on), NNI_TYPE_BOOL); + if (s->s_sock_ops.sock_filter != NULL) { nni_msgq_set_filter( s->s_urq, s->s_sock_ops.sock_filter, s->s_data); diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 0065806d..dfb750d4 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -247,6 +247,16 @@ nni_posix_epdesc_listen(nni_posix_epdesc *ed) return (rv); } +#if defined(SO_REUSEADDR) && !defined(NNG_PLATFORM_WSL) + if (ss->ss_family != AF_UNIX) { + int on = 1; + // If for some reason this doesn't work, it's probably ok. + // Second bind will fail. + (void) setsockopt( + fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + } +#endif + if (bind(fd, (struct sockaddr *) ss, len) < 0) { rv = nni_plat_errno(errno); nni_mtx_unlock(&ed->mtx); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 03f09579..a0f54b5d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -190,12 +190,13 @@ add_nng_compat_test(compat_iovec 5) add_nng_compat_test(compat_device 5) add_nng_compat_test(compat_pair 5) add_nng_compat_test(compat_pipeline 5) +add_nng_compat_test(compat_poll 5) add_nng_compat_test(compat_reqrep 5) add_nng_compat_test(compat_survey 5) add_nng_compat_test(compat_reqttl 5) add_nng_compat_test(compat_shutdown 5) add_nng_compat_test(compat_surveyttl 5) -add_nng_compat_test(compat_poll 5) +add_nng_compat_test(compat_tcp 5) # These are special tests for compat mode, not inherited from the # legacy libnanomsg suite. diff --git a/tests/compat_tcp.c b/tests/compat_tcp.c new file mode 100644 index 00000000..fee271e9 --- /dev/null +++ b/tests/compat_tcp.c @@ -0,0 +1,238 @@ +/* + Copyright (c) 2012 Martin Sustrik All rights reserved. + Copyright 2015 Garrett D'Amore <garrett@damore.org> + Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom + the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. +*/ + +#include <nanomsg/nn.h> +#include <nanomsg/pair.h> +#include <nanomsg/pubsub.h> +#include <nanomsg/tcp.h> + +#include "compat_testutil.h" + +/* Tests TCP transport. */ + +int sc; + +int main (int argc, const char *argv[]) +{ + int rc; + int sb; + int i; + int opt; + size_t sz; + int s1, s2; + void * dummy_buf; + char addr[128]; + char socket_address[128]; + + int port = get_test_port(argc, argv); + + test_addr_from(socket_address, "tcp", "127.0.0.1", port); + + /* Try closing bound but unconnected socket. */ + sb = test_socket (AF_SP, NN_PAIR); + test_bind (sb, socket_address); + test_close (sb); + + /* Try closing a TCP socket while it not connected. At the same time + test specifying the local address for the connection. */ + sc = test_socket (AF_SP, NN_PAIR); +#if 0 // NNG doesn't support local binding + test_addr_from(addr, "tcp", "127.0.0.1;127.0.0.1", port); +#else + test_addr_from(addr, "tcp", "127.0.0.1", port); +#endif + test_connect (sc, addr); + test_close (sc); + + /* Open the socket anew. */ + sc = test_socket (AF_SP, NN_PAIR); + + /* Check NODELAY socket option. */ + sz = sizeof (opt); + rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz); + errno_assert (rc == 0); + nn_assert (sz == sizeof (opt)); + nn_assert (opt == 0); + opt = 2; + rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt)); + nn_assert (rc < 0 && nn_errno () == EINVAL); + opt = 1; + rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt)); + errno_assert (rc == 0); + sz = sizeof (opt); + rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz); + errno_assert (rc == 0); + nn_assert (sz == sizeof (opt)); + nn_assert (opt == 1); + + /* Try using invalid address strings. */ + rc = nn_connect (sc, "tcp://*:"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + rc = nn_connect (sc, "tcp://*:1000000"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + rc = nn_connect (sc, "tcp://*:some_port"); + nn_assert (rc < 0); +#if 0 // NNG does not support local interfaces + rc = nn_connect (sc, "tcp://eth10000;127.0.0.1:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == ENODEV); +#endif + rc = nn_connect (sc, "tcp://127.0.0.1"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); +#if 0 // NNG permits this -- the interface will get an ephemeral port + rc = nn_bind (sc, "tcp://127.0.0.1:"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); +#endif + rc = nn_bind (sc, "tcp://127.0.0.1:1000000"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); +#if 0 // NNG doesn't do interface binding here. + rc = nn_bind (sc, "tcp://eth10000:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == ENODEV); +#endif + rc = nn_connect (sc, "tcp://:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + rc = nn_connect (sc, "tcp://-hostname:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + rc = nn_connect (sc, "tcp://abc.123.---.#:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + +#if 0 // NNG is ok with this -- its a valid IPv6 address. + rc = nn_connect (sc, "tcp://[::1]:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); +#endif + + rc = nn_connect (sc, "tcp://abc...123:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + rc = nn_connect (sc, "tcp://.123:5555"); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + + /* Connect correctly. Do so before binding the peer socket. */ + test_connect (sc, socket_address); + + /* Leave enough time for at least on re-connect attempt. */ + nn_sleep (200); + + sb = test_socket (AF_SP, NN_PAIR); + test_bind (sb, socket_address); + + /* Ping-pong test. */ + for (i = 0; i != 100; ++i) { + + test_send (sc, "ABC"); + test_recv (sb, "ABC"); + + test_send (sb, "DEF"); + test_recv (sc, "DEF"); + } + + /* Batch transfer test. */ + for (i = 0; i != 100; ++i) { + test_send (sc, "0123456789012345678901234567890123456789"); + } + for (i = 0; i != 100; ++i) { + test_recv (sb, "0123456789012345678901234567890123456789"); + } + + test_close (sc); + test_close (sb); + + /* Test whether connection rejection is handled decently. */ + sb = test_socket (AF_SP, NN_PAIR); + test_bind (sb, socket_address); + s1 = test_socket (AF_SP, NN_PAIR); + test_connect (s1, socket_address); + s2 = test_socket (AF_SP, NN_PAIR); + test_connect (s2, socket_address); + nn_sleep (100); + test_close (s2); + test_close (s1); + test_close (sb); + + /* Test two sockets binding to the same address. */ + sb = test_socket (AF_SP, NN_PAIR); + test_bind (sb, socket_address); + s1 = test_socket (AF_SP, NN_PAIR); + + rc = nn_bind (s1, socket_address); + nn_assert (rc < 0); + errno_assert (nn_errno () == EADDRINUSE); + + sc = test_socket (AF_SP, NN_PAIR); + test_connect (sc, socket_address); + nn_sleep (100); + test_send (sb, "ABC"); + test_recv (sc, "ABC"); + test_close (sb); + test_close (sc); + test_close (s1); + + /* Test NN_RCVMAXSIZE limit */ + sb = test_socket (AF_SP, NN_PAIR); + test_bind (sb, socket_address); + s1 = test_socket (AF_SP, NN_PAIR); + test_connect (s1, socket_address); + opt = 4; + rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); + nn_assert (rc == 0); + nn_sleep (100); + test_send (s1, "ABC"); + test_recv (sb, "ABC"); + test_send (s1, "0123456789012345678901234567890123456789"); + rc = nn_recv (sb, &dummy_buf, NN_MSG, NN_DONTWAIT); + nn_assert (rc < 0); + errno_assert (nn_errno () == EAGAIN); + test_close (sb); + test_close (s1); + + /* Test that NN_RCVMAXSIZE can be -1, but not lower */ + sb = test_socket (AF_SP, NN_PAIR); + opt = -1; + rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); + nn_assert (rc >= 0); + opt = -2; + rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); + nn_assert (rc < 0); + errno_assert (nn_errno () == EINVAL); + test_close (sb); + + /* Test closing a socket that is waiting to connect. */ + sc = test_socket (AF_SP, NN_PAIR); + test_connect (sc, socket_address); + nn_sleep (100); + test_close (sc); + + return 0; +} diff --git a/tests/tcp.c b/tests/tcp.c index d29ceb8d..0018fce7 100644 --- a/tests/tcp.c +++ b/tests/tcp.c @@ -134,7 +134,8 @@ TestMain("TCP Transport", { So(nng_pair_open(&s) == 0); Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == NNG_ENOTSUP); + So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == 0); + So(v == true); So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_NODELAY, &v) == 0); So(v == true); @@ -183,8 +184,8 @@ TestMain("TCP Transport", { So(nng_pair_open(&s) == 0); Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == - NNG_ENOTSUP); + So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == 0); + So(v == false); So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_KEEPALIVE, &v) == 0); So(v == false); diff --git a/tests/tls.c b/tests/tls.c index 8ecf342a..1f1f244c 100644 --- a/tests/tls.c +++ b/tests/tls.c @@ -486,7 +486,8 @@ TestMain("TLS Transport", { So(nng_pair_open(&s) == 0); Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == NNG_ENOTSUP); + So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == 0); + So(v == true); So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_NODELAY, &v) == 0); So(v == true); @@ -535,8 +536,8 @@ TestMain("TLS Transport", { So(nng_pair_open(&s) == 0); Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == - NNG_ENOTSUP); + So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == 0); + So(v == false); So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_KEEPALIVE, &v) == 0); So(v == false); |
