Skip to content

Commit

Permalink
tcp/tls: Remove support for local interface address in dialer URLs
Browse files Browse the repository at this point in the history
This was an undocumented capability provided for libnanomsg.  The
correct way to obtain the same functionality is to use `NNG_OPT_LOCADDR`.
  • Loading branch information
gdamore committed Nov 17, 2024
1 parent 48d0c03 commit ff66ec5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 219 deletions.
7 changes: 7 additions & 0 deletions docs/ref/migrate/nanomsg.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,11 @@ There are some exceptions. Be aware that the numeric values are _not_ the same.
| `EMFILE` | [`NNG_ENOFILES`] |
| `ENOSPC` | [`NNG_ENOSPC`] |

## Local Addresses for Dialing

The ability to specify the source address in the UROL,to use when
using `nn_dial` inside the URL is not present in NNG. The correct
way to specify the local address is using the `NNG_OPT_LOCADDR` option on the
dialer before starting to dial.

{{#include ../xref.md}}
8 changes: 8 additions & 0 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ Support for very old TLS versions 1.0 and 1.1 is removed.
Further, the `NNG_TLS_1_0` and `NNG_TLS_1_1` constants are also removed.
Applications should use `NNG_TLS_1_2` or even `NNG_TLS_1_3` instead.

## Support for Local Addresses in Dial URLs Removed

NNG 1.x had an undocumented ability to specify the local address to bind
to when dialing, by using the local address in front of the destination
address separated by a semicolon. This was provided for legacy libnanomsg
compatilibility, and is no longer offered. The correct way to specify a
local address is by setting `NNG_OPT_LOCADDR` on the dialer.

## Option Functions

The previously deprecated `nng_pipe_getopt_xxx` family of functions is removed.
Expand Down
79 changes: 4 additions & 75 deletions src/sp/transport/tcp/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,65 +724,6 @@ tcptran_ep_close(void *arg)
nni_mtx_unlock(&ep->mtx);
}

// This parses off the optional source address that this transport uses.
// The special handling of this URL format is quite honestly an historical
// mistake, which we would remove if we could.
static int
tcptran_url_parse_source(nng_url *url, nng_sockaddr *sa, const nng_url *surl)
{
int af;
char *semi;
char *src;
size_t len;
int rv;
nni_aio *aio;

// We modify the URL. This relies on the fact that the underlying
// transport does not free this, so we can just use references.

url->u_scheme = surl->u_scheme;
url->u_port = surl->u_port;
url->u_hostname = surl->u_hostname;

if ((semi = strchr(url->u_hostname, ';')) == NULL) {
memset(sa, 0, sizeof(*sa));
return (0);
}

len = (size_t) (semi - url->u_hostname);
url->u_hostname = semi + 1;

if (strcmp(surl->u_scheme, "tcp") == 0) {
af = NNG_AF_UNSPEC;
} else if (strcmp(surl->u_scheme, "tcp4") == 0) {
af = NNG_AF_INET;
#ifdef NNG_ENABLE_IPV6
} else if (strcmp(surl->u_scheme, "tcp6") == 0) {
af = NNG_AF_INET6;
#endif
} else {
return (NNG_EADDRINVAL);
}

if ((src = nni_alloc(len + 1)) == NULL) {
return (NNG_ENOMEM);
}
memcpy(src, surl->u_hostname, len);
src[len] = '\0';

if ((rv = nni_aio_alloc(&aio, NULL, NULL)) != 0) {
nni_free(src, len + 1);
return (rv);
}

nni_resolv_ip(src, "0", af, true, sa, aio);
nni_aio_wait(aio);
rv = nni_aio_result(aio);
nni_aio_free(aio);
nni_free(src, len + 1);
return (rv);
}

static void
tcptran_timer_cb(void *arg)
{
Expand Down Expand Up @@ -923,11 +864,9 @@ tcptran_ep_init(tcptran_ep **epp, nng_url *url, nni_sock *sock)
static int
tcptran_dialer_init(void **dp, nng_url *url, nni_dialer *ndialer)
{
tcptran_ep *ep;
int rv;
nng_sockaddr srcsa;
nni_sock *sock = nni_dialer_sock(ndialer);
nng_url myurl;
tcptran_ep *ep;
int rv;
nni_sock *sock = nni_dialer_sock(ndialer);

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

if ((rv = tcptran_url_parse_source(&myurl, &srcsa, url)) != 0) {
return (rv);
}

if ((rv = tcptran_ep_init(&ep, url, sock)) != 0) {
return (rv);
}

if ((rv != 0) ||
((rv = nni_aio_alloc(&ep->connaio, tcptran_dial_cb, ep)) != 0) ||
((rv = nng_stream_dialer_alloc_url(&ep->dialer, &myurl)) != 0)) {
tcptran_ep_fini(ep);
return (rv);
}
if ((srcsa.s_family != NNG_AF_UNSPEC) &&
((rv = nni_stream_dialer_set(ep->dialer, NNG_OPT_LOCADDR, &srcsa,
sizeof(srcsa), NNI_TYPE_SOCKADDR)) != 0)) {
((rv = nng_stream_dialer_alloc_url(&ep->dialer, url)) != 0)) {
tcptran_ep_fini(ep);
return (rv);
}
Expand Down
36 changes: 0 additions & 36 deletions src/sp/transport/tcp/tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,6 @@ test_tcp_wild_card_bind(void)
NUTS_CLOSE(s1);
}

void
test_tcp_local_address_connect(void)
{

nng_socket s1;
nng_socket s2;
char addr[NNG_MAXADDRLEN];
uint16_t port;

NUTS_OPEN(s1);
NUTS_OPEN(s2);
port = nuts_next_port();
(void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port);
NUTS_PASS(nng_listen(s1, addr, NULL, 0));
(void) snprintf(
addr, sizeof(addr), "tcp://127.0.0.1;127.0.0.1:%u", port);
NUTS_PASS(nng_dial(s2, addr, NULL, 0));
NUTS_CLOSE(s2);
NUTS_CLOSE(s1);
}

void
test_tcp_port_zero_bind(void)
{
Expand All @@ -91,19 +70,6 @@ test_tcp_port_zero_bind(void)
NUTS_CLOSE(s1);
}

void
test_tcp_bad_local_interface(void)
{
nng_socket s1;
int rv;

NUTS_OPEN(s1);
rv = nng_dial(s1, "tcp://bogus1;127.0.0.1:80", NULL, 0),
NUTS_TRUE(rv != 0);
NUTS_TRUE(rv != NNG_ECONNREFUSED);
NUTS_CLOSE(s1);
}

void
test_tcp_non_local_address(void)
{
Expand Down Expand Up @@ -244,8 +210,6 @@ NUTS_TESTS = {
{ "tcp wild card connect fail", test_tcp_wild_card_connect_fail },
{ "tcp wild card bind", test_tcp_wild_card_bind },
{ "tcp port zero bind", test_tcp_port_zero_bind },
{ "tcp local address connect", test_tcp_local_address_connect },
{ "tcp bad local interface", test_tcp_bad_local_interface },
{ "tcp non-local address", test_tcp_non_local_address },
{ "tcp malformed address", test_tcp_malformed_address },
{ "tcp no delay option", test_tcp_no_delay_option },
Expand Down
79 changes: 4 additions & 75 deletions src/sp/transport/tls/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,65 +684,6 @@ tlstran_ep_close(void *arg)
nni_mtx_unlock(&ep->mtx);
}

// This parses off the optional source address that this transport uses.
// The special handling of this URL format is quite honestly an historical
// mistake, which we would remove if we could.
static int
tlstran_url_parse_source(nni_url *url, nng_sockaddr *sa, const nni_url *surl)
{
int af;
char *semi;
char *src;
size_t len;
int rv;
nni_aio *aio;

// We modify the URL. This relies on the fact that the underlying
// transport does not free this, so we can just use references.

url->u_scheme = surl->u_scheme;
url->u_port = surl->u_port;
url->u_hostname = surl->u_hostname;

if ((semi = strchr(url->u_hostname, ';')) == NULL) {
memset(sa, 0, sizeof(*sa));
return (0);
}

len = (size_t) (semi - url->u_hostname);
url->u_hostname = semi + 1;

if (strcmp(surl->u_scheme, "tls+tcp") == 0) {
af = NNG_AF_UNSPEC;
} else if (strcmp(surl->u_scheme, "tls+tcp4") == 0) {
af = NNG_AF_INET;
#ifdef NNG_ENABLE_IPV6
} else if (strcmp(surl->u_scheme, "tls+tcp6") == 0) {
af = NNG_AF_INET6;
#endif
} else {
return (NNG_EADDRINVAL);
}

if ((src = nni_alloc(len + 1)) == NULL) {
return (NNG_ENOMEM);
}
memcpy(src, surl->u_hostname, len);
src[len] = '\0';

if ((rv = nni_aio_alloc(&aio, NULL, NULL)) != 0) {
nni_free(src, len + 1);
return (rv);
}

nni_resolv_ip(src, "0", af, 1, sa, aio);
nni_aio_wait(aio);
rv = nni_aio_result(aio);
nni_aio_free(aio);
nni_free(src, len + 1);
return (rv);
}

static void
tlstran_timer_cb(void *arg)
{
Expand Down Expand Up @@ -886,11 +827,9 @@ tlstran_ep_init(tlstran_ep **epp, nng_url *url, nni_sock *sock)
static int
tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer)
{
tlstran_ep *ep;
int rv;
nng_sockaddr srcsa;
nni_sock *sock = nni_dialer_sock(ndialer);
nni_url myurl;
tlstran_ep *ep;
int rv;
nni_sock *sock = nni_dialer_sock(ndialer);

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

if ((rv = tlstran_url_parse_source(&myurl, &srcsa, url)) != 0) {
return (rv);
}

if (((rv = tlstran_ep_init(&ep, url, sock)) != 0) ||
((rv = nni_aio_alloc(&ep->connaio, tlstran_dial_cb, ep)) != 0)) {
return (rv);
}

if ((rv != 0) ||
((rv = nng_stream_dialer_alloc_url(&ep->dialer, &myurl)) != 0)) {
tlstran_ep_fini(ep);
return (rv);
}
if ((srcsa.s_family != NNG_AF_UNSPEC) &&
((rv = nni_stream_dialer_set(ep->dialer, NNG_OPT_LOCADDR, &srcsa,
sizeof(srcsa), NNI_TYPE_SOCKADDR)) != 0)) {
((rv = nng_stream_dialer_alloc_url(&ep->dialer, url)) != 0)) {
tlstran_ep_fini(ep);
return (rv);
}
Expand Down
33 changes: 0 additions & 33 deletions src/sp/transport/tls/tls_tran_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,38 +132,6 @@ test_tls_port_zero_bind(void)
nng_tls_config_free(c2);
}

void
test_tls_local_address_connect(void)
{

nng_socket s1;
nng_socket s2;
nng_tls_config *c1, *c2;
nng_dialer d;
nng_listener l;
char addr[NNG_MAXADDRLEN];
uint16_t port;

c1 = tls_server_config();
c2 = tls_client_config();
NUTS_OPEN(s1);
NUTS_OPEN(s2);
port = nuts_next_port();
(void) snprintf(addr, sizeof(addr), "tls+tcp://127.0.0.1:%u", port);
NUTS_PASS(nng_listener_create(&l, s1, addr));
NUTS_PASS(nng_listener_set_tls(l, c1));
NUTS_PASS(nng_listener_start(l, 0));
(void) snprintf(
addr, sizeof(addr), "tls+tcp://127.0.0.1;127.0.0.1:%u", port);
NUTS_PASS(nng_dialer_create(&d, s2, addr));
NUTS_PASS(nng_dialer_set_tls(d, c2));
NUTS_PASS(nng_dialer_start(d, 0));
NUTS_CLOSE(s2);
NUTS_CLOSE(s1);
nng_tls_config_free(c1);
nng_tls_config_free(c2);
}

void
test_tls_malformed_address(void)
{
Expand Down Expand Up @@ -364,7 +332,6 @@ NUTS_TESTS = {
{ "tls wild card connect fail", test_tls_wild_card_connect_fail },
{ "tls wild card bind", test_tls_wild_card_bind },
{ "tls port zero bind", test_tls_port_zero_bind },
{ "tls local address connect", test_tls_local_address_connect },
{ "tls malformed address", test_tls_malformed_address },
{ "tls no delay option", test_tls_no_delay_option },
{ "tls keep alive option", test_tls_keep_alive_option },
Expand Down

0 comments on commit ff66ec5

Please sign in to comment.