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/core/socket.c | |
| 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/core/socket.c')
| -rw-r--r-- | src/core/socket.c | 98 |
1 files changed, 62 insertions, 36 deletions
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) { |
