Skip to content

Commit

Permalink
Add support for ZT sockaddr properties (pipe).
Browse files Browse the repository at this point in the history
Also add a generic property test function to trantest.
  • Loading branch information
gdamore committed Oct 1, 2017
1 parent 3399027 commit b9b5c31
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ struct nng_sockaddr_in {
};

struct nng_sockaddr_zt {
uint16_t sa_family;
uint64_t sa_nwid;
uint64_t sa_nodeid;
uint32_t sa_port;
Expand Down
60 changes: 43 additions & 17 deletions src/transport/zerotier/zerotier.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#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;
Expand Down Expand Up @@ -98,11 +99,12 @@ typedef struct zt_fraglist zt_fraglist;
(ptr)[2] = (uint8_t)((uint32_t)(u)); \
} while (0)

static const uint16_t zt_ethertype = 0x901;
static const uint8_t zt_version = 0x01;
static const uint32_t zt_ephemeral = 0x800000u; // start of ephemeral ports
static const uint32_t zt_max_port = 0xffffffu; // largest port
static const uint32_t zt_port_mask = 0xffffffu; // mask of valid ports
static const uint16_t zt_ethertype = 0x901;
static const uint8_t zt_version = 0x01;
static const uint32_t zt_ephemeral = 0x800000u; // start of ephemeral ports
static const uint32_t zt_max_port = 0xffffffu; // largest port
static const uint32_t zt_port_mask = 0xffffffu; // mask of valid ports
static const uint32_t zt_port_shift = 24;

// These are compile time tunables for now.
enum zt_tunables {
Expand Down Expand Up @@ -2491,7 +2493,7 @@ zt_ep_connect(void *arg, nni_aio *aio)
}

if ((ep->ze_raddr >> 24) == 0) {
ep->ze_raddr |= (ep->ze_ztn->zn_self << 24);
ep->ze_raddr |= (ep->ze_ztn->zn_self << zt_port_shift);
}
nni_aio_list_append(&ep->ze_aios, aio);

Expand Down Expand Up @@ -2622,21 +2624,45 @@ zt_ep_getopt_ping_count(void *arg, void *data, size_t *szp)
return (nni_getopt_int(ep->ze_ping_count, data, szp));
}

static int
zt_pipe_getopt_locaddr(void *arg, void *data, size_t *szp)
{
zt_pipe * p = arg;
nng_sockaddr sa;
sa.s_un.s_zt.sa_family = NNG_AF_ZT;
sa.s_un.s_zt.sa_nwid = p->zp_nwid;
sa.s_un.s_zt.sa_nodeid = p->zp_laddr >> zt_port_shift;
sa.s_un.s_zt.sa_port = p->zp_laddr & zt_port_mask;
return (nni_getopt_sockaddr(&sa, data, szp));
}

static int
zt_pipe_getopt_remaddr(void *arg, void *data, size_t *szp)
{
zt_pipe * p = arg;
nng_sockaddr sa;
sa.s_un.s_zt.sa_family = NNG_AF_ZT;
sa.s_un.s_zt.sa_nwid = p->zp_nwid;
sa.s_un.s_zt.sa_nodeid = p->zp_raddr >> zt_port_shift;
sa.s_un.s_zt.sa_port = p->zp_raddr & zt_port_mask;
return (nni_getopt_sockaddr(&sa, data, szp));
}

static int
zt_pipe_getopt_mtu(void *arg, void *data, size_t *szp)
{
zt_pipe *p = arg;
return (nni_getopt_size(p->zp_mtu, 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
{ NNG_OPT_LOCADDR, zt_pipe_getopt_locaddr },
{ NNG_OPT_REMADDR, zt_pipe_getopt_remaddr },
{ NNG_ZT_OPT_MTU, zt_pipe_getopt_mtu },
// terminate list
{ NULL, NULL },
};

static nni_tran_pipe zt_pipe_ops = {
.p_fini = zt_pipe_fini,
.p_start = zt_pipe_start,
Expand Down
60 changes: 60 additions & 0 deletions tests/trantest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct {

unsigned trantest_port = 0;

typedef int (*trantest_proptest_t)(nng_msg *, nng_listener, nng_dialer);

void
trantest_next_address(char *out, const char *template)
{
Expand Down Expand Up @@ -169,6 +171,44 @@ trantest_send_recv(trantest *tt)
});
}

void
trantest_check_properties(trantest *tt, trantest_proptest_t f)
{
Convey("Properties test", {
nng_listener l;
nng_dialer d;
nng_msg * send;
nng_msg * recv;
size_t len;
nng_pipe p;
char url[NNG_MAXADDRLEN];
size_t sz;
int rv;

So(nng_listen(tt->repsock, tt->addr, &l, 0) == 0);
So(l != 0);
So(nng_dial(tt->reqsock, tt->addr, &d, 0) == 0);
So(d != 0);

nng_usleep(20000); // listener may be behind slightly

send = NULL;
So(nng_msg_alloc(&send, 0) == 0);
So(send != NULL);
So(nng_msg_append(send, "props", 5) == 0);

So(nng_sendmsg(tt->reqsock, send, 0) == 0);
recv = NULL;
So(nng_recvmsg(tt->repsock, &recv, 0) == 0);
So(recv != NULL);
So(nng_msg_len(recv) == 5);
So(strcmp(nng_msg_body(recv), "props") == 0);
rv = f(recv, l, d);
nng_msg_free(recv);
So(rv == 0);
});
}

void
trantest_send_recv_large(trantest *tt)
{
Expand Down Expand Up @@ -240,3 +280,23 @@ trantest_test_all(const char *addr)
trantest_send_recv_large(&tt);
})
}

void
trantest_test_extended(const char *addr, trantest_proptest_t f)
{
trantest tt;

Convey("Given transport", {
trantest_init(&tt, addr);

Reset({ trantest_fini(&tt); });

trantest_scheme(&tt);
trantest_conn_refused(&tt);
trantest_duplicate_listen(&tt);
trantest_listen_accept(&tt);
trantest_send_recv(&tt);
trantest_send_recv_large(&tt);
trantest_check_properties(&tt, f);
})
}
55 changes: 54 additions & 1 deletion tests/zt.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,59 @@ mkdir(const char *path, int mode)
#include <unistd.h>
#endif // WIN32

static int
check_props(nng_msg *msg, nng_listener l, nng_dialer d)
{
nng_sockaddr la, ra;
nng_pipe p;
size_t z;
size_t mtu;
uint64_t nwid;
p = nng_msg_get_pipe(msg);
So(p > 0);

// Check local address.
z = sizeof(nng_sockaddr);
So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0);
So(z == sizeof(la));
So(la.s_un.s_family == NNG_AF_ZT);
So(la.s_un.s_zt.sa_port == (trantest_port - 1));
So(la.s_un.s_zt.sa_nwid == 0xa09acf02337b057bull);
So(la.s_un.s_zt.sa_nodeid != 0);

// Check remote address.
z = sizeof(nng_sockaddr);
So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == 0);
So(z == sizeof(ra));
So(ra.s_un.s_family == NNG_AF_ZT);
So(ra.s_un.s_zt.sa_port != 0);
So(ra.s_un.s_zt.sa_nwid == 0xa09acf02337b057bull);
So(ra.s_un.s_zt.sa_nodeid == la.s_un.s_zt.sa_nodeid);

// Check network ID.
z = sizeof(nwid);
nwid = 0;
So(nng_pipe_getopt(p, "zt:nwid", &nwid, &z) == 0);
So(nwid = 0xa09acf02337b057bull);

z = sizeof(nwid);
nwid = 0;
So(nng_dialer_getopt(d, "zt:nwid", &nwid, &z) == 0);
So(nwid = 0xa09acf02337b057bull);

z = sizeof(nwid);
nwid = 0;
So(nng_listener_getopt(l, "zt:nwid", &nwid, &z) == 0);
So(nwid = 0xa09acf02337b057bull);

// Check MTU
z = sizeof(mtu);
So(nng_pipe_getopt(p, "zt:mtu", &mtu, &z) == 0);
So(mtu >= 1000 && mtu <= 10000);

return (0);
}

TestMain("ZeroTier Transport", {

char path1[NNG_MAXADDRLEN] = "/tmp/zt_server";
Expand Down Expand Up @@ -175,7 +228,7 @@ TestMain("ZeroTier Transport", {

});

trantest_test_all("zt://" NWID "/*:%u");
trantest_test_extended("zt://" NWID "/*:%u", check_props);

nng_fini();
})

0 comments on commit b9b5c31

Please sign in to comment.