From 94c88335f9de5090846504dfa5fc1656092efacf Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 7 Sep 2024 16:45:17 -0700 Subject: Introduce nni_url_to_address for common URL to sockaddr support. This will be used in UDP. It also lets us reduce some unnecessary code paths for redundant library initialization. --- src/core/url.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/core/url.c') diff --git a/src/core/url.c b/src/core/url.c index f0cd16ad..577b409f 100644 --- a/src/core/url.c +++ b/src/core/url.c @@ -598,3 +598,37 @@ nni_url_clone(nni_url **dstp, const nni_url *src) } #undef URL_COPYSTR + +// nni_url_to_address resolves a URL into a sockaddr, assuming the URL is for +// an IP address. +int +nni_url_to_address(nng_sockaddr *sa, const nng_url *url) +{ + int af; + nni_aio aio; + const char *h; + int rv; + + // This assumes the scheme is one that uses TCP/IP addresses. + + if (strchr(url->u_scheme, '4') != NULL) { + af = NNG_AF_INET; + } else if (strchr(url->u_scheme, '6') != NULL) { + af = NNG_AF_INET6; + } else { + af = NNG_AF_UNSPEC; + } + + nni_aio_init(&aio, NULL, NULL); + + h = url->u_hostname; + if ((h != NULL) && ((strcmp(h, "*") == 0) || (strcmp(h, "") == 0))) { + h = NULL; + } + + nni_resolv_ip(h, url->u_port, af, true, sa, &aio); + nni_aio_wait(&aio); + rv = nni_aio_result(&aio); + nni_aio_fini(&aio); + return (rv); +} -- cgit v1.2.3-70-g09d2