From be13c2e8845423cec17af429fc4e16a5d9749e47 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 26 Oct 2025 10:54:58 -0700 Subject: Replace nng_pipe_get_addr, nng_stream_get_addr, and the NNG_OPT_REMADDR option. More direct access methods are provided instead. This results in much lower friction when using, and is a step on the path to removing NNG_OPT_LOCADDR as well. We need to figure a solution for NNG_OPT_LOCADDR for dialers; for listeners there is little use in it either, and it will be removed. (Dialers will probably get a new NNG_OPT_BIND_IP option.) --- src/platform/tcp_stream_test.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'src/platform/tcp_stream_test.c') diff --git a/src/platform/tcp_stream_test.c b/src/platform/tcp_stream_test.c index a0943e6c..3dffed82 100644 --- a/src/platform/tcp_stream_test.c +++ b/src/platform/tcp_stream_test.c @@ -18,10 +18,8 @@ void test_tcp_stream(void) { - nng_stream_dialer *d = NULL; - nng_stream_listener *l = NULL; - nng_sockaddr sa; - uint8_t ip[4]; + nng_stream_dialer *d = NULL; + nng_stream_listener *l = NULL; nng_aio *daio = NULL; nng_aio *laio = NULL; nng_aio *maio = NULL; @@ -30,10 +28,12 @@ test_tcp_stream(void) nng_aio *aio1; nng_aio *aio2; nng_iov iov; - nng_sockaddr sa2; + const nng_sockaddr *sap1; + const nng_sockaddr *sap2; char buf1[5]; char buf2[5]; bool on; + int port; NUTS_PASS(nng_aio_alloc(&daio, NULL, NULL)); NUTS_PASS(nng_aio_alloc(&laio, NULL, NULL)); @@ -43,18 +43,12 @@ test_tcp_stream(void) NUTS_PASS(nng_stream_listener_alloc(&l, "tcp://127.0.0.1")); NUTS_PASS(nng_stream_listener_listen(l)); - - ip[0] = 127; - ip[1] = 0; - ip[2] = 0; - ip[3] = 1; - NUTS_PASS(nng_stream_listener_get_addr(l, NNG_OPT_LOCADDR, &sa)); - NUTS_TRUE(sa.s_in.sa_port != 0); - NUTS_TRUE(memcmp(&sa.s_in.sa_addr, ip, 4) == 0); + NUTS_PASS(nng_stream_listener_get_int(l, NNG_OPT_BOUND_PORT, &port)); + NUTS_TRUE(port > 0); + NUTS_TRUE(port <= 0xffff); char uri[64]; - snprintf(uri, sizeof(uri), "tcp://127.0.0.1:%d", - nuts_be16(sa.s_in.sa_port)); + snprintf(uri, sizeof(uri), "tcp://127.0.0.1:%d", port); NUTS_PASS(nng_stream_dialer_alloc(&d, uri)); nng_stream_dialer_dial(d, daio); @@ -105,16 +99,13 @@ test_tcp_stream(void) NUTS_TRUE(memcmp(buf1, buf2, 5) == 0); - NUTS_PASS(nng_stream_get_addr(c2, NNG_OPT_LOCADDR, &sa2)); - NUTS_TRUE(sa2.s_in.sa_family == NNG_AF_INET); - - NUTS_TRUE(sa2.s_in.sa_addr == sa.s_in.sa_addr); - NUTS_TRUE(sa2.s_in.sa_port == sa.s_in.sa_port); + sap1 = nng_stream_self_addr(c2); + NUTS_TRUE(sap1->s_in.sa_family == NNG_AF_INET); - NUTS_PASS(nng_stream_get_addr(c1, NNG_OPT_REMADDR, &sa2)); - NUTS_TRUE(sa2.s_in.sa_family == NNG_AF_INET); - NUTS_TRUE(sa2.s_in.sa_addr == sa.s_in.sa_addr); - NUTS_TRUE(sa2.s_in.sa_port == sa.s_in.sa_port); + sap2 = nng_stream_peer_addr(c1); + NUTS_TRUE(sap2->s_in.sa_family == NNG_AF_INET); + NUTS_TRUE(sap2->s_in.sa_addr == sap1->s_in.sa_addr); + NUTS_TRUE(sap2->s_in.sa_port == sap1->s_in.sa_port); nng_stream_listener_free(l); nng_stream_dialer_free(d); -- cgit v1.2.3-70-g09d2