aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2025-10-07 20:03:32 -0700
committerGarrett D'Amore <garrett@damore.org>2025-10-07 20:03:32 -0700
commit3971d119c129bf5685f9fd14d0f1f785581c3565 (patch)
tree12b08c053d07fab5af699229ef52e311b8182d56 /src
parent07191755f3a38cbac48d15523095136f69d8f772 (diff)
downloadnng-3971d119c129bf5685f9fd14d0f1f785581c3565.tar.gz
nng-3971d119c129bf5685f9fd14d0f1f785581c3565.tar.bz2
nng-3971d119c129bf5685f9fd14d0f1f785581c3565.zip
options: string options are passed by reference
This avoids needless allocations, and we offer for pipes (which need this because they might be ephemeral) the get_strdup, get_strcpy, and get_strlen forms. (Those do the copying or allocations while holding the pipe reference.)
Diffstat (limited to 'src')
-rw-r--r--src/core/options.c12
-rw-r--r--src/core/stream.c8
-rw-r--r--src/nng.c66
-rw-r--r--src/supplemental/websocket/websocket_test.c10
4 files changed, 75 insertions, 21 deletions
diff --git a/src/core/options.c b/src/core/options.c
index 23e63d1c..f3843c0d 100644
--- a/src/core/options.c
+++ b/src/core/options.c
@@ -1,5 +1,5 @@
//
-// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2025 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
@@ -9,9 +9,9 @@
// found online at https://opensource.org/licenses/MIT.
//
-#include "core/defs.h"
-#include "core/nng_impl.h"
+#include "defs.h"
#include "nng/nng.h"
+#include "nng_impl.h"
#include <stdio.h>
#include <string.h>
@@ -174,16 +174,12 @@ nni_copyout_sockaddr(
nng_err
nni_copyout_str(const char *str, void *dst, size_t *szp, nni_type t)
{
- char *s;
NNI_ARG_UNUSED(szp);
if (t != NNI_TYPE_STRING) {
return (NNG_EBADTYPE);
}
- if ((s = nni_strdup(str)) == NULL) {
- return (NNG_ENOMEM);
- }
- *(char **) dst = s;
+ *(const char **) dst = str;
return (NNG_OK);
}
diff --git a/src/core/stream.c b/src/core/stream.c
index 61a8a3ba..572f28d1 100644
--- a/src/core/stream.c
+++ b/src/core/stream.c
@@ -368,7 +368,7 @@ nng_stream_get_size(nng_stream *s, const char *n, size_t *v)
}
nng_err
-nng_stream_get_string(nng_stream *s, const char *n, char **v)
+nng_stream_get_string(nng_stream *s, const char *n, const char **v)
{
return (nni_stream_get(s, n, v, NULL, NNI_TYPE_STRING));
}
@@ -413,7 +413,8 @@ nng_stream_dialer_get_size(nng_stream_dialer *d, const char *n, size_t *v)
}
nng_err
-nng_stream_dialer_get_string(nng_stream_dialer *d, const char *n, char **v)
+nng_stream_dialer_get_string(
+ nng_stream_dialer *d, const char *n, const char **v)
{
return (nni_stream_dialer_get(d, n, v, NULL, NNI_TYPE_STRING));
}
@@ -456,7 +457,8 @@ nng_stream_listener_get_size(nng_stream_listener *l, const char *n, size_t *v)
}
nng_err
-nng_stream_listener_get_string(nng_stream_listener *l, const char *n, char **v)
+nng_stream_listener_get_string(
+ nng_stream_listener *l, const char *n, const char **v)
{
return (nni_stream_listener_get(l, n, v, NULL, NNI_TYPE_STRING));
}
diff --git a/src/nng.c b/src/nng.c
index 39da87d4..423ddb64 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -780,7 +780,7 @@ nng_dialer_get_size(nng_dialer id, const char *n, size_t *v)
}
int
-nng_dialer_get_string(nng_dialer id, const char *n, char **v)
+nng_dialer_get_string(nng_dialer id, const char *n, const char **v)
{
return (dialer_get(id, n, v, NULL, NNI_TYPE_STRING));
}
@@ -909,7 +909,7 @@ nng_listener_get_size(nng_listener id, const char *n, size_t *v)
}
int
-nng_listener_get_string(nng_listener id, const char *n, char **v)
+nng_listener_get_string(nng_listener id, const char *n, const char **v)
{
return (listener_get(id, n, v, NULL, NNI_TYPE_STRING));
}
@@ -1358,12 +1358,72 @@ nng_pipe_get_size(nng_pipe id, const char *n, size_t *v)
}
nng_err
-nng_pipe_get_string(nng_pipe id, const char *n, char **v)
+nng_pipe_get_string(nng_pipe id, const char *n, const char **v)
{
return (pipe_get(id, n, v, NULL, NNI_TYPE_STRING));
}
nng_err
+nng_pipe_get_strcpy(nng_pipe p, const char *n, char *buf, size_t len)
+{
+ nng_err rv;
+ nni_pipe *pipe;
+ const char *s;
+
+ if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
+ return (rv);
+ }
+ rv = nni_pipe_getopt(pipe, n, &s, NULL, NNI_TYPE_STRING);
+ if (rv == NNG_OK) {
+ if (nni_strlcpy(buf, s != NULL ? s : "", len) >= len) {
+ rv = NNG_ENOSPC;
+ }
+ }
+ nni_pipe_rele(pipe);
+ return (rv);
+}
+
+nng_err
+nng_pipe_get_strdup(nng_pipe p, const char *n, char **v)
+{
+ nng_err rv;
+ nni_pipe *pipe;
+ const char *s;
+
+ if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
+ return (rv);
+ }
+ rv = nni_pipe_getopt(pipe, n, &s, NULL, NNI_TYPE_STRING);
+ if (rv == NNG_OK) {
+ if (s == NULL) {
+ *v = NULL;
+ } else if ((*v = nni_strdup(s)) == NULL) {
+ rv = NNG_ENOMEM;
+ }
+ }
+ nni_pipe_rele(pipe);
+ return (rv);
+}
+
+nng_err
+nng_pipe_get_strlen(nng_pipe p, const char *n, size_t *len)
+{
+ nng_err rv;
+ nni_pipe *pipe;
+ const char *s;
+
+ if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
+ return (rv);
+ }
+ rv = nni_pipe_getopt(pipe, n, &s, NULL, NNI_TYPE_STRING);
+ if (rv == NNG_OK) {
+ *len = s == NULL ? 0 : strlen(s);
+ }
+ nni_pipe_rele(pipe);
+ return (rv);
+}
+
+nng_err
nng_pipe_get_ms(nng_pipe id, const char *n, nng_duration *v)
{
return (pipe_get(id, n, v, NULL, NNI_TYPE_DURATION));
diff --git a/src/supplemental/websocket/websocket_test.c b/src/supplemental/websocket/websocket_test.c
index 98bcf658..cb502ef4 100644
--- a/src/supplemental/websocket/websocket_test.c
+++ b/src/supplemental/websocket/websocket_test.c
@@ -13,7 +13,7 @@
#include "sha1.h"
-#include <nuts.h>
+#include "../../testing/nuts.h"
void
test_websocket_wildcard(void)
@@ -138,7 +138,7 @@ test_websocket_conn_props(void)
nng_stream *c2 = NULL;
char uri[64];
bool on;
- char *str;
+ const char *str;
uint16_t port;
int rv;
@@ -208,18 +208,14 @@ test_websocket_conn_props(void)
NUTS_PASS(
nng_stream_get_string(c1, NNG_OPT_WS_HEADER "NNG-Req", &str));
NUTS_MATCH(str, "True");
- nng_strfree(str);
NUTS_PASS(
nng_stream_get_string(c2, NNG_OPT_WS_HEADER "NNG-Rep", &str));
NUTS_MATCH(str, "True");
- nng_strfree(str);
NUTS_PASS(nng_stream_get_string(
c1, NNG_OPT_WS_HEADER "Sec-WebSocket-Version", &str));
- NUTS_TRUE(str != NULL);
- NUTS_TRUE(strcmp(str, "13") == 0);
- nng_strfree(str);
+ NUTS_MATCH(str, "13");
nng_stream_close(c1);
nng_stream_close(c2);