From 85aff44e00e836eda618d4f1cf013bce38b3fd44 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 17 Nov 2024 18:23:17 -0800 Subject: 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. --- src/core/tcp.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'src/core/tcp.c') 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. +// Copyright 2024 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // Copyright 2019 Devolutions // @@ -10,6 +10,7 @@ // #include +#include #include #include @@ -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); -- cgit v1.2.3-70-g09d2