Skip to content

Commit

Permalink
remove legacy support for configuration of transport options on socket
Browse files Browse the repository at this point in the history
Transport specific options should be configured on the end point.
This has the most impact for TLS, as TLS dialers and listeners will
need to be allocated apriori, to configure TLS options.

Some legacy tests were removed... we're going to remove the legacy
libnanomsg compatibility layer anyway.
  • Loading branch information
gdamore committed Oct 21, 2024
1 parent d05ff49 commit 53e8dbb
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 926 deletions.
29 changes: 1 addition & 28 deletions src/compat/nanomsg/compat_tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,6 @@ test_bad_addresses(void)
NUTS_NN_PASS(nn_close(s));
}

void
test_no_delay(void)
{
int s;
int opt;
size_t sz;
NUTS_TRUE((s = nn_socket(AF_SP, NN_PAIR)) >= 0);

sz = sizeof(opt);
NUTS_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz));
NUTS_TRUE(sz == sizeof(opt));
NUTS_TRUE(opt == 0);
opt = 2;
NUTS_NN_FAIL(
nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz), EINVAL);

opt = 1;
NUTS_NN_PASS(nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz));

opt = 3;
NUTS_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz));
NUTS_TRUE(sz == sizeof(opt));
NUTS_TRUE(opt == 1);
NUTS_NN_PASS(nn_close(s));
}

void
test_ping_pong(void)
{
Expand Down Expand Up @@ -201,7 +175,7 @@ test_max_recv_size(void)
int n;
size_t sz;
char buf[64];
char *addr;
char *addr;

NUTS_ADDR(addr, "tcp");

Expand Down Expand Up @@ -251,7 +225,6 @@ TEST_LIST = {
{ "compat tcp connect and close ", test_connect_and_close },
{ "compat tcp bind and connect ", test_bind_and_connect },
{ "compat tcp invalid addresses", test_bad_addresses },
{ "compat tcp no delay option", test_no_delay },
{ "compat tcp ping pong", test_ping_pong },
{ "compat tcp pair reject", test_pair_reject },
{ "compat tcp addr in use", test_addr_in_use },
Expand Down
71 changes: 15 additions & 56 deletions src/core/sock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_recv_timeout(void)
{
nng_socket s1;
uint64_t now;
nng_msg * msg = NULL;
nng_msg *msg = NULL;

NUTS_OPEN(s1);
NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 10));
Expand All @@ -32,7 +32,7 @@ test_recv_nonblock(void)
{
nng_socket s1;
uint64_t now;
nng_msg * msg = NULL;
nng_msg *msg = NULL;

NUTS_OPEN(s1);
NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 10));
Expand All @@ -48,7 +48,7 @@ test_send_timeout(void)
{
nng_socket s1;
uint64_t now;
nng_msg * msg;
nng_msg *msg;

NUTS_OPEN(s1);
NUTS_PASS(nng_msg_alloc(&msg, 0));
Expand All @@ -66,7 +66,7 @@ test_send_nonblock(void)
{
nng_socket s1;
uint64_t now;
nng_msg * msg;
nng_msg *msg;

NUTS_OPEN(s1);
NUTS_PASS(nng_msg_alloc(&msg, 0));
Expand Down Expand Up @@ -108,9 +108,9 @@ test_socket_name(void)
{
nng_socket s1;
char name[128]; // 64 is max
char * str;
char *str;
long id;
char * end;
char *end;
size_t sz;

sz = sizeof(name);
Expand Down Expand Up @@ -175,8 +175,8 @@ test_send_recv(void)
int len;
size_t sz;
nng_duration to = 3000; // 3 seconds
char * buf;
char * a = "inproc://t1";
char *buf;
char *a = "inproc://t1";

NUTS_OPEN(s1);
NUTS_OPEN(s2);
Expand Down Expand Up @@ -215,8 +215,8 @@ test_send_recv_zero_length(void)
int len;
size_t sz;
nng_duration to = 3000; // 3 seconds
char * buf;
char * a = "inproc://send-recv-zero-length";
char *buf;
char *a = "inproc://send-recv-zero-length";

NUTS_OPEN(s1);
NUTS_OPEN(s2);
Expand Down Expand Up @@ -259,11 +259,11 @@ test_connection_refused(void)
void
test_late_connection(void)
{
char * buf;
char *buf;
size_t sz;
nng_socket s1;
nng_socket s2;
char * a = "inproc://asy";
char *a = "inproc://asy";

NUTS_OPEN(s1);
NUTS_OPEN(s2);
Expand All @@ -287,7 +287,7 @@ test_late_connection(void)
void
test_address_busy(void)
{
char * a = "inproc://eaddrinuse";
char *a = "inproc://eaddrinuse";
nng_listener l = NNG_LISTENER_INITIALIZER;
nng_dialer d = NNG_DIALER_INITIALIZER;
nng_socket s1;
Expand Down Expand Up @@ -323,7 +323,7 @@ test_endpoint_types(void)
nng_listener l = NNG_LISTENER_INITIALIZER;
nng_dialer d2;
nng_listener l2;
char * a = "inproc://mumble...";
char *a = "inproc://mumble...";
bool b;

NUTS_OPEN(s1);
Expand Down Expand Up @@ -400,30 +400,10 @@ test_listener_options(void)

NUTS_OPEN(s1);

#ifndef NNG_ELIDE_DEPRECATED
// Create a listener with the specified options
NUTS_PASS(nng_socket_set_size(s1, NNG_OPT_RECVMAXSZ, 543));
NUTS_PASS(nng_listener_create(&l, s1, "inproc://listener_opts"));
NUTS_PASS(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 543);

// Verify endpoint overrides
NUTS_PASS(nng_listener_set_size(l, NNG_OPT_RECVMAXSZ, 678));
NUTS_PASS(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 678);
NUTS_PASS(nng_socket_get_size(s1, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 543);

// And socket overrides again
NUTS_PASS(nng_socket_set_size(s1, NNG_OPT_RECVMAXSZ, 911));
NUTS_PASS(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 911);
#else
NUTS_PASS(nng_listener_create(&l, s1, "inproc://listener_opts"));
NUTS_PASS(nng_listener_set_size(l, NNG_OPT_RECVMAXSZ, 678));
NUTS_PASS(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 678);
#endif

// Cannot set invalid options
NUTS_FAIL(nng_listener_set_size(l, "BAD_OPT", 1), NNG_ENOTSUP);
Expand Down Expand Up @@ -456,31 +436,10 @@ test_dialer_options(void)

NUTS_OPEN(s1);

#ifndef NNG_ELIDE_DEPRECATED
// NOTE: This test will fail if eliding deprecated behavior.
// Create a dialer with the specified options
NUTS_PASS(nng_socket_set_size(s1, NNG_OPT_RECVMAXSZ, 543));
NUTS_PASS(nng_dialer_create(&d, s1, "inproc://dialer_opts"));
NUTS_PASS(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 543);

// Verify endpoint overrides
NUTS_PASS(nng_dialer_set_size(d, NNG_OPT_RECVMAXSZ, 678));
NUTS_PASS(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 678);
NUTS_PASS(nng_socket_get_size(s1, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 543);

// And socket overrides again
NUTS_PASS(nng_socket_set_size(s1, NNG_OPT_RECVMAXSZ, 911));
NUTS_PASS(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 911);
#else
NUTS_PASS(nng_dialer_create(&d, s1, "inproc://dialer_opts"));
NUTS_PASS(nng_dialer_set_size(d, NNG_OPT_RECVMAXSZ, 678));
NUTS_PASS(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &sz));
NUTS_TRUE(sz == 678);
#endif

// Cannot set invalid options
NUTS_FAIL(nng_dialer_set_size(d, "BAD_OPT", 1), NNG_ENOTSUP);
Expand Down Expand Up @@ -588,7 +547,7 @@ test_size_options(void)
nng_socket s1;
size_t val;
size_t sz;
char * opt;
char *opt;

char *cases[] = {
NNG_OPT_RECVMAXSZ,
Expand Down
83 changes: 0 additions & 83 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,57 +1036,6 @@ nni_sock_setopt(
0) {
return (rv);
}

#if !defined(NNG_ELIDE_DEPRECATED)
// TCP options, set via socket is deprecated.
} else if ((strcmp(name, NNG_OPT_TCP_KEEPALIVE) == 0) ||
(strcmp(name, NNG_OPT_TCP_NODELAY)) == 0) {
if ((rv = nni_copyin_bool(NULL, v, sz, t)) != 0) {
return (rv);
}
#endif

#if defined(NNG_SUPP_TLS) && !defined(NNG_ELIDE_DEPRECATED)
// TLS options may not be supported if TLS is not
// compiled in. Supporting all these is deprecated.
} else if (strcmp(name, NNG_OPT_TLS_CONFIG) == 0) {
nng_tls_config *tc;
if ((rv = nni_copyin_ptr((void **) &tc, v, sz, t)) != 0) {
return (rv);
}
// place a hold on this configuration object
nng_tls_config_hold(tc);

} else if ((strcmp(name, NNG_OPT_TLS_SERVER_NAME) == 0) ||
(strcmp(name, NNG_OPT_TLS_CA_FILE) == 0) ||
(strcmp(name, NNG_OPT_TLS_CERT_KEY_FILE) == 0)) {
if ((t != NNI_TYPE_OPAQUE) && (t != NNI_TYPE_STRING)) {
return (NNG_EBADTYPE);
}
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
} else if ((strcmp(name, NNG_OPT_TLS_AUTH_MODE) == 0)) {
// 0, 1, or 2 (none, optional, required)
if ((rv = nni_copyin_int(NULL, v, sz, 0, 2, t)) != 0) {
return (rv);
}
#endif

#if defined(NNG_PLATFORM_POSIX) && !defined(NNG_ELIDE_DEPRECATED)
} else if (strcmp(name, NNG_OPT_IPC_PERMISSIONS) == 0) {
// UNIX mode bits are 0777, but allow set id and sticky bits
if ((rv = nni_copyin_int(NULL, v, sz, 0, 07777, t)) != 0) {
return (rv);
}
#endif

#if defined(NNG_PLATFORM_WINDOWS) && !defined(NNG_ELIDE_DEPRECATED)
} else if (strcmp(name, NNG_OPT_IPC_SECURITY_DESCRIPTOR) == 0) {
if ((rv = nni_copyin_ptr(NULL, v, sz, t)) == 0) {
return (rv);
}
#endif
}

// Prepare a copy of the socket option.
Expand Down Expand Up @@ -1122,38 +1071,6 @@ nni_sock_setopt(
}
}

#ifndef NNG_ELIDE_DEPRECATED
nni_dialer *d;
nni_listener *l;

// Apply the options. Failure to set any option on any
// transport (other than ENOTSUP) stops the operation
// altogether. Its important that transport wide checks
// properly pre-validate.
NNI_LIST_FOREACH (&s->s_listeners, l) {
int x;
x = nni_listener_setopt(l, optv->name, optv->data, sz, t);
if (x != NNG_ENOTSUP) {
if ((rv = x) != 0) {
nni_mtx_unlock(&s->s_mx);
nni_free_opt(optv);
return (rv);
}
}
}
NNI_LIST_FOREACH (&s->s_dialers, d) {
int x;
x = nni_dialer_setopt(d, optv->name, optv->data, sz, t);
if (x != NNG_ENOTSUP) {
if ((rv = x) != 0) {
nni_mtx_unlock(&s->s_mx);
nni_free_opt(optv);
return (rv);
}
}
}
#endif

if (rv == 0) {
// Remove and toss the old value; we are using a new one.
if (oldv != NULL) {
Expand Down
3 changes: 2 additions & 1 deletion src/platform/posix/posix_tcpdial.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ nni_tcp_dialer_init(nni_tcp_dialer **dp)
return (NNG_ENOMEM);
}
nni_mtx_init(&d->mtx);
d->closed = false;
d->closed = false;
d->nodelay = true;
nni_aio_list_init(&d->connq);
nni_atomic_init_bool(&d->fini);
nni_atomic_init64(&d->ref);
Expand Down
3 changes: 2 additions & 1 deletion src/platform/posix/posix_tcplisten.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
Expand Down Expand Up @@ -56,6 +56,7 @@ nni_tcp_listener_init(nni_tcp_listener **lp)
l->pfd = NULL;
l->closed = false;
l->started = false;
l->nodelay = true;

nni_aio_list_init(&l->acceptq);
*lp = l;
Expand Down
1 change: 1 addition & 0 deletions src/platform/windows/win_tcpdial.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ nni_tcp_dialer_init(nni_tcp_dialer **dp)
ZeroMemory(d, sizeof(*d));
nni_mtx_init(&d->mtx);
nni_aio_list_init(&d->aios);
d->nodelay = true;

// Create a scratch socket for use with ioctl.
s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
Expand Down
Loading

0 comments on commit 53e8dbb

Please sign in to comment.