-
Notifications
You must be signed in to change notification settings - Fork 55
/
ZeroTierSockets.h
3136 lines (2807 loc) · 104 KB
/
ZeroTierSockets.h
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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
*
* This defines the external C API for ZeroTier Sockets
*/
#ifndef ZTS_SOCKETS_H
#define ZTS_SOCKETS_H
#ifdef __cplusplus
extern "C" {
#endif
//----------------------------------------------------------------------------//
// Error codes //
//----------------------------------------------------------------------------//
/** Common error return values */
typedef enum {
/** No error */
ZTS_ERR_OK = 0,
/** Socket error, see `zts_errno` */
ZTS_ERR_SOCKET = -1,
/** This operation is not allowed at this time. Or possibly the node hasn't been started */
ZTS_ERR_SERVICE = -2,
/** Invalid argument */
ZTS_ERR_ARG = -3,
/** No result (not necessarily an error) */
ZTS_ERR_NO_RESULT = -4,
/** Consider filing a bug report */
ZTS_ERR_GENERAL = -5
} zts_error_t;
//----------------------------------------------------------------------------//
// Event codes //
//----------------------------------------------------------------------------//
/** Event codes used by the (optional) callback API */
typedef enum {
/**
* Node has been initialized
*
* This is the first event generated, and is always sent. It may occur
* before node's constructor returns.
*
*/
ZTS_EVENT_NODE_UP = 200,
/**
* Node is online -- at least one upstream node appears reachable
*
*/
ZTS_EVENT_NODE_ONLINE = 201,
/**
* Node is offline -- network does not seem to be reachable by any available
* strategy
*
*/
ZTS_EVENT_NODE_OFFLINE = 202,
/**
* Node is shutting down
*
* This is generated within Node's destructor when it is being shut down.
* It's done for convenience, since cleaning up other state in the event
* handler may appear more idiomatic.
*
*/
ZTS_EVENT_NODE_DOWN = 203,
/**
* A fatal error has occurred. One possible reason is:
*
* Your identity has collided with another node's ZeroTier address
*
* This happens if two different public keys both hash (via the algorithm
* in Identity::generate()) to the same 40-bit ZeroTier address.
*
* This is something you should "never" see, where "never" is defined as
* once per 2^39 new node initializations / identity creations. If you do
* see it, you're going to see it very soon after a node is first
* initialized.
*
* This is reported as an event rather than a return code since it's
* detected asynchronously via error messages from authoritative nodes.
*
* If this occurs, you must shut down and delete the node, delete the
* identity.secret record/file from the data store, and restart to generate
* a new identity. If you don't do this, you will not be able to communicate
* with other nodes.
*
* We'd automate this process, but we don't think silently deleting
* private keys or changing our address without telling the calling code
* is good form. It violates the principle of least surprise.
*
* You can technically get away with not handling this, but we recommend
* doing so in a mature reliable application. Besides, handling this
* condition is a good way to make sure it never arises. It's like how
* umbrellas prevent rain and smoke detectors prevent fires. They do, right?
*
* Meta-data: none
*/
ZTS_EVENT_NODE_FATAL_ERROR = 204,
/** Network ID does not correspond to a known network */
ZTS_EVENT_NETWORK_NOT_FOUND = 210,
/** The version of ZeroTier inside libzt is too old */
ZTS_EVENT_NETWORK_CLIENT_TOO_OLD = 211,
/** The configuration for a network has been requested (no action needed) */
ZTS_EVENT_NETWORK_REQ_CONFIG = 212,
/** The node joined the network successfully (no action needed) */
ZTS_EVENT_NETWORK_OK = 213,
/** The node is not allowed to join the network (you must authorize node) */
ZTS_EVENT_NETWORK_ACCESS_DENIED = 214,
/** The node has received an IPv4 address from the network controller */
ZTS_EVENT_NETWORK_READY_IP4 = 215,
/** The node has received an IPv6 address from the network controller */
ZTS_EVENT_NETWORK_READY_IP6 = 216,
/** Deprecated */
ZTS_EVENT_NETWORK_READY_IP4_IP6 = 217,
/** Network controller is unreachable */
ZTS_EVENT_NETWORK_DOWN = 218,
/** Network change received from controller */
ZTS_EVENT_NETWORK_UPDATE = 219,
/** TCP/IP stack (lwIP) is up (for debug purposes) */
ZTS_EVENT_STACK_UP = 220,
/** TCP/IP stack (lwIP) id down (for debug purposes) */
ZTS_EVENT_STACK_DOWN = 221,
/** lwIP netif up (for debug purposes) */
ZTS_EVENT_NETIF_UP = 230,
/** lwIP netif down (for debug purposes) */
ZTS_EVENT_NETIF_DOWN = 231,
/** lwIP netif removed (for debug purposes) */
ZTS_EVENT_NETIF_REMOVED = 232,
/** lwIP netif link up (for debug purposes) */
ZTS_EVENT_NETIF_LINK_UP = 233,
/** lwIP netif link down (for debug purposes) */
ZTS_EVENT_NETIF_LINK_DOWN = 234,
/** A direct P2P path to peer is known */
ZTS_EVENT_PEER_DIRECT = 240,
/** A direct P2P path to peer is NOT known. Traffic is now relayed */
ZTS_EVENT_PEER_RELAY = 241,
/** A peer is unreachable. Check NAT/Firewall settings */
ZTS_EVENT_PEER_UNREACHABLE = 242,
/** A new path to a peer was discovered */
ZTS_EVENT_PEER_PATH_DISCOVERED = 243,
/** A known path to a peer is now considered dead */
ZTS_EVENT_PEER_PATH_DEAD = 244,
/** A new managed network route was added */
ZTS_EVENT_ROUTE_ADDED = 250,
/** A managed network route was removed */
ZTS_EVENT_ROUTE_REMOVED = 251,
/** A new managed IPv4 address was assigned to this peer */
ZTS_EVENT_ADDR_ADDED_IP4 = 260,
/** A managed IPv4 address assignment was removed from this peer */
ZTS_EVENT_ADDR_REMOVED_IP4 = 261,
/** A new managed IPv4 address was assigned to this peer */
ZTS_EVENT_ADDR_ADDED_IP6 = 262,
/** A managed IPv6 address assignment was removed from this peer */
ZTS_EVENT_ADDR_REMOVED_IP6 = 263,
/** The node's secret key (identity) */
ZTS_EVENT_STORE_IDENTITY_SECRET = 270,
/** The node's public key (identity) */
ZTS_EVENT_STORE_IDENTITY_PUBLIC = 271,
/** The node has received an updated planet config */
ZTS_EVENT_STORE_PLANET = 272,
/** New reachability hints and peer configuration */
ZTS_EVENT_STORE_PEER = 273,
/** New network config */
ZTS_EVENT_STORE_NETWORK = 274
} zts_event_t;
//----------------------------------------------------------------------------//
// zts_errno Error codes //
//----------------------------------------------------------------------------//
/**
* Error variable set after each `zts_*` socket call. Provides additional error context.
*/
extern int zts_errno;
typedef enum {
/** Operation not permitted */
ZTS_EPERM = 1,
/** No such file or directory */
ZTS_ENOENT = 2,
/** No such process */
ZTS_ESRCH = 3,
/** Interrupted system call */
ZTS_EINTR = 4,
/** I/O error */
ZTS_EIO = 5,
/** No such device or address */
ZTS_ENXIO = 6,
/** Bad file number */
ZTS_EBADF = 9,
/** Try again */
ZTS_EAGAIN = 11,
/** Operation would block */
ZTS_EWOULDBLOCK = ZTS_EAGAIN,
/** Out of memory */
ZTS_ENOMEM = 12,
/** Permission denied */
ZTS_EACCES = 13,
/** Bad address */
ZTS_EFAULT = 14,
/** Device or resource busy */
ZTS_EBUSY = 16,
/** File exists */
ZTS_EEXIST = 17,
/** No such device */
ZTS_ENODEV = 19,
/** Invalid argument */
ZTS_EINVAL = 22,
/** File table overflow */
ZTS_ENFILE = 23,
/** Too many open files */
ZTS_EMFILE = 24,
/** Function not implemented */
ZTS_ENOSYS = 38,
/** Socket operation on non-socket */
ZTS_ENOTSOCK = 88,
/** Destination address required */
ZTS_EDESTADDRREQ = 89,
/** Message too long */
ZTS_EMSGSIZE = 90,
/** Protocol wrong type for socket */
ZTS_EPROTOTYPE = 91,
/** Protocol not available */
ZTS_ENOPROTOOPT = 92,
/** Protocol not supported */
ZTS_EPROTONOSUPPORT = 93,
/** Socket type not supported */
ZTS_ESOCKTNOSUPPORT = 94,
/** Operation not supported on transport endpoint */
ZTS_EOPNOTSUPP = 95,
/** Protocol family not supported */
ZTS_EPFNOSUPPORT = 96,
/** Address family not supported by protocol */
ZTS_EAFNOSUPPORT = 97,
/** Address already in use */
ZTS_EADDRINUSE = 98,
/** Cannot assign requested address */
ZTS_EADDRNOTAVAIL = 99,
/** Network is down */
ZTS_ENETDOWN = 100,
/** Network is unreachable */
ZTS_ENETUNREACH = 101,
/** Software caused connection abort */
ZTS_ECONNABORTED = 103,
/** Connection reset by peer */
ZTS_ECONNRESET = 104,
/** No buffer space available */
ZTS_ENOBUFS = 105,
/** Transport endpoint is already connected */
ZTS_EISCONN = 106,
/** Transport endpoint is not connected */
ZTS_ENOTCONN = 107,
/** Connection timed out */
ZTS_ETIMEDOUT = 110,
/* Connection refused */
ZTS_ECONNREFUSED = 111,
/** No route to host */
ZTS_EHOSTUNREACH = 113,
/** Operation already in progress */
ZTS_EALREADY = 114,
/** Operation now in progress */
ZTS_EINPROGRESS = 115
} zts_errno_t;
//----------------------------------------------------------------------------//
// Misc definitions //
//----------------------------------------------------------------------------//
/**
* Length of human-readable MAC address string
*/
#define ZTS_MAC_ADDRSTRLEN 18
/**
* Max length of human-readable IPv4 string
*/
#define ZTS_INET_ADDRSTRLEN 16
/**
* Max length of human-readable IPv6 string
*/
#define ZTS_INET6_ADDRSTRLEN 46
/**
* Maximum (and required) length of string buffers used to receive
* string-format IP addresses from the API. This is set to `ZTS_INET6_ADDRSTRLEN`
* to handle all cases: `ZTS_AF_INET` and `ZTS_AF_INET6`
*/
#define ZTS_IP_MAX_STR_LEN ZTS_INET6_ADDRSTRLEN
/**
* Required buffer length to safely receive data store items
*/
#define ZTS_STORE_DATA_LEN 4096
/**
* Maximum length of network short name
*/
#define ZTS_MAX_NETWORK_SHORT_NAME_LENGTH 127
/**
* Maximum number of pushed routes on a network
*/
#define ZTS_MAX_NETWORK_ROUTES 32
/**
* Maximum number of statically assigned IP addresses per network endpoint
* using ZT address management (not DHCP)
*/
#define ZTS_MAX_ASSIGNED_ADDRESSES 16
/**
* Maximum number of direct network paths to a given peer
*/
#define ZTS_MAX_PEER_NETWORK_PATHS 16
/**
* Maximum number of multicast groups a device / network interface can be
* subscribed to at once
*/
#define ZTS_MAX_MULTICAST_SUBSCRIPTIONS 1024
#define ZTS_MAX_ENDPOINT_STR_LEN ZTS_INET6_ADDRSTRLEN + 6
//----------------------------------------------------------------------------//
// Misc //
//----------------------------------------------------------------------------//
#if ! defined(ZTS_ENABLE_PYTHON) && ! defined(ZTS_ENABLE_PINVOKE) && ! defined(ZTS_ENABLE_JAVA)
#define ZTS_C_API_ONLY 1
#endif
#if ! ZTS_NO_STDINT_H
#include <stdint.h>
#endif
#if defined(_MSC_VER)
#ifndef ssize_t
// TODO: Should be SSIZE_T, would require lwIP patch
// #include <BaseTsd.h>
// typedef SSIZE_T ssize_t;
typedef int ssize_t;
#endif
#else
#include <unistd.h>
#endif
#ifdef ZTS_ENABLE_PINVOKE
// Used by P/INVOKE wrappers
typedef void (*CppCallback)(void* msg);
#endif
//----------------------------------------------------------------------------//
// Common definitions and structures for interoperability between zts_* and //
// lwIP functions. Some of the code in the following section is a borrowed //
// from the lwIP codebase so that the user doesn't need to include headers //
// from that project in addition to the ZeroTier SDK headers. The license //
// applying to this code borrowed from lwIP is produced below and only //
// applies to the portions of code which are merely renamed versions of //
// their lwIP counterparts. The rest of the code in this C API file is //
// governed by the license text provided at the beginning of this file. //
//----------------------------------------------------------------------------//
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
/** 255.255.255.255 */
#define ZTS_IPADDR_NONE ((uint32_t)0xffffffffUL)
/** 127.0.0.1 */
#define ZTS_IPADDR_LOOPBACK ((uint32_t)0x7f000001UL)
/** 0.0.0.0 */
#define ZTS_IPADDR_ANY ((uint32_t)0x00000000UL)
/** 255.255.255.255 */
#define ZTS_IPADDR_BROADCAST ((uint32_t)0xffffffffUL)
/** 255.255.255.255 */
#define ZTS_INADDR_NONE ZTS_IPADDR_NONE
/** 127.0.0.1 */
#define ZTS_INADDR_LOOPBACK ZTS_IPADDR_LOOPBACK
/** 0.0.0.0 */
#define ZTS_INADDR_ANY ZTS_IPADDR_ANY
/** 255.255.255.255 */
#define ZTS_INADDR_BROADCAST ZTS_IPADDR_BROADCAST
// Socket protocol types
#define ZTS_SOCK_STREAM 0x0001
#define ZTS_SOCK_DGRAM 0x0002
#define ZTS_SOCK_RAW 0x0003
// Socket family types
#define ZTS_AF_UNSPEC 0x0000
#define ZTS_AF_INET 0x0002
#define ZTS_AF_INET6 0x000a
#define ZTS_PF_INET ZTS_AF_INET
#define ZTS_PF_INET6 ZTS_AF_INET6
#define ZTS_PF_UNSPEC ZTS_AF_UNSPEC
// Protocol command types
#define ZTS_IPPROTO_IP 0x0000
#define ZTS_IPPROTO_ICMP 0x0001
#define ZTS_IPPROTO_TCP 0x0006
#define ZTS_IPPROTO_UDP 0x0011
#define ZTS_IPPROTO_IPV6 0x0029
#define ZTS_IPPROTO_ICMPV6 0x003a
#define ZTS_IPPROTO_UDPLITE 0x0088
#define ZTS_IPPROTO_RAW 0x00ff
// send() and recv() flags
#define ZTS_MSG_PEEK 0x0001
#define ZTS_MSG_WAITALL 0x0002 // NOT YET SUPPORTED
#define ZTS_MSG_OOB 0x0004 // NOT YET SUPPORTED
#define ZTS_MSG_DONTWAIT 0x0008
#define ZTS_MSG_MORE 0x0010
// Macro's for defining ioctl() command values
#define ZTS_IOCPARM_MASK 0x7fU
#define ZTS_IOC_VOID 0x20000000UL
#define ZTS_IOC_OUT 0x40000000UL
#define ZTS_IOC_IN 0x80000000UL
#define ZTS_IOC_INOUT (ZTS_IOC_IN | ZTS_IOC_OUT)
#define ZTS_IO(x, y) (ZTS_IOC_VOID | ((x) << 8) | (y))
#define ZTS_IOR(x, y, t) (ZTS_IOC_OUT | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))
#define ZTS_IOW(x, y, t) (ZTS_IOC_IN | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))
// ioctl() commands
#define ZTS_FIONREAD ZTS_IOR('f', 127, unsigned long)
#define ZTS_FIONBIO ZTS_IOW('f', 126, unsigned long)
//----------------------------------------------------------------------------//
// Custom but still mostly standard socket interface structures //
//----------------------------------------------------------------------------//
typedef uint32_t zts_socklen_t;
typedef uint32_t zts_in_addr_t;
typedef uint16_t zts_in_port_t;
typedef uint8_t zts_sa_family_t;
struct zts_in_addr {
#if defined(_WIN32)
zts_in_addr_t S_addr;
#else
// A definition in winsock may conflict with s_addr
zts_in_addr_t s_addr;
#endif
};
struct zts_in6_addr {
union un {
uint32_t u32_addr[4];
uint8_t u8_addr[16];
} un;
//#define s6_addr un.u8_addr
};
/**
* Address structure to specify an IPv4 endpoint
*/
struct zts_sockaddr_in {
uint8_t sin_len;
zts_sa_family_t sin_family;
zts_in_port_t sin_port;
struct zts_in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
};
/**
* Address structure to specify an IPv6 endpoint
*/
struct zts_sockaddr_in6 {
uint8_t sin6_len; // length of this structure
zts_sa_family_t sin6_family; // ZTS_AF_INET6
zts_in_port_t sin6_port; // Transport layer port #
uint32_t sin6_flowinfo; // IPv6 flow information
struct zts_in6_addr sin6_addr; // IPv6 address
uint32_t sin6_scope_id; // Set of interfaces for scope
};
/**
* Pointers to socket address structures are often cast to this type
*/
struct zts_sockaddr {
uint8_t sa_len;
zts_sa_family_t sa_family;
char sa_data[14];
};
/**
* Address structure large enough to hold IPv4 and IPv6 addresses
*/
struct zts_sockaddr_storage {
uint8_t s2_len;
zts_sa_family_t ss_family;
char s2_data1[2];
uint32_t s2_data2[3];
uint32_t s2_data3[3];
};
//----------------------------------------------------------------------------//
// Callback Structures //
//----------------------------------------------------------------------------//
/**
* Runtime details about the current node
*/
typedef struct {
/**
* Node ID
*/
uint64_t node_id;
/**
* Port used by ZeroTier to send and receive traffic
*/
uint16_t port_primary;
/**
* Port used by ZeroTier to send and receive traffic
*/
uint16_t port_secondary;
/**
* Port used by ZeroTier to send and receive traffic
*/
uint16_t port_tertiary;
/**
* ZT Major version
*/
uint8_t ver_major;
/**
* ZT Minor version
*/
uint8_t ver_minor;
/**
* ZT Patch revision
*/
uint8_t ver_rev;
} zts_node_info_t;
/**
* Details about an assigned address that was added or removed
*/
typedef struct {
uint64_t net_id;
struct zts_sockaddr_storage addr;
} zts_addr_info_t;
/**
* Virtual network status codes
*/
typedef enum {
/**
* Waiting for network configuration (also means revision == 0)
*/
ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
/**
* Configuration received and we are authorized
*/
ZTS_NETWORK_STATUS_OK = 1,
/**
* Netconf master told us 'nope'
*/
ZTS_NETWORK_STATUS_ACCESS_DENIED = 2,
/**
* Netconf master exists, but this virtual network does not
*/
ZTS_NETWORK_STATUS_NOT_FOUND = 3,
/**
* Initialization of network failed or other internal error
*/
ZTS_NETWORK_STATUS_PORT_ERROR = 4,
/**
* ZeroTier core version too old
*/
ZTS_NETWORK_STATUS_CLIENT_TOO_OLD = 5
} zts_network_status_t;
/**
* Virtual network type codes
*/
typedef enum {
/**
* Private networks are authorized via certificates of membership
*/
ZTS_NETWORK_TYPE_PRIVATE = 0,
/**
* Public networks have no access control -- they'll always be AUTHORIZED
*/
ZTS_NETWORK_TYPE_PUBLIC = 1
} zts_net_info_type_t;
/**
* A route to be pushed on a virtual network
*/
typedef struct {
/**
* Target network / netmask bits (in port field) or NULL or 0.0.0.0/0
* for default
*/
struct zts_sockaddr_storage target;
/**
* Gateway IP address (port ignored) or NULL (family == 0) for LAN-local
* (no gateway)
*/
struct zts_sockaddr_storage via;
/**
* Route flags
*/
uint16_t flags;
/**
* Route metric (not currently used)
*/
uint16_t metric;
} zts_route_info_t;
/**
* An Ethernet multicast group
*/
typedef struct {
/**
* MAC address (least significant 48 bits)
*/
uint64_t mac;
/**
* Additional distinguishing information (usually zero)
*/
unsigned long adi;
} zts_multicast_group_t;
/**
* The peer's trust hierarchy role
*/
typedef enum {
/**
* Ordinary node
*/
ZTS_PEER_ROLE_LEAF = 0,
/**
* Moon root
*/
ZTS_PEER_ROLE_MOON = 1,
/**
* Planetary root
*/
ZTS_PEER_ROLE_PLANET = 2
} zts_peer_role_t;
/**
* Virtual network configuration
*/
typedef struct {
/**
* 64-bit ZeroTier network ID
*/
uint64_t net_id;
/**
* Ethernet MAC (48 bits) that should be assigned to port
*/
uint64_t mac;
/**
* Network name (from network configuration master)
*/
char name[ZTS_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
/**
* Network configuration request status
*/
zts_network_status_t status;
/**
* Network type
*/
zts_net_info_type_t type;
/**
* Maximum interface MTU
*/
unsigned int mtu;
/**
* If nonzero, the network this port belongs to indicates DHCP availability
*
* This is a suggestion. The underlying implementation is free to ignore it
* for security or other reasons. This is simply a netconf parameter that
* means 'DHCP is available on this network.'
*/
int dhcp;
/**
* If nonzero, this port is allowed to bridge to other networks
*
* This is informational. If this is false (0), bridged packets will simply
* be dropped and bridging won't work.
*/
int bridge;
/**
* If nonzero, this network supports and allows broadcast
* (ff:ff:ff:ff:ff:ff) traffic
*/
int broadcast_enabled;
/**
* If the network is in PORT_ERROR state, this is the (negative) error code
* most recently reported
*/
int port_error;
/**
* Revision number as reported by controller or 0 if still waiting for
* config
*/
unsigned long netconf_rev;
/**
* Number of assigned addresses
*/
unsigned int assigned_addr_count;
/**
* ZeroTier-assigned addresses (in sockaddr_storage structures)
*
* For IP, the port number of the sockaddr_XX structure contains the number
* of bits in the address netmask. Only the IP address and port are used.
* Other fields like interface number can be ignored.
*
* This is only used for ZeroTier-managed address assignments sent by the
* virtual network's configuration master.
*/
struct zts_sockaddr_storage assigned_addrs[ZTS_MAX_ASSIGNED_ADDRESSES];
/**
* Number of ZT-pushed routes
*/
unsigned int route_count;
/**
* Routes (excluding those implied by assigned addresses and their masks)
*/
zts_route_info_t routes[ZTS_MAX_NETWORK_ROUTES];
/**
* Number of multicast groups subscribed
*/
unsigned int multicast_sub_count;
/**
* Multicast groups to which this network's device is subscribed
*/
struct {
uint64_t mac; /* MAC in lower 48 bits */
uint32_t adi; /* Additional distinguishing information, usually zero
except for IPv4 ARP groups */
} multicast_subs[ZTS_MAX_MULTICAST_SUBSCRIPTIONS];
} zts_net_info_t;
/**
* Physical network path to a peer
*/
typedef struct {
/**
* Address of endpoint
*/
struct zts_sockaddr_storage address;
/**
* Time of last send in milliseconds or 0 for never
*/
uint64_t last_tx;
/**
* Time of last receive in milliseconds or 0 for never
*/
uint64_t last_rx;
/**
* Is this a trusted path? If so this will be its nonzero ID.
*/
uint64_t trusted_path_id;
/**
* One-way latency
*/
float latency;
float unused_0;
float unused_1;
float unused_2;
float unused_3;
float unused_4;
uint64_t unused_5;
uint64_t unused_6;
float unused_7;
/**
* Name of physical interface (for monitoring)
*/
char* ifname;
/**
* Is path expired?
*/
int expired;
/**
* Is path preferred?
*/
int preferred;
} zts_path_t;
/**
* Peer status result buffer
*/
typedef struct {
/**
* ZeroTier address (40 bits)
*/
uint64_t peer_id;
/**
* Remote major version or -1 if not known
*/
int ver_major;
/**
* Remote minor version or -1 if not known
*/
int ver_minor;
/**
* Remote revision or -1 if not known
*/
int ver_rev;
/**
* Last measured latency in milliseconds or -1 if unknown
*/
int latency;
/**
* What trust hierarchy role does this device have?
*/
zts_peer_role_t role;
/**
* Number of paths (size of paths[])
*/
unsigned int path_count;
/**
* Whether this peer was ever reachable via an aggregate link
*/
int unused_0;
/**
* Known network paths to peer
*/
zts_path_t paths[ZTS_MAX_PEER_NETWORK_PATHS];
} zts_peer_info_t;
#define ZTS_MAX_NUM_ROOTS 16
#define ZTS_MAX_ENDPOINTS_PER_ROOT 32
/**
* Structure used to specify a root topology (aka a world)
*/
typedef struct {
char* public_id_str[ZTS_MAX_NUM_ROOTS];
char* endpoint_ip_str[ZTS_MAX_NUM_ROOTS][ZTS_MAX_ENDPOINTS_PER_ROOT];
} zts_root_set_t;
/**
* Structure used to convey information about a virtual network
* interface (netif) to a user application.
*/
typedef struct {
/**
* The virtual network that this interface was created for
*/
uint64_t net_id;
/**
* The hardware address assigned to this interface
*/
uint64_t mac;
/**
* The MTU for this interface
*/
int mtu;
} zts_netif_info_t;
/**
* Callback message
*/
typedef struct {
/**
* Event identifier
*/
int16_t event_code;
/**
* Node status
*/
zts_node_info_t* node;
/**
* Network information
*/
zts_net_info_t* network;
/**
* Netif status
*/
zts_netif_info_t* netif;
/**
* Managed routes
*/
zts_route_info_t* route;
/**
* Peer info
*/
zts_peer_info_t* peer;
/**
* Assigned address
*/
zts_addr_info_t* addr;
/**
* Binary data (identities, planets, network configs, peer hints, etc)
*/
void* cache;
/**
* Length of data message or structure
*/
int len;
} zts_event_msg_t;