diff options
31 files changed, 319 insertions, 656 deletions
diff --git a/src/core/defs.h b/src/core/defs.h index 3a3f23ff..5c06cf53 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -47,12 +47,10 @@ typedef struct nni_listener nni_listener; typedef struct nni_pipe nni_pipe; typedef struct nni_tran nni_tran; -typedef struct nni_tran_option nni_tran_option; typedef struct nni_tran_dialer_ops nni_tran_dialer_ops; typedef struct nni_tran_listener_ops nni_tran_listener_ops; typedef struct nni_tran_pipe_ops nni_tran_pipe_ops; -typedef struct nni_proto_option nni_proto_option; typedef struct nni_proto_ctx_ops nni_proto_ctx_ops; typedef struct nni_proto_sock_ops nni_proto_sock_ops; typedef struct nni_proto_pipe_ops nni_proto_pipe_ops; diff --git a/src/core/dialer.c b/src/core/dialer.c index 11faed7c..a74f30f0 100644 --- a/src/core/dialer.c +++ b/src/core/dialer.c @@ -389,7 +389,7 @@ int nni_dialer_setopt(nni_dialer *d, const char *name, const void *val, size_t sz, nni_opt_type t) { - nni_tran_option *o; + nni_option *o; if (strcmp(name, NNG_OPT_URL) == 0) { return (NNG_EREADONLY); @@ -430,7 +430,7 @@ int nni_dialer_getopt( nni_dialer *d, const char *name, void *valp, size_t *szp, nni_opt_type t) { - nni_tran_option *o; + nni_option *o; if (strcmp(name, NNG_OPT_RECONNMAXT) == 0) { int rv; diff --git a/src/core/listener.c b/src/core/listener.c index 135478de..84f8cafd 100644 --- a/src/core/listener.c +++ b/src/core/listener.c @@ -364,7 +364,7 @@ int nni_listener_setopt(nni_listener *l, const char *name, const void *val, size_t sz, nni_opt_type t) { - nni_tran_option *o; + nni_option *o; if (strcmp(name, NNG_OPT_URL) == 0) { return (NNG_EREADONLY); @@ -388,7 +388,7 @@ int nni_listener_getopt( nni_listener *l, const char *name, void *valp, size_t *szp, nni_opt_type t) { - nni_tran_option *o; + nni_option *o; for (o = l->l_ops.l_options; o && o->o_name; o++) { if (strcmp(o->o_name, name) != 0) { diff --git a/src/core/options.h b/src/core/options.h index ac64f9e3..f0ab9811 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -51,4 +51,27 @@ extern int nni_copyout_u64(uint64_t, void *, size_t *, nni_opt_type); // then it passes through a pointer, created by nni_strdup(). extern int nni_copyout_str(const char *, void *, size_t *, nni_opt_type); +// nni_option is used for socket, protocol, transport, and similar options. +// Note that only for transports, the o_set member may be called with a NULL +// instance parameter, in which case the request should only validate the +// argument and do nothing further. +typedef struct nni_option_s nni_option; +struct nni_option_s { + // o_name is the name of the option. + const char *o_name; + + // o_get is used to retrieve the value of the option. The + // size supplied will limit how much data is copied. Regardless, + // the actual size of the object that would have been copied + // is supplied by the function in the size. If the object did + // not fit, then NNG_EINVAL is returned. + int (*o_get)(void *, void *, size_t *, nni_opt_type); + + // o_set is used to set the value of the option. For transport + // endpoints only, the instance parameter (first argument) may be + // NULL, in which case only a generic validation of the parameters + // is performed. (This is used when setting socket options before + int (*o_set)(void *, const void *, size_t, nni_opt_type); +}; + #endif // CORE_OPTIONS_H diff --git a/src/core/pipe.c b/src/core/pipe.c index d04c8b28..9357cee4 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -289,7 +289,7 @@ int nni_pipe_getopt( nni_pipe *p, const char *name, void *val, size_t *szp, nni_opt_type t) { - nni_tran_option *o; + nni_option *o; for (o = p->p_tran_ops.p_options; o && o->o_name; o++) { if (strcmp(o->o_name, name) != 0) { diff --git a/src/core/protocol.h b/src/core/protocol.h index 12ca7e71..f164ee45 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -11,6 +11,8 @@ #ifndef CORE_PROTOCOL_H #define CORE_PROTOCOL_H +#include "core/options.h" + // Protocol implementation details. Protocols must implement the // interfaces in this file. Note that implementing new protocols is // not necessarily intended to be a trivial task. The protocol developer @@ -21,13 +23,6 @@ // As a consequence, most of the concurrency in nng exists in the protocol // implementations. -struct nni_proto_option { - const char *o_name; - int o_type; - int (*o_get)(void *, void *, size_t *, nni_opt_type); - int (*o_set)(void *, const void *, size_t, nni_opt_type); -}; - // nni_proto_pipe contains protocol-specific per-pipe operations. struct nni_proto_pipe_ops { // pipe_init creates the protocol-specific per pipe data structure. @@ -80,7 +75,7 @@ struct nni_proto_ctx_ops { void (*ctx_drain)(void *, nni_aio *); // ctx_options array. - nni_proto_option *ctx_options; + nni_option *ctx_options; }; struct nni_proto_sock_ops { @@ -123,7 +118,7 @@ struct nni_proto_sock_ops { void (*sock_drain)(void *, nni_aio *); // Options. Must not be NULL. Final entry should have NULL name. - nni_proto_option *sock_options; + nni_option *sock_options; }; typedef struct nni_proto_id { diff --git a/src/core/socket.c b/src/core/socket.c index 22fa5c07..888200ef 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -33,13 +33,6 @@ struct nni_ctx { nng_duration c_rcvtimeo; }; -typedef struct sock_option { - const char * o_name; - nni_opt_type o_type; - int (*o_get)(nni_sock *, void *, size_t *, nni_opt_type); - int (*o_set)(nni_sock *, const void *, size_t, nni_opt_type); -} sock_option; - typedef struct nni_sockopt { nni_list_node node; char * name; @@ -120,22 +113,24 @@ static void nni_ctx_destroy(nni_ctx *); static void dialer_shutdown_locked(nni_dialer *); static void listener_shutdown_locked(nni_listener *); +#define SOCK(s) ((nni_sock *) (s)) + static int -sock_get_fd(nni_sock *s, int flag, int *fdp) +sock_get_fd(void *s, int flag, int *fdp) { int rv; nni_pollable *p; - if ((flag & nni_sock_flags(s)) == 0) { + if ((flag & nni_sock_flags(SOCK(s))) == 0) { return (NNG_ENOTSUP); } switch (flag) { case NNI_PROTO_FLAG_SND: - rv = nni_msgq_get_sendable(s->s_uwq, &p); + rv = nni_msgq_get_sendable(SOCK(s)->s_uwq, &p); break; case NNI_PROTO_FLAG_RCV: - rv = nni_msgq_get_recvable(s->s_urq, &p); + rv = nni_msgq_get_recvable(SOCK(s)->s_urq, &p); break; default: rv = NNG_EINVAL; @@ -150,62 +145,62 @@ sock_get_fd(nni_sock *s, int flag, int *fdp) } static int -sock_get_sendfd(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendfd(void *s, void *buf, size_t *szp, nni_opt_type t) { int fd; int rv; - if ((rv = sock_get_fd(s, NNI_PROTO_FLAG_SND, &fd)) != 0) { + if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_SND, &fd)) != 0) { return (rv); } return (nni_copyout_int(fd, buf, szp, t)); } static int -sock_get_recvfd(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvfd(void *s, void *buf, size_t *szp, nni_opt_type t) { int fd; int rv; - if ((rv = sock_get_fd(s, NNI_PROTO_FLAG_RCV, &fd)) != 0) { + if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_RCV, &fd)) != 0) { return (rv); } return (nni_copyout_int(fd, buf, szp, t)); } static int -sock_get_raw(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_raw(void *s, void *buf, size_t *szp, nni_opt_type t) { - bool raw = ((nni_sock_flags(s) & NNI_PROTO_FLAG_RAW) != 0); + bool raw = ((nni_sock_flags(SOCK(s)) & NNI_PROTO_FLAG_RAW) != 0); return (nni_copyout_bool(raw, buf, szp, t)); } static int -sock_set_recvtimeo(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_recvtimeo(void *s, const void *buf, size_t sz, nni_opt_type t) { - return (nni_copyin_ms(&s->s_rcvtimeo, buf, sz, t)); + return (nni_copyin_ms(&SOCK(s)->s_rcvtimeo, buf, sz, t)); } static int -sock_get_recvtimeo(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvtimeo(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_ms(s->s_rcvtimeo, buf, szp, t)); + return (nni_copyout_ms(SOCK(s)->s_rcvtimeo, buf, szp, t)); } static int -sock_set_sendtimeo(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sendtimeo(void *s, const void *buf, size_t sz, nni_opt_type t) { - return (nni_copyin_ms(&s->s_sndtimeo, buf, sz, t)); + return (nni_copyin_ms(&SOCK(s)->s_sndtimeo, buf, sz, t)); } static int -sock_get_sendtimeo(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendtimeo(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_ms(s->s_sndtimeo, buf, szp, t)); + return (nni_copyout_ms(SOCK(s)->s_sndtimeo, buf, szp, t)); } static int -sock_set_recvbuf(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_recvbuf(void *s, const void *buf, size_t sz, nni_opt_type t) { int len; int rv; @@ -213,19 +208,19 @@ sock_set_recvbuf(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, t)) != 0) { return (rv); } - return (nni_msgq_resize(s->s_urq, len)); + return (nni_msgq_resize(SOCK(s)->s_urq, len)); } static int -sock_get_recvbuf(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvbuf(void *s, void *buf, size_t *szp, nni_opt_type t) { - int len = nni_msgq_cap(s->s_urq); + int len = nni_msgq_cap(SOCK(s)->s_urq); return (nni_copyout_int(len, buf, szp, t)); } static int -sock_set_sendbuf(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sendbuf(void *s, const void *buf, size_t sz, nni_opt_type t) { int len; int rv; @@ -233,117 +228,106 @@ sock_set_sendbuf(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, t)) != 0) { return (rv); } - return (nni_msgq_resize(s->s_uwq, len)); + return (nni_msgq_resize(SOCK(s)->s_uwq, len)); } static int -sock_get_sendbuf(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendbuf(void *s, void *buf, size_t *szp, nni_opt_type t) { - int len = nni_msgq_cap(s->s_uwq); + int len = nni_msgq_cap(SOCK(s)->s_uwq); return (nni_copyout_int(len, buf, szp, t)); } static int -sock_get_sockname(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sockname(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_str(s->s_name, buf, szp, t)); + return (nni_copyout_str(SOCK(s)->s_name, buf, szp, t)); } static int -sock_set_sockname(nni_sock *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sockname(void *s, const void *buf, size_t sz, nni_opt_type t) { - return (nni_copyin_str(s->s_name, buf, sizeof(s->s_name), sz, t)); + return (nni_copyin_str( + SOCK(s)->s_name, buf, sizeof(SOCK(s)->s_name), sz, t)); } static int -sock_get_proto(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_proto(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_int(nni_sock_proto_id(s), buf, szp, t)); + return (nni_copyout_int(nni_sock_proto_id(SOCK(s)), buf, szp, t)); } static int -sock_get_peer(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_peer(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_int(nni_sock_peer_id(s), buf, szp, t)); + return (nni_copyout_int(nni_sock_peer_id(SOCK(s)), buf, szp, t)); } static int -sock_get_protoname(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_protoname(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_str(nni_sock_proto_name(s), buf, szp, t)); + return (nni_copyout_str(nni_sock_proto_name(SOCK(s)), buf, szp, t)); } static int -sock_get_peername(nni_sock *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_peername(void *s, void *buf, size_t *szp, nni_opt_type t) { - return (nni_copyout_str(nni_sock_peer_name(s), buf, szp, t)); + return (nni_copyout_str(nni_sock_peer_name(SOCK(s)), buf, szp, t)); } -static const sock_option sock_options[] = { +static const nni_option sock_options[] = { { .o_name = NNG_OPT_RECVTIMEO, - .o_type = NNI_TYPE_DURATION, .o_get = sock_get_recvtimeo, .o_set = sock_set_recvtimeo, }, { .o_name = NNG_OPT_SENDTIMEO, - .o_type = NNI_TYPE_DURATION, .o_get = sock_get_sendtimeo, .o_set = sock_set_sendtimeo, }, { .o_name = NNG_OPT_RECVFD, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_recvfd, }, { .o_name = NNG_OPT_SENDFD, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_sendfd, }, { .o_name = NNG_OPT_RECVBUF, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_recvbuf, .o_set = sock_set_recvbuf, }, { .o_name = NNG_OPT_SENDBUF, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_sendbuf, .o_set = sock_set_sendbuf, }, { .o_name = NNG_OPT_SOCKNAME, - .o_type = NNI_TYPE_STRING, .o_get = sock_get_sockname, .o_set = sock_set_sockname, }, { .o_name = NNG_OPT_RAW, - .o_type = NNI_TYPE_BOOL, .o_get = sock_get_raw, }, { .o_name = NNG_OPT_PROTO, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_proto, }, { .o_name = NNG_OPT_PEER, - .o_type = NNI_TYPE_INT32, .o_get = sock_get_peer, }, { .o_name = NNG_OPT_PROTONAME, - .o_type = NNI_TYPE_STRING, .o_get = sock_get_protoname, }, { .o_name = NNG_OPT_PEERNAME, - .o_type = NNI_TYPE_STRING, .o_get = sock_get_peername, }, // terminate list @@ -975,13 +959,12 @@ int nni_sock_setopt( nni_sock *s, const char *name, const void *v, size_t sz, nni_opt_type t) { - int rv = NNG_ENOTSUP; - nni_dialer * d; - nni_listener * l; - nni_sockopt * optv; - nni_sockopt * oldv = NULL; - const sock_option * sso; - const nni_proto_option *pso; + int rv = NNG_ENOTSUP; + nni_dialer * d; + nni_listener * l; + nni_sockopt * optv; + nni_sockopt * oldv = NULL; + const nni_option *opt; nni_mtx_lock(&s->s_mx); if (s->s_closing) { @@ -992,29 +975,29 @@ nni_sock_setopt( // Protocol options. The protocol can override options that // the socket framework would otherwise supply, like buffer // sizes. - for (pso = s->s_sock_ops.sock_options; pso->o_name != NULL; pso++) { - if (strcmp(pso->o_name, name) != 0) { + for (opt = s->s_sock_ops.sock_options; opt->o_name != NULL; opt++) { + if (strcmp(opt->o_name, name) != 0) { continue; } - if (pso->o_set == NULL) { + if (opt->o_set == NULL) { nni_mtx_unlock(&s->s_mx); return (NNG_EREADONLY); } - rv = pso->o_set(s->s_data, v, sz, t); + rv = opt->o_set(s->s_data, v, sz, t); nni_mtx_unlock(&s->s_mx); return (rv); } // Some options do not go down to transports. Handle them directly. - for (sso = sock_options; sso->o_name != NULL; sso++) { - if (strcmp(sso->o_name, name) != 0) { + for (opt = sock_options; opt->o_name != NULL; opt++) { + if (strcmp(opt->o_name, name) != 0) { continue; } - if (sso->o_set == NULL) { + if (opt->o_set == NULL) { nni_mtx_unlock(&s->s_mx); return (NNG_EREADONLY); } - rv = sso->o_set(s, v, sz, t); + rv = opt->o_set(s, v, sz, t); nni_mtx_unlock(&s->s_mx); return (rv); } @@ -1122,10 +1105,9 @@ int nni_sock_getopt( nni_sock *s, const char *name, void *val, size_t *szp, nni_opt_type t) { - int rv = NNG_ENOTSUP; - nni_sockopt * sopt; - const sock_option * sso; - const nni_proto_option *pso; + int rv = NNG_ENOTSUP; + nni_sockopt * sopt; + const nni_option *opt; nni_mtx_lock(&s->s_mx); if (s->s_closing) { @@ -1136,29 +1118,29 @@ nni_sock_getopt( // Protocol specific options. The protocol can override // options like the send buffer or notification descriptors // this way. - for (pso = s->s_sock_ops.sock_options; pso->o_name != NULL; pso++) { - if (strcmp(name, pso->o_name) != 0) { + for (opt = s->s_sock_ops.sock_options; opt->o_name != NULL; opt++) { + if (strcmp(name, opt->o_name) != 0) { continue; } - if (pso->o_get == NULL) { + if (opt->o_get == NULL) { nni_mtx_unlock(&s->s_mx); return (NNG_EWRITEONLY); } - rv = pso->o_get(s->s_data, val, szp, t); + rv = opt->o_get(s->s_data, val, szp, t); nni_mtx_unlock(&s->s_mx); return (rv); } // Socket generic options. - for (sso = sock_options; sso->o_name != NULL; sso++) { - if (strcmp(name, sso->o_name) != 0) { + for (opt = sock_options; opt->o_name != NULL; opt++) { + if (strcmp(name, opt->o_name) != 0) { continue; } - if (sso->o_get == NULL) { + if (opt->o_get == NULL) { nni_mtx_unlock(&s->s_mx); return (NNG_EWRITEONLY); } - rv = sso->o_get(s, val, szp, t); + rv = opt->o_get(s, val, szp, t); nni_mtx_unlock(&s->s_mx); return (rv); } @@ -1372,9 +1354,9 @@ int nni_ctx_getopt( nni_ctx *ctx, const char *opt, void *v, size_t *szp, nni_opt_type t) { - nni_sock * sock = ctx->c_sock; - nni_proto_option *o; - int rv = NNG_ENOTSUP; + nni_sock * sock = ctx->c_sock; + nni_option *o; + int rv = NNG_ENOTSUP; nni_mtx_lock(&sock->s_mx); if (strcmp(opt, NNG_OPT_RECVTIMEO) == 0) { @@ -1402,9 +1384,9 @@ int nni_ctx_setopt( nni_ctx *ctx, const char *opt, const void *v, size_t sz, nni_opt_type t) { - nni_sock * sock = ctx->c_sock; - nni_proto_option *o; - int rv = NNG_ENOTSUP; + nni_sock * sock = ctx->c_sock; + nni_option *o; + int rv = NNG_ENOTSUP; nni_mtx_lock(&sock->s_mx); if (strcmp(opt, NNG_OPT_RECVTIMEO) == 0) { diff --git a/src/core/transport.c b/src/core/transport.c index e27c349e..185ab779 100644 --- a/src/core/transport.c +++ b/src/core/transport.c @@ -120,7 +120,7 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ) NNI_LIST_FOREACH (&nni_tran_list, t) { const nni_tran_dialer_ops * dops; const nni_tran_listener_ops *lops; - const nni_tran_option * o; + const nni_option * o; // Generally we look for endpoint options. We check both // dialers and listeners. @@ -133,9 +133,8 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ) nni_mtx_unlock(&nni_tran_lk); return (NNG_EREADONLY); } - - rv = (o->o_chk != NULL) ? o->o_chk(v, sz, typ) : 0; nni_mtx_unlock(&nni_tran_lk); + rv = o->o_set(NULL, v, sz, typ); return (rv); } lops = t->t_tran.tran_listener; @@ -147,9 +146,8 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ) nni_mtx_unlock(&nni_tran_lk); return (NNG_EREADONLY); } - - rv = (o->o_chk != NULL) ? o->o_chk(v, sz, typ) : 0; nni_mtx_unlock(&nni_tran_lk); + rv = o->o_set(NULL, v, sz, typ); return (rv); } } diff --git a/src/core/transport.h b/src/core/transport.h index 8e5bba48..1736f8ea 100644 --- a/src/core/transport.h +++ b/src/core/transport.h @@ -11,6 +11,8 @@ #ifndef CORE_TRANSPORT_H #define CORE_TRANSPORT_H +#include "core/options.h" + // We quite intentionally use a signature where the upper word is nonzero, // which ensures that if we get garbage we will reject it. This is more // likely to mismatch than all zero bytes would. The actual version is @@ -27,28 +29,6 @@ #define NNI_TRANSPORT_V5 0x54520005 #define NNI_TRANSPORT_VERSION NNI_TRANSPORT_V5 -// Option handlers. -struct nni_tran_option { - // o_name is the name of the option. - const char *o_name; - - // o_type is the type of the option. - nni_opt_type o_type; - - // o_get retrieves the value of the option. The first argument is the - // dialer, listener, or pipe where the request is being made. - int (*o_get)(void *, void *, size_t *, nni_opt_type); - - // o_set sets the value of the option. The first argument is the - // dialer, listener, or pipe where the request is being made. - int (*o_set)(void *, const void *, size_t, nni_opt_type); - - // o_chk checks to see if the proposed value is legal -- this is - // checks only the type, size, and generic range validation. This - // function can be called before any transport objects are created. - int (*o_chk)(const void *, size_t, nni_opt_type); -}; - // Endpoint operations are called by the socket in a // protocol-independent fashion. The socket makes individual calls, // which are expected to block if appropriate (except for destroy), or @@ -81,7 +61,7 @@ struct nni_tran_dialer_ops { // d_options is an array of dialer options. The final // element must have a NULL name. If this member is NULL, then // no dialer specific options are available. - nni_tran_option *d_options; + nni_option *d_options; }; struct nni_tran_listener_ops { @@ -111,7 +91,7 @@ struct nni_tran_listener_ops { // l_options is an array of listener options. The final // element must have a NULL name. If this member is NULL, then // no dialer specific options are available. - nni_tran_option *l_options; + nni_option *l_options; }; // Pipe operations are entry points called by the socket. These may be @@ -160,7 +140,7 @@ struct nni_tran_pipe_ops { // p_options is an array of pipe options. The final element // must have a NULL name. If this member is NULL, then no // transport specific options are available. - nni_tran_option *p_options; + nni_option *p_options; }; // Transport implementation details. Transports must implement the diff --git a/src/protocol/bus0/bus.c b/src/protocol/bus0/bus.c index ed2a244f..aba0a04c 100644 --- a/src/protocol/bus0/bus.c +++ b/src/protocol/bus0/bus.c @@ -439,7 +439,7 @@ static nni_proto_pipe_ops bus0_pipe_ops = { .pipe_stop = bus0_pipe_stop, }; -static nni_proto_option bus0_sock_options[] = { +static nni_option bus0_sock_options[] = { // terminate list { .o_name = NULL, diff --git a/src/protocol/pair0/pair.c b/src/protocol/pair0/pair.c index fef2acaf..4e708a61 100644 --- a/src/protocol/pair0/pair.c +++ b/src/protocol/pair0/pair.c @@ -269,7 +269,7 @@ static nni_proto_pipe_ops pair0_pipe_ops = { .pipe_stop = pair0_pipe_stop, }; -static nni_proto_option pair0_sock_options[] = { +static nni_option pair0_sock_options[] = { // terminate list { .o_name = NULL, diff --git a/src/protocol/pair1/pair.c b/src/protocol/pair1/pair.c index a474f0d9..451122ea 100644 --- a/src/protocol/pair1/pair.c +++ b/src/protocol/pair1/pair.c @@ -521,16 +521,14 @@ static nni_proto_pipe_ops pair1_pipe_ops = { .pipe_stop = pair1_pipe_stop, }; -static nni_proto_option pair1_sock_options[] = { +static nni_option pair1_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = pair1_sock_get_maxttl, .o_set = pair1_sock_set_maxttl, }, { .o_name = NNG_OPT_PAIR1_POLY, - .o_type = NNI_TYPE_BOOL, .o_get = pair1_sock_get_poly, .o_set = pair1_sock_set_poly, }, diff --git a/src/protocol/pipeline0/pull.c b/src/protocol/pipeline0/pull.c index b0f089b1..cc98c895 100644 --- a/src/protocol/pipeline0/pull.c +++ b/src/protocol/pipeline0/pull.c @@ -216,7 +216,7 @@ static nni_proto_pipe_ops pull0_pipe_ops = { .pipe_stop = pull0_pipe_stop, }; -static nni_proto_option pull0_sock_options[] = { +static nni_option pull0_sock_options[] = { // terminate list { .o_name = NULL, diff --git a/src/protocol/pipeline0/push.c b/src/protocol/pipeline0/push.c index c31bec4e..1c0657a8 100644 --- a/src/protocol/pipeline0/push.c +++ b/src/protocol/pipeline0/push.c @@ -228,7 +228,7 @@ static nni_proto_pipe_ops push0_pipe_ops = { .pipe_stop = push0_pipe_stop, }; -static nni_proto_option push0_sock_options[] = { +static nni_option push0_sock_options[] = { // terminate list { .o_name = NULL, diff --git a/src/protocol/pubsub0/pub.c b/src/protocol/pubsub0/pub.c index e1c182e8..2567a5b6 100644 --- a/src/protocol/pubsub0/pub.c +++ b/src/protocol/pubsub0/pub.c @@ -304,7 +304,7 @@ static nni_proto_pipe_ops pub0_pipe_ops = { .pipe_stop = pub0_pipe_stop, }; -static nni_proto_option pub0_sock_options[] = { +static nni_option pub0_sock_options[] = { // terminate list { .o_name = NULL, diff --git a/src/protocol/pubsub0/sub.c b/src/protocol/pubsub0/sub.c index 7bb4485a..5240fc83 100644 --- a/src/protocol/pubsub0/sub.c +++ b/src/protocol/pubsub0/sub.c @@ -352,15 +352,13 @@ static nni_proto_pipe_ops sub0_pipe_ops = { .pipe_stop = sub0_pipe_stop, }; -static nni_proto_option sub0_sock_options[] = { +static nni_option sub0_sock_options[] = { { .o_name = NNG_OPT_SUB_SUBSCRIBE, - .o_type = NNI_TYPE_OPAQUE, .o_set = sub0_subscribe, }, { .o_name = NNG_OPT_SUB_UNSUBSCRIBE, - .o_type = NNI_TYPE_OPAQUE, .o_set = sub0_unsubscribe, }, // terminate list diff --git a/src/protocol/reqrep0/rep.c b/src/protocol/reqrep0/rep.c index 875918f5..5f856f8e 100644 --- a/src/protocol/reqrep0/rep.c +++ b/src/protocol/reqrep0/rep.c @@ -679,21 +679,18 @@ static nni_proto_ctx_ops rep0_ctx_ops = { .ctx_recv = rep0_ctx_recv, }; -static nni_proto_option rep0_sock_options[] = { +static nni_option rep0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = rep0_sock_get_maxttl, .o_set = rep0_sock_set_maxttl, }, { .o_name = NNG_OPT_RECVFD, - .o_type = NNI_TYPE_INT32, .o_get = rep0_sock_get_recvfd, }, { .o_name = NNG_OPT_SENDFD, - .o_type = NNI_TYPE_INT32, .o_get = rep0_sock_get_sendfd, }, // terminate list diff --git a/src/protocol/reqrep0/req.c b/src/protocol/reqrep0/req.c index 139427a2..69ef27f2 100644 --- a/src/protocol/reqrep0/req.c +++ b/src/protocol/reqrep0/req.c @@ -851,10 +851,9 @@ static nni_proto_pipe_ops req0_pipe_ops = { .pipe_stop = req0_pipe_stop, }; -static nni_proto_option req0_ctx_options[] = { +static nni_option req0_ctx_options[] = { { .o_name = NNG_OPT_REQ_RESENDTIME, - .o_type = NNI_TYPE_DURATION, .o_get = req0_ctx_get_resendtime, .o_set = req0_ctx_set_resendtime, }, @@ -871,27 +870,23 @@ static nni_proto_ctx_ops req0_ctx_ops = { .ctx_options = req0_ctx_options, }; -static nni_proto_option req0_sock_options[] = { +static nni_option req0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = req0_sock_get_maxttl, .o_set = req0_sock_set_maxttl, }, { .o_name = NNG_OPT_REQ_RESENDTIME, - .o_type = NNI_TYPE_DURATION, .o_get = req0_sock_get_resendtime, .o_set = req0_sock_set_resendtime, }, { .o_name = NNG_OPT_RECVFD, - .o_type = NNI_TYPE_INT32, .o_get = req0_sock_get_recvfd, }, { .o_name = NNG_OPT_SENDFD, - .o_type = NNI_TYPE_INT32, .o_get = req0_sock_get_sendfd, }, // terminate list diff --git a/src/protocol/reqrep0/xrep.c b/src/protocol/reqrep0/xrep.c index d89722d6..09c11cda 100644 --- a/src/protocol/reqrep0/xrep.c +++ b/src/protocol/reqrep0/xrep.c @@ -408,10 +408,9 @@ static nni_proto_pipe_ops xrep0_pipe_ops = { .pipe_stop = xrep0_pipe_stop, }; -static nni_proto_option xrep0_sock_options[] = { +static nni_option xrep0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = xrep0_sock_get_maxttl, .o_set = xrep0_sock_set_maxttl, }, diff --git a/src/protocol/reqrep0/xreq.c b/src/protocol/reqrep0/xreq.c index 271c59a6..119b2449 100644 --- a/src/protocol/reqrep0/xreq.c +++ b/src/protocol/reqrep0/xreq.c @@ -289,10 +289,9 @@ static nni_proto_pipe_ops xreq0_pipe_ops = { .pipe_stop = xreq0_pipe_stop, }; -static nni_proto_option xreq0_sock_options[] = { +static nni_option xreq0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = xreq0_sock_get_maxttl, .o_set = xreq0_sock_set_maxttl, }, diff --git a/src/protocol/survey0/respond.c b/src/protocol/survey0/respond.c index 593b9bae..caecf719 100644 --- a/src/protocol/survey0/respond.c +++ b/src/protocol/survey0/respond.c @@ -656,22 +656,19 @@ static nni_proto_ctx_ops resp0_ctx_ops = { .ctx_recv = resp0_ctx_recv, }; -static nni_proto_option resp0_sock_options[] = { +static nni_option resp0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = resp0_sock_get_maxttl, .o_set = resp0_sock_set_maxttl, }, { .o_name = NNG_OPT_RECVFD, - .o_type = NNI_TYPE_INT32, .o_get = resp0_sock_get_recvfd, .o_set = NULL, }, { .o_name = NNG_OPT_SENDFD, - .o_type = NNI_TYPE_INT32, .o_get = resp0_sock_get_sendfd, .o_set = NULL, }, diff --git a/src/protocol/survey0/survey.c b/src/protocol/survey0/survey.c index a2eb7379..3ecc1457 100644 --- a/src/protocol/survey0/survey.c +++ b/src/protocol/survey0/survey.c @@ -551,10 +551,9 @@ static nni_proto_pipe_ops surv0_pipe_ops = { .pipe_stop = surv0_pipe_stop, }; -static nni_proto_option surv0_ctx_options[] = { +static nni_option surv0_ctx_options[] = { { .o_name = NNG_OPT_SURVEYOR_SURVEYTIME, - .o_type = NNI_TYPE_DURATION, .o_get = surv0_ctx_get_surveytime, .o_set = surv0_ctx_set_surveytime, }, @@ -570,27 +569,23 @@ static nni_proto_ctx_ops surv0_ctx_ops = { .ctx_options = surv0_ctx_options, }; -static nni_proto_option surv0_sock_options[] = { +static nni_option surv0_sock_options[] = { { .o_name = NNG_OPT_SURVEYOR_SURVEYTIME, - .o_type = NNI_TYPE_DURATION, .o_get = surv0_sock_get_surveytime, .o_set = surv0_sock_set_surveytime, }, { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = surv0_sock_get_maxttl, .o_set = surv0_sock_set_maxttl, }, { .o_name = NNG_OPT_RECVFD, - .o_type = NNI_TYPE_INT32, .o_get = surv0_sock_get_recvfd, }, { .o_name = NNG_OPT_SENDFD, - .o_type = NNI_TYPE_INT32, .o_get = surv0_sock_get_sendfd, }, // terminate list diff --git a/src/protocol/survey0/xrespond.c b/src/protocol/survey0/xrespond.c index 9c9a1a16..865a94f3 100644 --- a/src/protocol/survey0/xrespond.c +++ b/src/protocol/survey0/xrespond.c @@ -385,10 +385,9 @@ static nni_proto_pipe_ops xresp0_pipe_ops = { .pipe_stop = xresp0_pipe_stop, }; -static nni_proto_option xresp0_sock_options[] = { +static nni_option xresp0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = xresp0_sock_get_maxttl, .o_set = xresp0_sock_set_maxttl, }, diff --git a/src/protocol/survey0/xsurvey.c b/src/protocol/survey0/xsurvey.c index 83a7c589..9cf5af1f 100644 --- a/src/protocol/survey0/xsurvey.c +++ b/src/protocol/survey0/xsurvey.c @@ -356,10 +356,9 @@ static nni_proto_pipe_ops xsurv0_pipe_ops = { .pipe_stop = xsurv0_pipe_stop, }; -static nni_proto_option xsurv0_sock_options[] = { +static nni_option xsurv0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_type = NNI_TYPE_INT32, .o_get = xsurv0_sock_get_maxttl, .o_set = xsurv0_sock_set_maxttl, }, diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c index 6199e949..798a946f 100644 --- a/src/transport/inproc/inproc.c +++ b/src/transport/inproc/inproc.c @@ -642,7 +642,8 @@ inproc_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) nni_inproc_ep *ep = arg; size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->rcvmax = val; nni_stat_set_value(&ep->st_rcvmaxsz, val); @@ -651,21 +652,13 @@ inproc_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) return (rv); } -static int -inproc_check_recvmaxsz(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, data, sz, 0, NNI_MAXSZ, t)); -} - -static nni_tran_option nni_inproc_pipe_options[] = { +static nni_option nni_inproc_pipe_options[] = { { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = nni_inproc_pipe_get_addr, }, { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = nni_inproc_pipe_get_addr, }, // terminate list @@ -684,13 +677,11 @@ static nni_tran_pipe_ops nni_inproc_pipe_ops = { .p_options = nni_inproc_pipe_options, }; -static nni_tran_option nni_inproc_ep_options[] = { +static nni_option nni_inproc_ep_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = inproc_ep_get_recvmaxsz, .o_set = inproc_ep_set_recvmaxsz, - .o_chk = inproc_check_recvmaxsz, }, // terminate list { diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c index d6280a69..25d53be8 100644 --- a/src/transport/ipc/ipc.c +++ b/src/transport/ipc/ipc.c @@ -871,7 +871,9 @@ ipctran_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) ipctran_ep *ep = arg; size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (ep != NULL)) { + ipctran_pipe *p; nni_mtx_lock(&ep->mtx); ep->rcvmax = val; @@ -937,12 +939,6 @@ ipctran_ep_get_locaddr(void *arg, void *buf, size_t *szp, nni_opt_type t) } static int -ipctran_check_recvmaxsz(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, data, sz, 0, NNI_MAXSZ, t)); -} - -static int ipctran_ep_set_perms(void *arg, const void *data, size_t sz, nni_opt_type t) { ipctran_ep *ep = arg; @@ -951,7 +947,8 @@ ipctran_ep_set_perms(void *arg, const void *data, size_t sz, nni_opt_type t) // Probably we could further limit this -- most systems don't have // meaningful chmod beyond the lower 9 bits. - if ((rv = nni_copyin_int(&val, data, sz, 0, 0x7FFFFFFF, t)) == 0) { + if (((rv = nni_copyin_int(&val, data, sz, 0, 0x7FFFFFFF, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nni_ipc_listener_set_permissions(ep->listener, val); nni_mtx_unlock(&ep->mtx); @@ -960,19 +957,13 @@ ipctran_ep_set_perms(void *arg, const void *data, size_t sz, nni_opt_type t) } static int -ipctran_check_perms(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_int(NULL, data, sz, 0, 0x7FFFFFFF, t)); -} - -static int ipctran_ep_set_sec_desc(void *arg, const void *data, size_t sz, nni_opt_type t) { ipctran_ep *ep = arg; void * ptr; int rv; - if ((rv = nni_copyin_ptr(&ptr, data, sz, t)) == 0) { + if (((rv = nni_copyin_ptr(&ptr, data, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nni_ipc_listener_set_security_descriptor( ep->listener, ptr); @@ -981,41 +972,29 @@ ipctran_ep_set_sec_desc(void *arg, const void *data, size_t sz, nni_opt_type t) return (rv); } -static int -ipctran_check_sec_desc(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_ptr(NULL, data, sz, t)); -} - -static nni_tran_option ipctran_pipe_options[] = { +static nni_option ipctran_pipe_options[] = { { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ipctran_pipe_get_addr, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ipctran_pipe_get_addr, }, { .o_name = NNG_OPT_IPC_PEER_UID, - .o_type = NNI_TYPE_UINT64, .o_get = ipctran_pipe_get_peer_uid, }, { .o_name = NNG_OPT_IPC_PEER_GID, - .o_type = NNI_TYPE_UINT64, .o_get = ipctran_pipe_get_peer_gid, }, { .o_name = NNG_OPT_IPC_PEER_PID, - .o_type = NNI_TYPE_UINT64, .o_get = ipctran_pipe_get_peer_pid, }, { .o_name = NNG_OPT_IPC_PEER_ZONEID, - .o_type = NNI_TYPE_UINT64, .o_get = ipctran_pipe_get_peer_zoneid, }, // terminate list @@ -1035,17 +1014,14 @@ static nni_tran_pipe_ops ipctran_pipe_ops = { .p_options = ipctran_pipe_options, }; -static nni_tran_option ipctran_ep_dialer_options[] = { +static nni_option ipctran_ep_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ipctran_ep_get_recvmaxsz, .o_set = ipctran_ep_set_recvmaxsz, - .o_chk = ipctran_check_recvmaxsz, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ipctran_ep_get_locaddr, }, // terminate list @@ -1054,32 +1030,23 @@ static nni_tran_option ipctran_ep_dialer_options[] = { }, }; -static nni_tran_option ipctran_ep_listener_options[] = { +static nni_option ipctran_ep_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ipctran_ep_get_recvmaxsz, .o_set = ipctran_ep_set_recvmaxsz, - .o_chk = ipctran_check_recvmaxsz, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ipctran_ep_get_locaddr, }, { .o_name = NNG_OPT_IPC_SECURITY_DESCRIPTOR, - .o_type = NNI_TYPE_POINTER, - .o_get = NULL, .o_set = ipctran_ep_set_sec_desc, - .o_chk = ipctran_check_sec_desc, }, { .o_name = NNG_OPT_IPC_PERMISSIONS, - .o_type = NNI_TYPE_INT32, - .o_get = NULL, .o_set = ipctran_ep_set_perms, - .o_chk = ipctran_check_perms, }, // terminate list { diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index be8f6bc3..4f7dbd72 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -996,7 +996,8 @@ tcptran_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) tcptran_ep *ep = arg; size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (ep != NULL)) { tcptran_pipe *p; nni_mtx_lock(&ep->mtx); ep->rcvmax = val; @@ -1025,7 +1026,7 @@ tcptran_ep_set_nodelay(void *arg, const void *v, size_t sz, nni_opt_type t) tcptran_ep *ep = arg; bool val; int rv; - if ((rv = nni_copyin_bool(&val, v, sz, t)) == 0) { + if (((rv = nni_copyin_bool(&val, v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->nodelay = val; nni_mtx_unlock(&ep->mtx); @@ -1051,7 +1052,7 @@ tcptran_ep_set_keepalive(void *arg, const void *v, size_t sz, nni_opt_type t) tcptran_ep *ep = arg; bool val; int rv; - if ((rv = nni_copyin_bool(&val, v, sz, t)) == 0) { + if (((rv = nni_copyin_bool(&val, v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->keepalive = val; nni_mtx_unlock(&ep->mtx); @@ -1114,37 +1115,21 @@ tcptran_ep_get_locaddr(void *arg, void *buf, size_t *szp, nni_opt_type t) return (rv); } -static int -tcptran_check_bool(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_bool(NULL, v, sz, t)); -} - -static int -tcptran_check_recvmaxsz(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)); -} - -static nni_tran_option tcptran_pipe_options[] = { +static nni_option tcptran_pipe_options[] = { { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tcptran_pipe_get_locaddr, }, { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tcptran_pipe_get_remaddr, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_pipe_get_keepalive, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_pipe_get_nodelay, }, // terminate list @@ -1164,32 +1149,25 @@ static nni_tran_pipe_ops tcptran_pipe_ops = { .p_options = tcptran_pipe_options, }; -static nni_tran_option tcptran_dialer_options[] = { +static nni_option tcptran_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = tcptran_ep_get_recvmaxsz, .o_set = tcptran_ep_set_recvmaxsz, - .o_chk = tcptran_check_recvmaxsz, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = tcptran_ep_get_url, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_ep_get_nodelay, .o_set = tcptran_ep_set_nodelay, - .o_chk = tcptran_check_bool, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_ep_get_keepalive, .o_set = tcptran_ep_set_keepalive, - .o_chk = tcptran_check_bool, }, // terminate list { @@ -1197,37 +1175,29 @@ static nni_tran_option tcptran_dialer_options[] = { }, }; -static nni_tran_option tcptran_listener_options[] = { +static nni_option tcptran_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = tcptran_ep_get_recvmaxsz, .o_set = tcptran_ep_set_recvmaxsz, - .o_chk = tcptran_check_recvmaxsz, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tcptran_ep_get_locaddr, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = tcptran_ep_get_url, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_ep_get_nodelay, .o_set = tcptran_ep_set_nodelay, - .o_chk = tcptran_check_bool, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tcptran_ep_get_keepalive, .o_set = tcptran_ep_set_keepalive, - .o_chk = tcptran_check_bool, }, // terminate list { diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c index 867be9b8..35356054 100644 --- a/src/transport/tls/tls.c +++ b/src/transport/tls/tls.c @@ -15,8 +15,8 @@ #include "core/nng_impl.h" #include "nng/supplemental/tls/tls.h" -#include "supplemental/tls/tls_api.h" #include "nng/transport/tls/tls.h" +#include "supplemental/tls/tls_api.h" // TLS over TCP transport. Platform specific TCP operations must be // supplied as well, and uses the supplemental TLS v1.2 code. It is not @@ -1000,7 +1000,7 @@ tlstran_ep_set_nodelay(void *arg, const void *v, size_t sz, nni_opt_type t) tlstran_ep *ep = arg; bool val; int rv; - if ((rv = nni_copyin_bool(&val, v, sz, t)) == 0) { + if (((rv = nni_copyin_bool(&val, v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->nodelay = val; nni_mtx_unlock(&ep->mtx); @@ -1025,7 +1025,8 @@ tlstran_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) tlstran_ep *ep = arg; size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->rcvmax = val; nni_mtx_unlock(&ep->mtx); @@ -1039,7 +1040,7 @@ tlstran_ep_set_keepalive(void *arg, const void *v, size_t sz, nni_opt_type t) tlstran_ep *ep = arg; bool val; int rv; - if ((rv = nni_copyin_bool(&val, v, sz, t)) == 0) { + if (((rv = nni_copyin_bool(&val, v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); ep->keepalive = val; nni_mtx_unlock(&ep->mtx); @@ -1070,12 +1071,6 @@ tlstran_ep_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t) } static int -tlstran_check_bool(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_bool(NULL, v, sz, t)); -} - -static int tlstran_ep_get_url(void *arg, void *v, size_t *szp, nni_opt_type t) { tlstran_ep *ep = arg; @@ -1106,27 +1101,10 @@ tlstran_ep_get_locaddr(void *arg, void *buf, size_t *szp, nni_opt_type t) } static int -tlstran_check_recvmaxsz(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)); -} - -static int -tlstran_check_config(const void *data, size_t sz, nni_opt_type t) -{ - void *v; - int rv; - if (((rv = nni_copyin_ptr(&v, data, sz, t)) == 0) && (v == NULL)) { - rv = NNG_EINVAL; - } - return (rv); -} - -static int tlstran_ep_set_config(void *arg, const void *data, size_t sz, nni_opt_type t) { tlstran_ep * ep = arg; - nng_tls_config *cfg, *old; + nng_tls_config *cfg; int rv; if ((rv = nni_copyin_ptr((void **) &cfg, data, sz, t)) != 0) { @@ -1135,13 +1113,17 @@ tlstran_ep_set_config(void *arg, const void *data, size_t sz, nni_opt_type t) if (cfg == NULL) { return (NNG_EINVAL); } - nni_mtx_lock(&ep->mtx); - old = ep->cfg; - nni_tls_config_hold(cfg); - ep->cfg = cfg; - nni_mtx_unlock(&ep->mtx); - if (old != NULL) { - nni_tls_config_fini(old); + if (ep != NULL) { + nng_tls_config *old; + + nni_mtx_lock(&ep->mtx); + old = ep->cfg; + nni_tls_config_hold(cfg); + ep->cfg = cfg; + nni_mtx_unlock(&ep->mtx); + if (old != NULL) { + nni_tls_config_fini(old); + } } return (0); } @@ -1179,7 +1161,7 @@ tlstran_ep_set_ca_file(void *arg, const void *v, size_t sz, nni_opt_type t) tlstran_ep *ep = arg; int rv; - if ((rv = tlstran_check_string(v, sz, t)) == 0) { + if (((rv = tlstran_check_string(v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nng_tls_config_ca_file(ep->cfg, v); nni_mtx_unlock(&ep->mtx); @@ -1188,13 +1170,6 @@ tlstran_ep_set_ca_file(void *arg, const void *v, size_t sz, nni_opt_type t) } static int -tlstran_check_auth_mode(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_int(NULL, v, sz, NNG_TLS_AUTH_MODE_NONE, - NNG_TLS_AUTH_MODE_REQUIRED, t)); -} - -static int tlstran_ep_set_auth_mode(void *arg, const void *v, size_t sz, nni_opt_type t) { tlstran_ep *ep = arg; @@ -1203,7 +1178,7 @@ tlstran_ep_set_auth_mode(void *arg, const void *v, size_t sz, nni_opt_type t) rv = nni_copyin_int(&mode, v, sz, NNG_TLS_AUTH_MODE_NONE, NNG_TLS_AUTH_MODE_REQUIRED, t); - if (rv == 0) { + if ((rv == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nng_tls_config_auth_mode(ep->cfg, mode); nni_mtx_unlock(&ep->mtx); @@ -1217,7 +1192,7 @@ tlstran_ep_set_server_name(void *arg, const void *v, size_t sz, nni_opt_type t) tlstran_ep *ep = arg; int rv; - if ((rv = tlstran_check_string(v, sz, t)) == 0) { + if (((rv = tlstran_check_string(v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nng_tls_config_server_name(ep->cfg, v); nni_mtx_unlock(&ep->mtx); @@ -1232,7 +1207,7 @@ tlstran_ep_set_cert_key_file( tlstran_ep *ep = arg; int rv; - if ((rv = tlstran_check_string(v, sz, t)) == 0) { + if (((rv = tlstran_check_string(v, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&ep->mtx); rv = nng_tls_config_cert_key_file(ep->cfg, v, NULL); nni_mtx_unlock(&ep->mtx); @@ -1248,30 +1223,25 @@ tlstran_pipe_get_verified(void *arg, void *v, size_t *szp, nni_opt_type t) return (nni_copyout_bool(nni_tls_verified(p->tls), v, szp, t)); } -static nni_tran_option tlstran_pipe_options[] = { +static nni_option tlstran_pipe_options[] = { { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tlstran_pipe_get_locaddr, }, { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tlstran_pipe_get_remaddr, }, { .o_name = NNG_OPT_TLS_VERIFIED, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_pipe_get_verified, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_pipe_get_keepalive, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_pipe_get_nodelay, }, // terminate list @@ -1291,63 +1261,46 @@ static nni_tran_pipe_ops tlstran_pipe_ops = { .p_options = tlstran_pipe_options, }; -static nni_tran_option tlstran_dialer_options[] = { +static nni_option tlstran_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = tlstran_ep_get_recvmaxsz, .o_set = tlstran_ep_set_recvmaxsz, - .o_chk = tlstran_check_recvmaxsz, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = tlstran_ep_get_url, }, { .o_name = NNG_OPT_TLS_CONFIG, - .o_type = NNI_TYPE_POINTER, .o_get = tlstran_ep_get_config, .o_set = tlstran_ep_set_config, - .o_chk = tlstran_check_config, }, { .o_name = NNG_OPT_TLS_CERT_KEY_FILE, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_cert_key_file, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TLS_CA_FILE, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_ca_file, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TLS_AUTH_MODE, - .o_type = NNI_TYPE_INT32, // enum really .o_set = tlstran_ep_set_auth_mode, - .o_chk = tlstran_check_auth_mode, }, { .o_name = NNG_OPT_TLS_SERVER_NAME, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_server_name, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_ep_get_nodelay, .o_set = tlstran_ep_set_nodelay, - .o_chk = tlstran_check_bool, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_ep_get_keepalive, .o_set = tlstran_ep_set_keepalive, - .o_chk = tlstran_check_bool, }, // terminate list { @@ -1355,68 +1308,50 @@ static nni_tran_option tlstran_dialer_options[] = { }, }; -static nni_tran_option tlstran_listener_options[] = { +static nni_option tlstran_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = tlstran_ep_get_recvmaxsz, .o_set = tlstran_ep_set_recvmaxsz, - .o_chk = tlstran_check_recvmaxsz, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = tlstran_ep_get_url, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = tlstran_ep_get_locaddr, }, { .o_name = NNG_OPT_TLS_CONFIG, - .o_type = NNI_TYPE_POINTER, .o_get = tlstran_ep_get_config, .o_set = tlstran_ep_set_config, - .o_chk = tlstran_check_config, }, { .o_name = NNG_OPT_TLS_CERT_KEY_FILE, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_cert_key_file, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TLS_CA_FILE, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_ca_file, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TLS_AUTH_MODE, - .o_type = NNI_TYPE_INT32, // enum really .o_set = tlstran_ep_set_auth_mode, - .o_chk = tlstran_check_auth_mode, }, { .o_name = NNG_OPT_TLS_SERVER_NAME, - .o_type = NNI_TYPE_STRING, .o_set = tlstran_ep_set_server_name, - .o_chk = tlstran_check_string, }, { .o_name = NNG_OPT_TCP_NODELAY, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_ep_get_nodelay, .o_set = tlstran_ep_set_nodelay, - .o_chk = tlstran_check_bool, }, { .o_name = NNG_OPT_TCP_KEEPALIVE, - .o_type = NNI_TYPE_BOOL, .o_get = tlstran_ep_get_keepalive, .o_set = tlstran_ep_set_keepalive, - .o_chk = tlstran_check_bool, }, // terminate list { diff --git a/src/transport/ws/websocket.c b/src/transport/ws/websocket.c index 12a1ef17..1d5358f6 100644 --- a/src/transport/ws/websocket.c +++ b/src/transport/ws/websocket.c @@ -14,8 +14,8 @@ #include <string.h> #include "core/nng_impl.h" -#include "supplemental/http/http_api.h" #include "nng/supplemental/tls/tls.h" +#include "supplemental/http/http_api.h" #include "supplemental/tls/tls_api.h" #include "supplemental/websocket/websocket.h" @@ -414,7 +414,8 @@ ws_dialer_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (d != NULL)) { nni_mtx_lock(&d->mtx); d->rcvmax = val; nni_mtx_unlock(&d->mtx); @@ -441,7 +442,8 @@ ws_listener_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t) size_t val; int rv; - if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) && + (l != NULL)) { nni_mtx_lock(&l->mtx); l->rcvmax = val; nni_mtx_unlock(&l->mtx); @@ -462,12 +464,6 @@ ws_listener_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t) } static int -ws_check_recvmaxsz(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)); -} - -static int ws_set_headers(nni_list *headers, const char *v) { char * dupstr; @@ -552,11 +548,10 @@ ws_dialer_set_reqhdrs(void *arg, const void *v, size_t sz, nni_opt_type t) ws_dialer *d = arg; int rv; - if (d->started) { - return (NNG_EBUSY); - } - - if ((rv = ws_check_string(v, sz, t)) == 0) { + if (((rv = ws_check_string(v, sz, t)) == 0) && (d != NULL)) { + if (d->started) { + return (NNG_EBUSY); + } nni_mtx_lock(&d->mtx); rv = ws_set_headers(&d->headers, v); nni_mtx_unlock(&d->mtx); @@ -570,10 +565,10 @@ ws_listener_set_reshdrs(void *arg, const void *v, size_t sz, nni_opt_type t) ws_listener *l = arg; int rv; - if (l->started) { - return (NNG_EBUSY); - } - if ((rv = ws_check_string(v, sz, t)) == 0) { + if (((rv = ws_check_string(v, sz, t)) == 0) && (l != NULL)) { + if (l->started) { + return (NNG_EBUSY); + } nni_mtx_lock(&l->mtx); rv = ws_set_headers(&l->headers, v); nni_mtx_unlock(&l->mtx); @@ -640,31 +635,26 @@ ws_pipe_get_tls_verified(void *arg, void *v, size_t *szp, nni_opt_type t) return (nni_copyout_bool(nni_ws_tls_verified(p->ws), v, szp, t)); } -static nni_tran_option ws_pipe_options[] = { +static nni_option ws_pipe_options[] = { { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ws_pipe_get_locaddr, }, { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = ws_pipe_get_remaddr, }, { .o_name = NNG_OPT_WS_REQUEST_HEADERS, - .o_type = NNI_TYPE_STRING, .o_get = ws_pipe_get_reqhdrs, }, { .o_name = NNG_OPT_WS_RESPONSE_HEADERS, - .o_type = NNI_TYPE_STRING, .o_get = ws_pipe_get_reshdrs, }, { .o_name = NNG_OPT_TLS_VERIFIED, - .o_type = NNI_TYPE_BOOL, .o_get = ws_pipe_get_tls_verified, }, // terminate list @@ -684,19 +674,15 @@ static nni_tran_pipe_ops ws_pipe_ops = { .p_options = ws_pipe_options, }; -static nni_tran_option ws_dialer_options[] = { +static nni_option ws_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ws_dialer_get_recvmaxsz, .o_set = ws_dialer_set_recvmaxsz, - .o_chk = ws_check_recvmaxsz, }, { .o_name = NNG_OPT_WS_REQUEST_HEADERS, - .o_type = NNI_TYPE_STRING, .o_set = ws_dialer_set_reqhdrs, - .o_chk = ws_check_string, }, // terminate list { @@ -704,19 +690,15 @@ static nni_tran_option ws_dialer_options[] = { }, }; -static nni_tran_option ws_listener_options[] = { +static nni_option ws_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ws_listener_get_recvmaxsz, .o_set = ws_listener_set_recvmaxsz, - .o_chk = ws_check_recvmaxsz, }, { .o_name = NNG_OPT_WS_RESPONSE_HEADERS, - .o_type = NNI_TYPE_STRING, .o_set = ws_listener_set_reshdrs, - .o_chk = ws_check_string, }, // terminate list { @@ -1006,17 +988,6 @@ wss_listener_get_tlsconfig(void *arg, void *v, size_t *szp, nni_opt_type t) } static int -wss_check_tlsconfig(const void *v, size_t sz, nni_opt_type t) -{ - void *p; - int rv; - if (((rv = nni_copyin_ptr(&p, v, sz, t)) == 0) && (p == NULL)) { - rv = NNG_EINVAL; - } - return (rv); -} - -static int wss_dialer_set_tlsconfig(void *arg, const void *v, size_t sz, nni_opt_type t) { ws_dialer * d = arg; @@ -1029,7 +1000,10 @@ wss_dialer_set_tlsconfig(void *arg, const void *v, size_t sz, nni_opt_type t) if (cfg == NULL) { return (NNG_EINVAL); } - return (nni_ws_dialer_set_tls(d->dialer, cfg)); + if (d != NULL) { + rv = nni_ws_dialer_set_tls(d->dialer, cfg); + } + return (rv); } static int @@ -1045,23 +1019,28 @@ wss_listener_set_tlsconfig(void *arg, const void *v, size_t sz, nni_opt_type t) if (cfg == NULL) { return (NNG_EINVAL); } - return (nni_ws_listener_set_tls(l->listener, cfg)); + if (l != NULL) { + rv = nni_ws_listener_set_tls(l->listener, cfg); + } + return (rv); } static int wss_dialer_set_cert_key_file( void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_dialer * d = arg; - int rv; - nng_tls_config *tls; + ws_dialer *d = arg; + int rv; - if (((rv = ws_check_string(v, sz, t)) != 0) || - ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0)) { - return (rv); + if (((rv = ws_check_string(v, sz, t)) == 0) && (d != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_cert_key_file(tls, v, NULL); + nni_tls_config_fini(tls); } - rv = nng_tls_config_cert_key_file(tls, v, NULL); - nni_tls_config_fini(tls); return (rv); } @@ -1069,95 +1048,98 @@ static int wss_listener_set_cert_key_file( void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_listener * l = arg; - int rv; - nng_tls_config *tls; + ws_listener *l = arg; + int rv; - if (((rv = ws_check_string(v, sz, t)) != 0) || - ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0)) { - return (rv); + if (((rv = ws_check_string(v, sz, t)) == 0) && (l != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_cert_key_file(tls, v, NULL); + nni_tls_config_fini(tls); } - rv = nng_tls_config_cert_key_file(tls, v, NULL); - nni_tls_config_fini(tls); return (rv); } static int wss_dialer_set_ca_file(void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_dialer * d = arg; - int rv; - nng_tls_config *tls; + ws_dialer *d = arg; + int rv; - if (((rv = ws_check_string(v, sz, t)) != 0) || - ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0)) { - return (rv); + if (((rv = ws_check_string(v, sz, t)) == 0) && (d != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_ca_file(tls, v); + nni_tls_config_fini(tls); } - rv = nng_tls_config_ca_file(tls, v); - nni_tls_config_fini(tls); return (rv); } static int wss_listener_set_ca_file(void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_listener * l = arg; - int rv; - nng_tls_config *tls; + ws_listener *l = arg; + int rv; - if (((rv = ws_check_string(v, sz, t)) != 0) || - ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0)) { - return (rv); + if (((rv = ws_check_string(v, sz, t)) == 0) && (l != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_ca_file(tls, v); + nni_tls_config_fini(tls); } - rv = nng_tls_config_ca_file(tls, v); - nni_tls_config_fini(tls); return (rv); } static int -wss_check_auth_mode(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_int(NULL, v, sz, NNG_TLS_AUTH_MODE_NONE, - NNG_TLS_AUTH_MODE_REQUIRED, t)); -} - -static int wss_dialer_set_auth_mode(void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_dialer * d = arg; - int rv; - nng_tls_config *tls; - int mode; + ws_dialer *d = arg; + int rv; + int mode; rv = nni_copyin_int(&mode, v, sz, NNG_TLS_AUTH_MODE_NONE, NNG_TLS_AUTH_MODE_REQUIRED, t); - if ((rv != 0) || - ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0)) { - return (rv); + if ((rv == 0) && (d != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_auth_mode(tls, mode); + nni_tls_config_fini(tls); } - rv = nng_tls_config_auth_mode(tls, mode); - nni_tls_config_fini(tls); return (rv); } static int wss_listener_set_auth_mode(void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_listener * l = arg; - int rv; - nng_tls_config *tls; - int mode; + ws_listener *l = arg; + int rv; + int mode; rv = nni_copyin_int(&mode, v, sz, NNG_TLS_AUTH_MODE_NONE, NNG_TLS_AUTH_MODE_REQUIRED, t); - if ((rv != 0) || - ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0)) { - return (rv); + if ((rv == 0) && (l != NULL)) { + nng_tls_config *tls; + + if ((rv = nni_ws_listener_get_tls(l->listener, &tls)) != 0) { + return (rv); + } + rv = nng_tls_config_auth_mode(tls, mode); + nni_tls_config_fini(tls); } - rv = nng_tls_config_auth_mode(tls, mode); - nni_tls_config_fini(tls); return (rv); } @@ -1165,64 +1147,52 @@ static int wss_dialer_set_tls_server_name( void *arg, const void *v, size_t sz, nni_opt_type t) { - ws_dialer * d = arg; - int rv; - nng_tls_config *tls; + ws_dialer *d = arg; + int rv; - if (((rv = ws_check_string(v, sz, t)) != 0) || - ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0)) { - return (rv); - } + if (((rv = ws_check_string(v, sz, t)) == 0) && (d != NULL)) { + nng_tls_config *tls; - rv = nng_tls_config_server_name(tls, v); - nni_tls_config_fini(tls); + if ((rv = nni_ws_dialer_get_tls(d->dialer, &tls)) != 0) { + return (rv); + } + + rv = nng_tls_config_server_name(tls, v); + nni_tls_config_fini(tls); + } return (rv); } -static nni_tran_option wss_dialer_options[] = { +static nni_option wss_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ws_dialer_get_recvmaxsz, .o_set = ws_dialer_set_recvmaxsz, - .o_chk = ws_check_recvmaxsz, }, { .o_name = NNG_OPT_WS_REQUEST_HEADERS, - .o_type = NNI_TYPE_STRING, .o_set = ws_dialer_set_reqhdrs, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_CONFIG, - .o_type = NNI_TYPE_POINTER, .o_get = wss_dialer_get_tlsconfig, .o_set = wss_dialer_set_tlsconfig, - .o_chk = wss_check_tlsconfig, }, { .o_name = NNG_OPT_TLS_CERT_KEY_FILE, - .o_type = NNI_TYPE_STRING, .o_set = wss_dialer_set_cert_key_file, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_CA_FILE, - .o_type = NNI_TYPE_STRING, .o_set = wss_dialer_set_ca_file, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_AUTH_MODE, - .o_type = NNI_TYPE_INT32, .o_set = wss_dialer_set_auth_mode, - .o_chk = wss_check_auth_mode, }, { .o_name = NNG_OPT_TLS_SERVER_NAME, - .o_type = NNI_TYPE_STRING, .o_set = wss_dialer_set_tls_server_name, - .o_chk = ws_check_string, }, // terminate list { @@ -1230,44 +1200,32 @@ static nni_tran_option wss_dialer_options[] = { }, }; -static nni_tran_option wss_listener_options[] = { +static nni_option wss_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = ws_listener_get_recvmaxsz, .o_set = ws_listener_set_recvmaxsz, - .o_chk = ws_check_recvmaxsz, }, { .o_name = NNG_OPT_WS_RESPONSE_HEADERS, - .o_type = NNI_TYPE_STRING, .o_set = ws_listener_set_reshdrs, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_CONFIG, - .o_type = NNI_TYPE_POINTER, .o_get = wss_listener_get_tlsconfig, .o_set = wss_listener_set_tlsconfig, - .o_chk = wss_check_tlsconfig, }, { .o_name = NNG_OPT_TLS_CERT_KEY_FILE, - .o_type = NNI_TYPE_STRING, .o_set = wss_listener_set_cert_key_file, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_CA_FILE, - .o_type = NNI_TYPE_STRING, .o_set = wss_listener_set_ca_file, - .o_chk = ws_check_string, }, { .o_name = NNG_OPT_TLS_AUTH_MODE, - .o_type = NNI_TYPE_INT32, .o_set = wss_listener_set_auth_mode, - .o_chk = wss_check_auth_mode, }, // terminate list { diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index a767e168..47dc4af8 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -2536,19 +2536,14 @@ zt_ep_connect(void *arg, nni_aio *aio) } static int -zt_ep_chk_recvmaxsz(const void *v, size_t sz, nni_opt_type t) -{ - return (nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)); -} - -static int zt_ep_set_recvmaxsz(void *arg, const void *data, size_t sz, nni_opt_type t) { zt_ep *ep = arg; size_t val; int rv; - if ((rv = nni_copyin_size(&val, data, sz, 0, NNI_MAXSZ, t)) == 0) { + if (((rv = nni_copyin_size(&val, data, sz, 0, NNI_MAXSZ, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&zt_lk); ep->ze_rcvmax = val; nni_mtx_unlock(&zt_lk); @@ -2568,7 +2563,7 @@ zt_ep_get_recvmaxsz(void *arg, void *data, size_t *szp, nni_opt_type t) } static int -zt_ep_chk_string(const void *data, size_t sz, nni_opt_type t) +zt_check_string(const void *data, size_t sz, nni_opt_type t) { size_t len; @@ -2588,7 +2583,7 @@ zt_ep_set_home(void *arg, const void *data, size_t sz, nni_opt_type t) int rv; zt_ep *ep = arg; - if ((rv = zt_ep_chk_string(data, sz, t)) == 0) { + if (((rv = zt_check_string(data, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&zt_lk); if (ep->ze_running) { rv = NNG_ESTATE; @@ -2634,26 +2629,6 @@ zt_ep_get_url(void *arg, void *data, size_t *szp, nni_opt_type t) } static int -zt_ep_chk_orbit(const void *data, size_t sz, nni_opt_type t) -{ - NNI_ARG_UNUSED(data); - switch (t) { - case NNI_TYPE_UINT64: - NNI_ASSERT(sz == sizeof(uint64_t)); - break; - case NNI_TYPE_OPAQUE: - if ((sz != sizeof(uint64_t)) && - (sz != (sizeof(uint64_t) * 2))) { - return (NNG_EINVAL); - } - break; - default: - return (NNG_EBADTYPE); - } - return (0); -} - -static int zt_ep_set_orbit(void *arg, const void *data, size_t sz, nni_opt_type t) { uint64_t moonid; @@ -2662,18 +2637,21 @@ zt_ep_set_orbit(void *arg, const void *data, size_t sz, nni_opt_type t) int rv; enum ZT_ResultCode zrv; - if ((rv = zt_ep_chk_orbit(data, sz, t)) != 0) { - return (rv); + if ((t != NNI_TYPE_UINT64) && (t != NNI_TYPE_OPAQUE)) { + return (NNG_EBADTYPE); } - if (sz == sizeof(uint64_t)) { memcpy(&moonid, data, sizeof(moonid)); peerid = 0; - } else { - NNI_ASSERT(sz == (2 * sizeof(uint64_t))); + } else if (sz == sizeof(uint64_t) * 2) { memcpy(&moonid, data, sizeof(moonid)); memcpy(&peerid, ((char *) data) + sizeof(uint64_t), sizeof(peerid)); + } else { + return (NNG_EINVAL); + } + if (ep == NULL) { + return (0); } nni_mtx_lock(&zt_lk); @@ -2688,20 +2666,15 @@ zt_ep_set_orbit(void *arg, const void *data, size_t sz, nni_opt_type t) } static int -zt_ep_chk_deorbit(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_u64(NULL, data, sz, t)); -} - -static int zt_ep_set_deorbit(void *arg, const void *data, size_t sz, nni_opt_type t) { - uint64_t moonid; - zt_ep * ep = arg; - enum ZT_ResultCode zrv; - int rv; + uint64_t moonid; + zt_ep * ep = arg; + int rv; - if ((rv = nni_copyin_u64(&moonid, data, sz, t)) == 0) { + if (((rv = nni_copyin_u64(&moonid, data, sz, t)) == 0) && + (ep != NULL)) { + enum ZT_ResultCode zrv; nni_mtx_lock(&zt_lk); if ((ep->ze_ztn == NULL) && ((rv = zt_node_find(ep)) != 0)) { @@ -2716,34 +2689,16 @@ zt_ep_set_deorbit(void *arg, const void *data, size_t sz, nni_opt_type t) } static int -zt_ep_chk_add_local_addr(const void *data, size_t sz, nni_opt_type t) -{ - int rv; - nng_sockaddr sa; - rv = nni_copyin_sockaddr(&sa, data, sz, t); - if (rv == 0) { - switch (sa.s_family) { - case NNG_AF_INET: - case NNG_AF_INET6: - break; - default: - return (NNG_EINVAL); - } - } - return (0); -} - -static int zt_ep_set_add_local_addr( void *arg, const void *data, size_t sz, nni_opt_type t) { - nng_sockaddr sa; - zt_ep * ep = arg; - enum ZT_ResultCode zrv; - int rv; - ZT_Node * zn; + nng_sockaddr sa; + zt_ep * ep = arg; + int rv; if ((rv = nni_copyin_sockaddr(&sa, data, sz, t)) == 0) { + enum ZT_ResultCode zrv; + ZT_Node * zn; struct sockaddr_storage ss; struct sockaddr_in * sin; struct sockaddr_in6 * sin6; @@ -2766,6 +2721,9 @@ zt_ep_set_add_local_addr( return (NNG_EINVAL); } + if (ep == NULL) { + return (0); + } nni_mtx_lock(&zt_lk); if ((ep->ze_ztn == NULL) && ((rv = zt_node_find(ep)) != 0)) { nni_mtx_unlock(&zt_lk); @@ -2780,33 +2738,26 @@ zt_ep_set_add_local_addr( } static int -zt_ep_chk_clear_local_addrs(const void *data, size_t sz, nni_opt_type t) -{ - NNI_ARG_UNUSED(data); - NNI_ARG_UNUSED(sz); - NNI_ARG_UNUSED(t); - return (0); -} - -static int zt_ep_set_clear_local_addrs( void *arg, const void *data, size_t sz, nni_opt_type t) { - zt_ep * ep = arg; - int rv; - ZT_Node *zn; + zt_ep *ep = arg; NNI_ARG_UNUSED(data); NNI_ARG_UNUSED(sz); NNI_ARG_UNUSED(t); - nni_mtx_lock(&zt_lk); - if ((ep->ze_ztn == NULL) && ((rv = zt_node_find(ep)) != 0)) { + if (ep != NULL) { + int rv; + ZT_Node *zn; + nni_mtx_lock(&zt_lk); + if ((ep->ze_ztn == NULL) && ((rv = zt_node_find(ep)) != 0)) { + nni_mtx_unlock(&zt_lk); + return (rv); + } + zn = ep->ze_ztn; + ZT_Node_clearLocalInterfaceAddresses(zn); nni_mtx_unlock(&zt_lk); - return (rv); } - zn = ep->ze_ztn; - ZT_Node_clearLocalInterfaceAddresses(zn); - nni_mtx_unlock(&zt_lk); return (0); } @@ -2881,19 +2832,13 @@ zt_ep_get_nw_status(void *arg, void *buf, size_t *szp, nni_opt_type t) } static int -zt_ep_chk_time(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_ms(NULL, data, sz, t)); -} - -static int zt_ep_set_ping_time(void *arg, const void *data, size_t sz, nni_opt_type t) { zt_ep * ep = arg; nng_duration val; int rv; - if ((rv = nni_copyin_ms(&val, data, sz, t)) == 0) { + if (((rv = nni_copyin_ms(&val, data, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&zt_lk); ep->ze_ping_time = val; nni_mtx_unlock(&zt_lk); @@ -2914,19 +2859,14 @@ zt_ep_get_ping_time(void *arg, void *data, size_t *szp, nni_opt_type t) } static int -zt_ep_chk_tries(const void *data, size_t sz, nni_opt_type t) -{ - return (nni_copyin_int(NULL, data, sz, 0, 1000000, t)); -} - -static int zt_ep_set_ping_tries(void *arg, const void *data, size_t sz, nni_opt_type t) { zt_ep *ep = arg; int val; int rv; - if ((rv = nni_copyin_int(&val, data, sz, 0, 1000000, t)) == 0) { + if (((rv = nni_copyin_int(&val, data, sz, 0, 1000000, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&zt_lk); ep->ze_ping_tries = val; nni_mtx_unlock(&zt_lk); @@ -2953,7 +2893,7 @@ zt_ep_set_conn_time(void *arg, const void *data, size_t sz, nni_opt_type t) nng_duration val; int rv; - if ((rv = nni_copyin_ms(&val, data, sz, t)) == 0) { + if (((rv = nni_copyin_ms(&val, data, sz, t)) == 0) && (ep != NULL)) { nni_mtx_lock(&zt_lk); ep->ze_conn_time = val; nni_mtx_unlock(&zt_lk); @@ -2980,7 +2920,8 @@ zt_ep_set_conn_tries(void *arg, const void *data, size_t sz, nni_opt_type t) int val; int rv; - if ((rv = nni_copyin_int(&val, data, sz, 0, 1000000, t)) == 0) { + if (((rv = nni_copyin_int(&val, data, sz, 0, 1000000, t)) == 0) && + (ep != NULL)) { nni_mtx_lock(&zt_lk); ep->ze_conn_tries = val; nni_mtx_unlock(&zt_lk); @@ -3051,35 +2992,29 @@ zt_pipe_get_mtu(void *arg, void *data, size_t *szp, nni_opt_type t) return (nni_copyout_size(p->zp_mtu, data, szp, t)); } -static nni_tran_option zt_pipe_options[] = { +static nni_option zt_pipe_options[] = { { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = zt_pipe_get_locaddr, }, { .o_name = NNG_OPT_REMADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = zt_pipe_get_remaddr, }, { .o_name = NNG_OPT_ZT_MTU, - .o_type = NNI_TYPE_SIZE, .o_get = zt_pipe_get_mtu, }, { .o_name = NNG_OPT_ZT_NWID, - .o_type = NNI_TYPE_UINT64, .o_get = zt_pipe_get_nwid, }, { .o_name = NNG_OPT_ZT_NODE, - .o_type = NNI_TYPE_UINT64, .o_get = zt_pipe_get_node, }, { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = zt_pipe_get_recvmaxsz, }, // terminate list @@ -3098,97 +3033,72 @@ static nni_tran_pipe_ops zt_pipe_ops = { .p_options = zt_pipe_options, }; -static nni_tran_option zt_dialer_options[] = { +static nni_option zt_dialer_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = zt_ep_get_recvmaxsz, .o_set = zt_ep_set_recvmaxsz, - .o_chk = zt_ep_chk_recvmaxsz, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_url, }, { .o_name = NNG_OPT_ZT_HOME, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_home, .o_set = zt_ep_set_home, - .o_chk = zt_ep_chk_string, }, { .o_name = NNG_OPT_ZT_NODE, - .o_type = NNI_TYPE_UINT64, .o_get = zt_ep_get_node, }, { .o_name = NNG_OPT_ZT_NWID, - .o_type = NNI_TYPE_UINT64, .o_get = zt_ep_get_nwid, }, { .o_name = NNG_OPT_ZT_NETWORK_STATUS, - .o_type = NNI_TYPE_INT32, // enumeration really .o_get = zt_ep_get_nw_status, }, { .o_name = NNG_OPT_ZT_NETWORK_NAME, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_nw_name, }, { .o_name = NNG_OPT_ZT_PING_TIME, - .o_type = NNI_TYPE_DURATION, .o_get = zt_ep_get_ping_time, .o_set = zt_ep_set_ping_time, - .o_chk = zt_ep_chk_time, }, { .o_name = NNG_OPT_ZT_PING_TRIES, - .o_type = NNI_TYPE_INT32, .o_get = zt_ep_get_ping_tries, .o_set = zt_ep_set_ping_tries, - .o_chk = zt_ep_chk_tries, }, { .o_name = NNG_OPT_ZT_CONN_TIME, - .o_type = NNI_TYPE_DURATION, .o_get = zt_ep_get_conn_time, .o_set = zt_ep_set_conn_time, - .o_chk = zt_ep_chk_time, }, { .o_name = NNG_OPT_ZT_CONN_TRIES, - .o_type = NNI_TYPE_INT32, .o_get = zt_ep_get_conn_tries, .o_set = zt_ep_set_conn_tries, - .o_chk = zt_ep_chk_tries, }, { .o_name = NNG_OPT_ZT_ORBIT, - .o_type = NNI_TYPE_UINT64, // use opaque for two .o_set = zt_ep_set_orbit, - .o_chk = zt_ep_chk_orbit, }, { .o_name = NNG_OPT_ZT_DEORBIT, - .o_type = NNI_TYPE_UINT64, .o_set = zt_ep_set_deorbit, - .o_chk = zt_ep_chk_deorbit, }, { .o_name = NNG_OPT_ZT_ADD_LOCAL_ADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_set = zt_ep_set_add_local_addr, - .o_chk = zt_ep_chk_add_local_addr, }, { .o_name = NNG_OPT_ZT_CLEAR_LOCAL_ADDRS, - .o_type = NNI_TYPE_OPAQUE, .o_set = zt_ep_set_clear_local_addrs, - .o_chk = zt_ep_chk_clear_local_addrs, }, // terminate list @@ -3197,75 +3107,57 @@ static nni_tran_option zt_dialer_options[] = { }, }; -static nni_tran_option zt_listener_options[] = { +static nni_option zt_listener_options[] = { { .o_name = NNG_OPT_RECVMAXSZ, - .o_type = NNI_TYPE_SIZE, .o_get = zt_ep_get_recvmaxsz, .o_set = zt_ep_set_recvmaxsz, - .o_chk = zt_ep_chk_recvmaxsz, }, { .o_name = NNG_OPT_URL, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_url, }, { .o_name = NNG_OPT_ZT_HOME, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_home, .o_set = zt_ep_set_home, - .o_chk = zt_ep_chk_string, }, { .o_name = NNG_OPT_ZT_NODE, - .o_type = NNI_TYPE_UINT64, .o_get = zt_ep_get_node, }, { .o_name = NNG_OPT_ZT_NWID, - .o_type = NNI_TYPE_UINT64, .o_get = zt_ep_get_nwid, }, { .o_name = NNG_OPT_ZT_NETWORK_STATUS, - .o_type = NNI_TYPE_INT32, // enumeration really .o_get = zt_ep_get_nw_status, }, { .o_name = NNG_OPT_ZT_NETWORK_NAME, - .o_type = NNI_TYPE_STRING, .o_get = zt_ep_get_nw_name, }, { .o_name = NNG_OPT_ZT_PING_TIME, - .o_type = NNI_TYPE_DURATION, .o_get = zt_ep_get_ping_time, .o_set = zt_ep_set_ping_time, - .o_chk = zt_ep_chk_time, }, { .o_name = NNG_OPT_ZT_PING_TRIES, - .o_type = NNI_TYPE_INT32, .o_get = zt_ep_get_ping_tries, .o_set = zt_ep_set_ping_tries, - .o_chk = zt_ep_chk_tries, }, { .o_name = NNG_OPT_ZT_ORBIT, - .o_type = NNI_TYPE_UINT64, // use opaque for two .o_set = zt_ep_set_orbit, - .o_chk = zt_ep_chk_orbit, }, { .o_name = NNG_OPT_ZT_DEORBIT, - .o_type = NNI_TYPE_UINT64, .o_set = zt_ep_set_deorbit, - .o_chk = zt_ep_chk_deorbit, }, { .o_name = NNG_OPT_LOCADDR, - .o_type = NNI_TYPE_SOCKADDR, .o_get = zt_ep_get_locaddr, }, // terminate list diff --git a/tests/tls.c b/tests/tls.c index 25f7960b..6dfcaf01 100644 --- a/tests/tls.c +++ b/tests/tls.c @@ -320,7 +320,6 @@ TestMain("TLS Transport", { nng_listener l; nng_dialer d; char * addr; - size_t sz; So(nng_tls_register() == 0); So(nng_pair_open(&s1) == 0); @@ -332,7 +331,6 @@ TestMain("TLS Transport", { So(nng_listener_create(&l, s1, "tls+tcp://127.0.0.1:0") == 0); So(init_listener_tls(l) == 0); So(nng_listener_start(l, 0) == 0); - sz = NNG_MAXADDRLEN; So(nng_listener_getopt_string(l, NNG_OPT_URL, &addr) == 0); So(nng_dialer_create(&d, s2, addr) == 0); So(init_dialer_tls(d) == 0); |
