From 23a38d766780f4749945d84316b4e0a71e707b15 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 4 Mar 2018 17:04:11 -0800 Subject: fixes #262 NNG_OPT_URL should be resolved This causes TCP, TLS, and ZT endpoints to resolve any wildcards, and even IP addresses, when reporting the listen URL. The dialer URL is reported unresolved. Test cases for this are added as well, and nngcat actually reports this if --verbose is supplied. --- src/transport/tcp/tcp.c | 26 +++++++++++++++++++++++++- src/transport/tls/tls.c | 28 ++++++++++++++++++++++++++-- src/transport/zerotier/zerotier.c | 21 ++++++++++++++++++++- 3 files changed, 71 insertions(+), 4 deletions(-) (limited to 'src/transport') diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index 475a77ff..2a23b88b 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -53,6 +53,8 @@ struct nni_tcp_ep { nni_aio * aio; nni_aio * user_aio; nni_url * url; + nng_sockaddr bsa; // bound addr + int mode; nni_mtx mtx; }; @@ -591,6 +593,7 @@ nni_tcp_ep_init(void **epp, nni_url *url, nni_sock *sock, int mode) return (rv); } ep->proto = nni_sock_proto(sock); + ep->mode = mode; *epp = ep; return (0); @@ -615,7 +618,7 @@ nni_tcp_ep_bind(void *arg) int rv; nni_mtx_lock(&ep->mtx); - rv = nni_plat_tcp_ep_listen(ep->tep); + rv = nni_plat_tcp_ep_listen(ep->tep, &ep->bsa); nni_mtx_unlock(&ep->mtx); return (rv); @@ -733,6 +736,22 @@ nni_tcp_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz) return (nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ)); } +static int +nni_tcp_ep_getopt_url(void *arg, void *v, size_t *szp) +{ + nni_tcp_ep *ep = arg; + char ustr[128]; + char ipstr[48]; // max for IPv6 addresses including [] + char portstr[6]; // max for 16-bit port + + if (ep->mode == NNI_EP_MODE_DIAL) { + return (nni_getopt_str(ep->url->u_rawurl, v, szp)); + } + nni_plat_tcp_ntop(&ep->bsa, ipstr, portstr); + snprintf(ustr, sizeof(ustr), "tcp://%s:%s", ipstr, portstr); + return (nni_getopt_str(ustr, v, szp)); +} + static int nni_tcp_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp) { @@ -780,6 +799,11 @@ static nni_tran_ep_option nni_tcp_ep_options[] = { .eo_getopt = nni_tcp_ep_getopt_recvmaxsz, .eo_setopt = nni_tcp_ep_setopt_recvmaxsz, }, + { + .eo_name = NNG_OPT_URL, + .eo_getopt = nni_tcp_ep_getopt_url, + .eo_setopt = NULL, + }, { .eo_name = NNG_OPT_LINGER, .eo_getopt = nni_tcp_ep_getopt_linger, diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c index f9de3367..cf849373 100644 --- a/src/transport/tls/tls.c +++ b/src/transport/tls/tls.c @@ -61,7 +61,9 @@ struct nni_tls_ep { nni_aio * user_aio; nni_mtx mtx; nng_tls_config * cfg; + nng_sockaddr bsa; nni_url * url; + int mode; }; static void nni_tls_pipe_send_cb(void *); @@ -597,7 +599,8 @@ nni_tls_ep_init(void **epp, nni_url *url, nni_sock *sock, int mode) return (NNG_ENOMEM); } nni_mtx_init(&ep->mtx); - ep->url = url; + ep->url = url; + ep->mode = mode; if (((rv = nni_plat_tcp_ep_init(&ep->tep, &lsa, &rsa, mode)) != 0) || ((rv = nni_tls_config_init(&ep->cfg, tlsmode)) != 0) || @@ -638,7 +641,7 @@ nni_tls_ep_bind(void *arg) int rv; nni_mtx_lock(&ep->mtx); - rv = nni_plat_tcp_ep_listen(ep->tep); + rv = nni_plat_tcp_ep_listen(ep->tep, &ep->bsa); nni_mtx_unlock(&ep->mtx); return (rv); @@ -746,6 +749,22 @@ nni_tls_ep_connect(void *arg, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } +static int +nni_tls_ep_getopt_url(void *arg, void *v, size_t *szp) +{ + nni_tls_ep *ep = arg; + char ustr[128]; + char ipstr[48]; // max for IPv6 addresses including [] + char portstr[6]; // max for 16-bit port + + if (ep->mode == NNI_EP_MODE_DIAL) { + return (nni_getopt_str(ep->url->u_rawurl, v, szp)); + } + nni_plat_tcp_ntop(&ep->bsa, ipstr, portstr); + snprintf(ustr, sizeof(ustr), "tls+tcp://%s:%s", ipstr, portstr); + return (nni_getopt_str(ustr, v, szp)); +} + static int nni_tls_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz) { @@ -906,6 +925,11 @@ static nni_tran_ep_option nni_tls_ep_options[] = { .eo_getopt = nni_tls_ep_getopt_linger, .eo_setopt = nni_tls_ep_setopt_linger, }, + { + .eo_name = NNG_OPT_URL, + .eo_getopt = nni_tls_ep_getopt_url, + .eo_setopt = NULL, + }, { .eo_name = NNG_OPT_TLS_CONFIG, .eo_getopt = tls_getopt_config, diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index f8ed4626..4a846301 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -230,7 +230,6 @@ struct zt_ep { uint64_t ze_nwid; int ze_mode; int ze_running; - nni_sockaddr ze_addr; uint64_t ze_raddr; // remote node address uint64_t ze_laddr; // local node address uint16_t ze_proto; @@ -2556,6 +2555,21 @@ zt_ep_getopt_home(void *arg, void *data, size_t *szp) return (nni_getopt_str(ep->ze_home, data, szp)); } +static int +zt_ep_getopt_url(void *arg, void *data, size_t *szp) +{ + char ustr[64]; // more than plenty + zt_ep * ep = arg; + uint64_t addr; + + addr = ep->ze_mode == NNI_EP_MODE_DIAL ? ep->ze_raddr : ep->ze_laddr; + snprintf(ustr, sizeof(ustr), "zt://%llx.%llx:%u", + (unsigned long long) addr >> zt_port_shift, + (unsigned long long) ep->ze_nwid, + (unsigned) (addr & zt_port_mask)); + return (nni_getopt_str(ustr, data, szp)); +} + static int zt_ep_setopt_orbit(void *arg, const void *data, size_t sz) { @@ -2724,6 +2738,11 @@ static nni_tran_ep_option zt_ep_options[] = { .eo_getopt = zt_ep_getopt_recvmaxsz, .eo_setopt = zt_ep_setopt_recvmaxsz, }, + { + .eo_name = NNG_OPT_URL, + .eo_getopt = zt_ep_getopt_url, + .eo_setopt = NULL, + }, { .eo_name = NNG_OPT_ZT_HOME, .eo_getopt = zt_ep_getopt_home, -- cgit v1.2.3-70-g09d2