From 249932f3a208260f6b9c99d778b22d51cfabe87b Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 15 Nov 2020 17:47:54 -0800 Subject: fixes #1071 tran_chkopt can be cleaned up This is a sweeping cleanup of the transport logic around options, and also harmonizes the names used when setting or getting options. Additionally, legacy methods are now moved into a separate file and can be elided via CMake or a preprocessor define. Fundamentally, the ability to set to transport options via the socket is deprecated; there are numerous problems with this and my earlier approaches to deal with this have been somewhat misguided. Further these approaches will not work with future protocol work that is planned (were some options need to be negotiated with peers at the time of connection establishment.) Documentation has been updated to reflect this. The test suites still make rather broad use of the older APIs, and will be converted later. --- src/core/socket.c | 85 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 15 deletions(-) (limited to 'src/core/socket.c') diff --git a/src/core/socket.c b/src/core/socket.c index 72c55c7a..f741b6f0 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -983,11 +983,9 @@ int nni_sock_setopt( nni_sock *s, const char *name, const void *v, size_t sz, nni_type t) { - int rv; - nni_dialer * d; - nni_listener *l; - nni_sockopt * optv; - nni_sockopt * oldv = NULL; + int rv; + nni_sockopt *optv; + nni_sockopt *oldv = NULL; nni_mtx_lock(&s->s_mx); if (s->s_closing) { @@ -1012,17 +1010,67 @@ nni_sock_setopt( } nni_mtx_unlock(&s->s_mx); - // Validation of generic and transport options. This is stateless, so - // transports should not fail to set an option later if they - // passed it here. + // Validation of generic and transport options. + // NOTE: Setting transport options via socket is deprecated. + // These options should be set on the endpoint to which they apply. if ((strcmp(name, NNG_OPT_RECONNMINT) == 0) || (strcmp(name, NNG_OPT_RECONNMAXT) == 0)) { - nng_duration ms; - if ((rv = nni_copyin_ms(&ms, v, sz, t)) != 0) { + if ((rv = nni_copyin_ms(NULL, v, sz, t)) != 0) { return (rv); } - } else if ((rv = nni_tran_chkopt(name, v, sz, t)) != 0) { - return (rv); + + } else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) { + if ((rv = nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)) != + 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) { + if ((rv = nni_copyin_ptr(NULL, v, sz, t)) != 0) { + return (rv); + } + } 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_SUPPRESS_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_SUPPRESS_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. @@ -1058,6 +1106,10 @@ nni_sock_setopt( } } +#ifndef NNG_ELIDE_DEPRCATED + 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 @@ -1084,6 +1136,7 @@ nni_sock_setopt( } } } +#endif if (rv == 0) { // Remove and toss the old value; we are using a new one. @@ -1151,10 +1204,12 @@ nni_sock_getopt( } } - if (sopt->sz > *szp) { - sz = *szp; + if (szp != NULL) { + if (sopt->sz > *szp) { + sz = *szp; + } + *szp = sopt->sz; } - *szp = sopt->sz; memcpy(val, sopt->data, sz); rv = 0; break; -- cgit v1.2.3-70-g09d2