diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-17 18:23:17 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-17 22:05:20 -0800 |
| commit | 85aff44e00e836eda618d4f1cf013bce38b3fd44 (patch) | |
| tree | 94b2dca800d6d254baae17932a017e031c17ce67 /src/core/tcp.c | |
| parent | ef82d4792bf59b1fe8053d9bb5ac924b443d8a78 (diff) | |
| download | nng-85aff44e00e836eda618d4f1cf013bce38b3fd44.tar.gz nng-85aff44e00e836eda618d4f1cf013bce38b3fd44.tar.bz2 nng-85aff44e00e836eda618d4f1cf013bce38b3fd44.zip | |
URL u_port should be a number not a string.
The idea here is to reduce the dynamic allocations used for
URLs, and also the back and forth with parsing begin strings
and port numbers. We always resolve to a port number, and
this is easier for everyone.
The real goal in the long term is to eliminate dynamic allocation
of the URL fields altogether, but that requires a little more
work. This is a step in the right direction.
Diffstat (limited to 'src/core/tcp.c')
| -rw-r--r-- | src/core/tcp.c | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/src/core/tcp.c b/src/core/tcp.c index 7fb67228..5d324a13 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2019 Devolutions <info@devolutions.net> // @@ -10,6 +10,7 @@ // #include <stdint.h> +#include <stdio.h> #include <string.h> #include <nng/nng.h> @@ -20,8 +21,8 @@ typedef struct { nng_stream_dialer ops; - char *host; - char *port; + char host[256]; + uint16_t port; int af; // address family bool closed; nng_sockaddr sa; @@ -155,8 +156,6 @@ tcp_dialer_free(void *arg) nni_tcp_dialer_fini(d->d); } nni_mtx_fini(&d->mtx); - nni_strfree(d->host); - nni_strfree(d->port); NNI_FREE_STRUCT(d); } @@ -236,17 +235,13 @@ nni_tcp_dialer_alloc(nng_stream_dialer **dp, const nng_url *url) { tcp_dialer *d; int rv; - const char *p; if ((rv = tcp_dialer_alloc(&d)) != 0) { return (rv); } - if (((p = url->u_port) == NULL) || (strlen(p) == 0)) { - p = nni_url_default_port(url->u_scheme); - } - - if ((strlen(p) == 0) || (strlen(url->u_hostname) == 0)) { + if ((url->u_port == 0) || strlen(url->u_hostname) == 0 || + strlen(url->u_hostname) >= sizeof(d->host)) { // Dialer needs both a destination hostname and port. tcp_dialer_free(d); return (NNG_EADDRINVAL); @@ -260,11 +255,8 @@ nni_tcp_dialer_alloc(nng_stream_dialer **dp, const nng_url *url) d->af = NNG_AF_UNSPEC; } - if (((d->host = nng_strdup(url->u_hostname)) == NULL) || - ((d->port = nng_strdup(p)) == NULL)) { - tcp_dialer_free(d); - return (NNG_ENOMEM); - } + snprintf(d->host, sizeof(d->host), "%s", url->u_hostname); + d->port = url->u_port; *dp = (void *) d; return (0); |
