From ee16d11a59120cd4d981e0dcb90741fa4141372a Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 22 Dec 2018 19:23:26 -0800 Subject: fixes #823 Define public IPC (#824) This introduces a basic IPC API, modeled on the TCP API, for direct access. Only connection options are exposed at present -- we need to add options for dialers and listeners (and particularly listener settings for permissions and security attributes.) Documentation is still outstanding, but a very limited test suite exists. --- src/platform/posix/posix_ipcconn.c | 93 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) (limited to 'src/platform/posix') diff --git a/src/platform/posix/posix_ipcconn.c b/src/platform/posix/posix_ipcconn.c index 0dae8bae..595ebe62 100644 --- a/src/platform/posix/posix_ipcconn.c +++ b/src/platform/posix/posix_ipcconn.c @@ -1,6 +1,7 @@ // // Copyright 2018 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV +// Copyright 2018 Devolutions // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -10,8 +11,6 @@ #include "core/nng_impl.h" -#ifdef NNG_PLATFORM_POSIX - #include #include #include @@ -40,6 +39,8 @@ #include "posix_ipc.h" +#include + static void ipc_conn_dowrite(nni_ipc_conn *c) { @@ -463,6 +464,19 @@ nni_ipc_conn_get_peer_pid(nni_ipc_conn *c, uint64_t *pid) return (0); } +int +nni_ipc_conn_sockname(nni_ipc_conn *c, nni_sockaddr *sa) +{ + struct sockaddr_storage ss; + socklen_t sslen = sizeof(ss); + int fd = nni_posix_pfd_fd(c->pfd); + + if (getsockname(fd, (void *) &ss, &sslen) != 0) { + return (nni_plat_errno(errno)); + } + return (nni_posix_sockaddr2nn(sa, &ss)); +} + int nni_posix_ipc_conn_init(nni_ipc_conn **cp, nni_posix_pfd *pfd) { @@ -502,4 +516,77 @@ nni_ipc_conn_fini(nni_ipc_conn *c) NNI_FREE_STRUCT(c); } -#endif // NNG_PLATFORM_POSIX +int +nni_ipc_conn_setopt( + nni_ipc_conn *c, const char *name, const void *val, size_t sz) +{ + NNI_ARG_UNUSED(c); + NNI_ARG_UNUSED(val); + NNI_ARG_UNUSED(sz); + if ((strcmp(name, NNG_OPT_LOCADDR) == 0) || + (strcmp(name, NNG_OPT_REMADDR) == 0) || + (strcmp(name, NNG_OPT_IPC_PEER_PID) == 0) || + (strcmp(name, NNG_OPT_IPC_PEER_UID) == 0) || + (strcmp(name, NNG_OPT_IPC_PEER_GID) == 0) || + (strcmp(name, NNG_OPT_IPC_PEER_ZONEID) == 0)) { + return (NNG_EREADONLY); + } + return (NNG_ENOTSUP); +} + +int +nni_ipc_conn_getopt(nni_ipc_conn *c, const char *name, void *val, size_t *szp) +{ + int rv; + uint64_t *idp = val; + + if ((strcmp(name, NNG_OPT_LOCADDR) == 0) || + (strcmp(name, NNG_OPT_REMADDR) == 0)) { + nng_sockaddr *sa = val; + if (*szp < sizeof(*sa)) { + return (NNG_EINVAL); + } + + if ((rv = nni_ipc_conn_sockname(c, sa)) == 0) { + *szp = sizeof(*sa); + } + return (rv); + } + if (strcmp(name, NNG_OPT_IPC_PEER_PID) == 0) { + if (*szp < sizeof(*idp)) { + return (NNG_EINVAL); + } + if ((rv = nni_ipc_conn_get_peer_pid(c, idp)) == 0) { + *szp = sizeof(*idp); + } + return (rv); + } + if (strcmp(name, NNG_OPT_IPC_PEER_UID) == 0) { + if (*szp < sizeof(*idp)) { + return (NNG_EINVAL); + } + if ((rv = nni_ipc_conn_get_peer_uid(c, idp)) == 0) { + *szp = sizeof(*idp); + } + return (rv); + } + if (strcmp(name, NNG_OPT_IPC_PEER_GID) == 0) { + if (*szp < sizeof(*idp)) { + return (NNG_EINVAL); + } + if ((rv = nni_ipc_conn_get_peer_gid(c, idp)) == 0) { + *szp = sizeof(*idp); + } + return (rv); + } + if (strcmp(name, NNG_OPT_IPC_PEER_ZONEID) == 0) { + if (*szp < sizeof(*idp)) { + return (NNG_EINVAL); + } + if ((rv = nni_ipc_conn_get_peer_zoneid(c, idp)) == 0) { + *szp = sizeof(*idp); + } + return (rv); + } + return (NNG_ENOTSUP); +} -- cgit v1.2.3-70-g09d2