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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
/*
* Copyright (c)2013-2020 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2024-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
/**
* @file
*
* Network control interface
*/
#include <inttypes.h>
#include <sys/types.h>
#include "Node.hpp"
#include "Mutex.hpp"
#include "OSUtils.hpp"
#include "Debug.hpp"
#include "NodeService.hpp"
#include "VirtualTap.hpp"
#include "Events.hpp"
#include "ZeroTierSockets.h"
using namespace ZeroTier;
#ifdef SDK_JNI
#include <jni.h>
#endif
namespace ZeroTier
{
extern NodeService *service;
extern Mutex serviceLock;
extern void (*_userEventCallbackFunc)(void *);
extern uint8_t allowNetworkCaching;
extern uint8_t allowPeerCaching;
extern uint8_t allowLocalConf;
#ifdef SDK_JNI
// References to JNI objects and VM kept for future callbacks
JavaVM *jvm = NULL;
jobject objRef = NULL;
jmethodID _userCallbackMethodRef = NULL;
#endif
}
int zts_allow_network_caching(uint8_t allowed = 1)
{
Mutex::Lock _l(serviceLock);
if(!service) {
allowNetworkCaching = allowed;
return ZTS_ERR_OK;
}
return ZTS_ERR_SERVICE;
}
int zts_allow_peer_caching(uint8_t allowed = 1)
{
Mutex::Lock _l(serviceLock);
if(!service) {
allowPeerCaching = allowed;
return ZTS_ERR_OK;
}
return ZTS_ERR_SERVICE;
}
int zts_allow_local_conf(uint8_t allowed = 1)
{
Mutex::Lock _l(serviceLock);
if(!service) {
allowLocalConf = allowed;
return ZTS_ERR_OK;
}
return ZTS_ERR_SERVICE;
}
int zts_start(const char *path, void (*callback)(void *), uint16_t port)
{
Mutex::Lock _l(serviceLock);
_lwip_driver_init();
if (service || _getState(ZTS_STATE_NODE_RUNNING)) {
// Service is already initialized
return ZTS_ERR_SERVICE;
}
if (_getState(ZTS_STATE_FREE_CALLED)) {
// Stack (presumably lwIP) has been dismantled,
// an application restart is required now
return ZTS_ERR_SERVICE;
}
#ifdef SDK_JNI
_userEventCallbackFunc = callback;
#else
_userEventCallbackFunc = callback;
#endif
if (!_isCallbackRegistered()) {
// Must have a callback
return ZTS_ERR_ARG;
}
if (!path) {
return ZTS_ERR_ARG;
}
if (port < 0 || port > 0xFFFF) {
return ZTS_ERR_ARG;
}
serviceParameters *params = new serviceParameters();
params->port = port;
params->path = std::string(path);
if (params->path.length() == 0) {
return ZTS_ERR_ARG;
}
int err;
int retval = ZTS_ERR_OK;
_setState(ZTS_STATE_CALLBACKS_RUNNING);
_setState(ZTS_STATE_NODE_RUNNING);
// Start the ZT service thread
#if defined(__WINDOWS__)
HANDLE serviceThread = CreateThread(NULL, 0, _runNodeService, (void*)params, 0, NULL);
HANDLE callbackThread = CreateThread(NULL, 0, _runCallbacks, NULL, 0, NULL);
#else
pthread_t service_thread;
pthread_t callback_thread;
if ((err = pthread_create(&service_thread, NULL, _runNodeService, (void*)params)) != 0) {
retval = err;
}
if ((err = pthread_create(&callback_thread, NULL, _runCallbacks, NULL)) != 0) {
retval = err;
}
#endif
#if defined(__linux__)
pthread_setname_np(service_thread, ZTS_SERVICE_THREAD_NAME);
pthread_setname_np(callback_thread, ZTS_EVENT_CALLBACK_THREAD_NAME);
#endif
if (retval != ZTS_ERR_OK) {
_clrState(ZTS_STATE_CALLBACKS_RUNNING);
_clrState(ZTS_STATE_NODE_RUNNING);
_clearRegisteredCallback();
//delete params;
}
return retval;
}
#ifdef SDK_JNI
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_start(
JNIEnv *env, jobject thisObj, jstring path, jobject callback, jint port)
{
if (!path) {
return ZTS_ERR_ARG;
}
jclass eventListenerClass = env->GetObjectClass(callback);
if(eventListenerClass == NULL) {
DEBUG_ERROR("Couldn't find class for ZeroTierEventListener instance");
return ZTS_ERR_ARG;
}
jmethodID eventListenerCallbackMethod = env->GetMethodID(eventListenerClass, "onZeroTierEvent", "(JI)V");
if(eventListenerCallbackMethod == NULL) {
DEBUG_ERROR("Couldn't find onZeroTierEvent method");
return ZTS_ERR_ARG;
}
// Reference used for later calls
objRef = env->NewGlobalRef(callback);
_userCallbackMethodRef = eventListenerCallbackMethod;
const char* utf_string = env->GetStringUTFChars(path, NULL);
if (!utf_string) {
return ZTS_ERR_GENERAL;
}
// using _userCallbackMethodRef
int retval = zts_start(utf_string, NULL, port);
env->ReleaseStringUTFChars(path, utf_string);
return retval;
}
#endif
int zts_stop()
{
Mutex::Lock _l(serviceLock);
if (_canPerformServiceOperation()) {
_clrState(ZTS_STATE_NODE_RUNNING);
service->terminate();
#if defined(__WINDOWS__)
WSACleanup();
#endif
return ZTS_ERR_OK;
}
return ZTS_ERR_SERVICE;
}
#ifdef SDK_JNI
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_stop(
JNIEnv *env, jobject thisObj)
{
zts_stop();
}
#endif
int zts_restart()
{
serviceLock.lock();
// Store callback references
#ifdef SDK_JNI
static jmethodID _tmpUserCallbackMethodRef = _userCallbackMethodRef;
#else
void (*_tmpUserEventCallbackFunc)(void *);
_tmpUserEventCallbackFunc = _userEventCallbackFunc;
#endif
int userProvidedPort = 0;
std::string userProvidedPath;
if (service) {
userProvidedPort = service->_userProvidedPort;
userProvidedPath = service->_userProvidedPath;
}
// Stop the service
if (_canPerformServiceOperation()) {
_clrState(ZTS_STATE_NODE_RUNNING);
service->terminate();
#if defined(__WINDOWS__)
WSACleanup();
#endif
}
else {
serviceLock.unlock();
return ZTS_ERR_SERVICE;
}
// Start again with same parameters as initial call
serviceLock.unlock();
while (service) {
zts_delay_ms(ZTS_CALLBACK_PROCESSING_INTERVAL);
}
/* Some of the logic in Java_com_zerotier_libzt_ZeroTier_start
is replicated here */
#ifdef SDK_JNI
_userCallbackMethodRef = _tmpUserCallbackMethodRef;
return zts_start(userProvidedPath.c_str(), NULL, userProvidedPort);
#else
//return zts_start(userProvidedPath.c_str(), _tmpUserEventCallbackFunc, userProvidedPort);
#endif
}
#ifdef SDK_JNI
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_restart(
JNIEnv *env, jobject thisObj)
{
zts_restart();
}
#endif
int zts_free()
{
Mutex::Lock _l(serviceLock);
if (_getState(ZTS_STATE_FREE_CALLED)) {
return ZTS_ERR_SERVICE;
}
_setState(ZTS_STATE_FREE_CALLED);
return zts_stop();
// TODO: add stack shutdown logic
}
#ifdef SDK_JNI
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_free(
JNIEnv *env, jobject thisObj)
{
zts_free();
}
#endif
uint64_t zts_get_node_id()
{
Mutex::Lock _l(serviceLock);
if (!_canPerformServiceOperation()) {
return ZTS_ERR_OK; // Not really
}
return service->getNode()->address();
}
#ifdef SDK_JNI
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1node_1id(
JNIEnv *env, jobject thisObj)
{
return zts_get_node_id();
}
#endif
int zts_get_node_status()
{
Mutex::Lock _l(serviceLock);
// Don't check _canPerformServiceOperation() here.
return service
&& service->getNode()
&& service->getNode()->online() ? ZTS_EVENT_NODE_ONLINE : ZTS_EVENT_NODE_OFFLINE;
}
#ifdef SDK_JNI
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1node_1status(
JNIEnv *env, jobject thisObj)
{
return zts_get_node_status();
}
#endif
int zts_get_network_status(uint64_t networkId)
{
Mutex::Lock _l(serviceLock);
if (!networkId) {
return ZTS_ERR_ARG;
}
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
/*
TODO:
ZTS_EVENT_NETWORK_READY_IP4
ZTS_EVENT_NETWORK_READY_IP6
ZTS_EVENT_NETWORK_READY_IP4_IP6
*/
return ZTS_ERR_NO_RESULT;
}
#ifdef SDK_JNI
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1network_1status(
JNIEnv *env, jobject thisObj, jlong networkId)
{
return zts_get_network_status(networkId);
}
#endif
int zts_get_peer_status(uint64_t peerId)
{
Mutex::Lock _l(serviceLock);
int retval = ZTS_ERR_OK;
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
return service->getPeerStatus(peerId);
}
#ifdef SDK_JNI
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1peer_1status(
JNIEnv *env, jobject thisObj, jlong peerId)
{
return zts_get_peer_status(peerId);
}
#endif
#ifdef SDK_JNI
/*
* Called from Java, saves a static reference to the VM so it can be used
* later to call a user-specified callback method from C.
*/
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_init(
JNIEnv *env, jobject thisObj)
{
jint rs = env->GetJavaVM(&jvm);
return rs != JNI_OK ? ZTS_ERR_GENERAL : ZTS_ERR_OK;
}
#endif
int zts_join(const uint64_t nwid)
{
Mutex::Lock _l(serviceLock);
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
else {
service->join(nwid);
}
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_join(
JNIEnv *env, jobject thisObj, jlong nwid)
{
return zts_join((uint64_t)nwid);
}
#endif
int zts_leave(const uint64_t nwid)
{
Mutex::Lock _l(serviceLock);
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
else {
service->leave(nwid);
}
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_leave(
JNIEnv *env, jobject thisObj, jlong nwid)
{
return zts_leave((uint64_t)nwid);
}
#endif
int zts_leave_all()
{
Mutex::Lock _l(serviceLock);
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
else {
service->leaveAll();
}
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
#endif
int zts_orbit(uint64_t moonWorldId, uint64_t moonSeed)
{
Mutex::Lock _l(serviceLock);
void *tptr = NULL;
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
} else {
service->getNode()->orbit(tptr, moonWorldId, moonSeed);
}
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
#endif
int zts_deorbit(uint64_t moonWorldId)
{
Mutex::Lock _l(serviceLock);
void *tptr = NULL;
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
} else {
service->getNode()->deorbit(tptr, moonWorldId);
}
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
#endif
int zts_get_6plane_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
{
if (!addr || !nwid || !nodeId) {
return ZTS_ERR_ARG;
}
InetAddress _6planeAddr = InetAddress::makeIpv66plane(nwid,nodeId);
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
memcpy(in6->sin6_addr.s6_addr, _6planeAddr.rawIpData(), sizeof(struct in6_addr));
return ZTS_ERR_OK;
}
int zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
{
if (!addr || !nwid || !nodeId) {
return ZTS_ERR_ARG;
}
InetAddress _rfc4193Addr = InetAddress::makeIpv6rfc4193(nwid,nodeId);
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
memcpy(in6->sin6_addr.s6_addr, _rfc4193Addr.rawIpData(), sizeof(struct in6_addr));
return ZTS_ERR_OK;
}
uint64_t zts_generate_adhoc_nwid_from_range(uint16_t startPortOfRange, uint16_t endPortOfRange)
{
char nwidStr[INET6_ADDRSTRLEN];
sprintf(nwidStr, "ff%04x%04x000000", startPortOfRange, endPortOfRange);
return strtoull(nwidStr, NULL, 16);
}
int zts_get_peers(struct zts_peer_details *pds, uint32_t *num)
{
Mutex::Lock _l(serviceLock);
if (!pds || !num) {
return ZTS_ERR_ARG;
}
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
ZT_PeerList *pl = service->getNode()->peers();
if (pl) {
if (*num < pl->peerCount) {
service->getNode()->freeQueryResult((void *)pl);
return ZTS_ERR_ARG;
}
*num = pl->peerCount;
for(unsigned long i=0;i<pl->peerCount;++i) {
memcpy(&(pds[i]), &(pl->peers[i]), sizeof(struct zts_peer_details));
for (unsigned int j=0; j<pl->peers[i].pathCount; j++) {
memcpy(&(pds[i].paths[j].address),
&(pl->peers[i].paths[j].address), sizeof(struct sockaddr_storage));
}
}
}
service->getNode()->freeQueryResult((void *)pl);
return ZTS_ERR_OK;
}
#ifdef SDK_JNI
#endif
int zts_get_peer(struct zts_peer_details *pd, uint64_t peerId)
{
Mutex::Lock _l(serviceLock);
if (!pd || !peerId) {
return ZTS_ERR_ARG;
}
if (!_canPerformServiceOperation()) {
return ZTS_ERR_SERVICE;
}
ZT_PeerList *pl = service->getNode()->peers();
int retval = ZTS_ERR_NO_RESULT;
if (pl) {
for(unsigned long i=0;i<pl->peerCount;++i) {
if (pl->peers[i].address == peerId) {
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
for (unsigned int j=0; j<pl->peers[i].pathCount; j++) {
memcpy(&(pd->paths[j].address),
&(pl->peers[i].paths[j].address), sizeof(struct sockaddr_storage));
}
retval = ZTS_ERR_OK;
break;
}
}
}
service->getNode()->freeQueryResult((void *)pl);
return retval;
}
#ifdef SDK_JNI
#endif
void _get_network_details_helper(uint64_t nwid, struct zts_network_details *nd)
{
/*
socklen_t addrlen;
VirtualTap *tap = vtapMap[nwid];
nd->nwid = tap->_nwid;
nd->mtu = tap->_mtu;
// assigned addresses
nd->num_addresses = tap->_ips.size() < ZTS_MAX_ASSIGNED_ADDRESSES ? tap->_ips.size() : ZTS_MAX_ASSIGNED_ADDRESSES;
for (int j=0; j<nd->num_addresses; j++) {
addrlen = tap->_ips[j].isV4() ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
memcpy(&(nd->addr[j]), &(tap->_ips[j]), addrlen);
}
// routes
nd->num_routes = ZTS_MAX_NETWORK_ROUTES;;
service->getRoutes(nwid, (ZT_VirtualNetworkRoute*)&(nd->routes)[0], &(nd->num_routes));
*/
}
void _get_network_details(uint64_t nwid, struct zts_network_details *nd)
{
/*
_vtaps_lock.lock();
_get_network_details_helper(nwid, nd);
_vtaps_lock.unlock();
*/
}
void _get_all_network_details(struct zts_network_details *nds, int *num)
{
/*
_vtaps_lock.lock();
*num = vtapMap.size();
int idx = 0;
std::map<uint64_t, VirtualTap*>::iterator it;
for (it = vtapMap.begin(); it != vtapMap.end(); it++) {
_get_network_details(it->first, &nds[idx]);
idx++;
}
_vtaps_lock.unlock();
*/
}
int zts_get_network_details(uint64_t nwid, struct zts_network_details *nd)
{
/*
serviceLock.lock();
int retval = ZTS_ERR_OK;
if (!nd || nwid == 0) {
retval = ZTS_ERR_ARG;
}
if (!service || _freeHasBeenCalled || _serviceIsShuttingDown) {
retval = ZTS_ERR_SERVICE;
}
if (retval == ZTS_ERR_OK) {
_get_network_details(nwid, nd);
}
serviceLock.unlock();
return retval;
*/
return 0;
}
#ifdef SDK_JNI
#endif
int zts_get_all_network_details(struct zts_network_details *nds, int *num)
{
/*
serviceLock.lock();
int retval = ZTS_ERR_OK;
if (!nds || !num) {
retval = ZTS_ERR_ARG;
}
if (!service || _freeHasBeenCalled || _serviceIsShuttingDown) {
retval = ZTS_ERR_SERVICE;
}
if (retval == ZTS_ERR_OK) {
_get_all_network_details(nds, num);
}
serviceLock.unlock();
return retval;
*/
return 0;
}
#ifdef SDK_JNI
#endif
void zts_delay_ms(long interval_ms)
{
#if defined(__WINDOWS__)
Sleep(interval_ms);
#else
struct timespec sleepValue = {0};
sleepValue.tv_nsec = interval_ms * 500000;
nanosleep(&sleepValue, NULL);
#endif
}
|