From f04cfd27e2d67b0fc89b079410fc11b55b6d1979 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 29 Aug 2017 14:17:11 -0700 Subject: Add improved getopt functions, pass integers by value. --- src/core/options.c | 47 +++++++++++++++++++++++++++++++++---------- src/core/options.h | 12 ++++++++--- src/core/socket.c | 4 ++-- src/protocol/bus/bus.c | 2 +- src/protocol/pair/pair_v0.c | 2 +- src/protocol/pair/pair_v1.c | 6 +++--- src/protocol/pipeline/pull.c | 2 +- src/protocol/pipeline/push.c | 2 +- src/protocol/pubsub/pub.c | 2 +- src/protocol/pubsub/sub.c | 2 +- src/protocol/reqrep/rep.c | 4 ++-- src/protocol/reqrep/req.c | 6 +++--- src/protocol/survey/respond.c | 4 ++-- src/protocol/survey/survey.c | 4 ++-- src/transport/ipc/ipc.c | 2 +- src/transport/tcp/tcp.c | 4 ++-- 16 files changed, 68 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/core/options.c b/src/core/options.c index 4027564c..76025710 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -123,44 +123,69 @@ nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv) } int -nni_getopt_usec(nni_duration *ptr, void *val, size_t *sizep) +nni_getopt_usec(nni_duration u, void *val, size_t *sizep) { - size_t sz = sizeof(*ptr); + size_t sz = sizeof(u); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof(*ptr); - memcpy(val, ptr, sz); + *sizep = sizeof(u); + memcpy(val, &u, sz); return (0); } int -nni_getopt_int(int *ptr, void *val, size_t *sizep) +nni_getopt_int(int i, void *val, size_t *sizep) { - size_t sz = sizeof(*ptr); + size_t sz = sizeof(i); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof(*ptr); - memcpy(val, ptr, sz); + *sizep = sizeof(i); + memcpy(val, &i, sz); return (0); } int -nni_getopt_size(size_t *ptr, void *val, size_t *sizep) +nni_getopt_u64(const uint64_t u, void *val, size_t *sizep) { - size_t sz = sizeof(*ptr); + size_t sz = sizeof(u); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof(*ptr); + *sizep = sizeof(u); + memcpy(val, &u, sz); + return (0); +} + +int +nni_getopt_str(const char *ptr, void *val, size_t *sizep) +{ + size_t len = strlen(ptr) + 1; + size_t sz; + + sz = (len > *sizep) ? *sizep : len; + *sizep = len; memcpy(val, ptr, sz); return (0); } +int +nni_getopt_size(size_t u, void *val, size_t *sizep) +{ + size_t sz = sizeof(u); + + if (sz > *sizep) { + sz = *sizep; + } + *sizep = sizeof(u); + memcpy(val, &u, sz); + return (0); +} + int nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) { diff --git a/src/core/options.h b/src/core/options.h index 03a6e37e..ed091703 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -26,7 +26,7 @@ extern int nni_getopt_buf(nni_msgq *, void *, size_t *); extern int nni_setopt_usec(nni_duration *, const void *, size_t); // nni_getopt_duration gets the duration. -extern int nni_getopt_usec(nni_duration *, void *, size_t *); +extern int nni_getopt_usec(nni_duration, void *, size_t *); // nni_setopt_int sets an integer, which must be between the minimum and // maximum values (inclusive). @@ -36,7 +36,13 @@ extern int nni_setopt_int(int *, const void *, size_t, int, int); #define NNI_MININT ((int) -2147483648) // nni_getopt_int gets an integer. -extern int nni_getopt_int(int *, void *, size_t *); +extern int nni_getopt_int(int, void *, size_t *); + +// nni_getopt_u64 gets an unsigned 64 bit number. +extern int nni_getopt_u64(uint64_t, void *, size_t *); + +// nni_getopt_str gets a C style string. +extern int nni_getopt_str(const char *, void *, size_t *); // nni_setopt_size sets a size_t option. extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t); @@ -47,7 +53,7 @@ extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t); #define NNI_MAXSZ ((size_t) 0xffffffff) // nni_getopt_size obtains a size_t option. -extern int nni_getopt_size(size_t *, void *, size_t *); +extern int nni_getopt_size(size_t, void *, size_t *); // nni_getopt_fd obtains a notification file descriptor. extern int nni_getopt_fd(nni_sock *, nni_notifyfd *, int, void *, size_t *); diff --git a/src/core/socket.c b/src/core/socket.c index 01fd9a0c..03ae5a9d 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -960,9 +960,9 @@ nni_sock_getopt(nni_sock *s, int opt, void *val, size_t *szp) } else if (opt == nng_optid_recvfd) { rv = nni_getopt_fd(s, &s->s_recv_fd, NNG_EV_CAN_RCV, val, szp); } else if (opt == nng_optid_reconnmint) { - rv = nni_getopt_usec(&s->s_reconn, val, szp); + rv = nni_getopt_usec(s->s_reconn, val, szp); } else if (opt == nng_optid_reconnmaxt) { - rv = nni_getopt_usec(&s->s_reconnmax, val, szp); + rv = nni_getopt_usec(s->s_reconnmax, val, szp); } else { NNI_LIST_FOREACH (&s->s_options, sopt) { if (sopt->opt == opt) { diff --git a/src/protocol/bus/bus.c b/src/protocol/bus/bus.c index c8d759f2..d9189729 100644 --- a/src/protocol/bus/bus.c +++ b/src/protocol/bus/bus.c @@ -343,7 +343,7 @@ bus_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/pair/pair_v0.c b/src/protocol/pair/pair_v0.c index 486ce43b..277d5cb1 100644 --- a/src/protocol/pair/pair_v0.c +++ b/src/protocol/pair/pair_v0.c @@ -250,7 +250,7 @@ pair0_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } else { rv = NNG_ENOTSUP; } diff --git a/src/protocol/pair/pair_v1.c b/src/protocol/pair/pair_v1.c index d6c8ea75..68dbc6f7 100644 --- a/src/protocol/pair/pair_v1.c +++ b/src/protocol/pair/pair_v1.c @@ -432,15 +432,15 @@ pair1_sock_getopt(void *arg, int opt, void *buf, size_t *szp) if (opt == nng_optid_raw) { nni_mtx_lock(&s->mtx); - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); nni_mtx_unlock(&s->mtx); } else if (opt == nng_optid_maxttl) { nni_mtx_lock(&s->mtx); - rv = nni_getopt_int(&s->ttl, buf, szp); + rv = nni_getopt_int(s->ttl, buf, szp); nni_mtx_unlock(&s->mtx); } else if (opt == nng_optid_pair1_poly) { nni_mtx_lock(&s->mtx); - rv = nni_getopt_int(&s->poly, buf, szp); + rv = nni_getopt_int(s->poly, buf, szp); nni_mtx_unlock(&s->mtx); } return (rv); diff --git a/src/protocol/pipeline/pull.c b/src/protocol/pipeline/pull.c index 0d66aab8..21c1613d 100644 --- a/src/protocol/pipeline/pull.c +++ b/src/protocol/pipeline/pull.c @@ -189,7 +189,7 @@ pull_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/pipeline/push.c b/src/protocol/pipeline/push.c index 5e32efee..b28f12c5 100644 --- a/src/protocol/pipeline/push.c +++ b/src/protocol/pipeline/push.c @@ -210,7 +210,7 @@ push_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/pubsub/pub.c b/src/protocol/pubsub/pub.c index b7ac361e..10a9760f 100644 --- a/src/protocol/pubsub/pub.c +++ b/src/protocol/pubsub/pub.c @@ -286,7 +286,7 @@ pub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/pubsub/sub.c b/src/protocol/pubsub/sub.c index 0dbad081..5f4b497d 100644 --- a/src/protocol/pubsub/sub.c +++ b/src/protocol/pubsub/sub.c @@ -274,7 +274,7 @@ sub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 14a3e46b..c26be0b0 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -362,9 +362,9 @@ rep_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_maxttl) { - rv = nni_getopt_int(&s->ttl, buf, szp); + rv = nni_getopt_int(s->ttl, buf, szp); } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index 5da5003f..1b68c6dd 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -273,13 +273,13 @@ req_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_req_resendtime) { - rv = nni_getopt_usec(&s->retry, buf, szp); + rv = nni_getopt_usec(s->retry, buf, szp); } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } else if (opt == nng_optid_maxttl) { - rv = nni_getopt_int(&s->ttl, buf, szp); + rv = nni_getopt_int(s->ttl, buf, szp); } return (rv); diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c index c3a9dd81..89e19e91 100644 --- a/src/protocol/survey/respond.c +++ b/src/protocol/survey/respond.c @@ -374,9 +374,9 @@ resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_maxttl) { - rv = nni_getopt_int(&s->ttl, buf, szp); + rv = nni_getopt_int(s->ttl, buf, szp); } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c index 09fe0768..e90c7f57 100644 --- a/src/protocol/survey/survey.c +++ b/src/protocol/survey/survey.c @@ -300,9 +300,9 @@ surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp) int rv = NNG_ENOTSUP; if (opt == nng_optid_surveyor_surveytime) { - rv = nni_getopt_usec(&s->survtime, buf, szp); + rv = nni_getopt_usec(s->survtime, buf, szp); } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(&s->raw, buf, szp); + rv = nni_getopt_int(s->raw, buf, szp); } return (rv); } diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c index f10f80b4..5abd9e3a 100644 --- a/src/transport/ipc/ipc.c +++ b/src/transport/ipc/ipc.c @@ -687,7 +687,7 @@ nni_ipc_ep_getopt(void *arg, int opt, void *v, size_t *szp) if (opt == nng_optid_recvmaxsz) { nni_mtx_lock(&ep->mtx); - rv = nni_getopt_size(&ep->rcvmax, v, szp); + rv = nni_getopt_size(ep->rcvmax, v, szp); nni_mtx_unlock(&ep->mtx); } return (rv); diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index 759e20f5..587c1af9 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -812,12 +812,12 @@ nni_tcp_ep_getopt(void *arg, int opt, void *v, size_t *szp) if (opt == nng_optid_recvmaxsz) { nni_mtx_lock(&ep->mtx); - rv = nni_getopt_size(&ep->rcvmax, v, szp); + rv = nni_getopt_size(ep->rcvmax, v, szp); nni_mtx_unlock(&ep->mtx); } else if (opt == nng_optid_linger) { nni_mtx_lock(&ep->mtx); - rv = nni_getopt_usec(&ep->linger, v, szp); + rv = nni_getopt_usec(ep->linger, v, szp); nni_mtx_unlock(&ep->mtx); } // XXX: add address properties -- cgit v1.2.3-70-g09d2