From 8ad296769192cf4628710ac0b228be2aca6d8dad Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 23 Aug 2017 11:23:22 -0700 Subject: Implement dynamic option numbering. This permits option numbers to be allocated based on string name. Eventually all the option values will be replaced with option names. This will facilitate transports (ZeroTier) that may need further options. --- src/protocol/pair/pair_v1.c | 46 +++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'src/protocol/pair') diff --git a/src/protocol/pair/pair_v1.c b/src/protocol/pair/pair_v1.c index 2b8a9120..09543830 100644 --- a/src/protocol/pair/pair_v1.c +++ b/src/protocol/pair/pair_v1.c @@ -69,6 +69,7 @@ pair1_sock_init(void **sp, nni_sock *nsock) { pair1_sock *s; int rv; + int poly; if ((s = NNI_ALLOC_STRUCT(s)) == NULL) { return (NNG_ENOMEM); @@ -83,6 +84,11 @@ pair1_sock_init(void **sp, nni_sock *nsock) nni_aio_init(&s->aio_getq, pair1_sock_getq_cb, s); nni_mtx_init(&s->mtx); + if ((rv = nni_option_register("polyamorous", &poly)) != 0) { + pair1_sock_fini(s); + return (rv); + } + s->nsock = nsock; s->raw = 0; s->poly = 0; @@ -393,30 +399,30 @@ pair1_sock_setopt(void *arg, int opt, const void *buf, size_t sz) pair1_sock *s = arg; int rv; - nni_mtx_lock(&s->mtx); - switch (opt) { - case NNG_OPT_RAW: + if (opt == nni_option_lookup("raw")) { + nni_mtx_lock(&s->mtx); if (s->started) { rv = NNG_ESTATE; } else { rv = nni_setopt_int(&s->raw, buf, sz, 0, 1); } - break; - case NNG_OPT_POLYAMOROUS: + nni_mtx_unlock(&s->mtx); + } else if (opt == nni_option_lookup("polyamorous")) { + nni_mtx_lock(&s->mtx); if (s->started) { rv = NNG_ESTATE; } else { rv = nni_setopt_int(&s->poly, buf, sz, 0, 1); } - break; - case NNG_OPT_MAXTTL: + nni_mtx_unlock(&s->mtx); + } else if (opt == nni_option_lookup("max-ttl")) { + nni_mtx_lock(&s->mtx); rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255); - break; - default: + nni_mtx_unlock(&s->mtx); + } else { rv = NNG_ENOTSUP; } - nni_mtx_unlock(&s->mtx); return (rv); } @@ -426,21 +432,21 @@ pair1_sock_getopt(void *arg, int opt, void *buf, size_t *szp) pair1_sock *s = arg; int rv; - nni_mtx_lock(&s->mtx); - switch (opt) { - case NNG_OPT_RAW: + if (opt == nni_option_lookup("raw")) { + nni_mtx_lock(&s->mtx); rv = nni_getopt_int(&s->raw, buf, szp); - break; - case NNG_OPT_MAXTTL: + nni_mtx_unlock(&s->mtx); + } else if (opt == nni_option_lookup("max-ttl")) { + nni_mtx_lock(&s->mtx); rv = nni_getopt_int(&s->ttl, buf, szp); - break; - case NNG_OPT_POLYAMOROUS: + nni_mtx_unlock(&s->mtx); + } else if (opt == nni_option_lookup("polyamorous")) { + nni_mtx_lock(&s->mtx); rv = nni_getopt_int(&s->poly, buf, szp); - break; - default: + nni_mtx_unlock(&s->mtx); + } else { rv = NNG_ENOTSUP; } - nni_mtx_unlock(&s->mtx); return (rv); } -- cgit v1.2.3-70-g09d2