aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-04-04 11:07:56 -0700
committerGarrett D'Amore <garrett@damore.org>2018-04-04 11:07:56 -0700
commit505a9bce029e51540739c853a6c9eef0ecfb2e90 (patch)
treed907679b6ab99bcb5da919db3d005d4976590c21
parent0aa1de1316b46bb4af23fdf26759bca08008eaf5 (diff)
downloadnng-505a9bce029e51540739c853a6c9eef0ecfb2e90.tar.gz
nng-505a9bce029e51540739c853a6c9eef0ecfb2e90.tar.bz2
nng-505a9bce029e51540739c853a6c9eef0ecfb2e90.zip
fixes #329 type checking not done for setopt
-rw-r--r--src/core/endpt.c6
-rw-r--r--src/core/options.c197
-rw-r--r--src/core/options.h38
-rw-r--r--src/core/protocol.h2
-rw-r--r--src/core/socket.c98
-rw-r--r--src/core/transport.c4
-rw-r--r--src/core/transport.h4
-rw-r--r--src/protocol/bus0/bus.c4
-rw-r--r--src/protocol/pair0/pair.c4
-rw-r--r--src/protocol/pair1/pair.c12
-rw-r--r--src/protocol/pipeline0/pull.c4
-rw-r--r--src/protocol/pipeline0/push.c4
-rw-r--r--src/protocol/pubsub0/pub.c4
-rw-r--r--src/protocol/pubsub0/sub.c10
-rw-r--r--src/protocol/reqrep0/rep.c8
-rw-r--r--src/protocol/reqrep0/req.c12
-rw-r--r--src/protocol/survey0/respond.c8
-rw-r--r--src/protocol/survey0/survey.c12
-rw-r--r--src/transport/ipc/ipc.c11
-rw-r--r--src/transport/tcp/tcp.c24
-rw-r--r--src/transport/tls/tls.c54
-rw-r--r--src/transport/ws/websocket.c68
-rw-r--r--src/transport/zerotier/zerotier.c110
-rw-r--r--tests/sock.c16
24 files changed, 420 insertions, 294 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c
index d4bc9b01..0cb59a14 100644
--- a/src/core/endpt.c
+++ b/src/core/endpt.c
@@ -598,13 +598,9 @@ nni_ep_setopt(nni_ep *ep, const char *name, const void *val, size_t sz, int t)
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);
+ rv = eo->eo_setopt(ep->ep_data, val, sz, t);
nni_mtx_unlock(&ep->ep_mtx);
return (rv);
}
diff --git a/src/core/options.c b/src/core/options.c
index 9410de5f..e0c72d25 100644
--- a/src/core/options.c
+++ b/src/core/options.c
@@ -14,139 +14,174 @@
#include <string.h>
int
-nni_chkopt_ms(const void *v, size_t sz)
+nni_copyin_ms(nni_duration *dp, const void *v, size_t sz, int typ)
{
- nni_duration val;
- if (sz != sizeof(val)) {
- return (NNG_EINVAL);
- }
- memcpy(&val, v, sz);
- if (val < -1) {
- return (NNG_EINVAL);
- }
- return (0);
-}
+ nni_duration dur;
-int
-nni_chkopt_int(const void *v, size_t sz, int minv, int maxv)
-{
- int val;
- if (sz != sizeof(val)) {
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_DURATION:
+ dur = *(nng_duration *) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(dur)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&dur, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
- memcpy(&val, v, sz);
- if ((val < minv) || (val > maxv)) {
+
+ if (dur < -1) {
return (NNG_EINVAL);
}
+ *dp = dur;
return (0);
}
int
-nni_chkopt_bool(size_t sz)
+nni_copyin_bool(bool *bp, const void *v, size_t sz, int typ)
{
- if (sz != sizeof(bool)) {
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_BOOL:
+ *bp = *(bool *) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(bool)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(bp, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
+
return (0);
}
int
-nni_chkopt_size(const void *v, size_t sz, size_t minv, size_t maxv)
+nni_copyin_int(int *ip, const void *v, size_t sz, int minv, int maxv, int typ)
{
- size_t val;
- if (sz != sizeof(val)) {
+ int i;
+
+ switch (typ) {
+ case NNI_TYPE_INT32:
+ i = *(int *) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(i)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&i, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
+ }
+ if (i > maxv) {
return (NNG_EINVAL);
}
- memcpy(&val, v, sz);
- if ((val < minv) || (val > maxv)) {
+ if (i < minv) {
return (NNG_EINVAL);
}
+ *ip = i;
return (0);
}
int
-nni_setopt_ms(nni_duration *dp, const void *v, size_t sz)
+nni_copyin_size(
+ size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv, int typ)
{
- nni_duration dur;
+ size_t val;
- if (sz != sizeof(*dp)) {
- return (NNG_EINVAL);
- }
- memcpy(&dur, v, sizeof(dur));
- if (dur < -1) {
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_SIZE:
+ val = *(size_t *) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(val)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&val, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
- *dp = dur;
- return (0);
-}
-int
-nni_setopt_bool(bool *bp, const void *v, size_t sz)
-{
- if (sz != sizeof(*bp)) {
+ val = *(size_t *) v;
+ if ((val > maxv) || (val < minv)) {
return (NNG_EINVAL);
}
- memcpy(bp, v, sizeof(*bp));
+ *sp = val;
return (0);
}
int
-nni_setopt_int(int *ip, const void *v, size_t sz, int minv, int maxv)
+nni_copyin_ptr(void **pp, const void *v, size_t sz, int typ)
{
- int i;
+ void *p;
- if (sz != sizeof(i)) {
- return (NNG_EINVAL);
- }
- memcpy(&i, v, sizeof(i));
- if (i > maxv) {
- return (NNG_EINVAL);
- }
- if (i < minv) {
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_POINTER:
+ p = *(void **) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(p)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&p, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
- *ip = i;
+ *pp = p;
return (0);
}
int
-nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv)
+nni_copyin_str(char *s, const void *v, size_t sz, size_t maxsz, int typ)
{
- size_t val;
+ size_t z;
- if (sz != sizeof(val)) {
- return (NNG_EINVAL);
- }
- memcpy(&val, v, sizeof(val));
- if (val > maxv) {
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_STRING:
+ z = strlen(v) + 1;
+ NNI_ASSERT(sz == z);
+ break;
+ case NNI_TYPE_OPAQUE:
+ if ((z = nni_strnlen(v, sz)) >= sz) {
+ return (NNG_EINVAL); // missing terminator
+ }
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
- if (val < minv) {
- return (NNG_EINVAL);
+ if (z > maxsz) {
+ return (NNG_EINVAL); // too long
}
- *sp = val;
+ memcpy(s, v, z);
return (0);
}
int
-nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz)
+nni_copyin_u64(uint64_t *up, const void *v, size_t sz, int typ)
{
- int len;
+ uint64_t u;
- if (sz < sizeof(len)) {
- return (NNG_EINVAL);
- }
- memcpy(&len, val, sizeof(len));
- if (len < 0) {
- return (NNG_EINVAL);
- }
- if (len > 8192) {
- // put a reasonable uppper limit on queue depth.
- // This is a count in messages, so the total queue
- // size could be quite large indeed in this case.
- return (NNG_EINVAL);
+ switch (typ) {
+ case NNI_TYPE_UINT64:
+ u = *(uint64_t *) v;
+ break;
+ case NNI_TYPE_OPAQUE:
+ if (sz != sizeof(u)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&u, v, sz);
+ break;
+ default:
+ return (NNG_EBADTYPE);
}
- return (nni_msgq_resize(mq, len));
+ *up = u;
+ return (0);
}
int
diff --git a/src/core/options.h b/src/core/options.h
index 35c3232f..dfc15d31 100644
--- a/src/core/options.h
+++ b/src/core/options.h
@@ -11,39 +11,27 @@
#ifndef CORE_OPTIONS_H
#define CORE_OPTIONS_H
-// Option helpers. These can be called from protocols or transports
-// in their own option handling, centralizing the logic for dealing with
-// variable sized options.
-
-// nni_setopt_buf sets the queue size for the message queue.
-extern int nni_setopt_buf(nni_msgq *, const void *, size_t);
-
-// nni_setopt_duration sets the duration. Durations must be legal,
-// either a positive value, 0, or -1 to indicate forever.
-extern int nni_setopt_ms(nni_duration *, const void *, size_t);
-
-// nni_setopt_bool sets a bool, or _Bool
-extern int nni_setopt_bool(bool *, const void *, size_t);
-
-// nni_setopt_int sets an integer, which must be between the minimum and
-// maximum values (inclusive).
-extern int nni_setopt_int(int *, const void *, size_t, int, int);
-
+// Integer limits.
#define NNI_MAXINT ((int) 2147483647)
#define NNI_MININT ((int) -2147483648)
-// nni_setopt_size sets a size_t option.
-extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t);
-
// We limit the maximum size to 4GB. That's intentional because some of the
// underlying protocols cannot cope with anything bigger than 32-bits.
#define NNI_MINSZ (0)
#define NNI_MAXSZ ((size_t) 0xffffffff)
-extern int nni_chkopt_bool(size_t);
-extern int nni_chkopt_ms(const void *, size_t);
-extern int nni_chkopt_int(const void *, size_t, int, int);
-extern int nni_chkopt_size(const void *, size_t, size_t, size_t);
+// Option helpers. These can be called from protocols or transports
+// in their own option handling, centralizing the logic for dealing with
+// variable sized options.
+
+extern int nni_copyin_ms(nni_duration *, const void *, size_t, int);
+extern int nni_copyin_bool(bool *, const void *, size_t, int);
+extern int nni_copyin_int(int *, const void *, size_t, int, int, int);
+extern int nni_copyin_size(
+ size_t *, const void *, size_t, size_t, size_t, int);
+extern int nni_copyin_str(char *, const void *, size_t, size_t, int);
+extern int nni_copyin_ptr(void **, const void *, size_t, int);
+extern int nni_copyin_u64(uint64_t *, const void *, size_t, int);
// nni_copyout_xxx copies out a type of the named value. It assumes that
// the type is aligned and the size correct, unless NNI_TYPE_OPAQUE is passed.
diff --git a/src/core/protocol.h b/src/core/protocol.h
index 1c341241..01e3d11c 100644
--- a/src/core/protocol.h
+++ b/src/core/protocol.h
@@ -51,7 +51,7 @@ struct nni_proto_sock_option {
const char *pso_name;
int pso_type;
int (*pso_getopt)(void *, void *, size_t *, int);
- int (*pso_setopt)(void *, const void *, size_t);
+ int (*pso_setopt)(void *, const void *, size_t, int);
};
struct nni_proto_sock_ops {
diff --git a/src/core/socket.c b/src/core/socket.c
index 9e98a9d9..40ef32f3 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -23,7 +23,7 @@ typedef struct nni_socket_option {
const char *so_name;
int so_type;
int (*so_getopt)(nni_sock *, void *, size_t *, int);
- int (*so_setopt)(nni_sock *, const void *, size_t);
+ int (*so_setopt)(nni_sock *, const void *, size_t, int);
} nni_socket_option;
typedef struct nni_sockopt {
@@ -99,6 +99,32 @@ nni_sock_can_recv_cb(void *arg, int flags)
}
}
+void
+nni_sock_set_sendable(nni_sock *s, bool cansend)
+{
+ nni_notifyfd *fd = &s->s_send_fd;
+ if (fd->sn_init) {
+ if (cansend) {
+ nni_plat_pipe_raise(fd->sn_wfd);
+ } else {
+ nni_plat_pipe_clear(fd->sn_rfd);
+ }
+ }
+}
+
+void
+nni_sock_set_recvable(nni_sock *s, bool canrecv)
+{
+ nni_notifyfd *fd = &s->s_recv_fd;
+ if (fd->sn_init) {
+ if (canrecv) {
+ nni_plat_pipe_raise(fd->sn_wfd);
+ } else {
+ nni_plat_pipe_clear(fd->sn_rfd);
+ }
+ }
+}
+
static int
nni_sock_get_fd(nni_sock *s, int flag, int *fdp)
{
@@ -167,9 +193,9 @@ nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_ms(&s->s_rcvtimeo, buf, sz));
+ return (nni_copyin_ms(&s->s_rcvtimeo, buf, sz, typ));
}
static int
@@ -179,9 +205,9 @@ nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_ms(&s->s_sndtimeo, buf, sz));
+ return (nni_copyin_ms(&s->s_sndtimeo, buf, sz, typ));
}
static int
@@ -191,9 +217,9 @@ nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_ms(&s->s_reconn, buf, sz));
+ return (nni_copyin_ms(&s->s_reconn, buf, sz, typ));
}
static int
@@ -203,9 +229,9 @@ nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_ms(&s->s_reconnmax, buf, sz));
+ return (nni_copyin_ms(&s->s_reconnmax, buf, sz, typ));
}
static int
@@ -215,9 +241,15 @@ nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_recvbuf(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_recvbuf(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_buf(s->s_urq, buf, sz));
+ int len;
+ int rv;
+
+ if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, typ)) != 0) {
+ return (rv);
+ }
+ return (nni_msgq_resize(s->s_urq, len));
}
static int
@@ -229,9 +261,15 @@ nni_sock_getopt_recvbuf(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_sendbuf(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_sendbuf(nni_sock *s, const void *buf, size_t sz, int typ)
{
- return (nni_setopt_buf(s->s_uwq, buf, sz));
+ int len;
+ int rv;
+
+ if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, typ)) != 0) {
+ return (rv);
+ }
+ return (nni_msgq_resize(s->s_uwq, len));
}
static int
@@ -249,13 +287,9 @@ nni_sock_getopt_sockname(nni_sock *s, void *buf, size_t *szp, int typ)
}
static int
-nni_sock_setopt_sockname(nni_sock *s, const void *buf, size_t sz)
+nni_sock_setopt_sockname(nni_sock *s, const void *buf, size_t sz, int typ)
{
- if (nni_strnlen(buf, sz) > sizeof(s->s_name) - 1) {
- return (NNG_EINVAL);
- }
- nni_strlcpy(s->s_name, buf, sizeof(s->s_name));
- return (0);
+ return (nni_copyin_str(s->s_name, buf, sizeof(s->s_name), sz, typ));
}
static const nni_socket_option nni_sock_options[] = {
@@ -748,7 +782,7 @@ nni_sock_close(nni_sock *s)
}
nni_mtx_unlock(&nni_sock_lk);
- // Wait for pipe and eps to finish closing.
+ // Wait for pipes, eps, and contexts to finish closing.
nni_mtx_lock(&s->s_mx);
while (
(!nni_list_empty(&s->s_pipes)) || (!nni_list_empty(&s->s_eps))) {
@@ -895,12 +929,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *v, size_t sz, int t)
nni_mtx_unlock(&s->s_mx);
return (NNG_EREADONLY);
}
- if ((pso->pso_type != NNI_TYPE_OPAQUE) &&
- (t != NNI_TYPE_OPAQUE) && (t != pso->pso_type)) {
- nni_mtx_unlock(&s->s_mx);
- return (NNG_EBADTYPE);
- }
- rv = pso->pso_setopt(s->s_data, v, sz);
+ rv = pso->pso_setopt(s->s_data, v, sz, t);
nni_mtx_unlock(&s->s_mx);
return (rv);
}
@@ -914,12 +943,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *v, size_t sz, int t)
nni_mtx_unlock(&s->s_mx);
return (NNG_EREADONLY);
}
- if ((sso->so_type != NNI_TYPE_OPAQUE) &&
- (t != NNI_TYPE_OPAQUE) && (t != sso->so_type)) {
- nni_mtx_unlock(&s->s_mx);
- return (NNG_EBADTYPE);
- }
- rv = sso->so_setopt(s, v, sz);
+ rv = sso->so_setopt(s, v, sz, t);
nni_mtx_unlock(&s->s_mx);
return (rv);
}
@@ -933,17 +957,19 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *v, size_t sz, int t)
// Validation of transport options. This is stateless, so transports
// should not fail to set an option later if they passed it here.
- rv = nni_tran_chkopt(name, v, sz);
+ rv = nni_tran_chkopt(name, v, sz, t);
// Also check a few generic things. We do this if no transport
// was found, or even if a transport rejected one of the settings.
if ((rv == NNG_ENOTSUP) || (rv == 0)) {
if ((strcmp(name, NNG_OPT_LINGER) == 0)) {
- rv = nni_chkopt_ms(v, sz);
+ nng_duration d;
+ rv = nni_copyin_ms(&d, v, sz, t);
} else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) {
+ size_t z;
// just a sanity test on the size; it also ensures that
// a size can be set even with no transport configured.
- rv = nni_chkopt_size(v, sz, 0, NNI_MAXSZ);
+ rv = nni_copyin_size(&z, v, sz, 0, NNI_MAXSZ, t);
}
}
@@ -1014,7 +1040,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *v, size_t sz, int t)
// will already have had a chance to veto this.
if (strcmp(name, NNG_OPT_LINGER) == 0) {
- rv = nni_setopt_ms(&s->s_linger, v, sz);
+ rv = nni_copyin_ms(&s->s_linger, v, sz, t);
}
if (rv == 0) {
diff --git a/src/core/transport.c b/src/core/transport.c
index b48c7da6..6c872b81 100644
--- a/src/core/transport.c
+++ b/src/core/transport.c
@@ -98,7 +98,7 @@ nni_tran_find(nni_url *url)
}
int
-nni_tran_chkopt(const char *name, const void *v, size_t sz)
+nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ)
{
nni_transport *t;
int rv = NNG_ENOTSUP;
@@ -118,7 +118,7 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz)
nni_mtx_unlock(&nni_tran_lk);
return (NNG_EREADONLY);
}
- if ((rv = eo->eo_setopt(NULL, v, sz)) != 0) {
+ if ((rv = eo->eo_setopt(NULL, v, sz, typ)) != 0) {
nni_mtx_unlock(&nni_tran_lk);
return (rv);
}
diff --git a/src/core/transport.h b/src/core/transport.h
index 7085126f..b1fecaa2 100644
--- a/src/core/transport.h
+++ b/src/core/transport.h
@@ -65,7 +65,7 @@ struct nni_tran_ep_option {
// performed, but the option should be sanity tested for presence
// and size. (This permits the core to validate that an option
// is reasonable and be set even before endpoints are created.)
- int (*eo_setopt)(void *, const void *, size_t);
+ int (*eo_setopt)(void *, const void *, size_t, int);
};
// Endpoint operations are called by the socket in a protocol-independent
@@ -172,7 +172,7 @@ struct nni_tran_pipe {
// These APIs are used by the framework internally, and not for use by
// transport implementations.
extern nni_tran *nni_tran_find(nni_url *);
-extern int nni_tran_chkopt(const char *, const void *, size_t);
+extern int nni_tran_chkopt(const char *, const void *, size_t, int);
extern int nni_tran_sys_init(void);
extern void nni_tran_sys_fini(void);
extern int nni_tran_register(const nni_tran *);
diff --git a/src/protocol/bus0/bus.c b/src/protocol/bus0/bus.c
index 00a9ca02..57fad341 100644
--- a/src/protocol/bus0/bus.c
+++ b/src/protocol/bus0/bus.c
@@ -334,10 +334,10 @@ bus0_pipe_recv(bus0_pipe *p)
}
static int
-bus0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+bus0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
bus0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/pair0/pair.c b/src/protocol/pair0/pair.c
index bc8be866..ccece972 100644
--- a/src/protocol/pair0/pair.c
+++ b/src/protocol/pair0/pair.c
@@ -232,10 +232,10 @@ pair0_sock_close(void *arg)
}
static int
-pair0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+pair0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
pair0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/pair1/pair.c b/src/protocol/pair1/pair.c
index ee67cf7b..becbbfa7 100644
--- a/src/protocol/pair1/pair.c
+++ b/src/protocol/pair1/pair.c
@@ -397,12 +397,12 @@ pair1_sock_close(void *arg)
}
static int
-pair1_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+pair1_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
pair1_sock *s = arg;
int rv;
nni_mtx_lock(&s->mtx);
- rv = s->started ? NNG_ESTATE : nni_setopt_bool(&s->raw, buf, sz);
+ rv = s->started ? NNG_ESTATE : nni_copyin_bool(&s->raw, buf, sz, typ);
nni_mtx_unlock(&s->mtx);
return (rv);
}
@@ -415,12 +415,12 @@ pair1_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ)
}
static int
-pair1_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+pair1_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ)
{
pair1_sock *s = arg;
int rv;
nni_mtx_lock(&s->mtx); // Have to be locked against recv cb.
- rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255);
+ rv = nni_copyin_int(&s->ttl, buf, sz, 1, 255, typ);
nni_mtx_unlock(&s->mtx);
return (rv);
}
@@ -433,12 +433,12 @@ pair1_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ)
}
static int
-pair1_sock_setopt_poly(void *arg, const void *buf, size_t sz)
+pair1_sock_setopt_poly(void *arg, const void *buf, size_t sz, int typ)
{
pair1_sock *s = arg;
int rv;
nni_mtx_lock(&s->mtx);
- rv = s->started ? NNG_ESTATE : nni_setopt_bool(&s->poly, buf, sz);
+ rv = s->started ? NNG_ESTATE : nni_copyin_bool(&s->poly, buf, sz, typ);
nni_mtx_unlock(&s->mtx);
return (rv);
}
diff --git a/src/protocol/pipeline0/pull.c b/src/protocol/pipeline0/pull.c
index d8f4641f..9aa7bea9 100644
--- a/src/protocol/pipeline0/pull.c
+++ b/src/protocol/pipeline0/pull.c
@@ -181,10 +181,10 @@ pull0_sock_close(void *arg)
}
static int
-pull0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+pull0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
pull0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/pipeline0/push.c b/src/protocol/pipeline0/push.c
index bbffb433..8c8fa13e 100644
--- a/src/protocol/pipeline0/push.c
+++ b/src/protocol/pipeline0/push.c
@@ -198,10 +198,10 @@ push0_getq_cb(void *arg)
}
static int
-push0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+push0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
push0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/pubsub0/pub.c b/src/protocol/pubsub0/pub.c
index 3f4ecea6..aaa22801 100644
--- a/src/protocol/pubsub0/pub.c
+++ b/src/protocol/pubsub0/pub.c
@@ -274,10 +274,10 @@ pub0_pipe_send_cb(void *arg)
}
static int
-pub0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+pub0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
pub0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/pubsub0/sub.c b/src/protocol/pubsub0/sub.c
index d7e9aea4..6b1f1173 100644
--- a/src/protocol/pubsub0/sub.c
+++ b/src/protocol/pubsub0/sub.c
@@ -190,11 +190,12 @@ sub0_putq_cb(void *arg)
// to replace this with a patricia trie, like old nanomsg had.
static int
-sub0_subscribe(void *arg, const void *buf, size_t sz)
+sub0_subscribe(void *arg, const void *buf, size_t sz, int typ)
{
sub0_sock * s = arg;
sub0_topic *topic;
sub0_topic *newtopic;
+ NNI_ARG_UNUSED(typ);
nni_mtx_lock(&s->lk);
NNI_LIST_FOREACH (&s->topics, topic) {
@@ -240,11 +241,12 @@ sub0_subscribe(void *arg, const void *buf, size_t sz)
}
static int
-sub0_unsubscribe(void *arg, const void *buf, size_t sz)
+sub0_unsubscribe(void *arg, const void *buf, size_t sz, int typ)
{
sub0_sock * s = arg;
sub0_topic *topic;
int rv;
+ NNI_ARG_UNUSED(typ);
nni_mtx_lock(&s->lk);
NNI_LIST_FOREACH (&s->topics, topic) {
@@ -276,10 +278,10 @@ sub0_unsubscribe(void *arg, const void *buf, size_t sz)
}
static int
-sub0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+sub0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
sub0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
diff --git a/src/protocol/reqrep0/rep.c b/src/protocol/reqrep0/rep.c
index 5ee6c1be..f62406cd 100644
--- a/src/protocol/reqrep0/rep.c
+++ b/src/protocol/reqrep0/rep.c
@@ -353,13 +353,13 @@ rep0_pipe_putq_cb(void *arg)
}
static int
-rep0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+rep0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
rep0_sock *s = arg;
int rv;
nni_mtx_lock(&s->lk);
- rv = nni_setopt_bool(&s->raw, buf, sz);
+ rv = nni_copyin_bool(&s->raw, buf, sz, typ);
nni_mtx_unlock(&s->lk);
return (rv);
}
@@ -372,10 +372,10 @@ rep0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ)
}
static int
-rep0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+rep0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ)
{
rep0_sock *s = arg;
- return (nni_setopt_int(&s->ttl, buf, sz, 1, 255));
+ return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, typ));
}
static int
diff --git a/src/protocol/reqrep0/req.c b/src/protocol/reqrep0/req.c
index 659578d4..0a5b566a 100644
--- a/src/protocol/reqrep0/req.c
+++ b/src/protocol/reqrep0/req.c
@@ -250,10 +250,10 @@ req0_pipe_stop(void *arg)
}
static int
-req0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+req0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
req0_sock *s = arg;
- return (nni_setopt_bool(&s->raw, buf, sz));
+ return (nni_copyin_bool(&s->raw, buf, sz, typ));
}
static int
@@ -264,10 +264,10 @@ req0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ)
}
static int
-req0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+req0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ)
{
req0_sock *s = arg;
- return (nni_setopt_int(&s->ttl, buf, sz, 1, 255));
+ return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, typ));
}
static int
@@ -278,10 +278,10 @@ req0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ)
}
static int
-req0_sock_setopt_resendtime(void *arg, const void *buf, size_t sz)
+req0_sock_setopt_resendtime(void *arg, const void *buf, size_t sz, int typ)
{
req0_sock *s = arg;
- return (nni_setopt_ms(&s->retry, buf, sz));
+ return (nni_copyin_ms(&s->retry, buf, sz, typ));
}
static int
diff --git a/src/protocol/survey0/respond.c b/src/protocol/survey0/respond.c
index 035e51b1..eeb09d2a 100644
--- a/src/protocol/survey0/respond.c
+++ b/src/protocol/survey0/respond.c
@@ -347,13 +347,13 @@ resp0_putq_cb(void *arg)
}
static int
-resp0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+resp0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
resp0_sock *s = arg;
int rv;
nni_mtx_lock(&s->mtx);
- rv = nni_setopt_bool(&s->raw, buf, sz);
+ rv = nni_copyin_bool(&s->raw, buf, sz, typ);
nni_mtx_unlock(&s->mtx);
return (rv);
}
@@ -366,10 +366,10 @@ resp0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ)
}
static int
-resp0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+resp0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ)
{
resp0_sock *s = arg;
- return (nni_setopt_int(&s->ttl, buf, sz, 1, 255));
+ return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, typ));
}
static int
diff --git a/src/protocol/survey0/survey.c b/src/protocol/survey0/survey.c
index 161a5002..a5909015 100644
--- a/src/protocol/survey0/survey.c
+++ b/src/protocol/survey0/survey.c
@@ -275,13 +275,13 @@ failed:
}
static int
-surv0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
+surv0_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ)
{
surv0_sock *s = arg;
int rv;
nni_mtx_lock(&s->mtx);
- if ((rv = nni_setopt_bool(&s->raw, buf, sz)) == 0) {
+ if ((rv = nni_copyin_bool(&s->raw, buf, sz, typ)) == 0) {
s->survid = 0;
nni_timer_cancel(&s->timer);
}
@@ -297,10 +297,10 @@ surv0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ)
}
static int
-surv0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+surv0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ)
{
surv0_sock *s = arg;
- return (nni_setopt_int(&s->ttl, buf, sz, 1, 255));
+ return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, typ));
}
static int
@@ -311,10 +311,10 @@ surv0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ)
}
static int
-surv0_sock_setopt_surveytime(void *arg, const void *buf, size_t sz)
+surv0_sock_setopt_surveytime(void *arg, const void *buf, size_t sz, int typ)
{
surv0_sock *s = arg;
- return (nni_setopt_ms(&s->survtime, buf, sz));
+ return (nni_copyin_ms(&s->survtime, buf, sz, typ));
}
static int
diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c
index 98d3f177..ecc9a962 100644
--- a/src/transport/ipc/ipc.c
+++ b/src/transport/ipc/ipc.c
@@ -719,14 +719,17 @@ nni_ipc_ep_connect(void *arg, nni_aio *aio)
}
static int
-nni_ipc_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz)
+nni_ipc_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz, int typ)
{
nni_ipc_ep *ep = arg;
+ size_t val;
+ int rv;
- if (ep == NULL) {
- return (nni_chkopt_size(data, sz, 0, NNI_MAXSZ));
+ rv = nni_copyin_size(&val, data, sz, 0, NNI_MAXSZ, typ);
+ if ((rv == 0) && (ep != NULL)) {
+ ep->rcvmax = val;
}
- return (nni_setopt_size(&ep->rcvmax, data, sz, 0, NNI_MAXSZ));
+ return (rv);
}
static int
diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c
index 9db5b016..80b372e9 100644
--- a/src/transport/tcp/tcp.c
+++ b/src/transport/tcp/tcp.c
@@ -760,13 +760,16 @@ nni_tcp_ep_connect(void *arg, nni_aio *aio)
}
static int
-nni_tcp_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz)
+nni_tcp_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz, int typ)
{
nni_tcp_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_size(v, sz, 0, NNI_MAXSZ));
+ size_t val;
+ int rv;
+ rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, typ);
+ if ((rv == 0) && (ep != NULL)) {
+ ep->rcvmax = val;
}
- return (nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ));
+ return (rv);
}
static int
@@ -793,13 +796,16 @@ nni_tcp_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp, int typ)
}
static int
-nni_tcp_ep_setopt_linger(void *arg, const void *v, size_t sz)
+nni_tcp_ep_setopt_linger(void *arg, const void *v, size_t sz, int typ)
{
- nni_tcp_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_ms(v, sz));
+ nni_tcp_ep * ep = arg;
+ nng_duration val;
+ int rv;
+
+ if (((rv = nni_copyin_ms(&val, v, sz, typ)) == 0) && (ep != NULL)) {
+ ep->linger = val;
}
- return (nni_setopt_ms(&ep->linger, v, sz));
+ return (rv);
}
static int
diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c
index a78e8085..69e36609 100644
--- a/src/transport/tls/tls.c
+++ b/src/transport/tls/tls.c
@@ -798,13 +798,17 @@ nni_tls_ep_getopt_url(void *arg, void *v, size_t *szp, int typ)
}
static int
-nni_tls_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz)
+nni_tls_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz, int typ)
{
nni_tls_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_size(v, sz, 0, NNI_MAXSZ));
+ size_t val;
+ int rv;
+
+ rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, typ);
+ if ((rv == 0) && (ep != NULL)) {
+ ep->rcvmax = val;
}
- return (nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ));
+ return (rv);
}
static int
@@ -815,13 +819,16 @@ nni_tls_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp, int typ)
}
static int
-nni_tls_ep_setopt_linger(void *arg, const void *v, size_t sz)
+nni_tls_ep_setopt_linger(void *arg, const void *v, size_t sz, int typ)
{
- nni_tls_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_ms(v, sz));
+ nni_tls_ep * ep = arg;
+ nng_duration val;
+ int rv;
+
+ if (((rv = nni_copyin_ms(&val, v, sz, typ)) == 0) && (ep != NULL)) {
+ ep->linger = val;
}
- return (nni_setopt_ms(&ep->linger, v, sz));
+ return (rv);
}
static int
@@ -832,15 +839,15 @@ nni_tls_ep_getopt_linger(void *arg, void *v, size_t *szp, int typ)
}
static int
-tls_setopt_config(void *arg, const void *data, size_t sz)
+tls_setopt_config(void *arg, const void *data, size_t sz, int typ)
{
nni_tls_ep * ep = arg;
nng_tls_config *cfg, *old;
+ int rv;
- if (sz != sizeof(cfg)) {
- return (NNG_EINVAL);
+ if ((rv = nni_copyin_ptr((void **) &cfg, data, sz, typ)) != 0) {
+ return (rv);
}
- memcpy(&cfg, data, sz);
if (cfg == NULL) {
return (NNG_EINVAL);
}
@@ -864,10 +871,13 @@ tls_getopt_config(void *arg, void *v, size_t *szp, int typ)
}
static int
-tls_setopt_ca_file(void *arg, const void *v, size_t sz)
+tls_setopt_ca_file(void *arg, const void *v, size_t sz, int typ)
{
nni_tls_ep *ep = arg;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -878,14 +888,14 @@ tls_setopt_ca_file(void *arg, const void *v, size_t sz)
}
static int
-tls_setopt_auth_mode(void *arg, const void *v, size_t sz)
+tls_setopt_auth_mode(void *arg, const void *v, size_t sz, int typ)
{
nni_tls_ep *ep = arg;
int mode;
int rv;
- rv = nni_setopt_int(
- &mode, v, sz, NNG_TLS_AUTH_MODE_NONE, NNG_TLS_AUTH_MODE_REQUIRED);
+ rv = nni_copyin_int(&mode, v, sz, NNG_TLS_AUTH_MODE_NONE,
+ NNG_TLS_AUTH_MODE_REQUIRED, typ);
if ((rv != 0) || (ep == NULL)) {
return (rv);
}
@@ -893,10 +903,13 @@ tls_setopt_auth_mode(void *arg, const void *v, size_t sz)
}
static int
-tls_setopt_server_name(void *arg, const void *v, size_t sz)
+tls_setopt_server_name(void *arg, const void *v, size_t sz, int typ)
{
nni_tls_ep *ep = arg;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -907,10 +920,13 @@ tls_setopt_server_name(void *arg, const void *v, size_t sz)
}
static int
-tls_setopt_cert_key_file(void *arg, const void *v, size_t sz)
+tls_setopt_cert_key_file(void *arg, const void *v, size_t sz, int typ)
{
nni_tls_ep *ep = arg;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
diff --git a/src/transport/ws/websocket.c b/src/transport/ws/websocket.c
index 1a83462a..0542d0c7 100644
--- a/src/transport/ws/websocket.c
+++ b/src/transport/ws/websocket.c
@@ -339,17 +339,21 @@ ws_ep_connect(void *arg, nni_aio *aio)
}
static int
-ws_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz)
+ws_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz, int typ)
{
ws_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_size(v, sz, 0, NNI_MAXSZ));
+ size_t val;
+ int rv;
+
+ rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, typ);
+ if ((rv == 0) && (ep != NULL)) {
+ ep->rcvmax = val;
}
- return (nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ));
+ return (rv);
}
static int
-ws_ep_setopt_headers(ws_ep *ep, const void *v, size_t sz)
+ws_ep_setopt_headers(ws_ep *ep, const char *v)
{
char * dupstr;
size_t duplen;
@@ -360,14 +364,6 @@ ws_ep_setopt_headers(ws_ep *ep, const void *v, size_t sz)
ws_hdr * h;
int rv;
- if (nni_strnlen(v, sz) >= sz) {
- return (NNG_EINVAL);
- }
-
- if (ep == NULL) {
- return (0);
- }
-
if (ep->started) {
return (NNG_EBUSY);
}
@@ -440,10 +436,14 @@ done:
}
static int
-ws_ep_setopt_reqhdrs(void *arg, const void *v, size_t sz)
+ws_ep_setopt_reqhdrs(void *arg, const void *v, size_t sz, int typ)
{
ws_ep *ep = arg;
+ if ((typ != NNI_TYPE_STRING) && (typ != NNI_TYPE_OPAQUE)) {
+ return (NNG_EBADTYPE);
+ }
+
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -451,14 +451,18 @@ ws_ep_setopt_reqhdrs(void *arg, const void *v, size_t sz)
if ((ep != NULL) && (ep->mode == NNI_EP_MODE_LISTEN)) {
return (NNG_EREADONLY);
}
- return (ws_ep_setopt_headers(ep, v, sz));
+ return (ws_ep_setopt_headers(ep, v));
}
static int
-ws_ep_setopt_reshdrs(void *arg, const void *v, size_t sz)
+ws_ep_setopt_reshdrs(void *arg, const void *v, size_t sz, int typ)
{
ws_ep *ep = arg;
+ if ((typ != NNI_TYPE_STRING) && (typ != NNI_TYPE_OPAQUE)) {
+ return (NNG_EBADTYPE);
+ }
+
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -466,7 +470,7 @@ ws_ep_setopt_reshdrs(void *arg, const void *v, size_t sz)
if ((ep != NULL) && (ep->mode == NNI_EP_MODE_DIAL)) {
return (NNG_EREADONLY);
}
- return (ws_ep_setopt_headers(ep, v, sz));
+ return (ws_ep_setopt_headers(ep, v));
}
static int
@@ -829,16 +833,15 @@ wss_ep_getopt_tlsconfig(void *arg, void *v, size_t *szp, int typ)
}
static int
-wss_ep_setopt_tlsconfig(void *arg, const void *v, size_t sz)
+wss_ep_setopt_tlsconfig(void *arg, const void *v, size_t sz, int typ)
{
ws_ep * ep = arg;
nng_tls_config *cfg;
int rv;
- if (sz != sizeof(cfg)) {
- return (NNG_EINVAL);
+ if ((rv = nni_copyin_ptr((void **) &cfg, v, sz, typ)) != 0) {
+ return (rv);
}
- memcpy(&cfg, v, sz);
if (cfg == NULL) {
// NULL is clearly invalid.
return (NNG_EINVAL);
@@ -855,12 +858,15 @@ wss_ep_setopt_tlsconfig(void *arg, const void *v, size_t sz)
}
static int
-wss_ep_setopt_tls_cert_key_file(void *arg, const void *v, size_t sz)
+wss_ep_setopt_tls_cert_key_file(void *arg, const void *v, size_t sz, int typ)
{
ws_ep * ep = arg;
int rv;
nng_tls_config *tls;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -874,12 +880,16 @@ wss_ep_setopt_tls_cert_key_file(void *arg, const void *v, size_t sz)
}
static int
-wss_ep_setopt_tls_ca_file(void *arg, const void *v, size_t sz)
+wss_ep_setopt_tls_ca_file(void *arg, const void *v, size_t sz, int typ)
{
ws_ep * ep = arg;
int rv;
nng_tls_config *tls;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
+
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
@@ -893,15 +903,15 @@ wss_ep_setopt_tls_ca_file(void *arg, const void *v, size_t sz)
}
static int
-wss_ep_setopt_tls_auth_mode(void *arg, const void *v, size_t sz)
+wss_ep_setopt_tls_auth_mode(void *arg, const void *v, size_t sz, int typ)
{
ws_ep * ep = arg;
int rv;
nng_tls_config *tls;
int mode;
- rv = nni_setopt_int(
- &mode, v, sz, NNG_TLS_AUTH_MODE_NONE, NNG_TLS_AUTH_MODE_REQUIRED);
+ rv = nni_copyin_int(&mode, v, sz, NNG_TLS_AUTH_MODE_NONE,
+ NNG_TLS_AUTH_MODE_REQUIRED, typ);
if ((rv != 0) || (ep == NULL)) {
return (rv);
}
@@ -912,12 +922,16 @@ wss_ep_setopt_tls_auth_mode(void *arg, const void *v, size_t sz)
}
static int
-wss_ep_setopt_tls_server_name(void *arg, const void *v, size_t sz)
+wss_ep_setopt_tls_server_name(void *arg, const void *v, size_t sz, int typ)
{
ws_ep * ep = arg;
int rv;
nng_tls_config *tls;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
+
if (nni_strnlen(v, sz) >= sz) {
return (NNG_EINVAL);
}
diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c
index 9fc0bc62..d0790b37 100644
--- a/src/transport/zerotier/zerotier.c
+++ b/src/transport/zerotier/zerotier.c
@@ -2535,14 +2535,17 @@ zt_ep_connect(void *arg, nni_aio *aio)
}
static int
-zt_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz)
+zt_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz, int typ)
{
zt_ep *ep = arg;
+ size_t val;
+ int rv;
- if (ep == NULL) {
- return (nni_chkopt_size(data, sz, 0, 0xffffffffu));
+ rv = nni_copyin_size(&val, data, sz, 0, 0xffffffffu, typ);
+ if ((rv == 0) && (ep != NULL)) {
+ ep->ze_rcvmax = val;
}
- return (nni_setopt_size(&ep->ze_rcvmax, data, sz, 0, 0xffffffffu));
+ return (rv);
}
static int
@@ -2553,12 +2556,16 @@ zt_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp, int typ)
}
static int
-zt_ep_setopt_home(void *arg, const void *data, size_t sz)
+zt_ep_setopt_home(void *arg, const void *data, size_t sz, int typ)
{
size_t len;
int rv;
zt_ep *ep = arg;
+ if ((typ != NNI_TYPE_OPAQUE) && (typ != NNI_TYPE_STRING)) {
+ return (NNG_EBADTYPE);
+ }
+
len = nni_strnlen(data, sz);
if ((len >= sz) || (len >= NNG_MAXADDRLEN)) {
return (NNG_EINVAL);
@@ -2602,23 +2609,37 @@ zt_ep_getopt_url(void *arg, void *data, size_t *szp, int typ)
}
static int
-zt_ep_setopt_orbit(void *arg, const void *data, size_t sz)
+zt_ep_setopt_orbit(void *arg, const void *data, size_t sz, int typ)
{
uint64_t moonid;
uint64_t peerid;
zt_ep * ep = arg;
enum ZT_ResultCode zrv;
+ switch (typ) {
+ 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);
+ }
+
if (sz == sizeof(uint64_t)) {
memcpy(&moonid, data, sizeof(moonid));
peerid = 0;
- } else if (sz == (2 * sizeof(uint64_t))) {
+ } else {
+ NNI_ASSERT(sz == (2 * sizeof(uint64_t)));
memcpy(&moonid, data, sizeof(moonid));
memcpy(&peerid, ((char *) data) + sizeof(uint64_t),
sizeof(peerid));
- } else {
- return (NNG_EINVAL);
}
+
nni_mtx_lock(&zt_lk);
zrv = ZT_Node_orbit(ep->ze_ztn->zn_znode, NULL, moonid, peerid);
nni_mtx_unlock(&zt_lk);
@@ -2627,22 +2648,23 @@ zt_ep_setopt_orbit(void *arg, const void *data, size_t sz)
}
static int
-zt_ep_setopt_deorbit(void *arg, const void *data, size_t sz)
+zt_ep_setopt_deorbit(void *arg, const void *data, size_t sz, int typ)
{
uint64_t moonid;
zt_ep * ep = arg;
enum ZT_ResultCode zrv;
+ int rv;
- if (sz == sizeof(uint64_t)) {
- memcpy(&moonid, data, sizeof(moonid));
- } else {
- return (NNG_EINVAL);
- }
- nni_mtx_lock(&zt_lk);
- zrv = ZT_Node_deorbit(ep->ze_ztn->zn_znode, NULL, moonid);
- nni_mtx_unlock(&zt_lk);
+ rv = nni_copyin_u64(&moonid, data, sz, typ);
+ if ((rv == 0) && (ep != NULL)) {
- return (zt_result(zrv));
+ nni_mtx_lock(&zt_lk);
+ zrv = ZT_Node_deorbit(ep->ze_ztn->zn_znode, NULL, moonid);
+ nni_mtx_unlock(&zt_lk);
+
+ rv = zt_result(zrv);
+ }
+ return (rv);
}
static int
@@ -2674,13 +2696,16 @@ zt_ep_getopt_nw_status(void *arg, void *buf, size_t *szp, int typ)
}
static int
-zt_ep_setopt_ping_time(void *arg, const void *data, size_t sz)
+zt_ep_setopt_ping_time(void *arg, const void *data, size_t sz, int typ)
{
- zt_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_ms(data, sz));
+ zt_ep * ep = arg;
+ nng_duration val;
+ int rv;
+
+ if (((rv = nni_copyin_ms(&val, data, sz, typ)) == 0) && (ep != NULL)) {
+ ep->ze_ping_time = val;
}
- return (nni_setopt_ms(&ep->ze_ping_time, data, sz));
+ return (rv);
}
static int
@@ -2691,13 +2716,17 @@ zt_ep_getopt_ping_time(void *arg, void *data, size_t *szp, int typ)
}
static int
-zt_ep_setopt_ping_tries(void *arg, const void *data, size_t sz)
+zt_ep_setopt_ping_tries(void *arg, const void *data, size_t sz, int typ)
{
zt_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_int(data, sz, 0, 1000000));
+ int val;
+ int rv;
+
+ if (((rv = nni_copyin_int(&val, data, sz, 0, 1000000, typ)) == 0) &&
+ (ep != NULL)) {
+ ep->ze_ping_tries = val;
}
- return (nni_setopt_int(&ep->ze_ping_tries, data, sz, 0, 1000000));
+ return (rv);
}
static int
@@ -2708,13 +2737,16 @@ zt_ep_getopt_ping_tries(void *arg, void *data, size_t *szp, int typ)
}
static int
-zt_ep_setopt_conn_time(void *arg, const void *data, size_t sz)
+zt_ep_setopt_conn_time(void *arg, const void *data, size_t sz, int typ)
{
- zt_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_ms(data, sz));
+ zt_ep * ep = arg;
+ nng_duration val;
+ int rv;
+
+ if (((rv = nni_copyin_ms(&val, data, sz, typ)) == 0) && (ep != NULL)) {
+ ep->ze_conn_time = val;
}
- return (nni_setopt_ms(&ep->ze_conn_time, data, sz));
+ return (rv);
}
static int
@@ -2725,13 +2757,17 @@ zt_ep_getopt_conn_time(void *arg, void *data, size_t *szp, int typ)
}
static int
-zt_ep_setopt_conn_tries(void *arg, const void *data, size_t sz)
+zt_ep_setopt_conn_tries(void *arg, const void *data, size_t sz, int typ)
{
zt_ep *ep = arg;
- if (ep == NULL) {
- return (nni_chkopt_int(data, sz, 0, 1000000));
+ int val;
+ int rv;
+
+ if (((rv = nni_copyin_int(&val, data, sz, 0, 1000000, typ)) == 0) &&
+ (ep != NULL)) {
+ ep->ze_conn_tries = val;
}
- return (nni_setopt_int(&ep->ze_conn_tries, data, sz, 0, 1000000));
+ return (rv);
}
static int
diff --git a/tests/sock.c b/tests/sock.c
index 30dcd1b5..a5b9bdc1 100644
--- a/tests/sock.c
+++ b/tests/sock.c
@@ -92,6 +92,7 @@ TestMain("Socket Operations", {
Convey("Sockname option works", {
char name[128]; // 64 is max
+ char * allocd;
size_t sz;
sz = sizeof(name);
So(nng_getopt(
@@ -110,13 +111,16 @@ TestMain("Socket Operations", {
memset(name, 'A', 64);
name[64] = '\0';
- So(nng_setopt(s1, NNG_OPT_SOCKNAME, name,
- strlen(name)) == NNG_EINVAL);
- So(nng_getopt(
- s1, NNG_OPT_SOCKNAME, name, &sz) == 0);
- So(sz == 6);
- So(strcmp(name, "hello") == 0);
+ // strings must be NULL terminated
+ So(nng_setopt(s1, NNG_OPT_SOCKNAME, name, 5) ==
+ NNG_EINVAL);
+
+ So(nng_getopt_string(
+ s1, NNG_OPT_SOCKNAME, &allocd) == 0);
+ So(strlen(allocd) == 5);
+ So(strcmp(allocd, "hello") == 0);
+ nng_strfree(allocd);
});
Convey("RAW option works", {