diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-04 11:07:56 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-04-04 11:07:56 -0700 |
| commit | 505a9bce029e51540739c853a6c9eef0ecfb2e90 (patch) | |
| tree | d907679b6ab99bcb5da919db3d005d4976590c21 /src/transport | |
| parent | 0aa1de1316b46bb4af23fdf26759bca08008eaf5 (diff) | |
| download | nng-505a9bce029e51540739c853a6c9eef0ecfb2e90.tar.gz nng-505a9bce029e51540739c853a6c9eef0ecfb2e90.tar.bz2 nng-505a9bce029e51540739c853a6c9eef0ecfb2e90.zip | |
fixes #329 type checking not done for setopt
Diffstat (limited to 'src/transport')
| -rw-r--r-- | src/transport/ipc/ipc.c | 11 | ||||
| -rw-r--r-- | src/transport/tcp/tcp.c | 24 | ||||
| -rw-r--r-- | src/transport/tls/tls.c | 54 | ||||
| -rw-r--r-- | src/transport/ws/websocket.c | 68 | ||||
| -rw-r--r-- | src/transport/zerotier/zerotier.c | 110 |
5 files changed, 171 insertions, 96 deletions
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 |
