From 446b150032f24c34644f0ab91ac6ab9206250865 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 17 Aug 2017 23:57:09 -0700 Subject: Endpoint API completely implemented. This supports creating listeners and dialers, managing options on them (though only a few options are supported at present), starting them and closing them, all independently. --- src/core/options.c | 79 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 19 deletions(-) (limited to 'src/core/options.c') diff --git a/src/core/options.c b/src/core/options.c index b243b262..ecfa437e 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -13,57 +13,98 @@ #include int -nni_setopt_usec(nni_duration *ptr, const void *val, size_t size) +nni_chkopt_usec(const void *v, size_t sz) +{ + nni_duration val; + if (sz != sizeof(val)) { + return (NNG_EINVAL); + } + memcpy(&val, v, sz); + if (val < -1) { + return (NNG_EINVAL); + } + return (0); +} + +int +nni_chkopt_int(const void *v, size_t sz, int minv, int maxv) +{ + int val; + if (sz != sizeof(val)) { + return (NNG_EINVAL); + } + memcpy(&val, v, sz); + if ((val < minv) || (val > maxv)) { + return (NNG_EINVAL); + } + return (0); +} + +int +nni_chkopt_size(const void *v, size_t sz, size_t minv, size_t maxv) +{ + size_t val; + if (sz != sizeof(val)) { + return (NNG_EINVAL); + } + memcpy(&val, v, sz); + if ((val < minv) || (val > maxv)) { + return (NNG_EINVAL); + } + return (0); +} + +int +nni_setopt_usec(nni_duration *dp, const void *v, size_t sz) { nni_duration dur; - if (size != sizeof(*ptr)) { + if (sz != sizeof(*dp)) { return (NNG_EINVAL); } - memcpy(&dur, val, sizeof(dur)); + memcpy(&dur, v, sizeof(dur)); if (dur < -1) { return (NNG_EINVAL); } - *ptr = dur; + *dp = dur; return (0); } int -nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval) +nni_setopt_int(int *ip, const void *v, size_t sz, int minv, int maxv) { - int v; + int i; - if (size != sizeof(v)) { + if (sz != sizeof(i)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof(v)); - if (v > maxval) { + memcpy(&i, v, sizeof(i)); + if (i > maxv) { return (NNG_EINVAL); } - if (v < minval) { + if (i < minv) { return (NNG_EINVAL); } - *ptr = v; + *ip = i; return (0); } int -nni_setopt_size( - size_t *ptr, const void *val, size_t size, size_t minval, size_t maxval) +nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv) { - size_t v; + size_t val; - if (size != sizeof(v)) { + if (sz != sizeof(val)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof(v)); - if (v > maxval) { + memcpy(&val, v, sizeof(val)); + if (val > maxv) { return (NNG_EINVAL); } - if (v < minval) { + if (val < minv) { return (NNG_EINVAL); } - *ptr = v; + *sp = val; return (0); } -- cgit v1.2.3-70-g09d2