aboutsummaryrefslogtreecommitdiff
path: root/src/transport
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-10-02 13:34:55 -0700
committerGarrett D'Amore <garrett@damore.org>2017-10-02 15:29:43 -0700
commit6e945e18f3f3e9b7f9ee614eac6d3bf681f768d9 (patch)
treefffa3e455ec657ce3205c01c12398076e7ae28f0 /src/transport
parentb9b5c31b19df95d672ddc76cdfde29318f78b5ea (diff)
downloadnng-6e945e18f3f3e9b7f9ee614eac6d3bf681f768d9.tar.gz
nng-6e945e18f3f3e9b7f9ee614eac6d3bf681f768d9.tar.bz2
nng-6e945e18f3f3e9b7f9ee614eac6d3bf681f768d9.zip
Added more complete tests, and changes to property handling.
We allow some properties to be set on endpoints after they are started; transports now responsible for checking that. (The new values will only apply to new connections of course!) We added short-hand functions for pipe properties, and also added uint64_t shorthands across the board. The zerotier documentation got some updates (corrections). We have also added a separate header now for the ZT stuff. Also, dialers and listeners do not intermix anymore -- we test that only a dialer can be used with setting dialer options, and likewise for listeners.
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/zerotier/zerotier.adoc4
-rw-r--r--src/transport/zerotier/zerotier.c71
-rw-r--r--src/transport/zerotier/zerotier.h110
3 files changed, 140 insertions, 45 deletions
diff --git a/src/transport/zerotier/zerotier.adoc b/src/transport/zerotier/zerotier.adoc
index dc838420..84501807 100644
--- a/src/transport/zerotier/zerotier.adoc
+++ b/src/transport/zerotier/zerotier.adoc
@@ -357,10 +357,10 @@ ZeroTier IDs, ZeroTier network IDs, and our own 24-bit ports.
The format shall be `zt://<nwid>/<ztid>:<port>`, where the `<nwid>`
component represents the 64-bit hexadecimal ZeroTier network ID,
the `<ztid>` represents the 40-bit hexadecimal ZeroTier Device ID,
-and the `<port>` is the 24-bit port number previously described.
+and the `<port>` is the 24-bit port number (decimal) previously described.
A responder may elide the `<ztid>/` portion, to just bind to itself,
-in which case the format will be `zt://<nwid>/<ztid>:<port>`.
+in which case the format will be `zt://<nwid>:<port>`.
A port number of 0 may be used when listening to indicate that a random
ephemeral port should be chosen.
diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c
index 46423b80..56acce03 100644
--- a/src/transport/zerotier/zerotier.c
+++ b/src/transport/zerotier/zerotier.c
@@ -15,6 +15,7 @@
#include <string.h>
#include "core/nng_impl.h"
+#include "zerotier.h"
#ifndef _WIN32
#include <unistd.h>
@@ -22,31 +23,16 @@
#include <ZeroTierOne.h>
-#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"
-#define NNG_ZT_OPT_MTU "zt:mtu"
-
-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;
-
// These values are supplied to help folks checking status. They are the
-// return values from zt_opt_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;
-int nng_zt_status_notfound = ZT_NETWORK_STATUS_NOT_FOUND;
-int nng_zt_status_error = ZT_NETWORK_STATUS_PORT_ERROR;
-int nng_zt_status_obsolete = ZT_NETWORK_STATUS_CLIENT_TOO_OLD;
+// return values from zt_opt_status. It's important that the status values
+// here match what the underlying ZeroTier core gives us.
+int nng_zt_network_status_configuring =
+ ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION;
+int nng_zt_network_status_ok = ZT_NETWORK_STATUS_OK;
+int nng_zt_network_status_denied = ZT_NETWORK_STATUS_ACCESS_DENIED;
+int nng_zt_network_status_notfound = ZT_NETWORK_STATUS_NOT_FOUND;
+int nng_zt_network_status_error = ZT_NETWORK_STATUS_PORT_ERROR;
+int nng_zt_network_status_obsolete = ZT_NETWORK_STATUS_CLIENT_TOO_OLD;
// ZeroTier Transport. This sits on the ZeroTier L2 network, which itself
// is implemented on top of UDP. This requires the 3rd party
@@ -248,6 +234,7 @@ struct zt_ep {
zt_node * ze_ztn;
uint64_t ze_nwid;
int ze_mode;
+ int ze_running;
nni_sockaddr ze_addr;
uint64_t ze_raddr; // remote node address
uint64_t ze_laddr; // local node address
@@ -1932,7 +1919,7 @@ zt_pipe_peer(void *arg)
}
static int
-zt_getopt_status(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp)
+zt_getopt_network_status(zt_node *ztn, uint64_t nwid, void *buf, size_t *szp)
{
ZT_VirtualNetworkConfig *vcfg;
int status;
@@ -1990,13 +1977,6 @@ zt_pipe_get_node(void *arg, void *buf, size_t *szp)
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
zt_pipe_cancel_ping(nni_aio *aio, int rv)
{
@@ -2308,6 +2288,7 @@ zt_ep_bind_locked(zt_ep *ep)
ep->ze_laddr = ztn->zn_self;
ep->ze_laddr <<= 24;
ep->ze_laddr |= port;
+ ep->ze_running = 1;
if ((rv = nni_idhash_insert(ztn->zn_eps, ep->ze_laddr, ep)) != 0) {
nni_idhash_remove(ztn->zn_ports, port);
@@ -2498,6 +2479,7 @@ zt_ep_connect(void *arg, nni_aio *aio)
nni_aio_list_append(&ep->ze_aios, aio);
ep->ze_creq_try = 1;
+ ep->ze_running = 1;
nni_aio_set_timeout(ep->ze_creq_aio, now + zt_conn_interval);
// This can't fail -- the only way the ze_creq_aio gets
@@ -2543,6 +2525,9 @@ zt_ep_setopt_home(void *arg, const void *data, size_t sz)
return (NNG_EINVAL);
}
if (ep != NULL) {
+ if (ep->ze_running) {
+ return (NNG_ESTATE);
+ }
nni_mtx_lock(&zt_lk);
nni_strlcpy(ep->ze_home, data, sizeof(ep->ze_home));
if ((rv = zt_node_find(ep)) != 0) {
@@ -2584,10 +2569,10 @@ zt_ep_getopt_network_name(void *arg, void *buf, size_t *szp)
}
static int
-zt_ep_getopt_status(void *arg, void *buf, size_t *szp)
+zt_ep_getopt_network_status(void *arg, void *buf, size_t *szp)
{
zt_ep *ep = arg;
- return (zt_getopt_status(ep->ze_ztn, ep->ze_nwid, buf, szp));
+ return (zt_getopt_network_status(ep->ze_ztn, ep->ze_nwid, buf, szp));
}
static int
@@ -2658,7 +2643,7 @@ zt_pipe_getopt_mtu(void *arg, void *data, size_t *szp)
static nni_tran_pipe_option zt_pipe_options[] = {
{ NNG_OPT_LOCADDR, zt_pipe_getopt_locaddr },
{ NNG_OPT_REMADDR, zt_pipe_getopt_remaddr },
- { NNG_ZT_OPT_MTU, zt_pipe_getopt_mtu },
+ { NNG_OPT_ZT_MTU, zt_pipe_getopt_mtu },
// terminate list
{ NULL, NULL },
};
@@ -2680,37 +2665,37 @@ static nni_tran_ep_option zt_ep_options[] = {
.eo_setopt = zt_ep_setopt_recvmaxsz,
},
{
- .eo_name = NNG_ZT_OPT_HOME,
+ .eo_name = NNG_OPT_ZT_HOME,
.eo_getopt = zt_ep_getopt_home,
.eo_setopt = zt_ep_setopt_home,
},
{
- .eo_name = NNG_ZT_OPT_NODE,
+ .eo_name = NNG_OPT_ZT_NODE,
.eo_getopt = zt_ep_getopt_node,
.eo_setopt = NULL,
},
{
- .eo_name = NNG_ZT_OPT_NWID,
+ .eo_name = NNG_OPT_ZT_NWID,
.eo_getopt = zt_ep_getopt_nwid,
.eo_setopt = NULL,
},
{
- .eo_name = NNG_ZT_OPT_STATUS,
- .eo_getopt = zt_ep_getopt_status,
+ .eo_name = NNG_OPT_ZT_NETWORK_STATUS,
+ .eo_getopt = zt_ep_getopt_network_status,
.eo_setopt = NULL,
},
{
- .eo_name = NNG_ZT_OPT_NETWORK_NAME,
+ .eo_name = NNG_OPT_ZT_NETWORK_NAME,
.eo_getopt = zt_ep_getopt_network_name,
.eo_setopt = NULL,
},
{
- .eo_name = NNG_ZT_OPT_PING_TIME,
+ .eo_name = NNG_OPT_ZT_PING_TIME,
.eo_getopt = zt_ep_getopt_ping_time,
.eo_setopt = zt_ep_setopt_ping_time,
},
{
- .eo_name = NNG_ZT_OPT_PING_COUNT,
+ .eo_name = NNG_OPT_ZT_PING_COUNT,
.eo_getopt = zt_ep_getopt_ping_count,
.eo_setopt = zt_ep_setopt_ping_count,
},
diff --git a/src/transport/zerotier/zerotier.h b/src/transport/zerotier/zerotier.h
new file mode 100644
index 00000000..ff33609e
--- /dev/null
+++ b/src/transport/zerotier/zerotier.h
@@ -0,0 +1,110 @@
+//
+// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#ifndef NNG_TRANSPORT_ZEROTIER_ZEROTIER_H
+#define NNG_TRANSPORT_ZEROTIER_ZEROTIER_H
+
+// ZeroTier Transport. This sits on the ZeroTier L2 network, which itself
+// is implemented on top of UDP. This requires the 3rd party
+// libzerotiercore library (which is GPLv3!) and platform specific UDP
+// functionality to be built in. Note that care must be taken to link
+// dynamically if one wishes to avoid making your entire application GPL3.
+// (Alternatively ZeroTier offers commercial licenses which may prevent
+// this particular problem.) This implementation does not make use of
+// certain advanced capabilities in ZeroTier such as more sophisticated
+// route management and TCP fallback. You need to have connectivity
+// to the Internet to use this. (Or at least to your Planetary root.)
+//
+// The ZeroTier URL format we support is zt://<nwid>/<ztid>:<port> where
+// the <nwid> component represents the 64-bit hexadecimal ZeroTier
+// network ID,the <ztid> represents the 40-bit hexadecimal ZeroTier
+// node (device) ID, and the <port> is a 24-bit (decimal) port number.
+//
+// A listener may elide the <ztid>/ portion, to just bind to itself,
+// in which case the format will be zt://<nwid>:<port>
+//
+// A listener may also use either 0 or * for the <port> to indicate that
+// a random local ephemeral port should be used.
+//
+// Because ZeroTier takes a while to establish connectivity, it is even
+// more important that applications using the ZeroTier transport not
+// assume that a connection will be immediately available. It can take
+// quite a few seconds for peer-to-peer connectivity to be established.
+//
+// The ZeroTier transport was funded by Capitar IT Group, BV.
+//
+// This transport is highly experimental.
+
+// ZeroTier transport-specific options.
+
+// NNG_OPT_ZT_HOME is a string containing a directory, where persistent
+// state (key files, etc.) will be stored. It should be protected from
+// unauthorized viewing and modification. This option must be set on an
+// endpoint or socket before the endpoint(s) are started. If the unset,
+// or an empty string, then no persistence is used and an ephemeral node
+// will be created instead. Note that different endpoints may use different
+// values for this option, and that will lead to each endpoint having a
+// different ZeroTier identity -- however only one ephemeral node will
+// be created for the application.
+#define NNG_OPT_ZT_HOME "zt:home"
+
+// NNG_OPT_ZT_NWID is the 64-bit network ID, represented using a uint64_t in
+// native byte order. This is a read-only option; it is derived automatically
+// from the URL.
+#define NNG_OPT_ZT_NWID "zt:nwid"
+
+// NNG_OPT_ZT_NODE is the 40-bit node ID, stored in native order in the low
+// 40-bits of a uint64_t, of the node. This is a read-only option.
+#define NNG_OPT_ZT_NODE "zt:node"
+
+// NNG_OPT_ZT_NETWORK_STATUS represents the status of the ZeroTier virtual
+// network. The option is a read-only value, stored as an integer, which
+// takes of the nng_zt_network_status_xxx values listed below.
+#define NNG_OPT_ZT_NETWORK_STATUS "zt:network-status"
+
+// NNG_OPT_ZT_NETWORK_NAME is a human-readable name for the ZeroTier virtual
+// network. This will only be set once the ZeroTier network has come up
+// as the name comes from the network controller. This is read-only, and
+// is presented as an ASCIIZ string.
+#define NNG_OPT_ZT_NETWORK_NAME "zt:network-name"
+
+// NNG_OPT_ZT_PING_TIME and NNG_OPT_ZT_PING_COUNT are used to send ping
+// requests when a connection appears to be idled. If a logical session
+// has not received traffic from it's peer for ping-time, then a ping packet
+// is sent. This will be done up to ping-count times. If no traffic from
+// the remote peer is seen after all ping requests are sent, then the peer
+// is assumed to be dead or offline, and the session is closed. The
+// NNG_OPT_ZT_PING_TIME is a duration (usec, stored as an nng_duration, and
+// NNG_OPT_ZT_PING_COUNT is an integer.) This ping process can be disabled
+// by setting either ping-time or ping-count to zero.
+#define NNG_OPT_ZT_PING_TIME "zt:ping-time"
+#define NNG_OPT_ZT_PING_COUNT "zt:ping-count"
+
+// NNG_OPT_ZT_MTU is a read-only size_t and contains the ZeroTier virtual
+// network MTU (i.e. the L2 payload MTU). Messages that are larger than this
+// (including our 20-byte header data) will be fragmented into multiple
+// virtual L2 frames.
+#define NNG_OPT_ZT_MTU "zt:mtu"
+
+// Network status values.
+// These values are supplied to help folks checking status. They are the
+// return values from zt_opt_status. We avoid hard coding them as defines,
+// to keep applications from baking in values that may change if the
+// underlying ZeroTier transport changes.
+extern int nng_zt_network_status_configuring;
+extern int nng_zt_network_status_ok;
+extern int nng_zt_network_status_denied;
+extern int nng_zt_network_status_notfound;
+extern int nng_zt_network_status_error;
+extern int nng_zt_network_status_obsolete;
+
+extern int nng_zt_register(void);
+
+#endif // NNG_TRANSPORT_ZEROTIER_ZEROTIER_H