forked from zerotier/libzt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeService.hpp
558 lines (432 loc) · 16.8 KB
/
NodeService.hpp
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
/*
* Copyright (c)2013-2021 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: 2026-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
*
* ZeroTier Node Service
*/
#ifndef ZTS_NODE_SERVICE_HPP
#define ZTS_NODE_SERVICE_HPP
#define ZTS_UNUSED_ARG(x) (void)x
#include "Binder.hpp"
#include "Mutex.hpp"
#include "Node.hpp"
#include "Phy.hpp"
#include "PortMapper.hpp"
#include "ZeroTierSockets.h"
#include "version.h"
#include <string>
#include <vector>
#define ZTS_SERVICE_THREAD_NAME "ZTServiceThread"
#define ZTS_EVENT_CALLBACK_THREAD_NAME "ZTEventCallbackThread"
// Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and
// also bridged via ZeroTier to the same LAN traffic will (if the OS is sane)
// prefer WiFi.
#define ZT_IF_METRIC 5000
// How often to check for new multicast subscriptions on a tap device
#define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
// How often to check for local interface addresses
#define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
// Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs
#define ZT_TCP_FALLBACK_AFTER 30000
// Fake TLS hello for TCP tunnel outgoing connections (TUNNELED mode)
static const char ZT_TCP_TUNNEL_HELLO[9] = { 0x17,
0x03,
0x03,
0x00,
0x04,
(char)ZEROTIER_ONE_VERSION_MAJOR,
(char)ZEROTIER_ONE_VERSION_MINOR,
(char)((ZEROTIER_ONE_VERSION_REVISION >> 8) & 0xff),
(char)(ZEROTIER_ONE_VERSION_REVISION & 0xff) };
#ifdef __WINDOWS__
#include <windows.h>
#endif
namespace ZeroTier {
class NodeService;
struct InetAddress;
class VirtualTap;
class MAC;
class Events;
/**
* A TCP connection and related state and buffers
*/
struct TcpConnection {
enum {
TCP_UNCATEGORIZED_INCOMING, // uncategorized incoming connection
TCP_HTTP_INCOMING,
TCP_HTTP_OUTGOING,
TCP_TUNNEL_OUTGOING // TUNNELED mode proxy outbound connection
} type;
NodeService* parent;
PhySocket* sock;
InetAddress remoteAddr;
uint64_t lastReceive;
std::string readq;
std::string writeq;
Mutex writeq_m;
};
/**
* ZeroTier node service
*/
class NodeService {
public:
/**
* Returned by node main if/when it terminates
*/
enum ReasonForTermination {
/**
* Instance is still running
*/
ONE_STILL_RUNNING = 0,
/**
* Normal shutdown
*/
ONE_NORMAL_TERMINATION = 1,
/**
* A serious unrecoverable error has occurred
*/
ONE_UNRECOVERABLE_ERROR = 2,
/**
* Your identity has collided with another
*/
ONE_IDENTITY_COLLISION = 3
};
/**
* Local settings for each network
*/
struct NetworkSettings {
/**
* Allow this network to configure IP addresses and routes?
*/
bool allowManaged;
/**
* Whitelist of addresses that can be configured by this network.
* If empty and allowManaged is true, allow all
* private/pseudoprivate addresses.
*/
std::vector<InetAddress> allowManagedWhitelist;
/**
* Allow configuration of IPs and routes within global (Internet) IP
* space?
*/
bool allowGlobal;
/**
* Allow overriding of system default routes for "full tunnel"
* operation?
*/
bool allowDefault;
};
Phy<NodeService*> _phy;
Node* _node;
uint64_t _nodeId;
unsigned int _primaryPort;
unsigned int _secondaryPort;
unsigned int _tertiaryPort;
unsigned int _randomPortRangeStart;
unsigned int _randomPortRangeEnd;
volatile unsigned int _udpPortPickerCounter;
std::map<uint64_t, unsigned int> peerCache;
// Local configuration and memo-ized information from it
Hashtable<uint64_t, std::vector<InetAddress> > _v4Hints;
Hashtable<uint64_t, std::vector<InetAddress> > _v6Hints;
Hashtable<uint64_t, std::vector<InetAddress> > _v4Blacklists;
Hashtable<uint64_t, std::vector<InetAddress> > _v6Blacklists;
std::vector<InetAddress> _globalV4Blacklist;
std::vector<InetAddress> _globalV6Blacklist;
std::vector<InetAddress> _allowManagementFrom;
std::vector<std::string> _interfacePrefixBlacklist;
Mutex _localConfig_m;
std::vector<InetAddress> explicitBind;
/*
* To attempt to handle NAT/gateway craziness we use three local UDP
* ports:
*
* [0] is the normal/default port, usually 9993
* [1] is a port derived from our ZeroTier address
* [2] is a port computed from the normal/default for use with
* uPnP/NAT-PMP mappings
*
* [2] exists because on some gateways trying to do regular NAT-t
* interferes destructively with uPnP port mapping behavior in very
* weird buggy ways. It's only used if uPnP/NAT-PMP is enabled in this
* build.
*/
unsigned int _ports[3] = { 0 };
Binder _binder;
// Time we last received a packet from a global address
uint64_t _lastDirectReceiveFromGlobal;
InetAddress _fallbackRelayAddress;
bool _allowTcpRelay;
bool _forceTcpRelay;
uint64_t _lastSendToGlobalV4;
// Active TCP/IP connections
std::vector<TcpConnection*> _tcpConnections;
Mutex _tcpConnections_m;
TcpConnection* _tcpFallbackTunnel;
// Last potential sleep/wake event
uint64_t _lastRestart;
// Deadline for the next background task service function
volatile int64_t _nextBackgroundTaskDeadline;
// Configured networks
struct NetworkState {
NetworkState() : tap((VirtualTap*)0)
{
// Real defaults are in network 'up' code in network event
// handler
settings.allowManaged = true;
settings.allowGlobal = false;
settings.allowDefault = false;
}
VirtualTap* tap;
ZT_VirtualNetworkConfig config; // memcpy() of raw config from core
std::vector<InetAddress> managedIps;
NetworkSettings settings;
};
std::map<uint64_t, NetworkState> _nets;
/** Lock to control access to network configuration data */
Mutex _nets_m;
/** Lock to control access to storage data */
Mutex _store_m;
/** Lock to control access to service run state */
Mutex _run_m;
// Set to false to force service to stop
volatile bool _run;
/** Lock to control access to termination reason */
Mutex _termReason_m;
// Termination status information
ReasonForTermination _termReason;
std::string _fatalErrorMessage;
// uPnP/NAT-PMP port mapper if enabled
bool _allowPortMapping;
#ifdef ZT_USE_MINIUPNPC
PortMapper* _portMapper;
#endif
bool _allowSecondaryPort;
uint8_t _allowNetworkCaching;
uint8_t _allowPeerCaching;
uint8_t _allowIdentityCaching;
uint8_t _allowRootSetCaching;
char _publicIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 };
char _secretIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 };
bool _userDefinedWorld;
char _rootsData[ZTS_STORE_DATA_LEN] = { 0 };
int _rootsDataLen = 0;
/** Whether the node has successfully come online */
bool _nodeIsOnline;
/** Whether we allow the NodeService to generate events for the user */
bool _eventsEnabled;
/** Storage path defined by the user */
std::string _homePath;
/** System to ingest events from this class and emit them to the user */
Events* _events;
NodeService();
~NodeService();
/** Main service loop */
ReasonForTermination run();
ReasonForTermination reasonForTermination() const;
std::string fatalErrorMessage() const;
/** Stop the node and service */
void terminate();
/** Apply or update managed IPs for a configured network */
void syncManagedStuff(NetworkState& n);
void phyOnDatagram(
PhySocket* sock,
void** uptr,
const struct sockaddr* localAddr,
const struct sockaddr* from,
void* data,
unsigned long len);
void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success);
int nodeVirtualNetworkConfigFunction(
uint64_t net_id,
void** nuptr,
enum ZT_VirtualNetworkConfigOperation op,
const ZT_VirtualNetworkConfig* nwc);
void nodeEventCallback(enum ZT_Event event, const void* metaData);
zts_net_info_t* prepare_network_details_msg(const NetworkState& n);
void generateSyntheticEvents();
void sendEventToUser(unsigned int zt_event_code, const void* obj, unsigned int len = 0);
/** Join a network */
int join(uint64_t net_id);
/** Leave a network */
int leave(uint64_t net_id);
/** Return whether the network is ready for transport services */
bool networkIsReady(uint64_t net_id) const;
/** Lock the service so we can perform queries */
void obtainLock() const;
/** Unlock the service */
void releaseLock() const;
/** Return number of assigned addresses on the network. Service must be locked. */
int addressCount(uint64_t net_id) const;
/** Return number of managed routes on the network. Service must be locked. */
int routeCount(uint64_t net_id) const;
/** Return number of multicast subscriptions on the network. Service must be locked. */
int multicastSubCount(uint64_t net_id) const;
/** Return number of known physical paths to the peer. Service must be locked. */
int pathCount(uint64_t peer_id) const;
int getAddrAtIdx(uint64_t net_id, unsigned int idx, char* dst, unsigned int len);
int getRouteAtIdx(
uint64_t net_id,
unsigned int idx,
char* target,
char* via,
unsigned int len,
uint16_t* flags,
uint16_t* metric);
int getMulticastSubAtIdx(uint64_t net_id, unsigned int idx, uint64_t* mac, uint32_t* adi);
int getPathAtIdx(uint64_t peer_id, unsigned int idx, char* path, unsigned int len);
/** Orbit a moon */
int orbit(uint64_t moonWorldId, uint64_t moonSeed);
/** De-orbit a moon */
int deorbit(uint64_t moonWorldId);
/** Return the integer-form of the node's identity */
uint64_t getNodeId();
/** Gets the node's identity */
int getIdentity(char* keypair, unsigned int* len);
/** Set the node's identity */
int setIdentity(const char* keypair, unsigned int len);
void nodeStatePutFunction(enum ZT_StateObjectType type, const uint64_t id[2], const void* data, unsigned int len);
int nodeStateGetFunction(enum ZT_StateObjectType type, const uint64_t id[2], void* data, unsigned int maxlen);
int nodeWirePacketSendFunction(
const int64_t localSocket,
const struct sockaddr_storage* addr,
const void* data,
unsigned int len,
unsigned int ttl);
void nodeVirtualNetworkFrameFunction(
uint64_t net_id,
void** nuptr,
uint64_t sourceMac,
uint64_t destMac,
unsigned int etherType,
unsigned int vlanId,
const void* data,
unsigned int len);
int nodePathCheckFunction(uint64_t ztaddr, const int64_t localSocket, const struct sockaddr_storage* remoteAddr);
int nodePathLookupFunction(uint64_t ztaddr, unsigned int family, struct sockaddr_storage* result);
void tapFrameHandler(
uint64_t net_id,
const MAC& from,
const MAC& to,
unsigned int etherType,
unsigned int vlanId,
const void* data,
unsigned int len);
int shouldBindInterface(const char* ifname, const InetAddress& ifaddr);
unsigned int _getRandomPort(unsigned int minPort, unsigned int maxPort);
int _trialBind(unsigned int port);
/** Return whether the NodeService is running */
int isRunning() const;
/** Return whether the node is online */
int nodeIsOnline() const;
/** Instruct the NodeService on where to look for identity files and caches */
int setHomePath(const char* homePath);
/** Set the primary port */
int setPrimaryPort(unsigned short primaryPort);
/** Set random range to select backup ports from */
int setRandomPortRange(unsigned short startPort, unsigned short endPort);
/** Get the primary port */
unsigned short getPrimaryPort() const;
/** Allow or disallow port-mapping */
int allowPortMapping(unsigned int allowed);
/** Allow or disallow backup port */
int allowSecondaryPort(unsigned int allowed);
/** Set the event system instance used to convey messages to the user */
int setUserEventSystem(Events* events);
/** Set the address and port for the tcp relay that ZeroTier should use */
void setTcpRelayAddress(const char* tcpRelayAddr, unsigned short tcpRelayPort);
/** Allow ZeroTier to use the TCP relay */
void allowTcpRelay(bool enabled);
/** Force ZeroTier to only use the the TCP relay */
void forceTcpRelay(bool enabled);
void enableEvents();
/** Set the roots definition */
int setRoots(const void* data, unsigned int len);
/** Enable or disable low-bandwidth mode (sends less ambient traffic, network updates happen less frequently) */
int setLowBandwidthMode(bool enabled);
/** Add Interface prefix to blacklist (prevents ZeroTier from using that interface) */
int addInterfacePrefixToBlacklist(const char* prefix, unsigned int len);
/** Return the MAC Address of the node in the given network */
uint64_t getMACAddress(uint64_t net_id) const;
/** Get the string format name of a network */
int getNetworkName(uint64_t net_id, char* dst, unsigned int len) const;
/** Allow ZeroTier to cache peer hints to storage */
int allowPeerCaching(unsigned int allowed);
/** Allow ZeroTier to cache network info to storage */
int allowNetworkCaching(unsigned int allowed);
/** Allow ZeroTier to write identities to storage */
int allowIdentityCaching(unsigned int allowed);
/** Allow ZeroTier to cache root definitions to storage */
int allowRootSetCaching(unsigned int allowed);
/** Return whether broadcast is enabled on the given network */
int getNetworkBroadcast(uint64_t net_id);
/** Return the MTU of the given network */
int getNetworkMTU(uint64_t net_id);
/** Return whether the network is public or private */
int getNetworkType(uint64_t net_id);
/** Return the status of the network join */
int getNetworkStatus(uint64_t net_id);
/** Get the first address assigned by the network */
int getFirstAssignedAddr(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr);
/** Get an array of assigned addresses for the given network */
int getAllAssignedAddr(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count);
/** Return whether a managed route of the given family has been assigned by the network */
int networkHasRoute(uint64_t net_id, unsigned int family);
/** Return whether an address of the given family has been assigned by the network */
int addrIsAssigned(uint64_t net_id, unsigned int family);
void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from)
{
ZTS_UNUSED_ARG(sockL);
ZTS_UNUSED_ARG(sockN);
ZTS_UNUSED_ARG(uptrL);
ZTS_UNUSED_ARG(uptrN);
ZTS_UNUSED_ARG(from);
}
void phyOnTcpClose(PhySocket* sock, void** uptr);
void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len);
void phyOnTcpWritable(PhySocket* sock, void** uptr);
void phyOnFileDescriptorActivity(PhySocket* sock, void** uptr, bool readable, bool writable)
{
ZTS_UNUSED_ARG(sock);
ZTS_UNUSED_ARG(uptr);
ZTS_UNUSED_ARG(readable);
ZTS_UNUSED_ARG(writable);
}
void phyOnUnixAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN)
{
ZTS_UNUSED_ARG(sockL);
ZTS_UNUSED_ARG(sockN);
ZTS_UNUSED_ARG(uptrL);
ZTS_UNUSED_ARG(uptrN);
}
void phyOnUnixClose(PhySocket* sock, void** uptr)
{
ZTS_UNUSED_ARG(sock);
ZTS_UNUSED_ARG(uptr);
}
void phyOnUnixData(PhySocket* sock, void** uptr, void* data, unsigned long len)
{
ZTS_UNUSED_ARG(sock);
ZTS_UNUSED_ARG(uptr);
ZTS_UNUSED_ARG(data);
ZTS_UNUSED_ARG(len);
}
void phyOnUnixWritable(PhySocket* sock, void** uptr)
{
ZTS_UNUSED_ARG(sock);
ZTS_UNUSED_ARG(uptr);
}
};
} // namespace ZeroTier
#endif