aboutsummaryrefslogtreecommitdiff
path: root/src/core/url.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-09-07 16:45:17 -0700
committerGarrett D'Amore <garrett@damore.org>2024-10-05 17:48:36 -0700
commit94c88335f9de5090846504dfa5fc1656092efacf (patch)
treed47b1c0045632481db871fdeb5d2a1e0b747dcc3 /src/core/url.c
parent29aff2bacc9e2cfa6dcc52155d2816582617f918 (diff)
downloadnng-94c88335f9de5090846504dfa5fc1656092efacf.tar.gz
nng-94c88335f9de5090846504dfa5fc1656092efacf.tar.bz2
nng-94c88335f9de5090846504dfa5fc1656092efacf.zip
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.
Diffstat (limited to 'src/core/url.c')
-rw-r--r--src/core/url.c34
1 files changed, 34 insertions, 0 deletions
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);
+}