aboutsummaryrefslogtreecommitdiff
path: root/src/core/endpt.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-03-19 16:02:37 -0700
committerGarrett D'Amore <garrett@damore.org>2018-03-20 09:53:58 -0700
commit9ca901c1b70b17d851426483d9f54611cfa8e395 (patch)
treea26b11e16f505ccdc77b5ac6681e0f9de705ff20 /src/core/endpt.c
parent9b886a9999247d87c9f6d389c3e65a4bd39be010 (diff)
downloadnng-9ca901c1b70b17d851426483d9f54611cfa8e395.tar.gz
nng-9ca901c1b70b17d851426483d9f54611cfa8e395.tar.bz2
nng-9ca901c1b70b17d851426483d9f54611cfa8e395.zip
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.
Diffstat (limited to 'src/core/endpt.c')
-rw-r--r--src/core/endpt.c37
1 files changed, 34 insertions, 3 deletions
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