1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
//
// 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.
//
#include "core/nng_impl.h"
#include "transport/inproc/inproc.h"
#include "transport/ipc/ipc.h"
#include "transport/tcp/tcp.h"
#include "transport/tls/tls.h"
#include "transport/ws/websocket.h"
#include "transport/zerotier/zerotier.h"
#include <stdio.h>
#include <string.h>
// For now the list of transports is hard-wired. Adding new transports
// to the system dynamically is something that might be considered later.
extern nni_tran nni_tcp_tran;
extern nni_tran nni_ipc_tran;
typedef struct nni_transport {
nni_tran t_tran;
char t_prefix[16]; // e.g. "tcp://" or "tls+tcp://"
nni_list_node t_node;
} nni_transport;
static nni_list nni_tran_list;
static nni_mtx nni_tran_lk;
static int nni_tran_inited;
int
nni_tran_register(const nni_tran *tran)
{
nni_transport *t;
int rv;
size_t sz;
// Its entirely possible that we are called before any sockets
// are opened. Make sure we are initialized. This has to be
// protected by a guard to prevent infinite recursion, since
// nni_init also winds up calling us.
if (!nni_tran_inited) {
nni_init();
}
if (tran->tran_version != NNI_TRANSPORT_VERSION) {
return (NNG_ENOTSUP);
}
nni_mtx_lock(&nni_tran_lk);
// Check to see if the transport is already registered...
NNI_LIST_FOREACH (&nni_tran_list, t) {
if (strcmp(tran->tran_scheme, t->t_tran.tran_scheme) == 0) {
if (tran->tran_init == t->t_tran.tran_init) {
// duplicate.
nni_mtx_unlock(&nni_tran_lk);
return (0);
}
nni_mtx_unlock(&nni_tran_lk);
return (NNG_ESTATE);
}
}
if ((t = NNI_ALLOC_STRUCT(t)) == NULL) {
nni_mtx_unlock(&nni_tran_lk);
return (NNG_ENOMEM);
}
t->t_tran = *tran;
sz = sizeof(t->t_prefix);
if ((nni_strlcpy(t->t_prefix, tran->tran_scheme, sz) >= sz) ||
(nni_strlcat(t->t_prefix, "://", sz) >= sz)) {
nni_mtx_unlock(&nni_tran_lk);
NNI_FREE_STRUCT(t);
return (NNG_EINVAL);
}
if ((rv = t->t_tran.tran_init()) != 0) {
nni_mtx_unlock(&nni_tran_lk);
NNI_FREE_STRUCT(t);
return (rv);
}
nni_list_append(&nni_tran_list, t);
nni_mtx_unlock(&nni_tran_lk);
return (0);
}
nni_tran *
nni_tran_find(const char *addr)
{
// address is of the form "<scheme>://blah..."
nni_transport *t;
nni_mtx_lock(&nni_tran_lk);
NNI_LIST_FOREACH (&nni_tran_list, t) {
if (strncmp(addr, t->t_prefix, strlen(t->t_prefix)) == 0) {
nni_mtx_unlock(&nni_tran_lk);
return (&t->t_tran);
}
}
nni_mtx_unlock(&nni_tran_lk);
return (NULL);
}
// nni_tran_parse_host_port is a convenience routine to parse the host portion
// of a URL (which includes a DNS name or IP address and an optional service
// name or port, separated by a colon) into its host and port name parts. It
// understands IPv6 address literals when surrounded by brackets ([]).
// If either component is empty, then NULL is passed back for the value,
// otherwise a string suitable for freeing with nni_strfree is supplied.
int
nni_tran_parse_host_port(const char *pair, char **hostp, char **portp)
{
const char *hstart;
const char *pstart;
char * host;
char * port;
size_t hlen, plen;
if (pair[0] == '[') {
hstart = pair + 1;
hlen = 0;
while (hstart[hlen] != ']') {
if (hstart[hlen] == '\0') {
return (NNG_EADDRINVAL);
}
hlen++;
}
pstart = hstart + hlen + 1; // skip over the trailing ']'
} else {
// Normal thing.
hstart = pair;
hlen = 0;
while ((hstart[hlen] != ':') && (hstart[hlen] != '\0')) {
hlen++;
}
pstart = hstart + hlen;
}
if (pstart[0] == ':') {
pstart++;
}
plen = strlen(pstart);
host = NULL;
if (hostp) {
if ((hlen > 1) || ((hlen == 1) && (*hstart != '*'))) {
if ((host = nni_alloc(hlen + 1)) == NULL) {
return (NNG_ENOMEM);
}
memcpy(host, hstart, hlen);
host[hlen] = '\0';
}
}
port = NULL;
if ((plen != 0) && (portp)) {
if ((port = nni_strdup(pstart)) == NULL) {
nni_strfree(host);
return (NNG_ENOMEM);
}
}
if (hostp) {
*hostp = host;
}
if (portp) {
*portp = port;
}
return (0);
}
int
nni_tran_chkopt(const char *name, const void *v, size_t sz)
{
nni_transport *t;
int rv = NNG_ENOTSUP;
nni_mtx_lock(&nni_tran_lk);
NNI_LIST_FOREACH (&nni_tran_list, t) {
const nni_tran_ep * ep;
const nni_tran_ep_option *eo;
// Generally we look for endpoint options.
ep = t->t_tran.tran_ep;
for (eo = ep->ep_options; eo && eo->eo_name != NULL; eo++) {
if (strcmp(name, eo->eo_name) != 0) {
continue;
}
if (eo->eo_setopt == NULL) {
nni_mtx_unlock(&nni_tran_lk);
return (NNG_EREADONLY);
}
if ((rv = eo->eo_setopt(NULL, v, sz)) != 0) {
nni_mtx_unlock(&nni_tran_lk);
return (rv);
}
}
}
nni_mtx_unlock(&nni_tran_lk);
return (rv);
}
// nni_tran_sys_init initializes the entire transport subsystem, including
// each individual transport.
typedef int (*nni_tran_ctor)(void);
// These are just the statically compiled in constructors.
// In the future we might want to support dynamic additions.
static nni_tran_ctor nni_tran_ctors[] = {
#ifdef NNG_TRANSPORT_INPROC
nng_inproc_register,
#endif
#ifdef NNG_TRANSPORT_IPC
nng_ipc_register,
#endif
#ifdef NNG_TRANSPORT_TCP
nng_tcp_register,
#endif
#ifdef NNG_TRANSPORT_TLS
nng_tls_register,
#endif
#ifdef NNG_TRANSPORT_WS
nng_ws_register,
#endif
#ifdef NNG_TRANSPORT_WSS
nng_wss_register,
#endif
#ifdef NNG_TRANSPORT_ZEROTIER
nng_zt_register,
#endif
NULL,
};
int
nni_tran_sys_init(void)
{
int i;
nni_tran_inited = 1;
NNI_LIST_INIT(&nni_tran_list, nni_transport, t_node);
nni_mtx_init(&nni_tran_lk);
for (i = 0; nni_tran_ctors[i] != NULL; i++) {
int rv;
if ((rv = (nni_tran_ctors[i])()) != 0) {
nni_tran_sys_fini();
return (rv);
}
}
return (0);
}
// nni_tran_sys_fini finalizes the entire transport system, including all
// transports.
void
nni_tran_sys_fini(void)
{
nni_transport *t;
while ((t = nni_list_first(&nni_tran_list)) != NULL) {
nni_list_remove(&nni_tran_list, t);
t->t_tran.tran_fini();
NNI_FREE_STRUCT(t);
}
nni_mtx_fini(&nni_tran_lk);
nni_tran_inited = 0;
}
|