diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-03-20 18:38:54 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-03-20 18:42:23 -0700 |
| commit | 6df40cb6eea9a4220d61c4c927ce5a857a12a338 (patch) | |
| tree | ac4b7ecbcb41a456eb4d0429fc180047656371ba | |
| parent | 9ca901c1b70b17d851426483d9f54611cfa8e395 (diff) | |
| download | nng-6df40cb6eea9a4220d61c4c927ce5a857a12a338.tar.gz nng-6df40cb6eea9a4220d61c4c927ce5a857a12a338.tar.bz2 nng-6df40cb6eea9a4220d61c4c927ce5a857a12a338.zip | |
fixes #301 String option handling for getopt
47 files changed, 651 insertions, 431 deletions
diff --git a/docs/man/libnng.3.adoc b/docs/man/libnng.3.adoc index 66b00698..c5c345f6 100644 --- a/docs/man/libnng.3.adoc +++ b/docs/man/libnng.3.adoc @@ -31,7 +31,9 @@ The following common functions exist in _libnng_. |=== |<<nng_alloc.3#,nng_alloc()>>|allocate memory |<<nng_free.3#,nng_free()>>|free memory +|<<nng_strdup.3#,nng_strdup()>>|duplicate string |<<nng_strerror.3#,nng_strerror()>>|return an error description +|<<nng_strfree.3#,nng_strfree()>>|free string |<<nng_version.3#,nng_version()>>|report library version |=== diff --git a/docs/man/nng_alloc.3.adoc b/docs/man/nng_alloc.3.adoc index 7d7adf07..a43140d1 100644 --- a/docs/man/nng_alloc.3.adoc +++ b/docs/man/nng_alloc.3.adoc @@ -33,17 +33,19 @@ case it can be directly passed to <<nng_send.3#,`nng_send()`>> using the flag `NNG_FLAG_ALLOC`. Alternatively, it can be freed when no longer needed using <<nng_free.3#,`nng_free()`>>. -WARNING: Do not use the system `free()` function to release this memory. +IMPORTANT: Do not use the system `free()` function to release this memory. On some platforms this may work, but it is not guaranteed and may lead to a crash or other undesirable and unpredictable behavior. == RETURN VALUES -This function returns 0 on success, and non-zero otherwise. +This function returns a pointer to the allocated memory on success, +and `NULL` otherwise. == ERRORS -`NNG_ENOMEM`:: Insufficient free memory exists. +No errors are returned, but a `NULL` return value should be +treated the same as `NNG_ENOMEM`. == SEE ALSO diff --git a/docs/man/nng_dialer_getopt.3.adoc b/docs/man/nng_dialer_getopt.3.adoc index 3fde89d6..bdabc5d9 100644 --- a/docs/man/nng_dialer_getopt.3.adoc +++ b/docs/man/nng_dialer_getopt.3.adoc @@ -33,6 +33,8 @@ int nng_dialer_getopt_size(nng_dialer d, const char *opt, size_t *zp); int nng_dialer_getopt_sockaddr(nng_dialer d, const char *opt, nng_sockaddr *sap); +int nng_dialer_getopt_string(nng_dialer d, const char *opt, char **strp); + int nng_dialer_getopt_uint64(nng_dialer d, const char *opt, uint64_t *u64p); ---- @@ -52,15 +54,12 @@ are documented with the transports and protocols themselves. In all of these forms, the option _opt_ is retrieved from the dialer _d_. The forms vary based on the type of the option they take. -TIP: Generally, it will be easier to use one of the typed versions of this -function. - -NOTE: No validation that the option is actually of the associated type is -performed, so the caller must take care to use the *correct* typed form. - The details of the type, size, and semantics of the option will depend on the actual option, and will be documented with the option itself. +TIP: Generally, it will be easier to use one of the typed versions of this +function. + ==== `nng_dialer_getopt()` This function is untyped and can be used to retrieve the value of any option. The caller must store a pointer to a buffer to receive the value in _val_, @@ -107,6 +106,13 @@ typically for buffer sizes, message maximum sizes, and similar options. This function is used to retrieve an <<nng_sockaddr.5#,`nng_sockaddr`>> into the value referenced by _sap_. +==== `nng_dialer_getopt_string()` + +This function is used to retrieve a string into _strp_. +This string is created from the source using <<nng_strdup.3#,`nng_strdup()`>> +and consequently must be freed by the caller using +<<nng_strfree.3#,`nng_strfree()`>> when it is no longer needed. + ==== `nng_dialer_getopt_uint64()` This function is used to retrieve a 64-bit unsigned value into the value referenced by _u64p_. @@ -119,7 +125,10 @@ These functions returns 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _d_ does not refer to an open dialer. +`NNG_EINVAL`:: Size of destination _val_ too small for object. +`NNG_ENOMEM`:: Insufficient memory exists. `NNG_ENOTSUP`:: The option _opt_ is not supported. `NNG_EWRITEONLY`:: The option _opt_ is write-only. @@ -127,7 +136,9 @@ These functions returns 0 on success, and non-zero otherwise. <<nng_dialer_create.3#,nng_dialer_create(3)>> <<nng_dialer_setopt.3#,nng_dialer_setopt(3)>> +<<nng_strdup.3#,nng_strdup(3)>>, <<nng_strerror.3#,nng_strerror(3)>>, +<<nng_strfree.3#,nng_strfree(3)>>, <<nng_dialer.5#,nng_dialer(5)>>, <<nng_duration.5#,nng_duration(5)>>, <<nng_options.5#,nng_options(5)>>, diff --git a/docs/man/nng_dialer_setopt.3.adoc b/docs/man/nng_dialer_setopt.3.adoc index 1bec71b2..330657d0 100644 --- a/docs/man/nng_dialer_setopt.3.adoc +++ b/docs/man/nng_dialer_setopt.3.adoc @@ -60,9 +60,6 @@ on the actual option, and will be documented with the option itself. TIP: Generally, it will be easier to use one of the typed forms instead. -NOTE: No validation that the option is actually of the associated -type is performed, so the caller must take care to use the *correct* typed form. - ==== `nng_dialer_setopt()` This function is untyped, and can be used to configure any arbitrary data. The _val_ pointer addresses the data to copy, and _valsz_ is the @@ -112,6 +109,7 @@ These functions return 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _d_ does not refer to an open dialer. `NNG_EINVAL`:: The value being passed is invalid. `NNG_ENOTSUP`:: The option _opt_ is not supported. diff --git a/docs/man/nng_free.3.adoc b/docs/man/nng_free.3.adoc index c5588a1c..d17f3da5 100644 --- a/docs/man/nng_free.3.adoc +++ b/docs/man/nng_free.3.adoc @@ -28,10 +28,10 @@ The `nng_free()` function deallocates a memory region of size _size_, that was previously allocated by <<nng_alloc.3#,`nng_alloc()`>> or <<nng_recv.3#,`nng_recv()`>> with the `NNG_FLAG_ALLOC` flag. -WARNING: It is very important that _size_ match the allocation size +IMPORTANT: It is very important that _size_ match the allocation size used to allocate the memory. -WARNING: Do not attempt to use this function to deallocate memory +IMPORTANT: Do not attempt to use this function to deallocate memory obtained by a call to the system `malloc()` or `calloc()` functions, or the {cpp} `new` operator. Doing so may result in unpredictable diff --git a/docs/man/nng_getopt.3.adoc b/docs/man/nng_getopt.3.adoc index bd4d4c70..af61132c 100644 --- a/docs/man/nng_getopt.3.adoc +++ b/docs/man/nng_getopt.3.adoc @@ -31,6 +31,8 @@ int nng_getopt_ptr(nng_socket s, const char *opt, void **ptr); int nng_getopt_size(nng_socket s, const char *opt, size_t *zp); +int nng_getopt_string(nng_socket s, const char *opt, char **strp); + int nng_getopt_uint64(nng_socket s, const char *opt, uint64_t *u64p); ----------- @@ -50,14 +52,11 @@ documented with the transports and protocols themselves. In all of these forms, the option _opt_ is retrieved from the socket _s_. The forms vary based on the type of the option they take. -TIP: Generally, it will be easier to use one of the typed forms instead. - -NOTE: No validation that the option is actually of the associated -type is performed, so the caller must take care to use the *correct* typed form. - The details of the type, size, and semantics of the option will depend on the actual option, and will be documented with the option itself. +TIP: Generally, it will be easier to use one of the typed forms instead. + ==== `nng_getopt()` This function is untyped and can be used to retrieve the value of any option. The caller must store a pointer to a buffer to receive the value in _val_, @@ -100,6 +99,12 @@ not copied, but instead the *pointer* to the object is copied. This function is used to retrieve a size into the pointer _zp_, typically for buffer sizes, message maximum sizes, and similar options. +==== `nng_getopt_string()` +This function is used to retrieve a string into _strp_. +This string is created from the source using <<nng_strdup.3#,`nng_strdup()`>> +and consequently must be freed by the caller using +<<nng_strfree.3#,`nng_strfree()`>> when it is no longer needed. + ==== `nng_getopt_uint64()` This function is used to retrieve a 64-bit unsigned value into the value referenced by _u64p_. @@ -112,7 +117,10 @@ These functions return 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _s_ does not refer to an open socket. +`NNG_EINVAL`:: Size of destination _val_ too small for object. +`NNG_ENOMEM`:: Insufficient memory exists. `NNG_ENOTSUP`:: The option _opt_ is not supported. `NNG_EWRITEONLY`:: The option _opt_ is write-only. @@ -122,7 +130,9 @@ These functions return 0 on success, and non-zero otherwise. <<nng_listener_getopt.3#,nng_listener_getopt(3)>>, <<nng_pipe_getopt.3#,nng_pipe_getopt(3)>>, <<nng_setopt.3#,nng_setopt(3)>>, +<<nng_strdup.3#,nng_strdup(3)>>, <<nng_strerror.3#,nng_strerror(3)>>, +<<nng_strfree.3#,nng_strfree(3)>>, <<nng_duration.5#,nng_duration(5)>>, <<nng_options.5#,nng_options(5)>>, <<nng_socket.5#,nng_socket(5)>>, diff --git a/docs/man/nng_listener_getopt.3.adoc b/docs/man/nng_listener_getopt.3.adoc index 1bafd1d6..3fe79868 100644 --- a/docs/man/nng_listener_getopt.3.adoc +++ b/docs/man/nng_listener_getopt.3.adoc @@ -33,6 +33,8 @@ int nng_listener_getopt_size(nng_listener l, const char *opt, size_t *zp); int nng_listener_getopt_sockaddr(nng_listener l, const char *opt, nng_sockaddr *sap); +int nng_listener_getopt_string(nng_listener l, const char *opt, char **strp); + int nng_listener_getopt_uint64(nng_listener l, const char *opt, uint64_t *u64p); ---- @@ -52,15 +54,12 @@ are documented with the transports and protocols themselves. In all of these forms, the option _opt_ is retrieved from the listener _l_. The forms vary based on the type of the option they take. -TIP: Generally, it will be easier to use one of the typed versions of this -function. - -NOTE: No validation that the option is actually of the associated type is -performed, so the caller must take care to use the *correct* typed form. - The details of the type, size, and semantics of the option will depend on the actual option, and will be documented with the option itself. +TIP: Generally, it will be easier to use one of the typed versions of this +function. + ==== `nng_listener_getopt()` This function is untyped and can be used to retrieve the value of any option. The caller must store a pointer to a buffer to receive the value in _val_, @@ -105,6 +104,12 @@ typically for buffer sizes, message maximum sizes, and similar options. This function is used to retrieve an <<nng_sockaddr.5#,`nng_sockaddr`>> into the value referenced by _sap_. +==== `nng_listener_getopt_string()` +This function is used to retrieve a string into _strp_. +This string is created from the source using <<nng_strdup.3#,`nng_strdup()`>> +and consequently must be freed by the caller using +<<nng_strfree.3#,`nng_strfree()`>> when it is no longer needed. + ==== `nng_listener_getopt_uint64()` This function is used to retrieve a 64-bit unsigned value into the value referenced by _u64p_. @@ -117,7 +122,10 @@ These functions return 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _l_ does not refer to an open listener. +`NNG_EINVAL`:: Size of destination _val_ too small for object. +`NNG_ENOMEM`:: Insufficient memory exists. `NNG_ENOTSUP`:: The option _opt_ is not supported. `NNG_EWRITEONLY`:: The option _opt_ is write-only. @@ -127,7 +135,9 @@ These functions return 0 on success, and non-zero otherwise. <<nng_listener_create.3#,nng_listener_create(3)>> <<nng_listener_setopt.3#,nng_listener_setopt(3)>> <<nng_getopt.3#,nng_getopt(3)>>, +<<nng_strdup.3#,nng_strdup(3)>>, <<nng_strerror.3#,nng_strerror(3)>>, +<<nng_strfree.3#,nng_strfree(3)>>, <<nng_duration.5#,nng_duration(5)>>, <<nng_listener.5#,nng_listener(5)>>, <<nng_options.5#,nng_options(5)>>, diff --git a/docs/man/nng_listener_setopt.3.adoc b/docs/man/nng_listener_setopt.3.adoc index 03a33a57..d24a4f06 100644 --- a/docs/man/nng_listener_setopt.3.adoc +++ b/docs/man/nng_listener_setopt.3.adoc @@ -58,9 +58,6 @@ on the actual option, and will be documented with the option itself. TIP: Generally, it will be easier to use one of the typed forms instead. -NOTE: No validation that the option is actually of the associated -type is performed, so the caller must take care to use the *correct* typed form. - ==== `nng_listener_setopt()` This function is untyped, and can be used to configure any arbitrary data. The _val_ pointer addresses the data to copy, and _valsz_ is the @@ -110,6 +107,7 @@ These functions return 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _l_ does not refer to an open listener. `NNG_EINVAL`:: The value being passed is invalid. `NNG_ENOTSUP`:: The option _opt_ is not supported. diff --git a/docs/man/nng_pipe_getopt.3.adoc b/docs/man/nng_pipe_getopt.3.adoc index fd607198..66216a73 100644 --- a/docs/man/nng_pipe_getopt.3.adoc +++ b/docs/man/nng_pipe_getopt.3.adoc @@ -31,6 +31,8 @@ int nng_dialer_getopt_ptr(nng_pipe p, const char *opt, void **ptr); int nng_pipe_getopt_sockaddr(nng_pipe p, const char *opt, nng_sockaddr *sap); +int nng_pipe_getopt_string(nng_pipe p, const char *opt, char **strp); + int nng_pipe_getopt_size(nng_pipe p, const char *opt, size_t *zp); int nng_pipe_getopt_uint64(nng_pipe p, const char *opt, uint64_t *u64p); @@ -81,8 +83,6 @@ This can be used to determine the size of the buffer needed to receive the object. TIP: Generally, it will be easier to use one of the typed forms instead. -Note however that no validation that the option is actually of the associated -type is performed, so the caller must take care to use the *correct* typed form. ==== `nng_pipe_getopt_bool()` @@ -117,6 +117,13 @@ typically for buffer sizes, message maximum sizes, and similar options. This function is used to retrieve an <<nng_sockaddr.5#,`nng_sockaddr`>> into _sap_. +==== `nng_pipe_getopt_string()` + +This function is used to retrieve a string into _strp_. +This string is created from the source using <<nng_strdup.3#,`nng_strdup()`>> +and consequently must be freed by the caller using +<<nng_strfree.3#,`nng_strfree()`>> when it is no longer needed. + ==== `nng_pipe_getopt_uint64()` This function is used to retriev a 64-bit unsigned value into the value @@ -130,8 +137,11 @@ These functions return 0 on success, and non-zero otherwise. == ERRORS +`NNG_EBADTYPE`:: Incorrect type for option. `NNG_ECLOSED`:: Parameter _p_ does not refer to an open pipe. `NNG_ENOTSUP`:: The option _opt_ is not supported. +`NNG_ENOMEM`:: Insufficient memory exists. +`NNG_EINVAL`:: Size of destination _val_ too small for object. `NNG_EWRITEONLY`:: The option _opt_ is write-only. == SEE ALSO @@ -140,7 +150,9 @@ These functions return 0 on success, and non-zero otherwise. <<nng_getopt.3#,nng_getopt(3)>>, <<nng_listener_setopt.3#,nng_listener_setopt(3)>> <<nng_msg_get_pipe.3#,nng_msg_get_pipe(3)>> +<<nng_strdup.3#,nng_strdup(3)>>, <<nng_strerror.3#,nng_strerror(3)>>, +<<nng_strfree.3#,nng_strfree(3)>>, <<nng_duration.5#,nng_duration(5)>>, <<nng_options.5#,nng_options(5)>>, <<nng_pipe.5#,nng_pipe(5)>>, diff --git a/docs/man/nng_setopt.3.adoc b/docs/man/nng_setopt.3.adoc index 915fb5de..91e54225 100644 --- a/docs/man/nng_setopt.3.adoc +++ b/docs/man/nng_setopt.3.adoc @@ -55,9 +55,6 @@ on the actual option, and will be documented with the option itself. TIP: Generally, it will be easier to use one of the typed versions of this function. -NOTE: No validation that the option is actually of the associated -type is performed, so the caller must take care to use the *correct* typed form. - ==== `nng_setopt()` This function is untyped, and can be used to configure any arbitrary data. The _val_ pointer addresses the data to copy, and _valsz_ is the diff --git a/docs/man/nng_strdup.3.adoc b/docs/man/nng_strdup.3.adoc new file mode 100644 index 00000000..23e23a43 --- /dev/null +++ b/docs/man/nng_strdup.3.adoc @@ -0,0 +1,55 @@ += nng_strdup(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_strdup - duplicate string + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +char *nng_strdup(const char *src); +---- + +== DESCRIPTION + +The `nng_strdup()` duplicates the string _src_ and returns it. + +This is logically equiavlent to using <<nng_alloc.3#,`nng_alloc()`>> +to allocate a region of memory of `strlen(s) + 1` bytes, and then +using `strcpy()` to copy the string into the destination before +returning it. + +The returned string should be deallocated with +<<nng_strfree.3#,`nng_strfree()`>>, or may be deallocated using the +<<nng_free.3#,`nng_free()`>> using the length of the returned string plus +one (for the `NUL` terminating byte). + +IMPORTANT: Do not use the system `free()` or similar functions to deallocate +the string, since those may use a different memory arena! + +== RETURN VALUES + +This function returns the new string on success, and `NULL` on failure. + +== ERRORS + +No errors are returned, but a `NULL` return value should be +treated the same as `NNG_ENOMEM`. + +== SEE ALSO + +<<nng_alloc.3#,nng_alloc(3)>>, +<<nng_free.3#,nng_free(3)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_strfree.3.adoc b/docs/man/nng_strfree.3.adoc new file mode 100644 index 00000000..2e6569a8 --- /dev/null +++ b/docs/man/nng_strfree.3.adoc @@ -0,0 +1,57 @@ += nng_strfree(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_free - free memory + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +void nng_strfree(char *str); +---- + +== DESCRIPTION + +The `nng_strfree()` function deallocates the string _str_. +This is equivalent to using <<nng_free.3#,`nng_free()`>> with +the length of _str_ plus one (for the `NUL` terminating byte) as +the size. + +IMPORTANT: This should only be used with strings that were allocated +by <<nng_strdup.3#,`nng_strdup()`>> or +<<nng_alloc.3#,`nng_alloc()`>>. +In all cases, the allocation size of the string must be the same +as `strlen(__str__) + 1`. + +IMPORTANT: Consequently, if the a string created with +<<nng_strdup.3#,`nng_strdup()`>> is modified to be shorter, then +it is incorrect to call this function. +(The <<nng_free.3#,`nng_free()`>> function can be used instead in that +case, using the length of the original string plus one for the size.) + +== RETURN VALUES + +None. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_alloc.3#,nng_alloc(3)>>, +<<nng_free.3#,nng_free(3)>>, +<<nng_strdup.3#,nng_strdup(3)>>, +<<nng.7#,nng(7)>> diff --git a/src/core/endpt.c b/src/core/endpt.c index 131ee1db..d4bc9b01 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -649,12 +649,8 @@ nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp, int t) if (eo->eo_getopt == NULL) { return (NNG_EWRITEONLY); } - if ((t != NNI_TYPE_OPAQUE) && - (eo->eo_type != NNI_TYPE_OPAQUE) && (t != eo->eo_type)) { - return (NNG_EBADTYPE); - } nni_mtx_lock(&ep->ep_mtx); - rv = eo->eo_getopt(ep->ep_data, valp, szp); + rv = eo->eo_getopt(ep->ep_data, valp, szp, t); nni_mtx_unlock(&ep->ep_mtx); return (rv); } @@ -663,11 +659,7 @@ nni_ep_getopt(nni_ep *ep, const char *name, void *valp, size_t *szp, int t) // override. This allows the URL to be created with wildcards, // that are resolved later. if (strcmp(name, NNG_OPT_URL) == 0) { - if (t != NNI_TYPE_OPAQUE) { - // XXX: Add NNI_TYPE_STRING. - return (NNG_EBADTYPE); - } - return (nni_getopt_str(ep->ep_url->u_rawurl, valp, szp)); + return (nni_copyout_str(ep->ep_url->u_rawurl, valp, szp, t)); } return (nni_sock_getopt(ep->ep_sock, name, valp, szp, t)); diff --git a/src/core/options.c b/src/core/options.c index f51a6abe..9410de5f 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -129,140 +129,166 @@ nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv) } int -nni_getopt_ms(nni_duration u, void *val, size_t *sizep) +nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) { - size_t sz = sizeof(u); + int len; - if (sz > *sizep) { - sz = *sizep; + if (sz < sizeof(len)) { + return (NNG_EINVAL); } - *sizep = sizeof(u); - memcpy(val, &u, sz); - return (0); + memcpy(&len, val, sizeof(len)); + if (len < 0) { + return (NNG_EINVAL); + } + if (len > 8192) { + // put a reasonable uppper limit on queue depth. + // This is a count in messages, so the total queue + // size could be quite large indeed in this case. + return (NNG_EINVAL); + } + return (nni_msgq_resize(mq, len)); } int -nni_getopt_sockaddr(const nng_sockaddr *sa, void *val, size_t *sizep) +nni_copyout(const void *src, size_t srcsz, void *dst, size_t *dstszp) { - size_t sz = sizeof(*sa); - - if (sz > *sizep) { - sz = *sizep; + int rv = 0; + size_t copysz = *dstszp; + // Assumption is that this is type NNI_TYPE_OPAQUE. + if (copysz > srcsz) { + copysz = srcsz; + } else if (srcsz > copysz) { + // destination too small. + rv = NNG_EINVAL; } - *sizep = sizeof(*sa); - memcpy(val, sa, sz); - return (0); + *dstszp = srcsz; + memcpy(dst, src, copysz); + return (rv); } int -nni_getopt_int(int i, void *val, size_t *sizep) +nni_copyout_bool(bool b, void *dst, size_t *szp, int typ) { - size_t sz = sizeof(i); - - if (sz > *sizep) { - sz = *sizep; + switch (typ) { + case NNI_TYPE_BOOL: + NNI_ASSERT(*szp == sizeof(b)); + *(bool *) dst = b; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&b, sizeof(b), dst, szp)); + default: + return (NNG_EBADTYPE); } - *sizep = sizeof(i); - memcpy(val, &i, sz); - return (0); } int -nni_getopt_u64(uint64_t u, void *val, size_t *sizep) +nni_copyout_int(int i, void *dst, size_t *szp, int typ) { - size_t sz = sizeof(u); - - if (sz > *sizep) { - sz = *sizep; + switch (typ) { + case NNI_TYPE_INT32: + NNI_ASSERT(*szp == sizeof(i)); + *(int *) dst = i; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&i, sizeof(i), dst, szp)); + default: + return (NNG_EBADTYPE); } - *sizep = sizeof(u); - memcpy(val, &u, sz); - return (0); } int -nni_getopt_str(const char *ptr, void *val, size_t *sizep) +nni_copyout_ms(nng_duration d, void *dst, size_t *szp, int typ) { - size_t len = strlen(ptr) + 1; - size_t sz; - - sz = (len > *sizep) ? *sizep : len; - *sizep = len; - memcpy(val, ptr, sz); - return (0); + switch (typ) { + case NNI_TYPE_DURATION: + NNI_ASSERT(*szp == sizeof(d)); + *(nng_duration *) dst = d; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&d, sizeof(d), dst, szp)); + default: + return (NNG_EBADTYPE); + } } int -nni_getopt_size(size_t u, void *val, size_t *sizep) +nni_copyout_ptr(void *p, void *dst, size_t *szp, int typ) { - size_t sz = sizeof(u); - - if (sz > *sizep) { - sz = *sizep; + switch (typ) { + case NNI_TYPE_POINTER: + NNI_ASSERT(*szp == sizeof(p)); + *(void **) dst = p; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&p, sizeof(p), dst, szp)); + default: + return (NNG_EBADTYPE); } - *sizep = sizeof(u); - memcpy(val, &u, sz); - return (0); } int -nni_getopt_bool(bool b, void *val, size_t *sizep) +nni_copyout_size(size_t s, void *dst, size_t *szp, int typ) { - size_t sz = sizeof(b); - - if (sz > *sizep) { - sz = *sizep; + switch (typ) { + case NNI_TYPE_SIZE: + NNI_ASSERT(*szp == sizeof(s)); + *(size_t *) dst = s; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&s, sizeof(s), dst, szp)); + default: + return (NNG_EBADTYPE); } - *sizep = sizeof(b); - memcpy(val, &b, sz); - return (0); } int -nni_getopt_ptr(void *ptr, void *val, size_t *sizep) +nni_copyout_sockaddr(const nng_sockaddr *sap, void *dst, size_t *szp, int typ) { - size_t sz = sizeof(ptr); - - if (sz > *sizep) { - sz = *sizep; + switch (typ) { + case NNI_TYPE_SOCKADDR: + NNI_ASSERT(*szp == sizeof(*sap)); + *(nng_sockaddr *) dst = *sap; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(sap, sizeof(*sap), dst, szp)); + default: + return (NNG_EBADTYPE); } - *sizep = sizeof(ptr); - memcpy(val, &ptr, sz); - return (0); } int -nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) +nni_copyout_u64(uint64_t u, void *dst, size_t *szp, int typ) { - int len; - - if (sz < sizeof(len)) { - return (NNG_EINVAL); + switch (typ) { + case NNI_TYPE_UINT64: + NNI_ASSERT(*szp == sizeof(u)); + *(uint64_t *) dst = u; + return (0); + case NNI_TYPE_OPAQUE: + return (nni_copyout(&u, sizeof(u), dst, szp)); + default: + return (NNG_EBADTYPE); } - memcpy(&len, val, sizeof(len)); - if (len < 0) { - return (NNG_EINVAL); - } - if (len > 8192) { - // put a reasonable uppper limit on queue depth. - // This is a count in messages, so the total queue - // size could be quite large indeed in this case. - return (NNG_EINVAL); - } - return (nni_msgq_resize(mq, len)); } int -nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) +nni_copyout_str(const char *str, void *dst, size_t *szp, int typ) { - int len = nni_msgq_cap(mq); - - size_t sz = *sizep; - - if (sz > sizeof(len)) { - sz = sizeof(len); + char *s; + + switch (typ) { + case NNI_TYPE_STRING: + NNI_ASSERT(*szp == sizeof(char *)); + if ((s = nni_strdup(str)) == NULL) { + return (NNG_ENOMEM); + } + *(char **) dst = s; + return (0); + + case NNI_TYPE_OPAQUE: + return (nni_copyout(str, strlen(str) + 1, dst, szp)); + + default: + return (NNG_EBADTYPE); } - memcpy(val, &len, sz); - *sizep = sizeof(len); - return (0); -}
\ No newline at end of file +} diff --git a/src/core/options.h b/src/core/options.h index f5743aae..35c3232f 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -18,22 +18,13 @@ // nni_setopt_buf sets the queue size for the message queue. extern int nni_setopt_buf(nni_msgq *, const void *, size_t); -// nni_getopt_buf gets the queue size for the message queue. -extern int nni_getopt_buf(nni_msgq *, void *, size_t *); - // nni_setopt_duration sets the duration. Durations must be legal, // either a positive value, 0, or -1 to indicate forever. extern int nni_setopt_ms(nni_duration *, const void *, size_t); -// nni_getopt_duration gets the duration. -extern int nni_getopt_ms(nni_duration, void *, size_t *); - // nni_setopt_bool sets a bool, or _Bool extern int nni_setopt_bool(bool *, const void *, size_t); -// nni_getopt_bool gets a bool (or _Bool) -extern int nni_getopt_bool(bool, void *, size_t *); - // nni_setopt_int sets an integer, which must be between the minimum and // maximum values (inclusive). extern int nni_setopt_int(int *, const void *, size_t, int, int); @@ -41,18 +32,6 @@ extern int nni_setopt_int(int *, const void *, size_t, int, int); #define NNI_MAXINT ((int) 2147483647) #define NNI_MININT ((int) -2147483648) -// nni_getopt_int gets an integer. -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_getopt_sockaddr gets an nng_sockaddr. -extern int nni_getopt_sockaddr(const nng_sockaddr *, 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); @@ -61,15 +40,24 @@ extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t); #define NNI_MINSZ (0) #define NNI_MAXSZ ((size_t) 0xffffffff) -// nni_getopt_size obtains a size_t option. -extern int nni_getopt_size(size_t, void *, size_t *); - -// nni_getopt_ptr obtains a pointer option. -extern int nni_getopt_ptr(void *, void *, size_t *); - extern int nni_chkopt_bool(size_t); extern int nni_chkopt_ms(const void *, size_t); extern int nni_chkopt_int(const void *, size_t, int, int); extern int nni_chkopt_size(const void *, size_t, size_t, size_t); +// nni_copyout_xxx copies out a type of the named value. It assumes that +// the type is aligned and the size correct, unless NNI_TYPE_OPAQUE is passed. +extern int nni_copyout(const void *, size_t, void *, size_t *); +extern int nni_copyout_bool(bool, void *, size_t *, int); +extern int nni_copyout_int(int, void *, size_t *, int); +extern int nni_copyout_ms(nng_duration, void *, size_t *, int); +extern int nni_copyout_ptr(void *, void *, size_t *, int); +extern int nni_copyout_size(size_t, void *, size_t *, int); +extern int nni_copyout_sockaddr(const nng_sockaddr *, void *, size_t *, int); +extern int nni_copyout_u64(uint64_t, void *, size_t *, int); + +// nni_copyout_str copies out a string. If the type is NNI_TYPE_STRING, +// then it passes through a pointer, created by nni_strdup(). +extern int nni_copyout_str(const char *, void *, size_t *, int); + #endif // CORE_OPTIONS_H diff --git a/src/core/pipe.c b/src/core/pipe.c index 40720906..a4148c65 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -297,11 +297,7 @@ nni_pipe_getopt(nni_pipe *p, const char *name, void *val, size_t *szp, int typ) if (strcmp(po->po_name, name) != 0) { continue; } - if ((typ != NNI_TYPE_OPAQUE) && - (po->po_type != NNI_TYPE_OPAQUE) && (typ != po->po_type)) { - return (NNG_EBADTYPE); - } - return (po->po_getopt(p->p_tran_data, val, szp)); + return (po->po_getopt(p->p_tran_data, val, szp, typ)); } // Maybe the endpoint knows? return (nni_ep_getopt(p->p_ep, name, val, szp, typ)); diff --git a/src/core/protocol.h b/src/core/protocol.h index e0e0e0d7..1c341241 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -50,7 +50,7 @@ struct nni_proto_pipe_ops { struct nni_proto_sock_option { const char *pso_name; int pso_type; - int (*pso_getopt)(void *, void *, size_t *); + int (*pso_getopt)(void *, void *, size_t *, int); int (*pso_setopt)(void *, const void *, size_t); }; diff --git a/src/core/socket.c b/src/core/socket.c index 40fdc9c8..9e98a9d9 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -22,7 +22,7 @@ static nni_mtx nni_sock_lk; typedef struct nni_socket_option { const char *so_name; int so_type; - int (*so_getopt)(nni_sock *, void *, size_t *); + int (*so_getopt)(nni_sock *, void *, size_t *, int); int (*so_setopt)(nni_sock *, const void *, size_t); } nni_socket_option; @@ -100,17 +100,13 @@ nni_sock_can_recv_cb(void *arg, int flags) } static int -nni_sock_getopt_fd(nni_sock *s, int flag, void *val, size_t *szp) +nni_sock_get_fd(nni_sock *s, int flag, int *fdp) { int rv; nni_notifyfd *fd; nni_msgq * mq; nni_msgq_cb cb; - if ((*szp < sizeof(int))) { - return (NNG_EINVAL); - } - if ((flag & nni_sock_flags(s)) == 0) { return (NNG_ENOTSUP); } @@ -131,35 +127,43 @@ nni_sock_getopt_fd(nni_sock *s, int flag, void *val, size_t *szp) return (NNG_EINVAL); } - // If we already inited this, just give back the same file descriptor. - if (fd->sn_init) { - memcpy(val, &fd->sn_rfd, sizeof(int)); - *szp = sizeof(int); - return (0); - } + // Open if not already done. + if (!fd->sn_init) { + if ((rv = nni_plat_pipe_open(&fd->sn_wfd, &fd->sn_rfd)) != 0) { + return (rv); + } - if ((rv = nni_plat_pipe_open(&fd->sn_wfd, &fd->sn_rfd)) != 0) { - return (rv); - } + nni_msgq_set_cb(mq, cb, fd); - nni_msgq_set_cb(mq, cb, fd); + fd->sn_init = 1; + } - fd->sn_init = 1; - *szp = sizeof(int); - memcpy(val, &fd->sn_rfd, sizeof(int)); + *fdp = fd->sn_rfd; return (0); } static int -nni_sock_getopt_sendfd(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_sendfd(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_sock_getopt_fd(s, NNI_PROTO_FLAG_SND, buf, szp)); + int fd; + int rv; + + if ((rv = nni_sock_get_fd(s, NNI_PROTO_FLAG_SND, &fd)) != 0) { + return (rv); + } + return (nni_copyout_int(fd, buf, szp, typ)); } static int -nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_sock_getopt_fd(s, NNI_PROTO_FLAG_RCV, buf, szp)); + int fd; + int rv; + + if ((rv = nni_sock_get_fd(s, NNI_PROTO_FLAG_RCV, &fd)) != 0) { + return (rv); + } + return (nni_copyout_int(fd, buf, szp, typ)); } static int @@ -169,9 +173,9 @@ nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_ms(s->s_rcvtimeo, buf, szp)); + return (nni_copyout_ms(s->s_rcvtimeo, buf, szp, typ)); } static int @@ -181,9 +185,9 @@ nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_ms(s->s_sndtimeo, buf, szp)); + return (nni_copyout_ms(s->s_sndtimeo, buf, szp, typ)); } static int @@ -193,9 +197,9 @@ nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_ms(s->s_reconn, buf, szp)); + return (nni_copyout_ms(s->s_reconn, buf, szp, typ)); } static int @@ -205,9 +209,9 @@ nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_ms(s->s_reconnmax, buf, szp)); + return (nni_copyout_ms(s->s_reconnmax, buf, szp, typ)); } static int @@ -217,9 +221,11 @@ nni_sock_setopt_recvbuf(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_recvbuf(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_recvbuf(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_buf(s->s_urq, buf, szp)); + int len = nni_msgq_cap(s->s_urq); + + return (nni_copyout_int(len, buf, szp, typ)); } static int @@ -229,15 +235,17 @@ nni_sock_setopt_sendbuf(nni_sock *s, const void *buf, size_t sz) } static int -nni_sock_getopt_sendbuf(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_sendbuf(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_buf(s->s_uwq, buf, szp)); + int len = nni_msgq_cap(s->s_uwq); + + return (nni_copyout_int(len, buf, szp, typ)); } static int -nni_sock_getopt_sockname(nni_sock *s, void *buf, size_t *szp) +nni_sock_getopt_sockname(nni_sock *s, void *buf, size_t *szp, int typ) { - return (nni_getopt_str(s->s_name, buf, szp)); + return (nni_copyout_str(s->s_name, buf, szp, typ)); } static int @@ -1051,12 +1059,7 @@ nni_sock_getopt(nni_sock *s, const char *name, void *val, size_t *szp, int t) nni_mtx_unlock(&s->s_mx); return (NNG_EWRITEONLY); } - 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_getopt(s->s_data, val, szp); + rv = pso->pso_getopt(s->s_data, val, szp, t); nni_mtx_unlock(&s->s_mx); return (rv); } @@ -1070,12 +1073,7 @@ nni_sock_getopt(nni_sock *s, const char *name, void *val, size_t *szp, int t) nni_mtx_unlock(&s->s_mx); return (NNG_EWRITEONLY); } - 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_getopt(s, val, szp); + rv = sso->so_getopt(s, val, szp, t); nni_mtx_unlock(&s->s_mx); return (rv); } diff --git a/src/core/transport.h b/src/core/transport.h index ebc14e83..7085126f 100644 --- a/src/core/transport.h +++ b/src/core/transport.h @@ -58,7 +58,7 @@ struct nni_tran_ep_option { int eo_type; // eo_getopt retrieves the value of the option. - int (*eo_getopt)(void *, void *, size_t *); + int (*eo_getopt)(void *, void *, size_t *, int); // eo_set sets the value of the option. If the first argument // (the endpoint) is NULL, then no actual set operation should be @@ -121,7 +121,7 @@ struct nni_tran_pipe_option { int po_type; // po_getopt retrieves the value of the option. - int (*po_getopt)(void *, void *, size_t *); + int (*po_getopt)(void *, void *, size_t *, int); }; // Pipe operations are entry points called by the socket. These may be called @@ -64,6 +64,18 @@ nng_free(void *buf, size_t sz) nni_free(buf, sz); } +char * +nng_strdup(const char *src) +{ + return (nni_strdup(src)); +} + +void +nng_strfree(char *s) +{ + nni_strfree(s); +} + int nng_recv(nng_socket sid, void *buf, size_t *szp, int flags) { @@ -482,6 +494,13 @@ nng_dialer_getopt_ptr(nng_dialer id, const char *name, void **vp) } int +nng_dialer_getopt_string(nng_dialer id, const char *name, char **vp) +{ + size_t sz = sizeof(*vp); + return (nng_dialer_getx(id, name, vp, &sz, NNI_TYPE_STRING)); +} + +int nng_dialer_getopt_ms(nng_dialer id, const char *name, nng_duration *vp) { size_t sz = sizeof(*vp); @@ -601,6 +620,13 @@ nng_listener_getopt_ptr(nng_listener id, const char *name, void **vp) } int +nng_listener_getopt_string(nng_listener id, const char *name, char **vp) +{ + size_t sz = sizeof(*vp); + return (nng_listener_getx(id, name, vp, &sz, NNI_TYPE_STRING)); +} + +int nng_listener_getopt_ms(nng_listener id, const char *name, nng_duration *vp) { size_t sz = sizeof(*vp); @@ -769,6 +795,13 @@ nng_getopt_ptr(nng_socket sid, const char *name, void **valp) } int +nng_getopt_string(nng_socket sid, const char *name, char **valp) +{ + size_t sz = sizeof(*valp); + return (nng_getx(sid, name, valp, &sz, NNI_TYPE_STRING)); +} + +int nng_device(nng_socket s1, nng_socket s2) { int rv; @@ -938,6 +971,13 @@ nng_pipe_getopt_sockaddr(nng_pipe id, const char *name, nng_sockaddr *sap) } int +nng_pipe_getopt_string(nng_pipe id, const char *name, char **valp) +{ + size_t sz = sizeof(*valp); + return (nng_pipe_getopt_x(id, name, valp, &sz, NNI_TYPE_STRING)); +} + +int nng_pipe_close(nng_pipe id) { int rv; @@ -178,6 +178,11 @@ NNG_DECL int nng_getopt_size(nng_socket, const char *, size_t *); NNG_DECL int nng_getopt_uint64(nng_socket, const char *, uint64_t *); NNG_DECL int nng_getopt_ptr(nng_socket, const char *, void **); +// nng_getopt_string is special -- it allocates a string to hold the +// resulting string, which should be freed with nng_strfree when it is +// no logner needed. +NNG_DECL int nng_getopt_string(nng_socket, const char *, char **); + // nng_listen creates a listening endpoint with no special options, // and starts it listening. It is functionally equivalent to the legacy // nn_bind(). The underlying endpoint is returned back to the caller in the @@ -242,6 +247,11 @@ NNG_DECL int nng_dialer_getopt_sockaddr( NNG_DECL int nng_dialer_getopt_uint64(nng_dialer, const char *, uint64_t *); NNG_DECL int nng_dialer_getopt_ptr(nng_dialer, const char *, void **); +// nng_dialer_getopt_string is special -- it allocates a string to hold the +// resulting string, which should be freed with nng_strfree when it is +// no logner needed. +NNG_DECL int nng_dialer_getopt_string(nng_dialer, const char *, char **); + // nng_listener_setopt sets an option for a dialer. This value is // not stored in the socket. Subsequent setopts on the socket may // override these value however. Note listener options may not be altered @@ -272,6 +282,11 @@ NNG_DECL int nng_listener_getopt_uint64( nng_listener, const char *, uint64_t *); NNG_DECL int nng_listener_getopt_ptr(nng_listener, const char *, void **); +// nng_listener_getopt_string is special -- it allocates a string to hold the +// resulting string, which should be freed with nng_strfree when it is +// no logner needed. +NNG_DECL int nng_listener_getopt_string(nng_listener, const char *, char **); + // nng_strerror returns a human readable string associated with the error // code supplied. NNG_DECL const char *nng_strerror(int); @@ -334,6 +349,13 @@ NNG_DECL void *nng_alloc(size_t); // calloc. NNG_DECL void nng_free(void *, size_t); +// nng_strdup duplicates the source string, using nng_alloc. The result +// should be freed with nng_strfree (or nng_free(strlen(s)+1)). +NNG_DECL char *nng_strdup(const char *); + +// nng_strfree is equivalent to nng_free(strlen(s)+1). +NNG_DECL void nng_strfree(char *); + // Async IO API. AIO structures can be thought of as "handles" to // support asynchronous operations. They contain the completion callback, and // a pointer to consumer data. This is similar to how overlapped I/O @@ -478,6 +500,7 @@ NNG_DECL int nng_pipe_getopt_size(nng_pipe, const char *, size_t *); NNG_DECL int nng_pipe_getopt_sockaddr(nng_pipe, const char *, nng_sockaddr *); NNG_DECL int nng_pipe_getopt_uint64(nng_pipe, const char *, uint64_t *); NNG_DECL int nng_pipe_getopt_ptr(nng_pipe, const char *, void **); +NNG_DECL int nng_pipe_getopt_string(nng_pipe, const char *, char **); NNG_DECL int nng_pipe_close(nng_pipe); // Flags. diff --git a/src/protocol/bus0/bus.c b/src/protocol/bus0/bus.c index 31b40fd5..00a9ca02 100644 --- a/src/protocol/bus0/bus.c +++ b/src/protocol/bus0/bus.c @@ -341,10 +341,10 @@ bus0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -bus0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +bus0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { bus0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/pair0/pair.c b/src/protocol/pair0/pair.c index 476eadd6..bc8be866 100644 --- a/src/protocol/pair0/pair.c +++ b/src/protocol/pair0/pair.c @@ -239,10 +239,10 @@ pair0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -pair0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +pair0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { pair0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/pair1/pair.c b/src/protocol/pair1/pair.c index 564118ae..ee67cf7b 100644 --- a/src/protocol/pair1/pair.c +++ b/src/protocol/pair1/pair.c @@ -408,10 +408,10 @@ pair1_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -pair1_sock_getopt_raw(void *arg, void *buf, size_t *szp) +pair1_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { pair1_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static int @@ -426,10 +426,10 @@ pair1_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) } static int -pair1_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +pair1_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ) { pair1_sock *s = arg; - return (nni_getopt_int(s->ttl, buf, szp)); + return (nni_copyout_int(s->ttl, buf, szp, typ)); } static int @@ -444,10 +444,10 @@ pair1_sock_setopt_poly(void *arg, const void *buf, size_t sz) } static int -pair1_sock_getopt_poly(void *arg, void *buf, size_t *szp) +pair1_sock_getopt_poly(void *arg, void *buf, size_t *szp, int typ) { pair1_sock *s = arg; - return (nni_getopt_bool(s->poly, buf, szp)); + return (nni_copyout_bool(s->poly, buf, szp, typ)); } static void diff --git a/src/protocol/pipeline0/pull.c b/src/protocol/pipeline0/pull.c index fec07c39..d8f4641f 100644 --- a/src/protocol/pipeline0/pull.c +++ b/src/protocol/pipeline0/pull.c @@ -188,10 +188,10 @@ pull0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -pull0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +pull0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { pull0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/pipeline0/push.c b/src/protocol/pipeline0/push.c index b6f6a824..bbffb433 100644 --- a/src/protocol/pipeline0/push.c +++ b/src/protocol/pipeline0/push.c @@ -205,10 +205,10 @@ push0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -push0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +push0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { push0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/pubsub0/pub.c b/src/protocol/pubsub0/pub.c index 36c27e7f..3f4ecea6 100644 --- a/src/protocol/pubsub0/pub.c +++ b/src/protocol/pubsub0/pub.c @@ -281,10 +281,10 @@ pub0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -pub0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +pub0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { pub0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/pubsub0/sub.c b/src/protocol/pubsub0/sub.c index cb7b0509..d7e9aea4 100644 --- a/src/protocol/pubsub0/sub.c +++ b/src/protocol/pubsub0/sub.c @@ -283,10 +283,10 @@ sub0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -sub0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +sub0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { sub0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static void diff --git a/src/protocol/reqrep0/rep.c b/src/protocol/reqrep0/rep.c index 429d55e7..5ee6c1be 100644 --- a/src/protocol/reqrep0/rep.c +++ b/src/protocol/reqrep0/rep.c @@ -365,10 +365,10 @@ rep0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -rep0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +rep0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { rep0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static int @@ -379,10 +379,10 @@ rep0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) } static int -rep0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +rep0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ) { rep0_sock *s = arg; - return (nni_getopt_int(s->ttl, buf, szp)); + return (nni_copyout_int(s->ttl, buf, szp, typ)); } static nni_msg * diff --git a/src/protocol/reqrep0/req.c b/src/protocol/reqrep0/req.c index 63ae07a0..659578d4 100644 --- a/src/protocol/reqrep0/req.c +++ b/src/protocol/reqrep0/req.c @@ -257,10 +257,10 @@ req0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -req0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +req0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { req0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static int @@ -271,10 +271,10 @@ req0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) } static int -req0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +req0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ) { req0_sock *s = arg; - return (nni_getopt_int(s->ttl, buf, szp)); + return (nni_copyout_int(s->ttl, buf, szp, typ)); } static int @@ -285,10 +285,10 @@ req0_sock_setopt_resendtime(void *arg, const void *buf, size_t sz) } static int -req0_sock_getopt_resendtime(void *arg, void *buf, size_t *szp) +req0_sock_getopt_resendtime(void *arg, void *buf, size_t *szp, int typ) { req0_sock *s = arg; - return (nni_getopt_ms(s->retry, buf, szp)); + return (nni_copyout_ms(s->retry, buf, szp, typ)); } // Raw and cooked mode differ in the way they send messages out. diff --git a/src/protocol/survey0/respond.c b/src/protocol/survey0/respond.c index 42db2cb0..035e51b1 100644 --- a/src/protocol/survey0/respond.c +++ b/src/protocol/survey0/respond.c @@ -359,10 +359,10 @@ resp0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -resp0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +resp0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { resp0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static int @@ -373,10 +373,10 @@ resp0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) } static int -resp0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +resp0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ) { resp0_sock *s = arg; - return (nni_getopt_int(s->ttl, buf, szp)); + return (nni_copyout_int(s->ttl, buf, szp, typ)); } static void diff --git a/src/protocol/survey0/survey.c b/src/protocol/survey0/survey.c index 40ace91b..161a5002 100644 --- a/src/protocol/survey0/survey.c +++ b/src/protocol/survey0/survey.c @@ -290,10 +290,10 @@ surv0_sock_setopt_raw(void *arg, const void *buf, size_t sz) } static int -surv0_sock_getopt_raw(void *arg, void *buf, size_t *szp) +surv0_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) { surv0_sock *s = arg; - return (nni_getopt_bool(s->raw, buf, szp)); + return (nni_copyout_bool(s->raw, buf, szp, typ)); } static int @@ -304,10 +304,10 @@ surv0_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) } static int -surv0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +surv0_sock_getopt_maxttl(void *arg, void *buf, size_t *szp, int typ) { surv0_sock *s = arg; - return (nni_getopt_int(s->ttl, buf, szp)); + return (nni_copyout_int(s->ttl, buf, szp, typ)); } static int @@ -318,10 +318,10 @@ surv0_sock_setopt_surveytime(void *arg, const void *buf, size_t sz) } static int -surv0_sock_getopt_surveytime(void *arg, void *buf, size_t *szp) +surv0_sock_getopt_surveytime(void *arg, void *buf, size_t *szp, int typ) { surv0_sock *s = arg; - return (nni_getopt_ms(s->survtime, buf, szp)); + return (nni_copyout_ms(s->survtime, buf, szp, typ)); } static void diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c index 14457623..e7253f7b 100644 --- a/src/transport/inproc/inproc.c +++ b/src/transport/inproc/inproc.c @@ -177,7 +177,7 @@ nni_inproc_pipe_peer(void *arg) } static int -nni_inproc_pipe_get_addr(void *arg, void *buf, size_t *szp) +nni_inproc_pipe_get_addr(void *arg, void *buf, size_t *szp, int typ) { nni_inproc_pipe *p = arg; nni_sockaddr sa; @@ -185,7 +185,7 @@ nni_inproc_pipe_get_addr(void *arg, void *buf, size_t *szp) memset(&sa, 0, sizeof(sa)); sa.s_inproc.sa_family = NNG_AF_INPROC; nni_strlcpy(sa.s_inproc.sa_name, p->addr, sizeof(sa.s_inproc.sa_name)); - return (nni_getopt_sockaddr(&sa, buf, szp)); + return (nni_copyout_sockaddr(&sa, buf, szp, typ)); } static int diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c index 9f95c2a9..9d25ed72 100644 --- a/src/transport/ipc/ipc.c +++ b/src/transport/ipc/ipc.c @@ -490,10 +490,10 @@ nni_ipc_pipe_peer(void *arg) } static int -nni_ipc_pipe_get_addr(void *arg, void *buf, size_t *szp) +nni_ipc_pipe_get_addr(void *arg, void *buf, size_t *szp, int typ) { nni_ipc_pipe *p = arg; - return (nni_getopt_sockaddr(&p->sa, buf, szp)); + return (nni_copyout_sockaddr(&p->sa, buf, szp, typ)); } static void @@ -689,17 +689,17 @@ nni_ipc_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz) } static int -nni_ipc_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp) +nni_ipc_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp, int typ) { nni_ipc_ep *ep = arg; - return (nni_getopt_size(ep->rcvmax, data, szp)); + return (nni_copyout_size(ep->rcvmax, data, szp, typ)); } static int -nni_ipc_ep_get_addr(void *arg, void *data, size_t *szp) +nni_ipc_ep_get_addr(void *arg, void *data, size_t *szp, int typ) { nni_ipc_ep *ep = arg; - return (nni_getopt_sockaddr(&ep->sa, data, szp)); + return (nni_copyout_sockaddr(&ep->sa, data, szp, typ)); } static nni_tran_pipe_option nni_ipc_pipe_options[] = { diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index 5741beea..4837c8d3 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -442,7 +442,7 @@ nni_tcp_pipe_peer(void *arg) } static int -nni_tcp_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) +nni_tcp_pipe_getopt_locaddr(void *arg, void *v, size_t *szp, int typ) { nni_tcp_pipe *p = arg; int rv; @@ -450,13 +450,13 @@ nni_tcp_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_plat_tcp_pipe_sockname(p->tpp, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } static int -nni_tcp_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) +nni_tcp_pipe_getopt_remaddr(void *arg, void *v, size_t *szp, int typ) { nni_tcp_pipe *p = arg; int rv; @@ -464,7 +464,7 @@ nni_tcp_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_plat_tcp_pipe_peername(p->tpp, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } @@ -736,7 +736,7 @@ nni_tcp_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz) } static int -nni_tcp_ep_getopt_url(void *arg, void *v, size_t *szp) +nni_tcp_ep_getopt_url(void *arg, void *v, size_t *szp, int typ) { nni_tcp_ep *ep = arg; char ustr[128]; @@ -744,18 +744,18 @@ nni_tcp_ep_getopt_url(void *arg, void *v, size_t *szp) char portstr[6]; // max for 16-bit port if (ep->mode == NNI_EP_MODE_DIAL) { - return (nni_getopt_str(ep->url->u_rawurl, v, szp)); + return (nni_copyout_str(ep->url->u_rawurl, v, szp, typ)); } nni_plat_tcp_ntop(&ep->bsa, ipstr, portstr); snprintf(ustr, sizeof(ustr), "tcp://%s:%s", ipstr, portstr); - return (nni_getopt_str(ustr, v, szp)); + return (nni_copyout_str(ustr, v, szp, typ)); } static int -nni_tcp_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp) +nni_tcp_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp, int typ) { nni_tcp_ep *ep = arg; - return (nni_getopt_size(ep->rcvmax, v, szp)); + return (nni_copyout_size(ep->rcvmax, v, szp, typ)); } static int @@ -769,10 +769,10 @@ nni_tcp_ep_setopt_linger(void *arg, const void *v, size_t sz) } static int -nni_tcp_ep_getopt_linger(void *arg, void *v, size_t *szp) +nni_tcp_ep_getopt_linger(void *arg, void *v, size_t *szp, int typ) { nni_tcp_ep *ep = arg; - return (nni_getopt_ms(ep->linger, v, szp)); + return (nni_copyout_ms(ep->linger, v, szp, typ)); } static nni_tran_pipe_option nni_tcp_pipe_options[] = { diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c index 9299ba2d..84125c48 100644 --- a/src/transport/tls/tls.c +++ b/src/transport/tls/tls.c @@ -453,7 +453,7 @@ nni_tls_pipe_peer(void *arg) } static int -nni_tls_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) +nni_tls_pipe_getopt_locaddr(void *arg, void *v, size_t *szp, int typ) { nni_tls_pipe *p = arg; int rv; @@ -461,13 +461,13 @@ nni_tls_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_tls_sockname(p->tls, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } static int -nni_tls_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) +nni_tls_pipe_getopt_remaddr(void *arg, void *v, size_t *szp, int typ) { nni_tls_pipe *p = arg; int rv; @@ -475,7 +475,7 @@ nni_tls_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_tls_peername(p->tls, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } @@ -749,7 +749,7 @@ nni_tls_ep_connect(void *arg, nni_aio *aio) } static int -nni_tls_ep_getopt_url(void *arg, void *v, size_t *szp) +nni_tls_ep_getopt_url(void *arg, void *v, size_t *szp, int typ) { nni_tls_ep *ep = arg; char ustr[128]; @@ -757,11 +757,11 @@ nni_tls_ep_getopt_url(void *arg, void *v, size_t *szp) char portstr[6]; // max for 16-bit port if (ep->mode == NNI_EP_MODE_DIAL) { - return (nni_getopt_str(ep->url->u_rawurl, v, szp)); + return (nni_copyout_str(ep->url->u_rawurl, v, szp, typ)); } nni_plat_tcp_ntop(&ep->bsa, ipstr, portstr); snprintf(ustr, sizeof(ustr), "tls+tcp://%s:%s", ipstr, portstr); - return (nni_getopt_str(ustr, v, szp)); + return (nni_copyout_str(ustr, v, szp, typ)); } static int @@ -775,10 +775,10 @@ nni_tls_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz) } static int -nni_tls_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp) +nni_tls_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp, int typ) { nni_tls_ep *ep = arg; - return (nni_getopt_size(ep->rcvmax, v, szp)); + return (nni_copyout_size(ep->rcvmax, v, szp, typ)); } static int @@ -792,10 +792,10 @@ nni_tls_ep_setopt_linger(void *arg, const void *v, size_t sz) } static int -nni_tls_ep_getopt_linger(void *arg, void *v, size_t *szp) +nni_tls_ep_getopt_linger(void *arg, void *v, size_t *szp, int typ) { nni_tls_ep *ep = arg; - return (nni_getopt_ms(ep->linger, v, szp)); + return (nni_copyout_ms(ep->linger, v, szp, typ)); } static int @@ -824,10 +824,10 @@ tls_setopt_config(void *arg, const void *data, size_t sz) } static int -tls_getopt_config(void *arg, void *v, size_t *szp) +tls_getopt_config(void *arg, void *v, size_t *szp, int typ) { nni_tls_ep *ep = arg; - return (nni_getopt_ptr(ep->cfg, v, szp)); + return (nni_copyout_ptr(ep->cfg, v, szp, typ)); } static int @@ -888,11 +888,11 @@ tls_setopt_cert_key_file(void *arg, const void *v, size_t sz) } static int -tls_getopt_verified(void *arg, void *v, size_t *szp) +tls_getopt_verified(void *arg, void *v, size_t *szp, int typ) { nni_tls_pipe *p = arg; - return (nni_getopt_bool(nni_tls_verified(p->tls), v, szp)); + return (nni_copyout_bool(nni_tls_verified(p->tls), v, szp, typ)); } static nni_tran_pipe_option nni_tls_pipe_options[] = { diff --git a/src/transport/ws/websocket.c b/src/transport/ws/websocket.c index 1b862d73..1a83462a 100644 --- a/src/transport/ws/websocket.c +++ b/src/transport/ws/websocket.c @@ -470,14 +470,14 @@ ws_ep_setopt_reshdrs(void *arg, const void *v, size_t sz) } static int -ws_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp) +ws_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp, int typ) { ws_ep *ep = arg; - return (nni_getopt_size(ep->rcvmax, v, szp)); + return (nni_copyout_size(ep->rcvmax, v, szp, typ)); } static int -ws_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) +ws_pipe_getopt_locaddr(void *arg, void *v, size_t *szp, int typ) { ws_pipe * p = arg; int rv; @@ -485,13 +485,13 @@ ws_pipe_getopt_locaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_ws_sock_addr(p->ws, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } static int -ws_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) +ws_pipe_getopt_remaddr(void *arg, void *v, size_t *szp, int typ) { ws_pipe * p = arg; int rv; @@ -499,13 +499,13 @@ ws_pipe_getopt_remaddr(void *arg, void *v, size_t *szp) memset(&sa, 0, sizeof(sa)); if ((rv = nni_ws_peer_addr(p->ws, &sa)) == 0) { - rv = nni_getopt_sockaddr(&sa, v, szp); + rv = nni_copyout_sockaddr(&sa, v, szp, typ); } return (rv); } static int -ws_pipe_getopt_reshdrs(void *arg, void *v, size_t *szp) +ws_pipe_getopt_reshdrs(void *arg, void *v, size_t *szp, int typ) { ws_pipe * p = arg; const char *s; @@ -513,11 +513,11 @@ ws_pipe_getopt_reshdrs(void *arg, void *v, size_t *szp) if ((s = nni_ws_response_headers(p->ws)) == NULL) { return (NNG_ENOMEM); } - return (nni_getopt_str(s, v, szp)); + return (nni_copyout_str(s, v, szp, typ)); } static int -ws_pipe_getopt_reqhdrs(void *arg, void *v, size_t *szp) +ws_pipe_getopt_reqhdrs(void *arg, void *v, size_t *szp, int typ) { ws_pipe * p = arg; const char *s; @@ -525,14 +525,14 @@ ws_pipe_getopt_reqhdrs(void *arg, void *v, size_t *szp) if ((s = nni_ws_request_headers(p->ws)) == NULL) { return (NNG_ENOMEM); } - return (nni_getopt_str(s, v, szp)); + return (nni_copyout_str(s, v, szp, typ)); } static int -ws_pipe_getopt_tls_verified(void *arg, void *v, size_t *szp) +ws_pipe_getopt_tls_verified(void *arg, void *v, size_t *szp, int typ) { ws_pipe *p = arg; - return (nni_getopt_bool(nni_ws_tls_verified(p->ws), v, szp)); + return (nni_copyout_bool(nni_ws_tls_verified(p->ws), v, szp, typ)); } static nni_tran_pipe_option ws_pipe_options[] = { @@ -815,14 +815,14 @@ wss_get_tls(ws_ep *ep, nng_tls_config **tlsp) } static int -wss_ep_getopt_tlsconfig(void *arg, void *v, size_t *szp) +wss_ep_getopt_tlsconfig(void *arg, void *v, size_t *szp, int typ) { ws_ep * ep = arg; nng_tls_config *tls; int rv; if (((rv = wss_get_tls(ep, &tls)) != 0) || - ((rv = nni_getopt_ptr(tls, v, szp)) != 0)) { + ((rv = nni_copyout_ptr(tls, v, szp, typ)) != 0)) { return (rv); } return (0); diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index 98dbe2bd..da22dc54 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -1927,7 +1927,8 @@ zt_pipe_peer(void *arg) } static int -zt_getopt_network_status(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp) +zt_getopt_nw_status( + zt_node *ztn, uint64_t nwid, void *buf, size_t *szp, int typ) { ZT_VirtualNetworkConfig *vcfg; int status; @@ -1964,11 +1965,11 @@ zt_getopt_network_status(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp) ZT_Node_freeQueryResult(ztn->zn_znode, vcfg); nni_mtx_unlock(&zt_lk); - return (nni_getopt_int(status, buf, szp)); + return (nni_copyout_int(status, buf, szp, typ)); } static int -zt_getopt_network_name(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp) +zt_getopt_nw_name(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp, int typ) { ZT_VirtualNetworkConfig *vcfg; int rv; @@ -1979,7 +1980,8 @@ zt_getopt_network_name(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp) nni_mtx_unlock(&zt_lk); return (NNG_ECLOSED); } - rv = nni_getopt_str(vcfg->name, buf, szp); + + rv = nni_copyout_str(vcfg->name, buf, szp, typ); ZT_Node_freeQueryResult(ztn->zn_znode, vcfg); nni_mtx_unlock(&zt_lk); @@ -1987,24 +1989,24 @@ zt_getopt_network_name(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp) } static int -zt_pipe_get_recvmaxsz(void *arg, void *buf, size_t *szp) +zt_pipe_get_recvmaxsz(void *arg, void *buf, size_t *szp, int typ) { zt_pipe *p = arg; - return (nni_getopt_size(p->zp_rcvmax, buf, szp)); + return (nni_copyout_size(p->zp_rcvmax, buf, szp, typ)); } static int -zt_pipe_get_nwid(void *arg, void *buf, size_t *szp) +zt_pipe_get_nwid(void *arg, void *buf, size_t *szp, int typ) { zt_pipe *p = arg; - return (nni_getopt_u64(p->zp_nwid, buf, szp)); + return (nni_copyout_u64(p->zp_nwid, buf, szp, typ)); } static int -zt_pipe_get_node(void *arg, void *buf, size_t *szp) +zt_pipe_get_node(void *arg, void *buf, size_t *szp, int typ) { zt_pipe *p = arg; - return (nni_getopt_u64(p->zp_laddr >> 24, buf, szp)); + return (nni_copyout_u64(p->zp_laddr >> 24, buf, szp, typ)); } static void @@ -2545,10 +2547,10 @@ zt_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp) +zt_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_size(ep->ze_rcvmax, data, szp)); + return (nni_copyout_size(ep->ze_rcvmax, data, szp, typ)); } static int @@ -2579,14 +2581,14 @@ zt_ep_setopt_home(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_home(void *arg, void *data, size_t *szp) +zt_ep_getopt_home(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_str(ep->ze_home, data, szp)); + return (nni_copyout_str(ep->ze_home, data, szp, typ)); } static int -zt_ep_getopt_url(void *arg, void *data, size_t *szp) +zt_ep_getopt_url(void *arg, void *data, size_t *szp, int typ) { char ustr[64]; // more than plenty zt_ep * ep = arg; @@ -2597,7 +2599,7 @@ zt_ep_getopt_url(void *arg, void *data, size_t *szp) (unsigned long long) addr >> zt_port_shift, (unsigned long long) ep->ze_nwid, (unsigned) (addr & zt_port_mask)); - return (nni_getopt_str(ustr, data, szp)); + return (nni_copyout_str(ustr, data, szp, typ)); } static int @@ -2645,31 +2647,31 @@ zt_ep_setopt_deorbit(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_node(void *arg, void *data, size_t *szp) +zt_ep_getopt_node(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_u64(ep->ze_ztn->zn_self, data, szp)); + return (nni_copyout_u64(ep->ze_ztn->zn_self, data, szp, typ)); } static int -zt_ep_getopt_nwid(void *arg, void *data, size_t *szp) +zt_ep_getopt_nwid(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_u64(ep->ze_nwid, data, szp)); + return (nni_copyout_u64(ep->ze_nwid, data, szp, typ)); } static int -zt_ep_getopt_network_name(void *arg, void *buf, size_t *szp) +zt_ep_getopt_nw_name(void *arg, void *buf, size_t *szp, int typ) { zt_ep *ep = arg; - return (zt_getopt_network_name(ep->ze_ztn, ep->ze_nwid, buf, szp)); + return (zt_getopt_nw_name(ep->ze_ztn, ep->ze_nwid, buf, szp, typ)); } static int -zt_ep_getopt_network_status(void *arg, void *buf, size_t *szp) +zt_ep_getopt_nw_status(void *arg, void *buf, size_t *szp, int typ) { zt_ep *ep = arg; - return (zt_getopt_network_status(ep->ze_ztn, ep->ze_nwid, buf, szp)); + return (zt_getopt_nw_status(ep->ze_ztn, ep->ze_nwid, buf, szp, typ)); } static int @@ -2683,10 +2685,10 @@ zt_ep_setopt_ping_time(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_ping_time(void *arg, void *data, size_t *szp) +zt_ep_getopt_ping_time(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_ms(ep->ze_ping_time, data, szp)); + return (nni_copyout_ms(ep->ze_ping_time, data, szp, typ)); } static int @@ -2700,10 +2702,10 @@ zt_ep_setopt_ping_tries(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_ping_tries(void *arg, void *data, size_t *szp) +zt_ep_getopt_ping_tries(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_int(ep->ze_ping_tries, data, szp)); + return (nni_copyout_int(ep->ze_ping_tries, data, szp, typ)); } static int @@ -2717,10 +2719,10 @@ zt_ep_setopt_conn_time(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_conn_time(void *arg, void *data, size_t *szp) +zt_ep_getopt_conn_time(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_ms(ep->ze_conn_time, data, szp)); + return (nni_copyout_ms(ep->ze_conn_time, data, szp, typ)); } static int @@ -2734,14 +2736,14 @@ zt_ep_setopt_conn_tries(void *arg, const void *data, size_t sz) } static int -zt_ep_getopt_conn_tries(void *arg, void *data, size_t *szp) +zt_ep_getopt_conn_tries(void *arg, void *data, size_t *szp, int typ) { zt_ep *ep = arg; - return (nni_getopt_int(ep->ze_conn_tries, data, szp)); + return (nni_copyout_int(ep->ze_conn_tries, data, szp, typ)); } static int -zt_pipe_getopt_locaddr(void *arg, void *data, size_t *szp) +zt_pipe_getopt_locaddr(void *arg, void *data, size_t *szp, int typ) { zt_pipe * p = arg; nng_sockaddr sa; @@ -2751,11 +2753,11 @@ zt_pipe_getopt_locaddr(void *arg, void *data, size_t *szp) sa.s_zt.sa_nwid = p->zp_nwid; sa.s_zt.sa_nodeid = p->zp_laddr >> zt_port_shift; sa.s_zt.sa_port = p->zp_laddr & zt_port_mask; - return (nni_getopt_sockaddr(&sa, data, szp)); + return (nni_copyout_sockaddr(&sa, data, szp, typ)); } static int -zt_pipe_getopt_remaddr(void *arg, void *data, size_t *szp) +zt_pipe_getopt_remaddr(void *arg, void *data, size_t *szp, int typ) { zt_pipe * p = arg; nng_sockaddr sa; @@ -2765,14 +2767,14 @@ zt_pipe_getopt_remaddr(void *arg, void *data, size_t *szp) sa.s_zt.sa_nwid = p->zp_nwid; sa.s_zt.sa_nodeid = p->zp_raddr >> zt_port_shift; sa.s_zt.sa_port = p->zp_raddr & zt_port_mask; - return (nni_getopt_sockaddr(&sa, data, szp)); + return (nni_copyout_sockaddr(&sa, data, szp, typ)); } static int -zt_pipe_getopt_mtu(void *arg, void *data, size_t *szp) +zt_pipe_getopt_mtu(void *arg, void *data, size_t *szp, int typ) { zt_pipe *p = arg; - return (nni_getopt_size(p->zp_mtu, data, szp)); + return (nni_copyout_size(p->zp_mtu, data, szp, typ)); } static nni_tran_pipe_option zt_pipe_options[] = { @@ -2856,13 +2858,13 @@ static nni_tran_ep_option zt_ep_options[] = { { .eo_name = NNG_OPT_ZT_NETWORK_STATUS, .eo_type = NNI_TYPE_INT32, // enumeration really - .eo_getopt = zt_ep_getopt_network_status, + .eo_getopt = zt_ep_getopt_nw_status, .eo_setopt = NULL, }, { .eo_name = NNG_OPT_ZT_NETWORK_NAME, .eo_type = NNI_TYPE_STRING, - .eo_getopt = zt_ep_getopt_network_name, + .eo_getopt = zt_ep_getopt_nw_name, .eo_setopt = NULL, }, { diff --git a/tests/pollfd.c b/tests/pollfd.c index aa07adab..0baa6b2d 100644 --- a/tests/pollfd.c +++ b/tests/pollfd.c @@ -116,20 +116,16 @@ TestMain("Poll FDs", { Convey("We cannot get a send FD for PULL", { nng_socket s3; int fd; - size_t sz; So(nng_pull0_open(&s3) == 0); Reset({ nng_close(s3); }); - sz = sizeof(fd); - So(nng_getopt(s3, NNG_OPT_SENDFD, &fd, &sz) == NNG_ENOTSUP); + So(nng_getopt_int(s3, NNG_OPT_SENDFD, &fd) == NNG_ENOTSUP); }); Convey("We cannot get a recv FD for PUSH", { nng_socket s3; int fd; - size_t sz; So(nng_push0_open(&s3) == 0); Reset({ nng_close(s3); }); - sz = sizeof(fd); - So(nng_getopt(s3, NNG_OPT_RECVFD, &fd, &sz) == NNG_ENOTSUP); + So(nng_getopt_int(s3, NNG_OPT_RECVFD, &fd) == NNG_ENOTSUP); }); }) diff --git a/tests/sock.c b/tests/sock.c index 75d7c140..30dcd1b5 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -198,18 +198,18 @@ TestMain("Socket Operations", { Convey("Short size is not copied", { size_t sz = 0; to = 0; - So(nng_getopt( - s1, NNG_OPT_SENDTIMEO, &to, &sz) == 0); + So(nng_getopt(s1, NNG_OPT_SENDTIMEO, &to, + &sz) == NNG_EINVAL); So(sz == sizeof(to)); So(to == 0); sz = 0; - So(nng_getopt( - s1, NNG_OPT_RECONNMINT, &to, &sz) == 0); + So(nng_getopt(s1, NNG_OPT_RECONNMINT, &to, + &sz) == NNG_EINVAL); So(to == 0); sz = 0; - So(nng_getopt( - s1, NNG_OPT_RECONNMAXT, &to, &sz) == 0); + So(nng_getopt(s1, NNG_OPT_RECONNMAXT, &to, + &sz) == NNG_EINVAL); So(to == 0); }); @@ -225,7 +225,7 @@ TestMain("Socket Operations", { int l = 5; size_t sz = 0; So(nng_getopt(s1, NNG_OPT_RECVBUF, &l, &sz) == - 0); + NNG_EINVAL); So(sz == sizeof(l)); So(l == 5); }); diff --git a/tests/tcp.c b/tests/tcp.c index 10d777da..58b4f15c 100644 --- a/tests/tcp.c +++ b/tests/tcp.c @@ -30,14 +30,13 @@ check_props_v4(nng_msg *msg) p = nng_msg_get_pipe(msg); So(p > 0); - z = sizeof(nng_sockaddr); - So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); - So(z == sizeof(la)); + So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); So(la.s_family == NNG_AF_INET); So(la.s_in.sa_port == htons(trantest_port - 1)); So(la.s_in.sa_port != 0); So(la.s_in.sa_addr == htonl(0x7f000001)); + // untyped z = sizeof(nng_sockaddr); So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == 0); So(z == sizeof(ra)); @@ -45,6 +44,10 @@ check_props_v4(nng_msg *msg) So(ra.s_in.sa_port != 0); So(ra.s_in.sa_addr == htonl(0x7f000001)); + So(nng_pipe_getopt_size(p, NNG_OPT_REMADDR, &z) == NNG_EBADTYPE); + z = 1; + So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == NNG_EINVAL); + return (0); } @@ -84,8 +87,7 @@ TestMain("TCP Transport", { nng_socket s1; nng_socket s2; nng_listener l; - char addr[NNG_MAXADDRLEN]; - size_t sz; + char * addr; So(nng_pair_open(&s1) == 0); So(nng_pair_open(&s2) == 0); @@ -94,9 +96,10 @@ TestMain("TCP Transport", { nng_close(s1); }); So(nng_listen(s1, "tcp://127.0.0.1:0", &l, 0) == 0); - sz = NNG_MAXADDRLEN; - So(nng_listener_getopt(l, NNG_OPT_URL, addr, &sz) == 0); + So(nng_listener_getopt_string(l, NNG_OPT_URL, &addr) == 0); + So(memcmp(addr, "tcp://", 6) == 0); So(nng_dial(s2, addr, NULL, 0) == 0); + nng_strfree(addr); }); Convey("Malformed TCP addresses do not panic", { diff --git a/tests/tls.c b/tests/tls.c index a441b453..087234ff 100644 --- a/tests/tls.c +++ b/tests/tls.c @@ -134,6 +134,8 @@ check_props_v4(nng_msg *msg) int i; So(nng_pipe_getopt_int(p, NNG_OPT_REMADDR, &i) == NNG_EBADTYPE); + z = 1; + So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == NNG_EINVAL); return (0); } @@ -303,7 +305,7 @@ TestMain("TLS Transport", { nng_socket s1; nng_socket s2; nng_listener l; - char addr[NNG_MAXADDRLEN]; + char * addr; size_t sz; So(nng_tls_register() == 0); @@ -315,8 +317,9 @@ TestMain("TLS Transport", { }); So(nng_listen(s1, "tls+tcp://127.0.0.1:0", &l, 0) == 0); sz = NNG_MAXADDRLEN; - So(nng_listener_getopt(l, NNG_OPT_URL, addr, &sz) == 0); + So(nng_listener_getopt_string(l, NNG_OPT_URL, &addr) == 0); So(nng_dial(s2, addr, NULL, 0) == 0); + nng_strfree(addr); }); Convey("Malformed TLS addresses do not panic", { diff --git a/tests/trantest.h b/tests/trantest.h index feb34be8..cbed58f3 100644 --- a/tests/trantest.h +++ b/tests/trantest.h @@ -281,8 +281,7 @@ trantest_send_recv(trantest *tt) nng_msg * recv; size_t len; nng_pipe p; - char url[NNG_MAXADDRLEN]; - size_t sz; + char * url; So(trantest_listen(tt, &l) == 0); So(l != 0); @@ -314,9 +313,9 @@ trantest_send_recv(trantest *tt) So(strcmp(nng_msg_body(recv), "acknowledge") == 0); p = nng_msg_get_pipe(recv); So(p != 0); - sz = sizeof(url); - So(nng_pipe_getopt(p, NNG_OPT_URL, url, &sz) == 0); + So(nng_pipe_getopt_string(p, NNG_OPT_URL, &url) == 0); So(strcmp(url, tt->addr) == 0); + nng_strfree(url); nng_msg_free(recv); }); } @@ -330,8 +329,7 @@ trantest_send_recv_multi(trantest *tt) nng_msg * send; nng_msg * recv; nng_pipe p; - char url[NNG_MAXADDRLEN]; - size_t sz; + char * url; int i; char msgbuf[16]; @@ -369,9 +367,9 @@ trantest_send_recv_multi(trantest *tt) So(strcmp(nng_msg_body(recv), msgbuf) == 0); p = nng_msg_get_pipe(recv); So(p != 0); - sz = sizeof(url); - So(nng_pipe_getopt(p, NNG_OPT_URL, url, &sz) == 0); + So(nng_pipe_getopt_string(p, NNG_OPT_URL, &url) == 0); So(strcmp(url, tt->addr) == 0); + nng_strfree(url); nng_msg_free(recv); } }); @@ -34,9 +34,7 @@ check_props_v4(nng_msg *msg) p = nng_msg_get_pipe(msg); So(p > 0); - z = sizeof(nng_sockaddr); - So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); - So(z == sizeof(la)); + So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); So(la.s_family == NNG_AF_INET); So(la.s_in.sa_port == htons(trantest_port - 1)); So(la.s_in.sa_port != 0); @@ -52,26 +50,34 @@ check_props_v4(nng_msg *msg) // Request Header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Key") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); + So(nng_pipe_getopt_string(p, NNG_OPT_WS_REQUEST_HEADERS, &buf) == 0); + So(strlen(buf) == len - 1); + nng_strfree(buf); // Response Header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Accept") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); + So(nng_pipe_getopt_string(p, NNG_OPT_WS_RESPONSE_HEADERS, &buf) == 0); + So(strlen(buf) == len - 1); + nng_strfree(buf); return (0); } diff --git a/tests/wss.c b/tests/wss.c index 58df8cbf..89a26623 100644 --- a/tests/wss.c +++ b/tests/wss.c @@ -156,26 +156,28 @@ check_props(nng_msg *msg) // Request header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Key") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); // Response header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Accept") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); return (0); } diff --git a/tests/wssfile.c b/tests/wssfile.c index 48736117..41fb8f9f 100644 --- a/tests/wssfile.c +++ b/tests/wssfile.c @@ -162,26 +162,34 @@ check_props(nng_msg *msg) // Request header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_REQUEST_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Key") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); + So(nng_pipe_getopt_string(p, NNG_OPT_WS_REQUEST_HEADERS, &buf) == 0); + So(strlen(buf) == len - 1); + nng_strfree(buf); // Response header z = 0; buf = NULL; - So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); + So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == + NNG_EINVAL); So(z > 0); len = z; - So((buf = nni_alloc(len)) != NULL); + So((buf = nng_alloc(len)) != NULL); So(nng_pipe_getopt(p, NNG_OPT_WS_RESPONSE_HEADERS, buf, &z) == 0); So(strstr(buf, "Sec-WebSocket-Accept") != NULL); So(z == len); - nni_free(buf, len); + nng_free(buf, len); + So(nng_pipe_getopt_string(p, NNG_OPT_WS_RESPONSE_HEADERS, &buf) == 0); + So(strlen(buf) == len - 1); + nng_strfree(buf); return (0); } @@ -42,17 +42,15 @@ mkdir(const char *path, int mode) static int check_props(nng_msg *msg) { - nng_sockaddr la, ra; - nng_pipe p; - size_t z; + nng_pipe p; p = nng_msg_get_pipe(msg); So(p > 0); // Check local address. Convey("Local address property works", { - z = sizeof(nng_sockaddr); - So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); - So(z == sizeof(la)); + nng_sockaddr la; + So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); + So(la.s_family == NNG_AF_ZT); So(la.s_zt.sa_port == (trantest_port - 1)); So(la.s_zt.sa_nwid == 0xa09acf02337b057bull); @@ -61,76 +59,65 @@ check_props(nng_msg *msg) Convey("Remote address property works", { // Check remote address. - uint64_t mynode; + uint64_t mynode; + nng_sockaddr ra; - z = sizeof(nng_sockaddr); - So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == 0); - So(z == sizeof(ra)); + So(nng_pipe_getopt_sockaddr(p, NNG_OPT_REMADDR, &ra) == 0); So(ra.s_family == NNG_AF_ZT); So(ra.s_zt.sa_port != 0); So(ra.s_zt.sa_nwid == 0xa09acf02337b057bull); - z = sizeof(mynode); - So(nng_pipe_getopt(p, NNG_OPT_ZT_NODE, &mynode, &z) == 0); + So(nng_pipe_getopt_uint64(p, NNG_OPT_ZT_NODE, &mynode) == 0); So(mynode != 0); So(ra.s_zt.sa_nodeid == mynode); }); Convey("NWID property works", { - uint64_t nwid; + uint64_t nwid = 0; - z = sizeof(nwid); - nwid = 0; - So(nng_pipe_getopt(p, NNG_OPT_ZT_NWID, &nwid, &z) == 0); + So(nng_pipe_getopt_uint64(p, NNG_OPT_ZT_NWID, &nwid) == 0); So(nwid = 0xa09acf02337b057bull); }); Convey("Network status property works", { - int s; - z = sizeof(s); - s = 0; - So(nng_pipe_getopt(p, NNG_OPT_ZT_NETWORK_STATUS, &s, &z) == 0); + int s = 0; + + So(nng_pipe_getopt_int(p, NNG_OPT_ZT_NETWORK_STATUS, &s) == 0); So(s == NNG_ZT_STATUS_UP); }); Convey("Ping properties work", { - int c; - nng_duration t; + int c = 0; + nng_duration t = 0; - z = sizeof(c); - c = 0; - So(nng_pipe_getopt(p, NNG_OPT_ZT_PING_TRIES, &c, &z) == 0); + So(nng_pipe_getopt_int(p, NNG_OPT_ZT_PING_TRIES, &c) == 0); So(c > 0 && c < 10); // actually 5... - t = 0; So(nng_pipe_getopt_ms(p, NNG_OPT_ZT_PING_TIME, &t) == 0); So(t > 1000 && t < 3600000); // 1 sec - 1 hour }); Convey("Home property works", { - char v[256]; - z = sizeof(v); - So(nng_pipe_getopt(p, NNG_OPT_ZT_HOME, v, &z) == 0); - So(strlen(v) < sizeof(v)); + char *v; + So(nng_pipe_getopt_string(p, NNG_OPT_ZT_HOME, &v) == 0); + nng_strfree(v); }); Convey("MTU property works", { size_t mtu; // Check MTU - z = sizeof(mtu); - So(nng_pipe_getopt(p, NNG_OPT_ZT_MTU, &mtu, &z) == 0); + So(nng_pipe_getopt_size(p, NNG_OPT_ZT_MTU, &mtu) == 0); So(mtu >= 1000 && mtu <= 10000); }); Convey("Network name property works", { - char name[NNG_MAXADDRLEN]; - size_t namesz; + char *name; - namesz = sizeof(name); - So(nng_pipe_getopt( - p, NNG_OPT_ZT_NETWORK_NAME, name, &namesz) == 0); + So(nng_pipe_getopt_string(p, NNG_OPT_ZT_NETWORK_NAME, &name) == + 0); So(strcmp(name, "nng_test_open") == 0); + nng_strfree(name); }); return (0); |
