Skip to content

Commit ff66ec5

Browse files
committed
tcp/tls: Remove support for local interface address in dialer URLs
This was an undocumented capability provided for libnanomsg. The correct way to obtain the same functionality is to use `NNG_OPT_LOCADDR`.
1 parent 48d0c03 commit ff66ec5

File tree

6 files changed

+23
-219
lines changed

6 files changed

+23
-219
lines changed

docs/ref/migrate/nanomsg.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,11 @@ There are some exceptions. Be aware that the numeric values are _not_ the same.
121121
| `EMFILE` | [`NNG_ENOFILES`] |
122122
| `ENOSPC` | [`NNG_ENOSPC`] |
123123

124+
## Local Addresses for Dialing
125+
126+
The ability to specify the source address in the UROL,to use when
127+
using `nn_dial` inside the URL is not present in NNG. The correct
128+
way to specify the local address is using the `NNG_OPT_LOCADDR` option on the
129+
dialer before starting to dial.
130+
124131
{{#include ../xref.md}}

docs/ref/migrate/nng1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ Support for very old TLS versions 1.0 and 1.1 is removed.
6060
Further, the `NNG_TLS_1_0` and `NNG_TLS_1_1` constants are also removed.
6161
Applications should use `NNG_TLS_1_2` or even `NNG_TLS_1_3` instead.
6262

63+
## Support for Local Addresses in Dial URLs Removed
64+
65+
NNG 1.x had an undocumented ability to specify the local address to bind
66+
to when dialing, by using the local address in front of the destination
67+
address separated by a semicolon. This was provided for legacy libnanomsg
68+
compatilibility, and is no longer offered. The correct way to specify a
69+
local address is by setting `NNG_OPT_LOCADDR` on the dialer.
70+
6371
## Option Functions
6472

6573
The previously deprecated `nng_pipe_getopt_xxx` family of functions is removed.

src/sp/transport/tcp/tcp.c

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -724,65 +724,6 @@ tcptran_ep_close(void *arg)
724724
nni_mtx_unlock(&ep->mtx);
725725
}
726726

727-
// This parses off the optional source address that this transport uses.
728-
// The special handling of this URL format is quite honestly an historical
729-
// mistake, which we would remove if we could.
730-
static int
731-
tcptran_url_parse_source(nng_url *url, nng_sockaddr *sa, const nng_url *surl)
732-
{
733-
int af;
734-
char *semi;
735-
char *src;
736-
size_t len;
737-
int rv;
738-
nni_aio *aio;
739-
740-
// We modify the URL. This relies on the fact that the underlying
741-
// transport does not free this, so we can just use references.
742-
743-
url->u_scheme = surl->u_scheme;
744-
url->u_port = surl->u_port;
745-
url->u_hostname = surl->u_hostname;
746-
747-
if ((semi = strchr(url->u_hostname, ';')) == NULL) {
748-
memset(sa, 0, sizeof(*sa));
749-
return (0);
750-
}
751-
752-
len = (size_t) (semi - url->u_hostname);
753-
url->u_hostname = semi + 1;
754-
755-
if (strcmp(surl->u_scheme, "tcp") == 0) {
756-
af = NNG_AF_UNSPEC;
757-
} else if (strcmp(surl->u_scheme, "tcp4") == 0) {
758-
af = NNG_AF_INET;
759-
#ifdef NNG_ENABLE_IPV6
760-
} else if (strcmp(surl->u_scheme, "tcp6") == 0) {
761-
af = NNG_AF_INET6;
762-
#endif
763-
} else {
764-
return (NNG_EADDRINVAL);
765-
}
766-
767-
if ((src = nni_alloc(len + 1)) == NULL) {
768-
return (NNG_ENOMEM);
769-
}
770-
memcpy(src, surl->u_hostname, len);
771-
src[len] = '\0';
772-
773-
if ((rv = nni_aio_alloc(&aio, NULL, NULL)) != 0) {
774-
nni_free(src, len + 1);
775-
return (rv);
776-
}
777-
778-
nni_resolv_ip(src, "0", af, true, sa, aio);
779-
nni_aio_wait(aio);
780-
rv = nni_aio_result(aio);
781-
nni_aio_free(aio);
782-
nni_free(src, len + 1);
783-
return (rv);
784-
}
785-
786727
static void
787728
tcptran_timer_cb(void *arg)
788729
{
@@ -923,11 +864,9 @@ tcptran_ep_init(tcptran_ep **epp, nng_url *url, nni_sock *sock)
923864
static int
924865
tcptran_dialer_init(void **dp, nng_url *url, nni_dialer *ndialer)
925866
{
926-
tcptran_ep *ep;
927-
int rv;
928-
nng_sockaddr srcsa;
929-
nni_sock *sock = nni_dialer_sock(ndialer);
930-
nng_url myurl;
867+
tcptran_ep *ep;
868+
int rv;
869+
nni_sock *sock = nni_dialer_sock(ndialer);
931870

932871
// Check for invalid URL components.
933872
if ((strlen(url->u_path) != 0) && (strcmp(url->u_path, "/") != 0)) {
@@ -939,23 +878,13 @@ tcptran_dialer_init(void **dp, nng_url *url, nni_dialer *ndialer)
939878
return (NNG_EADDRINVAL);
940879
}
941880

942-
if ((rv = tcptran_url_parse_source(&myurl, &srcsa, url)) != 0) {
943-
return (rv);
944-
}
945-
946881
if ((rv = tcptran_ep_init(&ep, url, sock)) != 0) {
947882
return (rv);
948883
}
949884

950885
if ((rv != 0) ||
951886
((rv = nni_aio_alloc(&ep->connaio, tcptran_dial_cb, ep)) != 0) ||
952-
((rv = nng_stream_dialer_alloc_url(&ep->dialer, &myurl)) != 0)) {
953-
tcptran_ep_fini(ep);
954-
return (rv);
955-
}
956-
if ((srcsa.s_family != NNG_AF_UNSPEC) &&
957-
((rv = nni_stream_dialer_set(ep->dialer, NNG_OPT_LOCADDR, &srcsa,
958-
sizeof(srcsa), NNI_TYPE_SOCKADDR)) != 0)) {
887+
((rv = nng_stream_dialer_alloc_url(&ep->dialer, url)) != 0)) {
959888
tcptran_ep_fini(ep);
960889
return (rv);
961890
}

src/sp/transport/tcp/tcp_test.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,6 @@ test_tcp_wild_card_bind(void)
4646
NUTS_CLOSE(s1);
4747
}
4848

49-
void
50-
test_tcp_local_address_connect(void)
51-
{
52-
53-
nng_socket s1;
54-
nng_socket s2;
55-
char addr[NNG_MAXADDRLEN];
56-
uint16_t port;
57-
58-
NUTS_OPEN(s1);
59-
NUTS_OPEN(s2);
60-
port = nuts_next_port();
61-
(void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port);
62-
NUTS_PASS(nng_listen(s1, addr, NULL, 0));
63-
(void) snprintf(
64-
addr, sizeof(addr), "tcp://127.0.0.1;127.0.0.1:%u", port);
65-
NUTS_PASS(nng_dial(s2, addr, NULL, 0));
66-
NUTS_CLOSE(s2);
67-
NUTS_CLOSE(s1);
68-
}
69-
7049
void
7150
test_tcp_port_zero_bind(void)
7251
{
@@ -91,19 +70,6 @@ test_tcp_port_zero_bind(void)
9170
NUTS_CLOSE(s1);
9271
}
9372

94-
void
95-
test_tcp_bad_local_interface(void)
96-
{
97-
nng_socket s1;
98-
int rv;
99-
100-
NUTS_OPEN(s1);
101-
rv = nng_dial(s1, "tcp://bogus1;127.0.0.1:80", NULL, 0),
102-
NUTS_TRUE(rv != 0);
103-
NUTS_TRUE(rv != NNG_ECONNREFUSED);
104-
NUTS_CLOSE(s1);
105-
}
106-
10773
void
10874
test_tcp_non_local_address(void)
10975
{
@@ -244,8 +210,6 @@ NUTS_TESTS = {
244210
{ "tcp wild card connect fail", test_tcp_wild_card_connect_fail },
245211
{ "tcp wild card bind", test_tcp_wild_card_bind },
246212
{ "tcp port zero bind", test_tcp_port_zero_bind },
247-
{ "tcp local address connect", test_tcp_local_address_connect },
248-
{ "tcp bad local interface", test_tcp_bad_local_interface },
249213
{ "tcp non-local address", test_tcp_non_local_address },
250214
{ "tcp malformed address", test_tcp_malformed_address },
251215
{ "tcp no delay option", test_tcp_no_delay_option },

src/sp/transport/tls/tls.c

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -684,65 +684,6 @@ tlstran_ep_close(void *arg)
684684
nni_mtx_unlock(&ep->mtx);
685685
}
686686

687-
// This parses off the optional source address that this transport uses.
688-
// The special handling of this URL format is quite honestly an historical
689-
// mistake, which we would remove if we could.
690-
static int
691-
tlstran_url_parse_source(nni_url *url, nng_sockaddr *sa, const nni_url *surl)
692-
{
693-
int af;
694-
char *semi;
695-
char *src;
696-
size_t len;
697-
int rv;
698-
nni_aio *aio;
699-
700-
// We modify the URL. This relies on the fact that the underlying
701-
// transport does not free this, so we can just use references.
702-
703-
url->u_scheme = surl->u_scheme;
704-
url->u_port = surl->u_port;
705-
url->u_hostname = surl->u_hostname;
706-
707-
if ((semi = strchr(url->u_hostname, ';')) == NULL) {
708-
memset(sa, 0, sizeof(*sa));
709-
return (0);
710-
}
711-
712-
len = (size_t) (semi - url->u_hostname);
713-
url->u_hostname = semi + 1;
714-
715-
if (strcmp(surl->u_scheme, "tls+tcp") == 0) {
716-
af = NNG_AF_UNSPEC;
717-
} else if (strcmp(surl->u_scheme, "tls+tcp4") == 0) {
718-
af = NNG_AF_INET;
719-
#ifdef NNG_ENABLE_IPV6
720-
} else if (strcmp(surl->u_scheme, "tls+tcp6") == 0) {
721-
af = NNG_AF_INET6;
722-
#endif
723-
} else {
724-
return (NNG_EADDRINVAL);
725-
}
726-
727-
if ((src = nni_alloc(len + 1)) == NULL) {
728-
return (NNG_ENOMEM);
729-
}
730-
memcpy(src, surl->u_hostname, len);
731-
src[len] = '\0';
732-
733-
if ((rv = nni_aio_alloc(&aio, NULL, NULL)) != 0) {
734-
nni_free(src, len + 1);
735-
return (rv);
736-
}
737-
738-
nni_resolv_ip(src, "0", af, 1, sa, aio);
739-
nni_aio_wait(aio);
740-
rv = nni_aio_result(aio);
741-
nni_aio_free(aio);
742-
nni_free(src, len + 1);
743-
return (rv);
744-
}
745-
746687
static void
747688
tlstran_timer_cb(void *arg)
748689
{
@@ -886,11 +827,9 @@ tlstran_ep_init(tlstran_ep **epp, nng_url *url, nni_sock *sock)
886827
static int
887828
tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer)
888829
{
889-
tlstran_ep *ep;
890-
int rv;
891-
nng_sockaddr srcsa;
892-
nni_sock *sock = nni_dialer_sock(ndialer);
893-
nni_url myurl;
830+
tlstran_ep *ep;
831+
int rv;
832+
nni_sock *sock = nni_dialer_sock(ndialer);
894833

895834
// Check for invalid URL components.
896835
if ((strlen(url->u_path) != 0) && (strcmp(url->u_path, "/") != 0)) {
@@ -902,23 +841,13 @@ tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer)
902841
return (NNG_EADDRINVAL);
903842
}
904843

905-
if ((rv = tlstran_url_parse_source(&myurl, &srcsa, url)) != 0) {
906-
return (rv);
907-
}
908-
909844
if (((rv = tlstran_ep_init(&ep, url, sock)) != 0) ||
910845
((rv = nni_aio_alloc(&ep->connaio, tlstran_dial_cb, ep)) != 0)) {
911846
return (rv);
912847
}
913848

914849
if ((rv != 0) ||
915-
((rv = nng_stream_dialer_alloc_url(&ep->dialer, &myurl)) != 0)) {
916-
tlstran_ep_fini(ep);
917-
return (rv);
918-
}
919-
if ((srcsa.s_family != NNG_AF_UNSPEC) &&
920-
((rv = nni_stream_dialer_set(ep->dialer, NNG_OPT_LOCADDR, &srcsa,
921-
sizeof(srcsa), NNI_TYPE_SOCKADDR)) != 0)) {
850+
((rv = nng_stream_dialer_alloc_url(&ep->dialer, url)) != 0)) {
922851
tlstran_ep_fini(ep);
923852
return (rv);
924853
}

src/sp/transport/tls/tls_tran_test.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,38 +132,6 @@ test_tls_port_zero_bind(void)
132132
nng_tls_config_free(c2);
133133
}
134134

135-
void
136-
test_tls_local_address_connect(void)
137-
{
138-
139-
nng_socket s1;
140-
nng_socket s2;
141-
nng_tls_config *c1, *c2;
142-
nng_dialer d;
143-
nng_listener l;
144-
char addr[NNG_MAXADDRLEN];
145-
uint16_t port;
146-
147-
c1 = tls_server_config();
148-
c2 = tls_client_config();
149-
NUTS_OPEN(s1);
150-
NUTS_OPEN(s2);
151-
port = nuts_next_port();
152-
(void) snprintf(addr, sizeof(addr), "tls+tcp://127.0.0.1:%u", port);
153-
NUTS_PASS(nng_listener_create(&l, s1, addr));
154-
NUTS_PASS(nng_listener_set_tls(l, c1));
155-
NUTS_PASS(nng_listener_start(l, 0));
156-
(void) snprintf(
157-
addr, sizeof(addr), "tls+tcp://127.0.0.1;127.0.0.1:%u", port);
158-
NUTS_PASS(nng_dialer_create(&d, s2, addr));
159-
NUTS_PASS(nng_dialer_set_tls(d, c2));
160-
NUTS_PASS(nng_dialer_start(d, 0));
161-
NUTS_CLOSE(s2);
162-
NUTS_CLOSE(s1);
163-
nng_tls_config_free(c1);
164-
nng_tls_config_free(c2);
165-
}
166-
167135
void
168136
test_tls_malformed_address(void)
169137
{
@@ -364,7 +332,6 @@ NUTS_TESTS = {
364332
{ "tls wild card connect fail", test_tls_wild_card_connect_fail },
365333
{ "tls wild card bind", test_tls_wild_card_bind },
366334
{ "tls port zero bind", test_tls_port_zero_bind },
367-
{ "tls local address connect", test_tls_local_address_connect },
368335
{ "tls malformed address", test_tls_malformed_address },
369336
{ "tls no delay option", test_tls_no_delay_option },
370337
{ "tls keep alive option", test_tls_keep_alive_option },

0 commit comments

Comments
 (0)