diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-02-25 17:13:38 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-02-25 18:39:56 -0800 |
| commit | 53a9740d1dcbad6be4b4c1a10a5f3fcbb97a5be9 (patch) | |
| tree | 00c49f3cd56f5797ec0d3f31dab689c6573fc6e6 /src/platform/posix/posix_resolv_gai.c | |
| parent | 8e62028a0db24364ea218007811e58ea11d0b64f (diff) | |
| download | nng-53a9740d1dcbad6be4b4c1a10a5f3fcbb97a5be9.tar.gz nng-53a9740d1dcbad6be4b4c1a10a5f3fcbb97a5be9.tar.bz2 nng-53a9740d1dcbad6be4b4c1a10a5f3fcbb97a5be9.zip | |
fixes #1496 Provide NNG_ENABLE_IPV6 option (disabled by default)
This also checks if the build system has the definitions for AF_INET6, which might
help in some embedded IPv4 only settings.
The resolver test is enhanced to include a check for IPv6 enabled in the kernel.
IPv6 support is enabled by default, of course.
Diffstat (limited to 'src/platform/posix/posix_resolv_gai.c')
| -rw-r--r-- | src/platform/posix/posix_resolv_gai.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c index 8eaa29f2..f522499e 100644 --- a/src/platform/posix/posix_resolv_gai.c +++ b/src/platform/posix/posix_resolv_gai.c @@ -33,6 +33,10 @@ #define AI_NUMERICSERV 0 #endif +#ifndef NNG_HAVE_INET6 +#undef NNG_ENABLE_IPV6 +#endif + static nni_mtx resolv_mtx = NNI_MTX_INITIALIZER; static nni_cv resolv_cv = NNI_CV_INITIALIZER(&resolv_mtx); static bool resolv_fini = false; @@ -182,17 +186,23 @@ resolv_task(resolv_item *item) rv = NNG_EADDRINVAL; for (probe = results; probe != NULL; probe = probe->ai_next) { - if ((probe->ai_addr->sa_family == AF_INET) || - (probe->ai_addr->sa_family == AF_INET6)) { + if (probe->ai_addr->sa_family == AF_INET) { break; } +#ifdef NNG_ENABLE_IPV6 + if (probe->ai_addr->sa_family == AF_INET6) { + break; + } +#endif } nni_mtx_lock(&resolv_mtx); if ((probe != NULL) && (item->aio != NULL)) { - struct sockaddr_in *sin; + struct sockaddr_in *sin; +#ifdef NNG_ENABLE_IPV6 struct sockaddr_in6 *sin6; - nng_sockaddr *sa = item->sa; +#endif + nng_sockaddr *sa = item->sa; switch (probe->ai_addr->sa_family) { case AF_INET: @@ -202,6 +212,7 @@ resolv_task(resolv_item *item) sa->s_in.sa_port = sin->sin_port; sa->s_in.sa_addr = sin->sin_addr.s_addr; break; +#ifdef NNG_ENABLE_IPV6 case AF_INET6: rv = 0; sin6 = (void *) probe->ai_addr; @@ -210,6 +221,7 @@ resolv_task(resolv_item *item) sa->s_in6.sa_scope = sin6->sin6_scope_id; memcpy(sa->s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16); break; +#endif } } nni_mtx_unlock(&resolv_mtx); @@ -238,12 +250,20 @@ nni_resolv_ip(const char *host, const char *serv, int af, bool passive, case NNG_AF_INET: fam = AF_INET; break; + +#ifdef NNG_ENABLE_IPV6 case NNG_AF_INET6: fam = AF_INET6; break; case NNG_AF_UNSPEC: fam = AF_UNSPEC; break; +#else + case NNG_AF_UNSPEC: + fam = AF_INET; + break; +#endif + default: nni_aio_finish_error(aio, NNG_ENOTSUP); return; @@ -342,13 +362,17 @@ parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) struct addrinfo hints; struct addrinfo *results; int rv; - bool v6 = false; - bool wrapped = false; char *port; char *host; char *buf; size_t buf_len; +#ifdef NNG_ENABLE_IPV6 + bool v6 = false; + bool wrapped = false; + char *s; +#endif + if (addr == NULL) { addr = ""; } @@ -359,12 +383,12 @@ parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) } memcpy(buf, addr, buf_len); host = buf; +#ifdef NNG_ENABLE_IPV6 if (*host == '[') { v6 = true; wrapped = true; host++; } else { - char *s; for (s = host; *s != '\0'; s++) { if (*s == '.') { break; @@ -394,6 +418,13 @@ parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) rv = NNG_EADDRINVAL; goto done; } +#else // NNG_ENABLE_IPV6 + for (port = host; *port != '\0'; port++) { + if (*port == ':') { + break; + } + } +#endif // NNG_ENABLE_IPV6 if ((!want_port) && (*port != '\0')) { rv = NNG_EADDRINVAL; @@ -408,9 +439,13 @@ parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST | AI_PASSIVE; +#ifdef NNG_ENABLE_IPV6 if (v6) { hints.ai_family = AF_INET6; } +#else + hints.ai_family = AF_INET; +#endif #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; #endif |
