From 9ca901c1b70b17d851426483d9f54611cfa8e395 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 19 Mar 2018 16:02:37 -0700 Subject: fixes #296 Typed options should validate option type fixes #302 nng_dialer/listener/pipe_getopt_sockaddr desired This adds plumbing to pass and check the type of options all the way through. NNG_ZT_OPT_ORBIT is type UINT64, but you can use the untyped form to pass two of them if needed. No typed access for retrieving strings yet. I think this should allocate a pointer and copy that out, but that's for later. --- src/core/endpt.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/core/endpt.c') diff --git a/src/core/endpt.c b/src/core/endpt.c index 4d3d9031..131ee1db 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -581,7 +581,7 @@ nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) } int -nni_ep_setopt(nni_ep *ep, const char *name, const void *val, size_t sz) +nni_ep_setopt(nni_ep *ep, const char *name, const void *val, size_t sz, int t) { nni_tran_ep_option *eo; @@ -598,6 +598,11 @@ nni_ep_setopt(nni_ep *ep, const char *name, const void *val, size_t sz) if (eo->eo_setopt == NULL) { return (NNG_EREADONLY); } + if ((t != NNI_TYPE_OPAQUE) && + (eo->eo_type != NNI_TYPE_OPAQUE) && (t != eo->eo_type)) { + return (NNG_EBADTYPE); + } + nni_mtx_lock(&ep->ep_mtx); rv = eo->eo_setopt(ep->ep_data, val, sz); nni_mtx_unlock(&ep->ep_mtx); @@ -614,7 +619,25 @@ nni_ep_mode(nni_ep *ep) } int -nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp) +nni_ep_opttype(nni_ep *ep, const char *name, int *tp) +{ + nni_tran_ep_option *eo; + + for (eo = ep->ep_ops.ep_options; eo && eo->eo_name; eo++) { + if (strcmp(eo->eo_name, name) == 0) { + *tp = eo->eo_type; + return (0); + } + } + if (strcmp(name, NNG_OPT_URL) == 0) { + *tp = NNI_TYPE_STRING; + return (0); + } + return (NNG_ENOTSUP); +} + +int +nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp, int t) { nni_tran_ep_option *eo; @@ -626,6 +649,10 @@ nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp) if (eo->eo_getopt == NULL) { return (NNG_EWRITEONLY); } + if ((t != NNI_TYPE_OPAQUE) && + (eo->eo_type != NNI_TYPE_OPAQUE) && (t != eo->eo_type)) { + return (NNG_EBADTYPE); + } nni_mtx_lock(&ep->ep_mtx); rv = eo->eo_getopt(ep->ep_data, valp, szp); nni_mtx_unlock(&ep->ep_mtx); @@ -636,10 +663,14 @@ nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp) // override. This allows the URL to be created with wildcards, // that are resolved later. if (strcmp(name, NNG_OPT_URL) == 0) { + if (t != NNI_TYPE_OPAQUE) { + // XXX: Add NNI_TYPE_STRING. + return (NNG_EBADTYPE); + } return (nni_getopt_str(ep->ep_url->u_rawurl, valp, szp)); } - return (nni_sock_getopt(ep->ep_sock, name, valp, szp)); + return (nni_sock_getopt(ep->ep_sock, name, valp, szp, t)); } void -- cgit v1.2.3-70-g09d2