aboutsummaryrefslogtreecommitdiff
path: root/src/transport
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-09-25 12:49:10 -0700
committerGarrett D'Amore <garrett@damore.org>2017-09-27 14:38:12 -0700
commit64db0f085be0c9efc6dca8d9e72d3e5a47cb792e (patch)
tree475520498d8ebe9e47e9785d8f9d209c87582400 /src/transport
parent86a96e5bf1b207a8b1aa925e1d9f73ce834505b8 (diff)
downloadnng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.tar.gz
nng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.tar.bz2
nng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.zip
Refactor option handling APIs.
This makes the APIs use string keys, and largely eliminates the use of integer option IDs altogether. The underlying registration for options is also now a bit richer, letting protcols and transports declare the actual options they use, rather than calling down into each entry point carte blanche and relying on ENOTSUP. This code may not be as fast as the integers was, but it is more intuitive, easier to extend, and is not on any hot code paths. (If you're diddling options on a hot code path you're doing something wrong.)
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/inproc/inproc.c51
-rw-r--r--src/transport/ipc/ipc.c124
-rw-r--r--src/transport/tcp/tcp.c124
-rw-r--r--src/transport/zerotier/zerotier.c364
4 files changed, 353 insertions, 310 deletions
diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c
index e3cc5143..3f013f92 100644
--- a/src/transport/inproc/inproc.c
+++ b/src/transport/inproc/inproc.c
@@ -177,25 +177,15 @@ nni_inproc_pipe_peer(void *arg)
}
static int
-nni_inproc_pipe_getopt(void *arg, int option, void *buf, size_t *szp)
+nni_inproc_pipe_get_addr(void *arg, void *buf, size_t *szp)
{
-#if 0
- nni_inproc_pipe *pipe = arg;
+ nni_inproc_pipe *p = arg;
+ nni_sockaddr sa;
- switch (option) {
- case NNG_OPT_LOCALADDR:
- case NNG_OPT_REMOTEADDR:
- len = strlen(pipe->addr) + 1;
- if (len > *szp) {
- (void) memcpy(buf, pipe->addr, *szp);
- } else {
- (void) memcpy(buf, pipe->addr, len);
- }
- *szp = len;
- return (0);
- }
-#endif
- return (NNG_ENOTSUP);
+ sa.s_un.s_inproc.sa_family = NNG_AF_INPROC;
+ nni_strlcpy(sa.s_un.s_inproc.sa_path, p->addr,
+ sizeof(sa.s_un.s_inproc.sa_path));
+ return (nni_getopt_sockaddr(&sa, buf, szp));
}
static int
@@ -442,13 +432,25 @@ nni_inproc_ep_accept(void *arg, nni_aio *aio)
nni_mtx_unlock(&nni_inproc.mx);
}
+static nni_tran_pipe_option nni_inproc_pipe_options[] = {
+ { NNG_OPT_LOCADDR, nni_inproc_pipe_get_addr },
+ { NNG_OPT_REMADDR, nni_inproc_pipe_get_addr },
+ // terminate list
+ { NULL, NULL },
+};
+
static nni_tran_pipe nni_inproc_pipe_ops = {
- .p_fini = nni_inproc_pipe_fini,
- .p_send = nni_inproc_pipe_send,
- .p_recv = nni_inproc_pipe_recv,
- .p_close = nni_inproc_pipe_close,
- .p_peer = nni_inproc_pipe_peer,
- .p_getopt = nni_inproc_pipe_getopt,
+ .p_fini = nni_inproc_pipe_fini,
+ .p_send = nni_inproc_pipe_send,
+ .p_recv = nni_inproc_pipe_recv,
+ .p_close = nni_inproc_pipe_close,
+ .p_peer = nni_inproc_pipe_peer,
+ .p_options = nni_inproc_pipe_options,
+};
+
+static nni_tran_ep_option nni_inproc_ep_options[] = {
+ // terminate list
+ { NULL, NULL, NULL },
};
static nni_tran_ep nni_inproc_ep_ops = {
@@ -458,8 +460,7 @@ static nni_tran_ep nni_inproc_ep_ops = {
.ep_bind = nni_inproc_ep_bind,
.ep_accept = nni_inproc_ep_accept,
.ep_close = nni_inproc_ep_close,
- .ep_setopt = NULL,
- .ep_getopt = NULL,
+ .ep_options = nni_inproc_ep_options,
};
// This is the inproc transport linkage, and should be the only global
diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c
index b65adfee..afe8afa8 100644
--- a/src/transport/ipc/ipc.c
+++ b/src/transport/ipc/ipc.c
@@ -16,14 +16,14 @@
// IPC transport. Platform specific IPC operations must be
// supplied as well. Normally the IPC is UNIX domain sockets or
-// Windows named pipes. Other platforms could use other mechanisms.
+// Windows named pipes. Other platforms could use other mechanisms,
+// but all implementations on the platform must use the same mechanism.
typedef struct nni_ipc_pipe nni_ipc_pipe;
typedef struct nni_ipc_ep nni_ipc_ep;
// nni_ipc_pipe is one end of an IPC connection.
struct nni_ipc_pipe {
- const char * addr;
nni_plat_ipc_pipe *ipp;
uint16_t peer;
uint16_t proto;
@@ -48,7 +48,7 @@ struct nni_ipc_pipe {
};
struct nni_ipc_ep {
- char addr[NNG_MAXADDRLEN + 1];
+ nni_sockaddr sa;
nni_plat_ipc_ep *iep;
uint16_t proto;
size_t rcvmax;
@@ -63,15 +63,6 @@ static void nni_ipc_pipe_nego_cb(void *);
static void nni_ipc_ep_cb(void *);
static int
-nni_ipc_tran_chkopt(int o, const void *data, size_t sz)
-{
- if (o == nng_optid_recvmaxsz) {
- return (nni_chkopt_size(data, sz, 0, NNI_MAXSZ));
- }
- return (NNG_ENOTSUP);
-}
-
-static int
nni_ipc_tran_init(void)
{
return (0);
@@ -132,10 +123,8 @@ nni_ipc_pipe_init(nni_ipc_pipe **pipep, nni_ipc_ep *ep, void *ipp)
p->proto = ep->proto;
p->rcvmax = ep->rcvmax;
p->ipp = ipp;
- p->addr = ep->addr;
p->sa.s_un.s_path.sa_family = NNG_AF_IPC;
- nni_strlcpy(p->sa.s_un.s_path.sa_path, p->addr + strlen("ipc://"),
- sizeof(p->sa.s_un.s_path.sa_path));
+ p->sa = ep->sa;
*pipep = p;
return (0);
@@ -463,22 +452,10 @@ nni_ipc_pipe_peer(void *arg)
}
static int
-nni_ipc_pipe_getopt(void *arg, int option, void *buf, size_t *szp)
+nni_ipc_pipe_get_addr(void *arg, void *buf, size_t *szp)
{
-
- nni_ipc_pipe *pipe = arg;
- size_t len;
- int rv;
-
- if ((option == nng_optid_locaddr) || (option == nng_optid_remaddr)) {
- rv = nni_getopt_sockaddr(&pipe->sa, buf, szp);
- } else if (option == nng_optid_recvmaxsz) {
- rv = nni_getopt_size(pipe->rcvmax, &buf, szp);
-
- } else {
- rv = NNG_ENOTSUP;
- }
- return (rv);
+ nni_ipc_pipe *p = arg;
+ return (nni_getopt_sockaddr(&p->sa, buf, szp));
}
static void
@@ -496,33 +473,28 @@ nni_ipc_ep_fini(void *arg)
static int
nni_ipc_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
{
- nni_ipc_ep * ep;
- int rv;
- nni_sockaddr sa;
- size_t sz;
+ nni_ipc_ep *ep;
+ int rv;
+ size_t sz;
if (strncmp(url, "ipc://", strlen("ipc://")) != 0) {
return (NNG_EADDRINVAL);
}
url += strlen("ipc://");
- sz = sizeof(sa.s_un.s_path.sa_path);
- sa.s_un.s_path.sa_family = NNG_AF_IPC;
-
- if (nni_strlcpy(sa.s_un.s_path.sa_path, url, sz) >= sz) {
- return (NNG_EADDRINVAL);
- }
-
if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
return (NNG_ENOMEM);
}
- if (nni_strlcpy(ep->addr, url, sizeof(ep->addr)) >= sizeof(ep->addr)) {
+ sz = sizeof(ep->sa.s_un.s_path.sa_path);
+ ep->sa.s_un.s_path.sa_family = NNG_AF_IPC;
+
+ if (nni_strlcpy(ep->sa.s_un.s_path.sa_path, url, sz) >= sz) {
NNI_FREE_STRUCT(ep);
return (NNG_EADDRINVAL);
}
- if ((rv = nni_plat_ipc_ep_init(&ep->iep, &sa, mode)) != 0) {
+ if ((rv = nni_plat_ipc_ep_init(&ep->iep, &ep->sa, mode)) != 0) {
NNI_FREE_STRUCT(ep);
return (rv);
}
@@ -666,41 +638,60 @@ nni_ipc_ep_connect(void *arg, nni_aio *aio)
}
static int
-nni_ipc_ep_setopt(void *arg, int opt, const void *v, size_t sz)
+nni_ipc_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz)
{
- int rv = NNG_ENOTSUP;
nni_ipc_ep *ep = arg;
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ);
- nni_mtx_unlock(&ep->mtx);
+ if (ep == NULL) {
+ return (nni_chkopt_size(data, sz, 0, NNI_MAXSZ));
}
- return (rv);
+ return (nni_setopt_size(&ep->rcvmax, data, sz, 0, NNI_MAXSZ));
}
static int
-nni_ipc_ep_getopt(void *arg, int opt, void *v, size_t *szp)
+nni_ipc_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp)
{
- int rv = NNG_ENOTSUP;
nni_ipc_ep *ep = arg;
+ return (nni_getopt_size(ep->rcvmax, data, szp));
+}
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_getopt_size(ep->rcvmax, v, szp);
- nni_mtx_unlock(&ep->mtx);
- }
- return (rv);
+static int
+nni_ipc_ep_get_addr(void *arg, void *data, size_t *szp)
+{
+ nni_ipc_ep *ep = arg;
+ return (nni_getopt_sockaddr(&ep->sa, data, szp));
}
+static nni_tran_pipe_option nni_ipc_pipe_options[] = {
+ { NNG_OPT_REMADDR, nni_ipc_pipe_get_addr },
+ { NNG_OPT_LOCADDR, nni_ipc_pipe_get_addr },
+ // terminate list
+ { NULL, NULL },
+};
+
static nni_tran_pipe nni_ipc_pipe_ops = {
- .p_fini = nni_ipc_pipe_fini,
- .p_start = nni_ipc_pipe_start,
- .p_send = nni_ipc_pipe_send,
- .p_recv = nni_ipc_pipe_recv,
- .p_close = nni_ipc_pipe_close,
- .p_peer = nni_ipc_pipe_peer,
- .p_getopt = nni_ipc_pipe_getopt,
+ .p_fini = nni_ipc_pipe_fini,
+ .p_start = nni_ipc_pipe_start,
+ .p_send = nni_ipc_pipe_send,
+ .p_recv = nni_ipc_pipe_recv,
+ .p_close = nni_ipc_pipe_close,
+ .p_peer = nni_ipc_pipe_peer,
+ .p_options = nni_ipc_pipe_options,
+};
+
+static nni_tran_ep_option nni_ipc_ep_options[] = {
+ {
+ .eo_name = NNG_OPT_RECVMAXSZ,
+ .eo_getopt = nni_ipc_ep_getopt_recvmaxsz,
+ .eo_setopt = nni_ipc_ep_setopt_recvmaxsz,
+ },
+ {
+ .eo_name = NNG_OPT_LOCADDR,
+ .eo_getopt = nni_ipc_ep_get_addr,
+ .eo_setopt = NULL,
+ },
+ // terminate list
+ { NULL, NULL, NULL },
};
static nni_tran_ep nni_ipc_ep_ops = {
@@ -710,8 +701,7 @@ static nni_tran_ep nni_ipc_ep_ops = {
.ep_bind = nni_ipc_ep_bind,
.ep_accept = nni_ipc_ep_accept,
.ep_close = nni_ipc_ep_close,
- .ep_setopt = nni_ipc_ep_setopt,
- .ep_getopt = nni_ipc_ep_getopt,
+ .ep_options = nni_ipc_ep_options,
};
// This is the IPC transport linkage, and should be the only global
diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c
index 587c1af9..413d8fa5 100644
--- a/src/transport/tcp/tcp.c
+++ b/src/transport/tcp/tcp.c
@@ -63,18 +63,6 @@ static void nni_tcp_pipe_nego_cb(void *);
static void nni_tcp_ep_cb(void *arg);
static int
-nni_tcp_tran_chkopt(int o, const void *data, size_t sz)
-{
- if (o == nng_optid_recvmaxsz) {
- return (nni_chkopt_size(data, sz, 0, NNI_MAXSZ));
- }
- if (o == nng_optid_linger) {
- return (nni_chkopt_usec(data, sz));
- }
- return (NNG_ENOTSUP);
-}
-
-static int
nni_tcp_tran_init(void)
{
return (0);
@@ -419,29 +407,6 @@ nni_tcp_pipe_peer(void *arg)
}
static int
-nni_tcp_pipe_getopt(void *arg, int option, void *buf, size_t *szp)
-{
-#if 0
- nni_inproc_pipe *pipe = arg;
- size_t len;
-
- switch (option) {
- case NNG_OPT_LOCALADDR:
- case NNG_OPT_REMOTEADDR:
- len = strlen(pipe->addr) + 1;
- if (len > *szp) {
- (void) memcpy(buf, pipe->addr, *szp);
- } else {
- (void) memcpy(buf, pipe->addr, len);
- }
- *szp = len;
- return (0);
- }
-#endif
- return (NNG_ENOTSUP);
-}
-
-static int
nni_tcp_parse_pair(char *pair, char **hostp, char **servp)
{
char *host, *serv, *end;
@@ -786,52 +751,71 @@ nni_tcp_ep_connect(void *arg, nni_aio *aio)
}
static int
-nni_tcp_ep_setopt(void *arg, int opt, const void *v, size_t sz)
+nni_tcp_ep_setopt_recvmaxsz(void *arg, const void *v, size_t sz)
{
- int rv = NNG_ENOTSUP;
nni_tcp_ep *ep = arg;
-
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ);
- nni_mtx_unlock(&ep->mtx);
-
- } else if (opt == nng_optid_linger) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_setopt_usec(&ep->linger, v, sz);
- nni_mtx_unlock(&ep->mtx);
+ if (ep == NULL) {
+ return (nni_chkopt_size(v, sz, 0, NNI_MAXSZ));
}
- return (rv);
+ return (nni_setopt_size(&ep->rcvmax, v, sz, 0, NNI_MAXSZ));
}
static int
-nni_tcp_ep_getopt(void *arg, int opt, void *v, size_t *szp)
+nni_tcp_ep_getopt_recvmaxsz(void *arg, void *v, size_t *szp)
{
- int rv = NNG_ENOTSUP;
nni_tcp_ep *ep = arg;
+ return (nni_getopt_size(ep->rcvmax, v, szp));
+}
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_getopt_size(ep->rcvmax, v, szp);
- nni_mtx_unlock(&ep->mtx);
-
- } else if (opt == nng_optid_linger) {
- nni_mtx_lock(&ep->mtx);
- rv = nni_getopt_usec(ep->linger, v, szp);
- nni_mtx_unlock(&ep->mtx);
+static int
+nni_tcp_ep_setopt_linger(void *arg, const void *v, size_t sz)
+{
+ nni_tcp_ep *ep = arg;
+ if (ep == NULL) {
+ return (nni_chkopt_usec(v, sz));
}
- // XXX: add address properties
- return (rv);
+ return (nni_setopt_usec(&ep->linger, v, sz));
}
+static int
+nni_tcp_ep_getopt_linger(void *arg, void *v, size_t *szp)
+{
+ nni_tcp_ep *ep = arg;
+ return (nni_getopt_usec(ep->linger, v, szp));
+}
+
+static nni_tran_pipe_option nni_tcp_pipe_options[] = {
+#if 0
+ { NNG_OPT_LOCADDR, nni_tcp_pipe_get_locaddr },
+ { NNG_OPT_REMADDR, nni_tcp_pipe_get_remaddr },
+#endif
+ // terminate list
+ { NULL, NULL }
+};
+
static nni_tran_pipe nni_tcp_pipe_ops = {
- .p_fini = nni_tcp_pipe_fini,
- .p_start = nni_tcp_pipe_start,
- .p_send = nni_tcp_pipe_send,
- .p_recv = nni_tcp_pipe_recv,
- .p_close = nni_tcp_pipe_close,
- .p_peer = nni_tcp_pipe_peer,
- .p_getopt = nni_tcp_pipe_getopt,
+ .p_fini = nni_tcp_pipe_fini,
+ .p_start = nni_tcp_pipe_start,
+ .p_send = nni_tcp_pipe_send,
+ .p_recv = nni_tcp_pipe_recv,
+ .p_close = nni_tcp_pipe_close,
+ .p_peer = nni_tcp_pipe_peer,
+ .p_options = nni_tcp_pipe_options,
+};
+
+static nni_tran_ep_option nni_tcp_ep_options[] = {
+ {
+ .eo_name = NNG_OPT_RECVMAXSZ,
+ .eo_getopt = nni_tcp_ep_getopt_recvmaxsz,
+ .eo_setopt = nni_tcp_ep_setopt_recvmaxsz,
+ },
+ {
+ .eo_name = NNG_OPT_LINGER,
+ .eo_getopt = nni_tcp_ep_getopt_linger,
+ .eo_setopt = nni_tcp_ep_setopt_linger,
+ },
+ // terminate list
+ { NULL, NULL, NULL },
};
static nni_tran_ep nni_tcp_ep_ops = {
@@ -841,8 +825,7 @@ static nni_tran_ep nni_tcp_ep_ops = {
.ep_bind = nni_tcp_ep_bind,
.ep_accept = nni_tcp_ep_accept,
.ep_close = nni_tcp_ep_close,
- .ep_setopt = nni_tcp_ep_setopt,
- .ep_getopt = nni_tcp_ep_getopt,
+ .ep_options = nni_tcp_ep_options,
};
// This is the TCP transport linkage, and should be the only global
@@ -852,7 +835,6 @@ struct nni_tran nni_tcp_tran = {
.tran_scheme = "tcp",
.tran_ep = &nni_tcp_ep_ops,
.tran_pipe = &nni_tcp_pipe_ops,
- .tran_chkopt = nni_tcp_tran_chkopt,
.tran_init = nni_tcp_tran_init,
.tran_fini = nni_tcp_tran_fini,
};
diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c
index 40af6e54..9182ec1f 100644
--- a/src/transport/zerotier/zerotier.c
+++ b/src/transport/zerotier/zerotier.c
@@ -22,26 +22,32 @@
#include <ZeroTierOne.h>
-const char *nng_opt_zt_home = "zt:home";
-const char *nng_opt_zt_nwid = "zt:nwid";
-const char *nng_opt_zt_node = "zt:node";
-const char *nng_opt_zt_status = "zt:status";
-const char *nng_opt_zt_network_name = "zt:network-name";
-const char *nng_opt_zt_local_port = "zt:local-port";
-const char *nng_opt_zt_ping_time = "zt:ping-time";
-const char *nng_opt_zt_ping_count = "zt:ping-count";
-
-int nng_optid_zt_home = -1;
-int nng_optid_zt_nwid = -1;
-int nng_optid_zt_node = -1;
-int nng_optid_zt_status = -1;
-int nng_optid_zt_network_name = -1;
-int nng_optid_zt_ping_time = -1;
-int nng_optid_zt_ping_count = -1;
-int nng_optid_zt_local_port = -1;
+#define NNG_ZT_OPT_HOME "zt:home"
+#define NNG_ZT_OPT_NWID "zt:nwid"
+#define NNG_ZT_OPT_NODE "zt:node"
+#define NNG_ZT_OPT_STATUS "zt:status"
+#define NNG_ZT_OPT_NETWORK_NAME "zt:network-name"
+#define NNG_ZT_OPT_PING_TIME "zt:ping-time"
+#define NNG_ZT_OPT_PING_COUNT "zt:ping-count"
+
+const char *nng_opt_zt_home = NNG_ZT_OPT_HOME;
+const char *nng_opt_zt_nwid = NNG_ZT_OPT_NWID;
+const char *nng_opt_zt_node = NNG_ZT_OPT_NODE;
+const char *nng_opt_zt_status = NNG_ZT_OPT_STATUS;
+const char *nng_opt_zt_network_name = NNG_ZT_OPT_NETWORK_NAME;
+const char *nng_opt_zt_ping_time = NNG_ZT_OPT_PING_TIME;
+const char *nng_opt_zt_ping_count = NNG_ZT_OPT_PING_COUNT;
+
+int zt_optid_home = -1;
+int zt_optid_nwid = -1;
+int zt_optid_node = -1;
+int zt_optid_status = -1;
+int zt_optid_network_name = -1;
+int zt_optid_ping_time = -1;
+int zt_optid_ping_count = -1;
// These values are supplied to help folks checking status. They are the
-// return values from nng_optid_zt_status.
+// return values from zt_optid_status.
int nng_zt_status_configuring = ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION;
int nng_zt_status_ok = ZT_NETWORK_STATUS_OK;
int nng_zt_status_denied = ZT_NETWORK_STATUS_ACCESS_DENIED;
@@ -1597,51 +1603,23 @@ done:
}
static int
-zt_chkopt(int opt, const void *dat, size_t sz)
-{
- if (opt == nng_optid_recvmaxsz) {
- // We cannot deal with message sizes larger
- // than 64k.
- return (nni_chkopt_size(dat, sz, 0, 0xffffffffU));
- }
- if (opt == nng_optid_zt_home) {
- size_t l = nni_strnlen(dat, sz);
- if ((l >= sz) || (l >= NNG_MAXADDRLEN)) {
- return (NNG_EINVAL);
- }
- // XXX: should we apply additional security
- // checks? home path is not null terminated
- return (0);
- }
- if (opt == nng_optid_zt_ping_count) {
- return (nni_chkopt_int(dat, sz, 0, 1000000));
- }
- if (opt == nng_optid_zt_ping_time) {
- return (nni_chkopt_usec(dat, sz));
- }
- return (NNG_ENOTSUP);
-}
-
-static int
zt_tran_init(void)
{
int rv;
- if (((rv = nni_option_register(nng_opt_zt_home, &nng_optid_zt_home)) !=
+ if (((rv = nni_option_register(nng_opt_zt_home, &zt_optid_home)) !=
0) ||
- ((rv = nni_option_register(nng_opt_zt_node, &nng_optid_zt_node)) !=
+ ((rv = nni_option_register(nng_opt_zt_node, &zt_optid_node)) !=
0) ||
- ((rv = nni_option_register(nng_opt_zt_nwid, &nng_optid_zt_nwid)) !=
+ ((rv = nni_option_register(nng_opt_zt_nwid, &zt_optid_nwid)) !=
+ 0) ||
+ ((rv = nni_option_register(nng_opt_zt_status, &zt_optid_status)) !=
0) ||
((rv = nni_option_register(
- nng_opt_zt_status, &nng_optid_zt_status)) != 0) ||
- ((rv = nni_option_register(nng_opt_zt_network_name,
- &nng_optid_zt_network_name)) != 0) ||
- ((rv = nni_option_register(
- nng_opt_zt_local_port, &nng_optid_zt_local_port)) != 0) ||
+ nng_opt_zt_network_name, &zt_optid_network_name)) != 0) ||
((rv = nni_option_register(
- nng_opt_zt_ping_count, &nng_optid_zt_ping_count)) != 0) ||
+ nng_opt_zt_ping_count, &zt_optid_ping_count)) != 0) ||
((rv = nni_option_register(
- nng_opt_zt_ping_time, &nng_optid_zt_ping_time)) != 0)) {
+ nng_opt_zt_ping_time, &zt_optid_ping_time)) != 0)) {
return (rv);
}
nni_mtx_init(&zt_lk);
@@ -1652,11 +1630,11 @@ zt_tran_init(void)
static void
zt_tran_fini(void)
{
- nng_optid_zt_home = -1;
- nng_optid_zt_nwid = -1;
- nng_optid_zt_node = -1;
- nng_optid_zt_ping_count = -1;
- nng_optid_zt_ping_time = -1;
+ zt_optid_home = -1;
+ zt_optid_nwid = -1;
+ zt_optid_node = -1;
+ zt_optid_ping_count = -1;
+ zt_optid_ping_time = -1;
zt_node *ztn;
nni_mtx_lock(&zt_lk);
@@ -2014,25 +1992,31 @@ zt_getopt_network_name(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp)
}
static int
-zt_pipe_getopt(void *arg, int option, void *buf, size_t *szp)
+zt_pipe_get_recvmaxsz(void *arg, void *buf, size_t *szp)
{
zt_pipe *p = arg;
- int rv;
+ return (nni_getopt_size(p->zp_rcvmax, buf, szp));
+}
- if (option == nng_optid_recvmaxsz) {
- rv = nni_getopt_size(p->zp_rcvmax, buf, szp);
- } else if (option == nng_optid_zt_nwid) {
- rv = nni_getopt_u64(p->zp_nwid, buf, szp);
- } else if (option == nng_optid_zt_node) {
- rv = nni_getopt_u64(p->zp_laddr >> 24, buf, szp);
- } else if (option == nng_optid_zt_status) {
- rv = zt_getopt_status(p->zp_ztn, p->zp_nwid, buf, szp);
- } else if (option == nng_optid_zt_network_name) {
- rv = zt_getopt_network_name(p->zp_ztn, p->zp_nwid, buf, szp);
- } else {
- rv = NNG_ENOTSUP;
- }
- return (rv);
+static int
+zt_pipe_get_nwid(void *arg, void *buf, size_t *szp)
+{
+ zt_pipe *p = arg;
+ return (nni_getopt_u64(p->zp_nwid, buf, szp));
+}
+
+static int
+zt_pipe_get_node(void *arg, void *buf, size_t *szp)
+{
+ zt_pipe *p = arg;
+ return (nni_getopt_u64(p->zp_laddr >> 24, buf, szp));
+}
+
+static int
+zt_pipe_get_status(void *arg, void *buf, size_t *szp)
+{
+ zt_pipe *p = arg;
+ return (zt_getopt_status(p->zp_ztn, p->zp_nwid, buf, szp));
}
static void
@@ -2553,96 +2537,184 @@ zt_ep_connect(void *arg, nni_aio *aio)
}
static int
-zt_ep_setopt(void *arg, int opt, const void *data, size_t size)
+zt_ep_setopt_recvmaxsz(void *arg, const void *data, size_t sz)
{
zt_ep *ep = arg;
- int i;
- int rv = NNG_ENOTSUP;
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&zt_lk);
- rv = nni_setopt_size(
- &ep->ze_rcvmax, data, size, 0, 0xffffffffu);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_home) {
- // XXX: check to make sure not started...
- i = nni_strnlen((const char *) data, size);
- if ((i >= size) || (i >= NNG_MAXADDRLEN)) {
- return (NNG_EINVAL);
- }
+ if (ep == NULL) {
+ return (nni_chkopt_size(data, sz, 0, 0xffffffffu));
+ }
+ return (nni_setopt_size(&ep->ze_rcvmax, data, sz, 0, 0xffffffffu));
+}
+
+static int
+zt_ep_getopt_recvmaxsz(void *arg, void *data, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (nni_getopt_size(ep->ze_rcvmax, data, szp));
+}
+
+static int
+zt_ep_setopt_home(void *arg, const void *data, size_t sz)
+{
+ int len;
+ int rv;
+ zt_ep *ep = arg;
+
+ len = nni_strnlen(data, sz);
+ if ((len >= sz) || (len >= NNG_MAXADDRLEN)) {
+ return (NNG_EINVAL);
+ }
+ if (ep != NULL) {
nni_mtx_lock(&zt_lk);
nni_strlcpy(ep->ze_home, data, sizeof(ep->ze_home));
- rv = zt_node_find(ep);
- if (rv != 0) {
+ if ((rv = zt_node_find(ep)) != 0) {
ep->ze_ztn = NULL;
}
nni_mtx_unlock(&zt_lk);
+ } else {
rv = 0;
- } else if (opt == nng_optid_zt_ping_count) {
- nni_mtx_lock(&zt_lk);
- rv =
- nni_setopt_int(&ep->ze_ping_count, data, size, 0, 1000000);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_ping_time) {
- nni_mtx_lock(&zt_lk);
- rv = nni_setopt_usec(&ep->ze_ping_time, data, size);
- nni_mtx_unlock(&zt_lk);
}
return (rv);
}
static int
-zt_ep_getopt(void *arg, int opt, void *data, size_t *szp)
+zt_ep_getopt_home(void *arg, void *data, size_t *szp)
{
zt_ep *ep = arg;
- int rv = NNG_ENOTSUP;
+ return (nni_getopt_str(ep->ze_home, data, szp));
+}
- if (opt == nng_optid_recvmaxsz) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_size(ep->ze_rcvmax, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_home) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_str(ep->ze_home, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_node) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_u64(ep->ze_ztn->zn_self, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_nwid) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_u64(ep->ze_nwid, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_ping_count) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_int(ep->ze_ping_count, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_ping_time) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_usec(ep->ze_ping_time, data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_local_port) {
- nni_mtx_lock(&zt_lk);
- rv = nni_getopt_int(
- (int) (ep->ze_laddr & zt_port_mask), data, szp);
- nni_mtx_unlock(&zt_lk);
- } else if (opt == nng_optid_zt_network_name) {
- rv =
- zt_getopt_network_name(ep->ze_ztn, ep->ze_nwid, data, szp);
- } else if (opt == nng_optid_zt_status) {
- rv = zt_getopt_status(ep->ze_ztn, ep->ze_nwid, data, szp);
+static int
+zt_ep_getopt_node(void *arg, void *data, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (nni_getopt_u64(ep->ze_ztn->zn_self, data, szp));
+}
+
+static int
+zt_ep_getopt_nwid(void *arg, void *data, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (nni_getopt_u64(ep->ze_nwid, data, szp));
+}
+
+static int
+zt_ep_getopt_network_name(void *arg, void *buf, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (zt_getopt_network_name(ep->ze_ztn, ep->ze_nwid, buf, szp));
+}
+
+static int
+zt_ep_getopt_status(void *arg, void *buf, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (zt_getopt_status(ep->ze_ztn, ep->ze_nwid, buf, szp));
+}
+
+static int
+zt_ep_setopt_ping_time(void *arg, const void *data, size_t sz)
+{
+ zt_ep *ep = arg;
+ if (ep == NULL) {
+ return (nni_chkopt_usec(data, sz));
}
- return (rv);
+ return (nni_setopt_usec(&ep->ze_ping_time, data, sz));
+}
+
+static int
+zt_ep_getopt_ping_time(void *arg, void *data, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (nni_getopt_usec(ep->ze_ping_time, data, szp));
+}
+
+static int
+zt_ep_setopt_ping_count(void *arg, const void *data, size_t sz)
+{
+ zt_ep *ep = arg;
+ if (ep == NULL) {
+ return (nni_chkopt_int(data, sz, 0, 1000000));
+ }
+ return (nni_setopt_int(&ep->ze_ping_count, data, sz, 0, 1000000));
+}
+
+static int
+zt_ep_getopt_ping_count(void *arg, void *data, size_t *szp)
+{
+ zt_ep *ep = arg;
+ return (nni_getopt_int(ep->ze_ping_count, data, szp));
}
+static nni_tran_pipe_option zt_pipe_options[] = {
+#if 0
+ { NNG_OPT_RECVMAXSZ, zt_pipe_getopt_recvmaxsz },
+ { NNG_ZT_OPT_NWID, zt_pipe_getopt_nwid },
+ { NNG_ZT_OPT_NODE, zt_pipe_getopt_node },
+ { NNG_ZT_OPT_STATUS, zt_pipe_getopt_status },
+ { NNG_ZT_OPT_NETWORK_NAME, zt_pipe_getopt_network_name },
+#endif
+#if 0
+ { NNG_OPT_LOCADDR, zt_pipe_get_locaddr },
+ { NNG_OPT_REMADDR, zt_pipe_get_remaddr }
+#endif
+ // terminate list
+ { NULL, NULL },
+};
static nni_tran_pipe zt_pipe_ops = {
- .p_fini = zt_pipe_fini,
- .p_start = zt_pipe_start,
- .p_send = zt_pipe_send,
- .p_recv = zt_pipe_recv,
- .p_close = zt_pipe_close,
- .p_peer = zt_pipe_peer,
- .p_getopt = zt_pipe_getopt,
+ .p_fini = zt_pipe_fini,
+ .p_start = zt_pipe_start,
+ .p_send = zt_pipe_send,
+ .p_recv = zt_pipe_recv,
+ .p_close = zt_pipe_close,
+ .p_peer = zt_pipe_peer,
+ .p_options = zt_pipe_options,
+};
+
+static nni_tran_ep_option zt_ep_options[] = {
+ {
+ .eo_name = NNG_OPT_RECVMAXSZ,
+ .eo_getopt = zt_ep_getopt_recvmaxsz,
+ .eo_setopt = zt_ep_setopt_recvmaxsz,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_HOME,
+ .eo_getopt = zt_ep_getopt_home,
+ .eo_setopt = zt_ep_setopt_home,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_NODE,
+ .eo_getopt = zt_ep_getopt_node,
+ .eo_setopt = NULL,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_NWID,
+ .eo_getopt = zt_ep_getopt_nwid,
+ .eo_setopt = NULL,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_STATUS,
+ .eo_getopt = zt_ep_getopt_status,
+ .eo_setopt = NULL,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_NETWORK_NAME,
+ .eo_getopt = zt_ep_getopt_network_name,
+ .eo_setopt = NULL,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_PING_TIME,
+ .eo_getopt = zt_ep_getopt_ping_time,
+ .eo_setopt = zt_ep_setopt_ping_time,
+ },
+ {
+ .eo_name = NNG_ZT_OPT_PING_COUNT,
+ .eo_getopt = zt_ep_getopt_ping_count,
+ .eo_setopt = zt_ep_setopt_ping_count,
+ },
+ // terminate list
+ { NULL, NULL, NULL },
};
static nni_tran_ep zt_ep_ops = {
@@ -2652,8 +2724,7 @@ static nni_tran_ep zt_ep_ops = {
.ep_bind = zt_ep_bind,
.ep_accept = zt_ep_accept,
.ep_close = zt_ep_close,
- .ep_setopt = zt_ep_setopt,
- .ep_getopt = zt_ep_getopt,
+ .ep_options = zt_ep_options,
};
// This is the ZeroTier transport linkage, and should be the
@@ -2663,7 +2734,6 @@ static struct nni_tran zt_tran = {
.tran_scheme = "zt",
.tran_ep = &zt_ep_ops,
.tran_pipe = &zt_pipe_ops,
- .tran_chkopt = zt_chkopt,
.tran_init = zt_tran_init,
.tran_fini = zt_tran_fini,
};