aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/CMakeLists.txt1
-rw-r--r--src/platform/posix/posix_resolv_gai.c49
-rw-r--r--src/platform/posix/posix_sockaddr.c48
-rw-r--r--src/platform/posix/posix_tcpdial.c28
-rw-r--r--src/platform/posix/posix_tcplisten.c15
5 files changed, 105 insertions, 36 deletions
diff --git a/src/platform/posix/CMakeLists.txt b/src/platform/posix/CMakeLists.txt
index b8e3782e..a00ffa50 100644
--- a/src/platform/posix/CMakeLists.txt
+++ b/src/platform/posix/CMakeLists.txt
@@ -62,6 +62,7 @@ if (NNG_PLATFORM_POSIX)
nng_check_sym(getpeerucred ucred.h NNG_HAVE_GETPEERUCRED)
nng_check_sym(atomic_flag_test_and_set stdatomic.h NNG_HAVE_STDATOMIC)
nng_check_sym(socketpair sys/socket.h NNG_HAVE_SOCKETPAIR)
+ nng_check_sym(AF_INET6 netinet/in.h NNG_HAVE_INET6)
nng_sources(
posix_impl.h
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
diff --git a/src/platform/posix/posix_sockaddr.c b/src/platform/posix/posix_sockaddr.c
index a569132c..63707310 100644
--- a/src/platform/posix/posix_sockaddr.c
+++ b/src/platform/posix/posix_sockaddr.c
@@ -1,5 +1,5 @@
//
-// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
@@ -22,17 +22,23 @@
#include <sys/types.h>
#include <sys/un.h>
+#ifndef NNG_HAVE_INET6
+#undef NNG_ENABLE_IPV6
+#endif
+
size_t
nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
{
- struct sockaddr_in * sin;
- struct sockaddr_in6 * sin6;
- struct sockaddr_un * spath;
- const nng_sockaddr_in * nsin;
- const nng_sockaddr_in6 * nsin6;
- const nng_sockaddr_path * nspath;
+ struct sockaddr_in *sin;
+ struct sockaddr_un *spath;
+ const nng_sockaddr_in *nsin;
+ const nng_sockaddr_path *nspath;
const nng_sockaddr_abstract *nsabs;
size_t sz;
+#ifdef NNG_ENABLE_IPV6
+ struct sockaddr_in6 *sin6;
+ const nng_sockaddr_in6 *nsin6;
+#endif
if ((sa == NULL) || (na == NULL)) {
return (0);
@@ -47,6 +53,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
sin->sin_addr.s_addr = nsin->sa_addr;
return (sizeof(*sin));
+#ifdef NNG_ENABLE_IPV6
case NNG_AF_INET6:
sin6 = (void *) sa;
nsin6 = &na->s_in6;
@@ -59,6 +66,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
sin6->sin6_scope_id = nsin6->sa_scope;
memcpy(sin6->sin6_addr.s6_addr, nsin6->sa_addr, 16);
return (sizeof(*sin6));
+#endif
case NNG_AF_IPC:
spath = (void *) sa;
@@ -75,18 +83,18 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
case NNG_AF_ABSTRACT:
spath = (void *) sa;
nsabs = &na->s_abstract;
- if (nsabs->sa_len >= sizeof (spath->sun_path)) {
+ if (nsabs->sa_len >= sizeof(spath->sun_path)) {
return (0);
}
memset(spath, 0, sizeof(*spath));
- spath->sun_family = PF_UNIX;
+ spath->sun_family = PF_UNIX;
spath->sun_path[0] = '\0'; // abstract starts with nul
// We support auto-bind with an empty string. There is
// a subtle caveat here, which is that we cannot bind to
// the *empty* name.
if (nsabs->sa_len == 0) {
- return (sizeof (sa_family_t)); // auto bind
+ return (sizeof(sa_family_t)); // auto bind
} else {
memcpy(&spath->sun_path[1], nsabs->sa_name,
nsabs->sa_len);
@@ -99,13 +107,15 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
int
nni_posix_sockaddr2nn(nni_sockaddr *na, const void *sa, size_t sz)
{
- const struct sockaddr_in * sin;
+ const struct sockaddr_in *sin;
+ const struct sockaddr_un *spath;
+ nng_sockaddr_in *nsin;
+ nng_sockaddr_path *nspath;
+ nng_sockaddr_abstract *nsabs;
+#ifdef NNG_ENABLE_IPV6
const struct sockaddr_in6 *sin6;
- const struct sockaddr_un * spath;
- nng_sockaddr_in * nsin;
- nng_sockaddr_in6 * nsin6;
- nng_sockaddr_path * nspath;
- nng_sockaddr_abstract * nsabs;
+ nng_sockaddr_in6 *nsin6;
+#endif
if ((na == NULL) || (sa == NULL)) {
return (-1);
@@ -121,6 +131,8 @@ nni_posix_sockaddr2nn(nni_sockaddr *na, const void *sa, size_t sz)
nsin->sa_port = sin->sin_port;
nsin->sa_addr = sin->sin_addr.s_addr;
break;
+
+#ifdef NNG_ENABLE_IPV6
case AF_INET6:
if (sz < sizeof(*sin6)) {
return (-1);
@@ -132,6 +144,8 @@ nni_posix_sockaddr2nn(nni_sockaddr *na, const void *sa, size_t sz)
nsin6->sa_scope = sin6->sin6_scope_id;
memcpy(nsin6->sa_addr, sin6->sin6_addr.s6_addr, 16);
break;
+#endif
+
case AF_UNIX:
// AF_UNIX can be NNG_AF_IPC, or NNG_AF_ABSTRACT.
spath = (void *) sa;
@@ -153,7 +167,7 @@ nni_posix_sockaddr2nn(nni_sockaddr *na, const void *sa, size_t sz)
nsabs->sa_len = sz - 1;
memcpy(nsabs->sa_name, &spath->sun_path[1], sz - 1);
} else {
- nspath = &na->s_ipc;
+ nspath = &na->s_ipc;
nspath->sa_family = NNG_AF_IPC;
nni_strlcpy(nspath->sa_path, spath->sun_path,
sizeof(nspath->sa_path));
diff --git a/src/platform/posix/posix_tcpdial.c b/src/platform/posix/posix_tcpdial.c
index 0af72cfa..cf3d9368 100644
--- a/src/platform/posix/posix_tcpdial.c
+++ b/src/platform/posix/posix_tcpdial.c
@@ -1,5 +1,5 @@
//
-// Copyright 2023 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
@@ -23,6 +23,10 @@
#include "posix_tcp.h"
+#ifndef NNG_HAVE_INET6
+#undef NNG_ENABLE_IPV6
+#endif
+
// Dialer stuff.
int
nni_tcp_dialer_init(nni_tcp_dialer **dp)
@@ -93,7 +97,7 @@ static void
tcp_dialer_cancel(nni_aio *aio, void *arg, int rv)
{
nni_tcp_dialer *d = arg;
- nni_tcp_conn * c;
+ nni_tcp_conn *c;
nni_mtx_lock(&d->mtx);
if ((!nni_aio_list_active(aio)) ||
@@ -113,9 +117,9 @@ tcp_dialer_cancel(nni_aio *aio, void *arg, int rv)
static void
tcp_dialer_cb(nni_posix_pfd *pfd, unsigned ev, void *arg)
{
- nni_tcp_conn * c = arg;
+ nni_tcp_conn *c = arg;
nni_tcp_dialer *d = c->dialer;
- nni_aio * aio;
+ nni_aio *aio;
int rv;
int ka;
int nd;
@@ -171,8 +175,8 @@ tcp_dialer_cb(nni_posix_pfd *pfd, unsigned ev, void *arg)
void
nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio)
{
- nni_tcp_conn * c;
- nni_posix_pfd * pfd = NULL;
+ nni_tcp_conn *c;
+ nni_posix_pfd *pfd = NULL;
struct sockaddr_storage ss;
size_t sslen;
int fd;
@@ -333,13 +337,15 @@ tcp_dialer_get_locaddr(void *arg, void *buf, size_t *szp, nni_type t)
static int
tcp_dialer_set_locaddr(void *arg, const void *buf, size_t sz, nni_type t)
{
- nni_tcp_dialer * d = arg;
+ nni_tcp_dialer *d = arg;
nng_sockaddr sa;
struct sockaddr_storage ss;
- struct sockaddr_in * sin;
- struct sockaddr_in6 * sin6;
+ struct sockaddr_in *sin;
size_t len;
int rv;
+#ifdef NNG_ENABLE_IPV6
+ struct sockaddr_in6 *sin6;
+#endif
if ((rv = nni_copyin_sockaddr(&sa, buf, sz, t)) != 0) {
return (rv);
@@ -356,12 +362,16 @@ tcp_dialer_set_locaddr(void *arg, const void *buf, size_t sz, nni_type t)
return (NNG_EADDRINVAL);
}
break;
+
+#ifdef NNG_ENABLE_IPV6
case AF_INET6:
sin6 = (void *) &ss;
if (sin6->sin6_port != 0) {
return (NNG_EADDRINVAL);
}
break;
+#endif // __NG_INET6
+
default:
return (NNG_EADDRINVAL);
}
diff --git a/src/platform/posix/posix_tcplisten.c b/src/platform/posix/posix_tcplisten.c
index e1c0b90c..95be4beb 100644
--- a/src/platform/posix/posix_tcplisten.c
+++ b/src/platform/posix/posix_tcplisten.c
@@ -27,6 +27,10 @@
#define SOCK_CLOEXEC 0
#endif
+#ifndef NNG_HAVE_INET6
+#undef NNG_ENABLE_IPV6
+#endif
+
#include "posix_tcp.h"
struct nni_tcp_listener {
@@ -94,7 +98,7 @@ tcp_listener_doaccept(nni_tcp_listener *l)
int nd;
int ka;
nni_posix_pfd *pfd;
- nni_tcp_conn * c;
+ nni_tcp_conn *c;
fd = nni_posix_pfd_fd(l->pfd);
@@ -203,10 +207,15 @@ nni_tcp_listener_listen(nni_tcp_listener *l, const nni_sockaddr *sa)
struct sockaddr_storage ss;
int rv;
int fd;
- nni_posix_pfd * pfd;
+ nni_posix_pfd *pfd;
if (((len = nni_posix_nn2sockaddr(&ss, sa)) == 0) ||
- ((ss.ss_family != AF_INET) && (ss.ss_family != AF_INET6))) {
+#ifdef NNG_ENABLE_IPV6
+ ((ss.ss_family != AF_INET) && (ss.ss_family != AF_INET6))
+#else
+ (ss.ss_family != AF_INET)
+#endif
+ ) {
return (NNG_EADDRINVAL);
}