ZeroTierCore.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. /*
  14. * This defines the external C API for ZeroTier's core network virtualization
  15. * engine.
  16. */
  17. #ifndef ZT_ZEROTIER_API_H
  18. #define ZT_ZEROTIER_API_H
  19. /* ZT_PACKED_STRUCT encloses structs whose contents should be bit-packed.
  20. * Nearly all compilers support this. These macros detect the compiler and
  21. * define it correctly for gcc/icc/clang or MSC. */
  22. #ifndef ZT_PACKED_STRUCT
  23. #if defined(__GCC__) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(__INTEL_COMPILER) || defined(__clang__)
  24. #define ZT_PACKED_STRUCT(D) D __attribute__((packed))
  25. #define ZT_PACKED_STRUCT_START
  26. #define ZT_PACKED_STRUCT_END __attribute__((packed))
  27. #endif
  28. #ifdef _MSC_VER
  29. #define ZT_PACKED_STRUCT(D) __pragma(pack(push,1)) D __pragma(pack(pop))
  30. #define ZT_PACKED_STRUCT_START __pragma(pack(push,1))
  31. #define ZT_PACKED_STRUCT_END __pragma(pack(pop))
  32. #endif
  33. #endif
  34. #ifndef ZT_PACKED_STRUCT
  35. #error Missing a macro to define ZT_PACKED_STRUCT for your compiler.
  36. #endif
  37. #ifdef __cplusplus
  38. #include <cstdint>
  39. extern "C" {
  40. #else
  41. #include <stdint.h>
  42. #endif
  43. #if defined(_WIN32) || defined(_WIN64)
  44. #include <WinSock2.h>
  45. #include <WS2tcpip.h>
  46. #include <Windows.h>
  47. #else
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #endif
  53. #ifndef ZT_SDK_API
  54. #define ZT_SDK_API
  55. #endif
  56. /****************************************************************************/
  57. /**
  58. * Default UDP port for devices running a ZeroTier endpoint
  59. *
  60. * NOTE: as of V2 this has changed to 893 since many NATs (even symmetric)
  61. * treat privileged ports in a special way. The old default was 9993.
  62. */
  63. #define ZT_DEFAULT_PORT 893
  64. /**
  65. * Minimum MTU, which is the minimum allowed by IPv6 and several specs
  66. */
  67. #define ZT_MIN_MTU 1280
  68. /**
  69. * Maximum MTU for ZeroTier virtual networks
  70. */
  71. #define ZT_MAX_MTU 10000
  72. /**
  73. * Minimum UDP payload size allowed
  74. */
  75. #define ZT_MIN_PHYSMTU 1400
  76. /**
  77. * Default UDP payload size (physical path MTU) not including UDP and IP overhead
  78. *
  79. * This is small enough for PPPoE and for Google Cloud's bizarrely tiny MTUs.
  80. * A 2800 byte payload still fits into two packets, so this should not impact
  81. * real world throughput at all vs the previous default of 1444.
  82. */
  83. #define ZT_DEFAULT_PHYSMTU 1432
  84. /**
  85. * Maximum physical UDP payload
  86. */
  87. #define ZT_MAX_PHYSPAYLOAD 10100
  88. /**
  89. * Headroom for max physical MTU
  90. */
  91. #define ZT_MAX_HEADROOM 224
  92. /**
  93. * Maximum payload MTU for UDP packets
  94. */
  95. #define ZT_MAX_PHYSMTU (ZT_MAX_PHYSPAYLOAD + ZT_MAX_HEADROOM)
  96. /**
  97. * Maximum length of network short name
  98. */
  99. #define ZT_MAX_NETWORK_SHORT_NAME_LENGTH 127
  100. /**
  101. * Maximum number of pushed routes on a network (via ZT in-band mechanisms)
  102. */
  103. #define ZT_MAX_NETWORK_ROUTES 64
  104. /**
  105. * Maximum number of statically assigned IP addresses (via ZT in-band mechanisms)
  106. */
  107. #define ZT_MAX_ZT_ASSIGNED_ADDRESSES 32
  108. /**
  109. * Maximum number of "specialists" on a network -- bridges, etc.
  110. *
  111. * A specialist is a node tagged with some special role like acting as
  112. * a promiscuous bridge, open relay, administrator, etc.
  113. */
  114. #define ZT_MAX_NETWORK_SPECIALISTS 256
  115. /**
  116. * Rules engine revision ID, which specifies rules engine capabilities
  117. */
  118. #define ZT_RULES_ENGINE_REVISION 1
  119. /**
  120. * Maximum number of base (non-capability) network rules
  121. */
  122. #define ZT_MAX_NETWORK_RULES 1024
  123. /**
  124. * Maximum number of capabilities per network per member
  125. */
  126. #define ZT_MAX_NETWORK_CAPABILITIES 128
  127. /**
  128. * Maximum number of tags per network per member
  129. */
  130. #define ZT_MAX_NETWORK_TAGS 128
  131. /**
  132. * Maximum number of direct network paths to a given peer
  133. *
  134. * Note that dual-stack configs may end up resulting in both IPv6 and IPv4
  135. * paths existing. This gives enough headroom for multipath configs with
  136. * dual stacks across the board.
  137. */
  138. #define ZT_MAX_PEER_NETWORK_PATHS 16
  139. /**
  140. * Maximum number of path configurations that can be set
  141. */
  142. #define ZT_MAX_CONFIGURABLE_PATHS 32
  143. /**
  144. * Maximum number of rules per capability object
  145. *
  146. * Capabilities normally contain only a few rules. The rules in a capability
  147. * should be short and to the point.
  148. */
  149. #define ZT_MAX_CAPABILITY_RULES 64
  150. /**
  151. * Maximum number of certificates of ownership to assign to a single network member
  152. *
  153. * Network members can have more than four IPs, etc., but right now there
  154. * is a protocol limit on how many COOs can be assigned. If your config needs
  155. * more than four authenticated IPs per node you may have personal problems.
  156. */
  157. #define ZT_MAX_CERTIFICATES_OF_OWNERSHIP 4
  158. /**
  159. * Global maximum length for capability chain of custody (including initial issue)
  160. */
  161. #define ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH 7
  162. /* Rule specification contants **********************************************/
  163. /**
  164. * Packet characteristics flag: packet direction, 1 if inbound 0 if outbound
  165. */
  166. #define ZT_RULE_PACKET_CHARACTERISTICS_INBOUND 0x8000000000000000ULL
  167. /**
  168. * Packet characteristics flag: multicast or broadcast destination MAC
  169. */
  170. #define ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST 0x4000000000000000ULL
  171. /**
  172. * Packet characteristics flag: broadcast destination MAC
  173. */
  174. #define ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST 0x2000000000000000ULL
  175. /**
  176. * Packet characteristics flag: sending IP address has a certificate of ownership
  177. */
  178. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED 0x1000000000000000ULL
  179. /**
  180. * Packet characteristics flag: sending MAC address has a certificate of ownership
  181. */
  182. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED 0x0800000000000000ULL
  183. /**
  184. * Packet characteristics flag: TCP left-most reserved bit
  185. */
  186. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_0 0x0000000000000800ULL
  187. /**
  188. * Packet characteristics flag: TCP middle reserved bit
  189. */
  190. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_1 0x0000000000000400ULL
  191. /**
  192. * Packet characteristics flag: TCP right-most reserved bit
  193. */
  194. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_2 0x0000000000000200ULL
  195. /**
  196. * Packet characteristics flag: TCP NS flag
  197. */
  198. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS 0x0000000000000100ULL
  199. /**
  200. * Packet characteristics flag: TCP CWR flag
  201. */
  202. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR 0x0000000000000080ULL
  203. /**
  204. * Packet characteristics flag: TCP ECE flag
  205. */
  206. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE 0x0000000000000040ULL
  207. /**
  208. * Packet characteristics flag: TCP URG flag
  209. */
  210. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG 0x0000000000000020ULL
  211. /**
  212. * Packet characteristics flag: TCP ACK flag
  213. */
  214. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK 0x0000000000000010ULL
  215. /**
  216. * Packet characteristics flag: TCP PSH flag
  217. */
  218. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH 0x0000000000000008ULL
  219. /**
  220. * Packet characteristics flag: TCP RST flag
  221. */
  222. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST 0x0000000000000004ULL
  223. /**
  224. * Packet characteristics flag: TCP SYN flag
  225. */
  226. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN 0x0000000000000002ULL
  227. /**
  228. * Packet characteristics flag: TCP FIN flag
  229. */
  230. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN 0x0000000000000001ULL
  231. /****************************************************************************/
  232. /**
  233. * Credential type IDs
  234. *
  235. * These are mostly used internally but are declared here so they can be used
  236. * in trace messages.
  237. */
  238. enum ZT_CredentialType
  239. {
  240. ZT_CREDENTIAL_TYPE_NULL = 0,
  241. ZT_CREDENTIAL_TYPE_COM = 1,
  242. ZT_CREDENTIAL_TYPE_CAPABILITY = 2,
  243. ZT_CREDENTIAL_TYPE_TAG = 3,
  244. ZT_CREDENTIAL_TYPE_COO = 4,
  245. ZT_CREDENTIAL_TYPE_REVOCATION = 6
  246. };
  247. /* Trace events are sent and received as packed structures of a fixed size.
  248. * Normally we don't use this form of brittle encoding but in this case the
  249. * performance benefit is non-trivial as events are generated in critical
  250. * areas of the code.
  251. *
  252. * NOTE: all integer fields larger than one byte are stored in big-endian
  253. * "network" byte order in these structures. */
  254. /**
  255. * Flag indicating that VL1 tracing should be generated
  256. */
  257. #define ZT_TRACE_FLAG_VL1 0x01
  258. /**
  259. * Flag indicating that VL2 (virtual network) tracing should be generated
  260. */
  261. #define ZT_TRACE_FLAG_VL2 0x02
  262. /**
  263. * Flag indicating that VL2 network filter tracing should be generated (separate because this can be very verbose)
  264. */
  265. #define ZT_TRACE_FLAG_VL2_FILTER 0x04
  266. /**
  267. * Flag indicating that VL2 multicast propagation should be reported
  268. */
  269. #define ZT_TRACE_FLAG_VL2_MULTICAST 0x08
  270. /**
  271. * Trace event types
  272. *
  273. * All trace event structures start with a size and type.
  274. */
  275. enum ZT_TraceEventType
  276. {
  277. /* VL1 events related to the peer-to-peer layer */
  278. ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE = 1,
  279. ZT_TRACE_VL1_TRYING_NEW_PATH = 2,
  280. ZT_TRACE_VL1_LEARNED_NEW_PATH = 3,
  281. ZT_TRACE_VL1_INCOMING_PACKET_DROPPED = 4,
  282. /* VL2 events relate to virtual networks, packet filtering, and authentication */
  283. ZT_TRACE_VL2_OUTGOING_FRAME_DROPPED = 100,
  284. ZT_TRACE_VL2_INCOMING_FRAME_DROPPED = 101,
  285. ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED = 102,
  286. ZT_TRACE_VL2_NETWORK_FILTER = 103
  287. };
  288. /**
  289. * Trace VL1 packet drop reasons
  290. */
  291. enum ZT_TracePacketDropReason
  292. {
  293. ZT_TRACE_PACKET_DROP_REASON_UNSPECIFIED = 0,
  294. ZT_TRACE_PACKET_DROP_REASON_PEER_TOO_OLD = 1,
  295. ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET = 2,
  296. ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED = 3,
  297. ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED = 4,
  298. ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT = 5,
  299. ZT_TRACE_PACKET_DROP_REASON_INVALID_COMPRESSED_DATA = 6,
  300. ZT_TRACE_PACKET_DROP_REASON_UNRECOGNIZED_VERB = 7
  301. };
  302. /**
  303. * Trace VL2 frame drop reasons
  304. */
  305. enum ZT_TraceFrameDropReason
  306. {
  307. ZT_TRACE_FRAME_DROP_REASON_UNSPECIFIED = 0,
  308. ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_REMOTE = 1,
  309. ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_LOCAL = 2,
  310. ZT_TRACE_FRAME_DROP_REASON_MULTICAST_DISABLED = 3,
  311. ZT_TRACE_FRAME_DROP_REASON_BROADCAST_DISABLED = 4,
  312. ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED = 5,
  313. ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED_AT_BRIDGE_REPLICATION = 6,
  314. ZT_TRACE_FRAME_DROP_REASON_PERMISSION_DENIED = 7
  315. };
  316. /**
  317. * Address types for ZT_TraceEventPathAddress
  318. *
  319. * These are currently the same as the types in Endpoint.hpp and should remain so
  320. * if possible for consistency. Not all of these are used (yet?) but they are defined
  321. * for possible future use and the structure is sized to support them.
  322. */
  323. enum ZT_TraceEventPathAddressType
  324. {
  325. ZT_TRACE_EVENT_PATH_TYPE_NIL = 0, /* none/empty */
  326. ZT_TRACE_EVENT_PATH_TYPE_INETADDR_V4 = 1, /* 4-byte IPv4 */
  327. ZT_TRACE_EVENT_PATH_TYPE_INETADDR_V6 = 2, /* 16-byte IPv6 */
  328. ZT_TRACE_EVENT_PATH_TYPE_DNSNAME = 3, /* C string */
  329. ZT_TRACE_EVENT_PATH_TYPE_ZEROTIER = 4, /* 5-byte ZeroTier + 48-byte identity hash */
  330. ZT_TRACE_EVENT_PATH_TYPE_URL = 5, /* C string */
  331. ZT_TRACE_EVENT_PATH_TYPE_ETHERNET = 6 /* 6-byte Ethernet */
  332. };
  333. /**
  334. * Reasons for trying new paths
  335. */
  336. enum ZT_TraceTryingNewPathReason
  337. {
  338. ZT_TRACE_TRYING_NEW_PATH_REASON_PACKET_RECEIVED_FROM_UNKNOWN_PATH = 1,
  339. ZT_TRACE_TRYING_NEW_PATH_REASON_RECEIVED_PUSH_DIRECT_PATHS = 2,
  340. ZT_TRACE_TRYING_NEW_PATH_REASON_RENDEZVOUS = 3,
  341. ZT_TRACE_TRYING_NEW_PATH_REASON_BOOTSTRAP_ADDRESS = 4,
  342. ZT_TRACE_TRYING_NEW_PATH_REASON_EXPLICITLY_SUGGESTED_ADDRESS = 5
  343. };
  344. /**
  345. * Reasons for credential rejection
  346. */
  347. enum ZT_TraceCredentialRejectionReason
  348. {
  349. ZT_TRACE_CREDENTIAL_REJECTION_REASON_SIGNATURE_VERIFICATION_FAILED = 1,
  350. ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED = 2,
  351. ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST = 3,
  352. ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID = 4
  353. };
  354. /**
  355. * Physical path address from a trace event
  356. *
  357. * This is a special packed address format that roughly mirrors Endpoint in the core
  358. * and is designed to support both present and future address types.
  359. */
  360. ZT_PACKED_STRUCT(struct ZT_TraceEventPathAddress
  361. {
  362. uint8_t type; /* ZT_TraceEventPathAddressType */
  363. uint8_t address[63]; /* Type-dependent address: 4-byte IPv4, 16-byte IPV6, etc. */
  364. uint16_t port; /* UDP/TCP port for address types for which this is meaningful */
  365. });
  366. /**
  367. * Header for all trace events
  368. *
  369. * All packet types begin with these fields in this order.
  370. */
  371. ZT_PACKED_STRUCT(struct ZT_TraceEvent
  372. {
  373. uint16_t evSize; /* sizeof(ZT_TraceEvent_XX structure) (inclusive size in bytes) */
  374. uint16_t evType; /* ZT_TraceEventType */
  375. });
  376. /* Temporary macros to make it easier to declare all ZT_TraceEvent's sub-types */
  377. #define _ZT_TRACE_EVENT_STRUCT_START(e) ZT_PACKED_STRUCT_START struct ZT_TraceEvent_##e { \
  378. uint16_t evSize; \
  379. uint16_t evType;
  380. #define _ZT_TRACE_EVENT_STRUCT_END() } ZT_PACKED_STRUCT_END;
  381. /**
  382. * Node is resetting all paths in a given address scope
  383. *
  384. * This happens when the node detects and external surface IP addressing change
  385. * via a trusted (usually root) peer. It's used to renegotiate links when nodes
  386. * move around on the network.
  387. */
  388. _ZT_TRACE_EVENT_STRUCT_START(VL1_RESETTING_PATHS_IN_SCOPE)
  389. uint64_t reporter; /* ZeroTier address that triggered the reset */
  390. uint8_t reporterIdentityHash[48]; /* full identity hash of triggering node's identity */
  391. struct ZT_TraceEventPathAddress from; /* physical origin of triggering packet */
  392. struct ZT_TraceEventPathAddress oldExternal; /* previous detected external address */
  393. struct ZT_TraceEventPathAddress newExternal; /* new detected external address */
  394. uint8_t scope; /* IP scope being reset */
  395. _ZT_TRACE_EVENT_STRUCT_END()
  396. /**
  397. * Node is trying a new path
  398. *
  399. * Paths are tried in response to PUSH_DIRECT_PATHS, RENDEZVOUS, and other places
  400. * we might hear of them. A node tries a path by sending a trial message to it.
  401. */
  402. _ZT_TRACE_EVENT_STRUCT_START(VL1_TRYING_NEW_PATH)
  403. uint64_t address; /* short address of node we're trying to reach */
  404. uint8_t identityHash[48]; /* identity hash of node we're trying to reach */
  405. struct ZT_TraceEventPathAddress physicalAddress; /* physical address being tried */
  406. struct ZT_TraceEventPathAddress triggerAddress; /* physical origin of triggering packet */
  407. uint64_t triggeringPacketId; /* packet ID of triggering packet */
  408. uint8_t triggeringPacketVerb; /* packet verb of triggering packet */
  409. uint64_t triggeredByAddress; /* short address of node triggering attempt */
  410. uint8_t triggeredByIdentityHash[48]; /* full identity hash of node triggering attempt */
  411. uint8_t reason; /* ZT_TraceTryingNewPathReason */
  412. _ZT_TRACE_EVENT_STRUCT_END()
  413. /**
  414. * Node has learned a new path to another node
  415. */
  416. _ZT_TRACE_EVENT_STRUCT_START(VL1_LEARNED_NEW_PATH)
  417. uint64_t packetId; /* packet ID of confirming packet */
  418. uint64_t address; /* short address of learned peer */
  419. uint8_t identityHash[48]; /* full identity hash of learned peer */
  420. struct ZT_TraceEventPathAddress physicalAddress; /* physical address learned */
  421. struct ZT_TraceEventPathAddress replaced; /* if non-empty, an older address that was replaced */
  422. _ZT_TRACE_EVENT_STRUCT_END()
  423. /**
  424. * An incoming packet was dropped at the VL1 level
  425. *
  426. * This indicates a packet that passed MAC check but was dropped for some other
  427. * reason such as rate limits, being malformed, etc.
  428. */
  429. _ZT_TRACE_EVENT_STRUCT_START(VL1_INCOMING_PACKET_DROPPED)
  430. uint64_t packetId; /* packet ID of failed packet */
  431. uint64_t networkId; /* VL2 network ID or 0 if unrelated to a network or unknown */
  432. uint64_t address; /* short address that sent packet */
  433. uint8_t identityHash[48]; /* full identity hash of sending node */
  434. struct ZT_TraceEventPathAddress physicalAddress; /* physical origin address of packet */
  435. uint8_t hops; /* hop count of packet */
  436. uint8_t verb; /* packet verb */
  437. uint8_t reason; /* ZT_TracePacketDropReason */
  438. _ZT_TRACE_EVENT_STRUCT_END()
  439. /**
  440. * Node declined to send a packet read from a local network port/tap
  441. */
  442. _ZT_TRACE_EVENT_STRUCT_START(VL2_OUTGOING_FRAME_DROPPED)
  443. uint64_t networkId; /* network ID */
  444. uint64_t sourceMac; /* source MAC address */
  445. uint64_t destMac; /* destination MAC address */
  446. uint16_t etherType; /* Ethernet type of frame */
  447. uint16_t frameLength; /* length of dropped frame */
  448. uint8_t frameHead[64]; /* first up to 64 bytes of dropped frame */
  449. uint8_t reason; /* ZT_TraceFrameDropReason */
  450. _ZT_TRACE_EVENT_STRUCT_END()
  451. /**
  452. * An incoming frame was dropped
  453. */
  454. _ZT_TRACE_EVENT_STRUCT_START(VL2_INCOMING_FRAME_DROPPED)
  455. uint64_t packetId; /* VL1 packet ID */
  456. uint64_t networkId; /* VL2 network ID */
  457. uint64_t sourceMac; /* 48-bit source MAC */
  458. uint64_t destMac; /* 48-bit destination MAC */
  459. uint64_t address; /* short address of sending peer */
  460. struct ZT_TraceEventPathAddress physicalAddress; /* physical source address of packet */
  461. uint8_t hops; /* hop count of packet */
  462. uint16_t frameLength; /* length of frame in bytes */
  463. uint8_t frameHead[64]; /* first up to 64 bytes of dropped frame */
  464. uint8_t verb; /* packet verb indicating how frame was sent */
  465. uint8_t credentialRequestSent; /* if non-zero a request for credentials was sent */
  466. uint8_t reason; /* ZT_TraceFrameDropReason */
  467. _ZT_TRACE_EVENT_STRUCT_END()
  468. /**
  469. * Node is requesting a new network config and certificate from a network controller
  470. */
  471. _ZT_TRACE_EVENT_STRUCT_START(VL2_NETWORK_CONFIG_REQUESTED)
  472. uint64_t networkId; /* VL2 network ID */
  473. _ZT_TRACE_EVENT_STRUCT_END()
  474. /**
  475. * Network filter trace results
  476. *
  477. * These are generated when filter tracing is enabled to allow filters to be debugged.
  478. * Format for rule set logs is documented elsewhere.
  479. */
  480. _ZT_TRACE_EVENT_STRUCT_START(VL2_NETWORK_FILTER)
  481. uint64_t networkId; /* VL2 network ID */
  482. uint8_t primaryRuleSetLog[512]; /* primary rule set log */
  483. uint8_t matchingCapabilityRuleSetLog[512]; /* capability rule set log (if any) */
  484. uint32_t matchingCapabilityId; /* capability ID or 0 if none */
  485. int64_t matchingCapabilityTimestamp; /* capability timestamp or 0 if none */
  486. uint64_t source; /* source ZeroTier address */
  487. uint64_t dest; /* destination ZeroTier address */
  488. uint64_t sourceMac; /* packet source MAC */
  489. uint64_t destMac; /* packet destination MAC */
  490. uint16_t frameLength; /* length of filtered frame */
  491. uint8_t frameHead[64]; /* first up to 64 bytes of filtered frame */
  492. uint16_t etherType; /* frame Ethernet type */
  493. uint16_t vlanId; /* frame VLAN ID (currently unused, always 0) */
  494. uint8_t noTee; /* if true noTee flag was set in filter */
  495. uint8_t inbound; /* direction: 1 for inbound, 0 for outbound */
  496. int8_t accept; /* 0: drop, 1: accept, 2: "super-accept" */
  497. _ZT_TRACE_EVENT_STRUCT_END()
  498. /**
  499. * An incoming credential from a peer was rejected
  500. */
  501. _ZT_TRACE_EVENT_STRUCT_START(VL2_CREDENTIAL_REJECTED)
  502. uint64_t networkId; /* VL2 network ID */
  503. uint64_t address; /* short address of sender */
  504. uint32_t credentialId; /* credential ID */
  505. int64_t credentialTimestamp; /* credential timestamp */
  506. uint8_t credentialType; /* credential type */
  507. uint8_t reason; /* ZT_TraceCredentialRejectionReason */
  508. _ZT_TRACE_EVENT_STRUCT_END()
  509. #undef _ZT_TRACE_EVENT_STRUCT_START
  510. #undef _ZT_TRACE_EVENT_STRUCT_END
  511. /****************************************************************************/
  512. /**
  513. * Function return code: OK (0) or error results
  514. *
  515. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  516. * occurs, the node should be considered to not be working correctly. These
  517. * indicate serious problems like an inaccessible data store or a compile
  518. * problem.
  519. */
  520. enum ZT_ResultCode
  521. {
  522. /**
  523. * Operation completed normally
  524. */
  525. ZT_RESULT_OK = 0,
  526. /**
  527. * Call produced no error but no action was taken
  528. */
  529. ZT_RESULT_OK_IGNORED = 1,
  530. // Fatal errors (>100, <1000)
  531. /**
  532. * Ran out of memory
  533. */
  534. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 100,
  535. /**
  536. * Data store is not writable or has failed
  537. */
  538. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 101,
  539. /**
  540. * Internal error (e.g. unexpected exception indicating bug or build problem)
  541. */
  542. ZT_RESULT_FATAL_ERROR_INTERNAL = 102,
  543. // Non-fatal errors (>1000)
  544. /**
  545. * Network ID not valid
  546. */
  547. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000,
  548. /**
  549. * The requested operation is not supported on this version or build
  550. */
  551. ZT_RESULT_ERROR_UNSUPPORTED_OPERATION = 1001,
  552. /**
  553. * The requested operation was given a bad parameter or was called in an invalid state
  554. */
  555. ZT_RESULT_ERROR_BAD_PARAMETER = 1002
  556. };
  557. /**
  558. * Macro to check for a fatal error result code
  559. *
  560. * @param x Result code
  561. * @return True if result code indicates a fatal error
  562. */
  563. #define ZT_ResultCode_isFatal(x) ((((int)(x)) >= 100)&&(((int)(x)) < 1000))
  564. /**
  565. * Status codes sent to status update callback when things happen
  566. */
  567. enum ZT_Event
  568. {
  569. /**
  570. * Node has been initialized
  571. *
  572. * This is the first event generated, and is always sent. It may occur
  573. * before Node's constructor returns.
  574. *
  575. * Meta-data: none
  576. */
  577. ZT_EVENT_UP = 0,
  578. /**
  579. * Node appears offline
  580. *
  581. * This indicates that the node doesn't seem to be able to reach anything,
  582. * or hasn't for a while. It's not a hard instantaneous thing.
  583. *
  584. * Meta-data: none
  585. */
  586. ZT_EVENT_OFFLINE = 1,
  587. /**
  588. * Node appears online
  589. *
  590. * This indicates that the node was offline but now seems to be able to
  591. * reach something. Like OFFLINE it's not a hard instantaneous thing but
  592. * more of an indicator for UI reporting purposes.
  593. *
  594. * Meta-data: none
  595. */
  596. ZT_EVENT_ONLINE = 2,
  597. /**
  598. * Node is shutting down
  599. *
  600. * This is generated within Node's destructor when it is being shut down.
  601. * It's done for convenience in case you want to clean up anything during
  602. * node shutdown in your node event handler.
  603. *
  604. * Meta-data: none
  605. */
  606. ZT_EVENT_DOWN = 3,
  607. // 4 once signaled identity collision but this is no longer an error
  608. /**
  609. * Trace (debugging) message
  610. *
  611. * These events are only generated if this is a TRACE-enabled build.
  612. * This is for local debug traces, not remote trace diagnostics.
  613. *
  614. * Meta-data: struct of type ZT_Trace_*
  615. */
  616. ZT_EVENT_TRACE = 5,
  617. /**
  618. * VERB_USER_MESSAGE received
  619. *
  620. * These are generated when a VERB_USER_MESSAGE packet is received via
  621. * ZeroTier VL1. This can be used for below-VL2 in-band application
  622. * specific signaling over the ZeroTier protocol.
  623. *
  624. * It's up to you to ensure that you handle these in a way that does
  625. * not introduce a remote security vulnerability into your app! If
  626. * your USER_MESSAGE code has a buffer overflow or other vulnerability
  627. * then your app will be vulnerable and this is not ZT's fault. :)
  628. *
  629. * Meta-data: ZT_UserMessage structure
  630. */
  631. ZT_EVENT_USER_MESSAGE = 6
  632. };
  633. /**
  634. * Identity type codes
  635. */
  636. enum ZT_Identity_Type
  637. {
  638. /* These values must be the same as in Identity.hpp in the core. */
  639. ZT_IDENTITY_TYPE_C25519 = 0,
  640. ZT_IDENTITY_TYPE_P384 = 1
  641. };
  642. /**
  643. * A ZeroTier identity (opaque)
  644. */
  645. typedef void ZT_Identity;
  646. /**
  647. * User message used with ZT_EVENT_USER_MESSAGE
  648. *
  649. * These are direct VL1 P2P messages for application use. Encryption and
  650. * authentication in the ZeroTier protocol will guarantee the origin
  651. * address and message content, but you are responsible for any other
  652. * levels of authentication or access control that are required. Any node
  653. * in the world can send you a user message! (Unless your network is air
  654. * gapped.)
  655. *
  656. * Pointers to id and data might not remain valid after the event is
  657. * received.
  658. */
  659. typedef struct
  660. {
  661. /**
  662. * Identity of sender
  663. */
  664. const ZT_Identity *id;
  665. /**
  666. * User message type ID
  667. */
  668. uint64_t typeId;
  669. /**
  670. * User message data
  671. */
  672. const void *data;
  673. /**
  674. * Length of data in bytes
  675. */
  676. unsigned int length;
  677. } ZT_UserMessage;
  678. /**
  679. * Current node status
  680. */
  681. typedef struct
  682. {
  683. /**
  684. * 40-bit ZeroTier address of this node
  685. */
  686. uint64_t address;
  687. /**
  688. * Actual identity object for this node
  689. */
  690. const ZT_Identity *identity;
  691. /**
  692. * Public identity in string-serialized form (safe to send to others)
  693. *
  694. * This pointer will remain valid as long as the node exists.
  695. */
  696. const char *publicIdentity;
  697. /**
  698. * Full identity including secret key in string-serialized form
  699. *
  700. * This pointer will remain valid as long as the node exists.
  701. */
  702. const char *secretIdentity;
  703. /**
  704. * True if some kind of connectivity appears available
  705. */
  706. int online;
  707. } ZT_NodeStatus;
  708. /**
  709. * Virtual network status codes
  710. */
  711. enum ZT_VirtualNetworkStatus
  712. {
  713. /**
  714. * Waiting for network configuration (also means revision == 0)
  715. */
  716. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  717. /**
  718. * Configuration received and we are authorized
  719. */
  720. ZT_NETWORK_STATUS_OK = 1,
  721. /**
  722. * Netconf master told us 'nope'
  723. */
  724. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  725. /**
  726. * Netconf master exists, but this virtual network does not
  727. */
  728. ZT_NETWORK_STATUS_NOT_FOUND = 3
  729. };
  730. /**
  731. * Virtual network type codes
  732. */
  733. enum ZT_VirtualNetworkType
  734. {
  735. /**
  736. * Private networks are authorized via certificates of membership
  737. */
  738. ZT_NETWORK_TYPE_PRIVATE = 0,
  739. /**
  740. * Public networks have no access control -- they'll always be AUTHORIZED
  741. */
  742. ZT_NETWORK_TYPE_PUBLIC = 1
  743. };
  744. /**
  745. * The type of a virtual network rules table entry
  746. *
  747. * These must be from 0 to 63 since the most significant two bits of each
  748. * rule type are NOT (MSB) and AND/OR.
  749. *
  750. * Each rule is composed of zero or more MATCHes followed by an ACTION.
  751. * An ACTION with no MATCHes is always taken.
  752. */
  753. enum ZT_VirtualNetworkRuleType
  754. {
  755. // 0 to 15 reserved for actions
  756. /**
  757. * Drop frame
  758. */
  759. ZT_NETWORK_RULE_ACTION_DROP = 0,
  760. /**
  761. * Accept and pass frame
  762. */
  763. ZT_NETWORK_RULE_ACTION_ACCEPT = 1,
  764. /**
  765. * Forward a copy of this frame to an observer (by ZT address)
  766. */
  767. ZT_NETWORK_RULE_ACTION_TEE = 2,
  768. /**
  769. * Exactly like TEE but mandates ACKs from observer
  770. */
  771. ZT_NETWORK_RULE_ACTION_WATCH = 3,
  772. /**
  773. * Drop and redirect this frame to another node (by ZT address)
  774. */
  775. ZT_NETWORK_RULE_ACTION_REDIRECT = 4,
  776. /**
  777. * Stop evaluating rule set (drops unless there are capabilities, etc.)
  778. */
  779. ZT_NETWORK_RULE_ACTION_BREAK = 5,
  780. /**
  781. * Place a matching frame in the specified QoS bucket
  782. */
  783. ZT_NETWORK_RULE_ACTION_PRIORITY = 6,
  784. /**
  785. * Maximum ID for an ACTION, anything higher is a MATCH
  786. */
  787. ZT_NETWORK_RULE_ACTION__MAX_ID = 15,
  788. // 16 to 63 reserved for match criteria
  789. ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS = 24,
  790. ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS = 25,
  791. ZT_NETWORK_RULE_MATCH_VLAN_ID = 26,
  792. ZT_NETWORK_RULE_MATCH_VLAN_PCP = 27,
  793. ZT_NETWORK_RULE_MATCH_VLAN_DEI = 28,
  794. ZT_NETWORK_RULE_MATCH_MAC_SOURCE = 29,
  795. ZT_NETWORK_RULE_MATCH_MAC_DEST = 30,
  796. ZT_NETWORK_RULE_MATCH_IPV4_SOURCE = 31,
  797. ZT_NETWORK_RULE_MATCH_IPV4_DEST = 32,
  798. ZT_NETWORK_RULE_MATCH_IPV6_SOURCE = 33,
  799. ZT_NETWORK_RULE_MATCH_IPV6_DEST = 34,
  800. ZT_NETWORK_RULE_MATCH_IP_TOS = 35,
  801. ZT_NETWORK_RULE_MATCH_IP_PROTOCOL = 36,
  802. ZT_NETWORK_RULE_MATCH_ETHERTYPE = 37,
  803. ZT_NETWORK_RULE_MATCH_ICMP = 38,
  804. ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE = 39,
  805. ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE = 40,
  806. ZT_NETWORK_RULE_MATCH_CHARACTERISTICS = 41,
  807. ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE = 42,
  808. ZT_NETWORK_RULE_MATCH_RANDOM = 43,
  809. ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE = 44,
  810. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND = 45,
  811. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR = 46,
  812. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR = 47,
  813. ZT_NETWORK_RULE_MATCH_TAGS_EQUAL = 48,
  814. ZT_NETWORK_RULE_MATCH_TAG_SENDER = 49,
  815. ZT_NETWORK_RULE_MATCH_TAG_RECEIVER = 50,
  816. ZT_NETWORK_RULE_MATCH_INTEGER_RANGE = 51,
  817. /**
  818. * Maximum ID allowed for a MATCH entry in the rules table
  819. */
  820. ZT_NETWORK_RULE_MATCH__MAX_ID = 63
  821. };
  822. /**
  823. * Network flow rule
  824. *
  825. * Rules are stored in a table in which one or more match entries is followed
  826. * by an action. If more than one match precedes an action, the rule is
  827. * the AND of all matches. An action with no match is always taken since it
  828. * matches anything. If nothing matches, the default action is DROP.
  829. *
  830. * This is designed to be a more memory-efficient way of storing rules than
  831. * a wide table, yet still fast and simple to access in code.
  832. */
  833. typedef struct
  834. {
  835. /**
  836. * Type and flags
  837. *
  838. * Bits are: NOTTTTTT
  839. *
  840. * N - If true, sense of match is inverted (no effect on actions)
  841. * O - If true, result is ORed with previous instead of ANDed (no effect on actions)
  842. * T - Rule or action type
  843. *
  844. * AND with 0x3f to get type, 0x80 to get NOT bit, and 0x40 to get OR bit.
  845. */
  846. uint8_t t;
  847. /**
  848. * Union containing the value of this rule -- which field is used depends on 't'
  849. */
  850. union {
  851. /**
  852. * IPv6 address in big-endian / network byte order and netmask bits
  853. */
  854. struct {
  855. uint8_t ip[16];
  856. uint8_t mask;
  857. } ipv6;
  858. /**
  859. * IPv4 address in big-endian / network byte order
  860. */
  861. struct {
  862. uint32_t ip;
  863. uint8_t mask;
  864. } ipv4;
  865. /**
  866. * Integer range match in packet payload
  867. *
  868. * This allows matching of ranges of integers up to 64 bits wide where
  869. * the range is +/- INT32_MAX. It's packed this way so it fits in 16
  870. * bytes and doesn't enlarge the overall size of this union.
  871. */
  872. struct {
  873. uint64_t start; // integer range start
  874. uint32_t end; // end of integer range (relative to start, inclusive, 0 for equality w/start)
  875. uint16_t idx; // index in packet of integer
  876. uint8_t format; // bits in integer (range 1-64, ((format&63)+1)) and endianness (MSB 1 for little, 0 for big)
  877. } intRange;
  878. /**
  879. * Packet characteristic flags being matched
  880. */
  881. uint64_t characteristics;
  882. /**
  883. * IP port range -- start-end inclusive -- host byte order
  884. */
  885. uint16_t port[2];
  886. /**
  887. * 40-bit ZeroTier address (in least significant bits, host byte order)
  888. */
  889. uint64_t zt;
  890. /**
  891. * 0 = never, UINT32_MAX = always
  892. */
  893. uint32_t randomProbability;
  894. /**
  895. * 48-bit Ethernet MAC address in big-endian order
  896. */
  897. uint8_t mac[6];
  898. /**
  899. * VLAN ID in host byte order
  900. */
  901. uint16_t vlanId;
  902. /**
  903. * VLAN PCP (least significant 3 bits)
  904. */
  905. uint8_t vlanPcp;
  906. /**
  907. * VLAN DEI (single bit / boolean)
  908. */
  909. uint8_t vlanDei;
  910. /**
  911. * Ethernet type in host byte order
  912. */
  913. uint16_t etherType;
  914. /**
  915. * IP protocol
  916. */
  917. uint8_t ipProtocol;
  918. /**
  919. * IP type of service a.k.a. DSCP field
  920. */
  921. struct {
  922. uint8_t mask;
  923. uint8_t value[2];
  924. } ipTos;
  925. /**
  926. * Ethernet packet size in host byte order (start-end, inclusive)
  927. */
  928. uint16_t frameSize[2];
  929. /**
  930. * ICMP type and code
  931. */
  932. struct {
  933. uint8_t type; // ICMP type, always matched
  934. uint8_t code; // ICMP code if matched
  935. uint8_t flags; // flag 0x01 means also match code, otherwise only match type
  936. } icmp;
  937. /**
  938. * For tag-related rules
  939. */
  940. struct {
  941. uint32_t id;
  942. uint32_t value;
  943. } tag;
  944. /**
  945. * Destinations for TEE and REDIRECT
  946. */
  947. struct {
  948. uint64_t address;
  949. uint32_t flags;
  950. uint16_t length;
  951. } fwd;
  952. /**
  953. * Quality of Service (QoS) bucket we want a frame to be placed in
  954. */
  955. uint8_t qosBucket;
  956. } v;
  957. } ZT_VirtualNetworkRule;
  958. /**
  959. * A route to be pushed on a virtual network
  960. */
  961. typedef struct
  962. {
  963. /**
  964. * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
  965. */
  966. struct sockaddr_storage target;
  967. /**
  968. * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
  969. */
  970. struct sockaddr_storage via;
  971. /**
  972. * Route flags
  973. */
  974. uint16_t flags;
  975. /**
  976. * Route metric
  977. */
  978. uint16_t metric;
  979. } ZT_VirtualNetworkRoute;
  980. /**
  981. * An Ethernet multicast group
  982. */
  983. typedef struct
  984. {
  985. /**
  986. * MAC address (least significant 48 bits)
  987. */
  988. uint64_t mac;
  989. /**
  990. * Additional distinguishing information (usually zero)
  991. */
  992. unsigned long adi;
  993. } ZT_MulticastGroup;
  994. /**
  995. * Virtual network configuration update type
  996. */
  997. enum ZT_VirtualNetworkConfigOperation
  998. {
  999. /**
  1000. * Network is coming up (either for the first time or after service restart)
  1001. */
  1002. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  1003. /**
  1004. * Network configuration has been updated
  1005. */
  1006. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  1007. /**
  1008. * Network is going down (not permanently)
  1009. */
  1010. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  1011. /**
  1012. * Network is going down permanently (leave/delete)
  1013. */
  1014. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  1015. };
  1016. /**
  1017. * Virtual network configuration
  1018. */
  1019. typedef struct
  1020. {
  1021. /**
  1022. * 64-bit ZeroTier network ID
  1023. */
  1024. uint64_t nwid;
  1025. /**
  1026. * Ethernet MAC (48 bits) that should be assigned to port
  1027. */
  1028. uint64_t mac;
  1029. /**
  1030. * Network name (from network configuration master)
  1031. */
  1032. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  1033. /**
  1034. * Network configuration request status
  1035. */
  1036. enum ZT_VirtualNetworkStatus status;
  1037. /**
  1038. * Network type
  1039. */
  1040. enum ZT_VirtualNetworkType type;
  1041. /**
  1042. * Maximum interface MTU
  1043. */
  1044. unsigned int mtu;
  1045. /**
  1046. * If nonzero, this port is allowed to bridge to other networks
  1047. *
  1048. * This is informational. If this is false (0), bridged packets will simply
  1049. * be dropped and bridging won't work.
  1050. */
  1051. int bridge;
  1052. /**
  1053. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  1054. */
  1055. int broadcastEnabled;
  1056. /**
  1057. * Revision number as reported by controller or 0 if still waiting for config
  1058. */
  1059. unsigned long netconfRevision;
  1060. /**
  1061. * Number of assigned addresses
  1062. */
  1063. unsigned int assignedAddressCount;
  1064. /**
  1065. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  1066. *
  1067. * For IP, the port number of the sockaddr_XX structure contains the number
  1068. * of bits in the address netmask. Only the IP address and port are used.
  1069. * Other fields like interface number can be ignored.
  1070. *
  1071. * This is only used for ZeroTier-managed address assignments sent by the
  1072. * virtual network's configuration master.
  1073. */
  1074. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  1075. /**
  1076. * Number of ZT-pushed routes
  1077. */
  1078. unsigned int routeCount;
  1079. /**
  1080. * Routes (excluding those implied by assigned addresses and their masks)
  1081. */
  1082. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  1083. } ZT_VirtualNetworkConfig;
  1084. /**
  1085. * A list of networks
  1086. */
  1087. typedef struct
  1088. {
  1089. ZT_VirtualNetworkConfig *networks;
  1090. unsigned long networkCount;
  1091. } ZT_VirtualNetworkList;
  1092. /**
  1093. * Address where this node could be reached via an external interface
  1094. */
  1095. typedef struct
  1096. {
  1097. /**
  1098. * IP and port as would be reachable by external nodes
  1099. */
  1100. struct sockaddr_storage address;
  1101. /**
  1102. * If nonzero this address is static and can be incorporated into this node's Locator
  1103. */
  1104. int permanent;
  1105. } ZT_InterfaceAddress;
  1106. /**
  1107. * Physical path configuration
  1108. */
  1109. typedef struct
  1110. {
  1111. /**
  1112. * If non-zero set this physical network path to be trusted to disable encryption and authentication
  1113. */
  1114. uint64_t trustedPathId;
  1115. /**
  1116. * Physical path MTU from ZT_MIN_PHYSMTU and ZT_MAX_PHYSMTU or <= 0 to use default
  1117. */
  1118. int mtu;
  1119. } ZT_PhysicalPathConfiguration;
  1120. /**
  1121. * Physical network path to a peer
  1122. */
  1123. typedef struct
  1124. {
  1125. /**
  1126. * Address of endpoint
  1127. */
  1128. struct sockaddr_storage address;
  1129. /**
  1130. * Time of last send in milliseconds or 0 for never
  1131. */
  1132. int64_t lastSend;
  1133. /**
  1134. * Time of last receive in milliseconds or 0 for never
  1135. */
  1136. int64_t lastReceive;
  1137. /**
  1138. * Is this a trusted path? If so this will be its nonzero ID.
  1139. */
  1140. uint64_t trustedPathId;
  1141. /**
  1142. * Is path alive?
  1143. */
  1144. int alive;
  1145. /**
  1146. * Is path preferred?
  1147. */
  1148. int preferred;
  1149. } ZT_PeerPhysicalPath;
  1150. /**
  1151. * Peer status result buffer
  1152. */
  1153. typedef struct
  1154. {
  1155. /**
  1156. * ZeroTier address (40 bits)
  1157. */
  1158. uint64_t address;
  1159. /**
  1160. * Peer identity
  1161. */
  1162. const ZT_Identity *identity;
  1163. /**
  1164. * Hash of identity public key(s)
  1165. */
  1166. uint8_t identityHash[48];
  1167. /**
  1168. * Remote major version or -1 if not known
  1169. */
  1170. int versionMajor;
  1171. /**
  1172. * Remote minor version or -1 if not known
  1173. */
  1174. int versionMinor;
  1175. /**
  1176. * Remote revision or -1 if not known
  1177. */
  1178. int versionRev;
  1179. /**
  1180. * Last measured latency in milliseconds or -1 if unknown
  1181. */
  1182. int latency;
  1183. /**
  1184. * If non-zero this peer is a root
  1185. */
  1186. int root;
  1187. /**
  1188. * Bootstrap address
  1189. *
  1190. * This is a memo-ized recently valid address that can be saved and used
  1191. * to attempt rapid reconnection with this peer. If the ss_family field
  1192. * is 0 this field is considered null/empty.
  1193. */
  1194. struct sockaddr_storage bootstrap;
  1195. /**
  1196. * Number of paths (size of paths[])
  1197. */
  1198. unsigned int pathCount;
  1199. /**
  1200. * Known network paths to peer
  1201. */
  1202. ZT_PeerPhysicalPath paths[ZT_MAX_PEER_NETWORK_PATHS];
  1203. } ZT_Peer;
  1204. /**
  1205. * List of peers
  1206. */
  1207. typedef struct
  1208. {
  1209. ZT_Peer *peers;
  1210. unsigned long peerCount;
  1211. } ZT_PeerList;
  1212. /**
  1213. * ZeroTier core state objects
  1214. */
  1215. enum ZT_StateObjectType
  1216. {
  1217. /**
  1218. * Null object -- ignored
  1219. */
  1220. ZT_STATE_OBJECT_NULL = 0,
  1221. /**
  1222. * Public address and public key
  1223. *
  1224. * Object ID: (unused)
  1225. * Canonical path: <HOME>/identity.public
  1226. * Persistence: required
  1227. */
  1228. ZT_STATE_OBJECT_IDENTITY_PUBLIC = 1,
  1229. /**
  1230. * Full identity with secret key
  1231. *
  1232. * Object ID: (unused)
  1233. * Canonical path: <HOME>/identity.secret
  1234. * Persistence: required, should be stored with restricted permissions e.g. mode 0600 on *nix
  1235. */
  1236. ZT_STATE_OBJECT_IDENTITY_SECRET = 2,
  1237. /**
  1238. * This node's locator
  1239. *
  1240. * Object ID: (unused)
  1241. * Canonical path: <HOME>/locator
  1242. * Persistence: optional
  1243. */
  1244. ZT_STATE_OBJECT_LOCATOR = 3,
  1245. /**
  1246. * Peer and related state
  1247. *
  1248. * Object ID: peer address
  1249. * Canonical path: <HOME>/peers.d/<ID> (10-digit address
  1250. * Persistence: optional, can be cleared at any time
  1251. */
  1252. ZT_STATE_OBJECT_PEER = 5,
  1253. /**
  1254. * Network configuration
  1255. *
  1256. * Object ID: network ID
  1257. * Canonical path: <HOME>/networks.d/<NETWORKID>.conf (16-digit hex ID)
  1258. * Persistence: required if network memberships should persist
  1259. */
  1260. ZT_STATE_OBJECT_NETWORK_CONFIG = 6,
  1261. /**
  1262. * Root list
  1263. *
  1264. * Object ID: (unused)
  1265. * Canonical path: <HOME>/roots
  1266. * Persistence: required if root settings should persist
  1267. */
  1268. ZT_STATE_OBJECT_ROOTS = 7
  1269. };
  1270. /**
  1271. * An instance of a ZeroTier One node (opaque)
  1272. */
  1273. typedef void ZT_Node;
  1274. /****************************************************************************/
  1275. /**
  1276. * Callback called to update virtual network port configuration
  1277. *
  1278. * This can be called at any time to update the configuration of a virtual
  1279. * network port. The parameter after the network ID specifies whether this
  1280. * port is being brought up, updated, brought down, or permanently deleted.
  1281. *
  1282. * This in turn should be used by the underlying implementation to create
  1283. * and configure tap devices at the OS (or virtual network stack) layer.
  1284. *
  1285. * The supplied config pointer is not guaranteed to remain valid, so make
  1286. * a copy if you want one.
  1287. *
  1288. * This should not call multicastSubscribe() or other network-modifying
  1289. * methods, as this could cause a deadlock in multithreaded or interrupt
  1290. * driven environments.
  1291. */
  1292. typedef void (*ZT_VirtualNetworkConfigFunction)(
  1293. ZT_Node *, /* Node */
  1294. void *, /* User ptr */
  1295. void *, /* Thread ptr */
  1296. uint64_t, /* Network ID */
  1297. void **, /* Modifiable network user PTR */
  1298. enum ZT_VirtualNetworkConfigOperation, /* Config operation */
  1299. const ZT_VirtualNetworkConfig *); /* Network configuration */
  1300. /**
  1301. * Function to send a frame out to a virtual network port
  1302. *
  1303. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  1304. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  1305. * (9) frame length.
  1306. */
  1307. typedef void (*ZT_VirtualNetworkFrameFunction)(
  1308. ZT_Node *, /* Node */
  1309. void *, /* User ptr */
  1310. void *, /* Thread ptr */
  1311. uint64_t, /* Network ID */
  1312. void **, /* Modifiable network user PTR */
  1313. uint64_t, /* Source MAC */
  1314. uint64_t, /* Destination MAC */
  1315. unsigned int, /* Ethernet type */
  1316. unsigned int, /* VLAN ID (0 for none) */
  1317. const void *, /* Frame data */
  1318. unsigned int); /* Frame length */
  1319. /**
  1320. * Callback for events
  1321. *
  1322. * Events are generated when the node's status changes in a significant way
  1323. * and on certain non-fatal errors and events of interest. The final void
  1324. * parameter points to event meta-data. The type of event meta-data (and
  1325. * whether it is present at all) is event type dependent. See the comments
  1326. * in the definition of ZT_Event.
  1327. */
  1328. typedef void (*ZT_EventCallback)(
  1329. ZT_Node *, /* Node */
  1330. void *, /* User ptr */
  1331. void *, /* Thread ptr */
  1332. enum ZT_Event, /* Event type */
  1333. const void *); /* Event payload (if applicable) */
  1334. /**
  1335. * Callback for storing and/or publishing state information
  1336. *
  1337. * See ZT_StateObjectType docs for information about each state object type
  1338. * and when and if it needs to be persisted.
  1339. *
  1340. * An object of length -1 is sent to indicate that an object should be
  1341. * deleted.
  1342. */
  1343. typedef void (*ZT_StatePutFunction)(
  1344. ZT_Node *, /* Node */
  1345. void *, /* User ptr */
  1346. void *, /* Thread ptr */
  1347. enum ZT_StateObjectType, /* State object type */
  1348. const uint64_t [2], /* State object ID (if applicable) */
  1349. const void *, /* State object data */
  1350. int); /* Length of data or -1 to delete */
  1351. /**
  1352. * Callback for retrieving stored state information
  1353. *
  1354. * This function should return the number of bytes actually stored to the
  1355. * buffer or -1 if the state object was not found. The buffer itself should
  1356. * be set to point to the data, and the last result parameter must point to
  1357. * a function that will be used to free the buffer when the core is done
  1358. * with it. This is very often just a pointer to free().
  1359. */
  1360. typedef int (*ZT_StateGetFunction)(
  1361. ZT_Node *, /* Node */
  1362. void *, /* User ptr */
  1363. void *, /* Thread ptr */
  1364. enum ZT_StateObjectType, /* State object type */
  1365. const uint64_t [2], /* State object ID (if applicable) */
  1366. void **, /* Result parameter: data */
  1367. void (**)(void *)); /* Result parameter: data free function */
  1368. /**
  1369. * Function to send a ZeroTier packet out over the physical wire (L2/L3)
  1370. *
  1371. * Parameters:
  1372. * (1) Node
  1373. * (2) User pointer
  1374. * (3) Local socket or -1 for "all" or "any"
  1375. * (4) Remote address
  1376. * (5) Packet data
  1377. * (6) Packet length
  1378. * (7) Desired IP TTL or 0 to use default
  1379. *
  1380. * If there is only one local socket, the local socket can be ignored.
  1381. * If the local socket is -1, the packet should be sent out from all
  1382. * bound local sockets or a random bound local socket.
  1383. *
  1384. * If TTL is nonzero, packets should have their IP TTL value set to this
  1385. * value if possible. If this is not possible it is acceptable to ignore
  1386. * this value and send anyway with normal or default TTL.
  1387. *
  1388. * The function must return zero on success and may return any error code
  1389. * on failure. Note that success does not (of course) guarantee packet
  1390. * delivery. It only means that the packet appears to have been sent.
  1391. */
  1392. typedef int (*ZT_WirePacketSendFunction)(
  1393. ZT_Node *, /* Node */
  1394. void *, /* User ptr */
  1395. void *, /* Thread ptr */
  1396. int64_t, /* Local socket */
  1397. const struct sockaddr_storage *, /* Remote address */
  1398. const void *, /* Packet data */
  1399. unsigned int, /* Packet length */
  1400. unsigned int); /* TTL or 0 to use default */
  1401. /**
  1402. * Function to check whether a path should be used for ZeroTier traffic
  1403. *
  1404. * Parameters:
  1405. * (1) Node
  1406. * (2) User pointer
  1407. * (3) ZeroTier address or 0 for none/any
  1408. * (4) Full identity or NULL for none/any
  1409. * (5) Local socket or -1 if unknown
  1410. * (6) Remote address
  1411. *
  1412. * This function must return nonzero (true) if the path should be used.
  1413. *
  1414. * If no path check function is specified, ZeroTier will still exclude paths
  1415. * that overlap with ZeroTier-assigned and managed IP address blocks. But the
  1416. * use of a path check function is recommended to ensure that recursion does
  1417. * not occur in cases where addresses are assigned by the OS or managed by
  1418. * an out of band mechanism like DHCP. The path check function should examine
  1419. * all configured ZeroTier interfaces and check to ensure that the supplied
  1420. * addresses will not result in ZeroTier traffic being sent over a ZeroTier
  1421. * interface (recursion).
  1422. */
  1423. typedef int (*ZT_PathCheckFunction)(
  1424. ZT_Node *, /* Node */
  1425. void *, /* User ptr */
  1426. void *, /* Thread ptr */
  1427. uint64_t, /* ZeroTier address */
  1428. const ZT_Identity *, /* Full identity of node */
  1429. int64_t, /* Local socket or -1 if unknown */
  1430. const struct sockaddr_storage *); /* Remote address */
  1431. /**
  1432. * Function to get physical addresses for ZeroTier peers
  1433. *
  1434. * Parameters:
  1435. * (1) Node
  1436. * (2) User pointer
  1437. * (3) ZeroTier address (least significant 40 bits)
  1438. * (4) Identity in string form
  1439. * (5) Desired address family or -1 for any
  1440. * (6) Buffer to fill with result
  1441. *
  1442. * If provided this function will be occasionally called to get physical
  1443. * addresses that might be tried to reach a ZeroTier address. It must
  1444. * return a nonzero (true) value if the result buffer has been filled
  1445. * with an address.
  1446. */
  1447. typedef int (*ZT_PathLookupFunction)(
  1448. ZT_Node *, /* Node */
  1449. void *, /* User ptr */
  1450. void *, /* Thread ptr */
  1451. uint64_t, /* ZeroTier address (40 bits) */
  1452. const ZT_Identity *, /* Full identity of node */
  1453. int, /* Desired ss_family or -1 for any */
  1454. struct sockaddr_storage *); /* Result buffer */
  1455. /****************************************************************************/
  1456. /**
  1457. * Structure for configuring ZeroTier core callback functions
  1458. */
  1459. struct ZT_Node_Callbacks
  1460. {
  1461. /**
  1462. * REQUIRED: Function to store and/or replicate state objects
  1463. */
  1464. ZT_StatePutFunction statePutFunction;
  1465. /**
  1466. * REQUIRED: Function to retrieve state objects from an object store
  1467. */
  1468. ZT_StateGetFunction stateGetFunction;
  1469. /**
  1470. * REQUIRED: Function to send packets over the physical wire
  1471. */
  1472. ZT_WirePacketSendFunction wirePacketSendFunction;
  1473. /**
  1474. * REQUIRED: Function to inject frames into a virtual network's TAP
  1475. */
  1476. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction;
  1477. /**
  1478. * REQUIRED: Function to be called when virtual networks are configured or changed
  1479. */
  1480. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction;
  1481. /**
  1482. * REQUIRED: Function to be called to notify external code of important events
  1483. */
  1484. ZT_EventCallback eventCallback;
  1485. /**
  1486. * OPTIONAL: Function to check whether a given physical path should be used for ZeroTier traffic
  1487. */
  1488. ZT_PathCheckFunction pathCheckFunction;
  1489. /**
  1490. * RECOMMENDED: Function to look up paths to ZeroTier nodes
  1491. */
  1492. ZT_PathLookupFunction pathLookupFunction;
  1493. };
  1494. /**
  1495. * Create a new ZeroTier node
  1496. *
  1497. * This will attempt to load its identity via the state get function in the
  1498. * callback struct. If that fails it will generate a new identity and store
  1499. * it. Identity generation can take anywhere from a few hundred milliseconds
  1500. * to a few seconds depending on your CPU speed.
  1501. *
  1502. * @param node Result: pointer is set to new node instance on success
  1503. * @param uptr User pointer to pass to functions/callbacks
  1504. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1505. * @param callbacks Callback function configuration
  1506. * @param now Current clock in milliseconds
  1507. * @return OK (0) or error code if a fatal error condition has occurred
  1508. */
  1509. ZT_SDK_API enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now);
  1510. /**
  1511. * Delete a node and free all resources it consumes
  1512. *
  1513. * If you are using multiple threads, all other threads must be shut down
  1514. * first. This can crash if processXXX() methods are in progress.
  1515. *
  1516. * @param node Node to delete
  1517. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1518. */
  1519. ZT_SDK_API void ZT_Node_delete(ZT_Node *node,void *tptr);
  1520. /**
  1521. * Process a packet received from the physical wire
  1522. *
  1523. * @param node Node instance
  1524. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1525. * @param now Current clock in milliseconds
  1526. * @param localSocket Local socket (you can use 0 if only one local socket is bound and ignore this)
  1527. * @param remoteAddress Origin of packet
  1528. * @param packetData Packet data
  1529. * @param packetLength Packet length
  1530. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1531. * @return OK (0) or error code if a fatal error condition has occurred
  1532. */
  1533. ZT_SDK_API enum ZT_ResultCode ZT_Node_processWirePacket(
  1534. ZT_Node *node,
  1535. void *tptr,
  1536. int64_t now,
  1537. int64_t localSocket,
  1538. const struct sockaddr_storage *remoteAddress,
  1539. const void *packetData,
  1540. unsigned int packetLength,
  1541. volatile int64_t *nextBackgroundTaskDeadline);
  1542. /**
  1543. * Process a frame from a virtual network port (tap)
  1544. *
  1545. * @param node Node instance
  1546. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1547. * @param now Current clock in milliseconds
  1548. * @param nwid ZeroTier 64-bit virtual network ID
  1549. * @param sourceMac Source MAC address (least significant 48 bits)
  1550. * @param destMac Destination MAC address (least significant 48 bits)
  1551. * @param etherType 16-bit Ethernet frame type
  1552. * @param vlanId 10-bit VLAN ID or 0 if none
  1553. * @param frameData Frame payload data
  1554. * @param frameLength Frame payload length
  1555. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1556. * @return OK (0) or error code if a fatal error condition has occurred
  1557. */
  1558. ZT_SDK_API enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  1559. ZT_Node *node,
  1560. void *tptr,
  1561. int64_t now,
  1562. uint64_t nwid,
  1563. uint64_t sourceMac,
  1564. uint64_t destMac,
  1565. unsigned int etherType,
  1566. unsigned int vlanId,
  1567. const void *frameData,
  1568. unsigned int frameLength,
  1569. volatile int64_t *nextBackgroundTaskDeadline);
  1570. /**
  1571. * Perform periodic background operations
  1572. *
  1573. * @param node Node instance
  1574. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1575. * @param now Current clock in milliseconds
  1576. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1577. * @return OK (0) or error code if a fatal error condition has occurred
  1578. */
  1579. ZT_SDK_API enum ZT_ResultCode ZT_Node_processBackgroundTasks(
  1580. ZT_Node *node,
  1581. void *tptr,
  1582. int64_t now,
  1583. volatile int64_t *nextBackgroundTaskDeadline);
  1584. /**
  1585. * Join a network
  1586. *
  1587. * This may generate calls to the port config callback before it returns,
  1588. * or these may be differed if a netconf is not available yet.
  1589. *
  1590. * If we are already a member of the network, nothing is done and OK is
  1591. * returned.
  1592. *
  1593. * @param node Node instance
  1594. * @param nwid 64-bit ZeroTier network ID
  1595. * @param uptr An arbitrary pointer to associate with this network (default: NULL)
  1596. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1597. * @return OK (0) or error code if a fatal error condition has occurred
  1598. */
  1599. ZT_SDK_API enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr);
  1600. /**
  1601. * Leave a network
  1602. *
  1603. * If a port has been configured for this network this will generate a call
  1604. * to the port config callback with a NULL second parameter to indicate that
  1605. * the port is now deleted.
  1606. *
  1607. * The uptr parameter is optional and is NULL by default. If it is not NULL,
  1608. * the pointer it points to is set to this network's uptr on success.
  1609. *
  1610. * @param node Node instance
  1611. * @param nwid 64-bit network ID
  1612. * @param uptr Target pointer is set to uptr (if not NULL)
  1613. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1614. * @return OK (0) or error code if a fatal error condition has occurred
  1615. */
  1616. ZT_SDK_API enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr);
  1617. /**
  1618. * Subscribe to an Ethernet multicast group
  1619. *
  1620. * ADI stands for additional distinguishing information. This defaults to zero
  1621. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1622. * and this must be done.
  1623. *
  1624. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1625. * broadcast address) but with an ADI equal to each IPv4 address in host
  1626. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1627. * a scalable multicast protocol with perfect address specificity.
  1628. *
  1629. * If this is not done, ARP will not work reliably.
  1630. *
  1631. * Multiple calls to subscribe to the same multicast address will have no
  1632. * effect. It is perfectly safe to do this.
  1633. *
  1634. * This does not generate an update call to networkConfigCallback().
  1635. *
  1636. * @param node Node instance
  1637. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1638. * @param nwid 64-bit network ID
  1639. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1640. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1641. * @return OK (0) or error code if a fatal error condition has occurred
  1642. */
  1643. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1644. /**
  1645. * Unsubscribe from an Ethernet multicast group (or all groups)
  1646. *
  1647. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1648. * you are not subscribed to a group this has no effect.
  1649. *
  1650. * This does not generate an update call to networkConfigCallback().
  1651. *
  1652. * @param node Node instance
  1653. * @param nwid 64-bit network ID
  1654. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1655. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1656. * @return OK (0) or error code if a fatal error condition has occurred
  1657. */
  1658. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1659. /**
  1660. * Add a root server (has no effect if already added)
  1661. *
  1662. * @param node Node instance
  1663. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1664. * @param identity Identity of this root server
  1665. * @param bootstrap Optional bootstrap address for initial contact
  1666. * @return OK (0) or error code if a fatal error condition has occurred
  1667. */
  1668. ZT_SDK_API enum ZT_ResultCode ZT_Node_addRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity,const struct sockaddr_storage *bootstrap);
  1669. /**
  1670. * Remove a root server
  1671. *
  1672. * This removes this node's root designation but does not prevent this node
  1673. * from communicating with it or close active paths to it.
  1674. *
  1675. * @param node Node instance
  1676. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1677. * @param identity Identity to remove
  1678. * @return OK (0) or error code if a fatal error condition has occurred
  1679. */
  1680. ZT_SDK_API enum ZT_ResultCode ZT_Node_removeRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity);
  1681. /**
  1682. * Get this node's 40-bit ZeroTier address
  1683. *
  1684. * @param node Node instance
  1685. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1686. */
  1687. ZT_SDK_API uint64_t ZT_Node_address(ZT_Node *node);
  1688. /**
  1689. * Get this node's identity
  1690. *
  1691. * The identity pointer returned by this function need not and should not be
  1692. * freed with ZT_Identity_delete(). It's valid until the node is deleted.
  1693. *
  1694. * @param node Node instance
  1695. * @return Identity
  1696. */
  1697. ZT_SDK_API const ZT_Identity *ZT_Node_identity(ZT_Node *node);
  1698. /**
  1699. * Get the status of this node
  1700. *
  1701. * @param node Node instance
  1702. * @param status Buffer to fill with current node status
  1703. */
  1704. ZT_SDK_API void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status);
  1705. /**
  1706. * Get a list of known peer nodes
  1707. *
  1708. * The pointer returned here must be freed with freeQueryResult()
  1709. * when you are done with it.
  1710. *
  1711. * @param node Node instance
  1712. * @return List of known peers or NULL on failure
  1713. */
  1714. ZT_SDK_API ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1715. /**
  1716. * Get the status of a virtual network
  1717. *
  1718. * The pointer returned here must be freed with freeQueryResult()
  1719. * when you are done with it.
  1720. *
  1721. * @param node Node instance
  1722. * @param nwid 64-bit network ID
  1723. * @return Network configuration or NULL if we are not a member of this network
  1724. */
  1725. ZT_SDK_API ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid);
  1726. /**
  1727. * Enumerate and get status of all networks
  1728. *
  1729. * @param node Node instance
  1730. * @return List of networks or NULL on failure
  1731. */
  1732. ZT_SDK_API ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1733. /**
  1734. * Set the network-associated user-defined pointer for a given network
  1735. *
  1736. * This will have no effect if the network ID is not recognized.
  1737. *
  1738. * @param node Node instance
  1739. * @param nwid Network ID
  1740. * @param ptr New network-associated pointer
  1741. */
  1742. ZT_SDK_API void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr);
  1743. /**
  1744. * Free a query result buffer
  1745. *
  1746. * Use this to free the return values of listNetworks(), listPeers(), etc.
  1747. *
  1748. * @param node Node instance
  1749. * @param qr Query result buffer
  1750. */
  1751. ZT_SDK_API void ZT_Node_freeQueryResult(ZT_Node *node,void *qr);
  1752. /**
  1753. * Set external interface addresses where this node could be reached
  1754. *
  1755. * @param node Node instance
  1756. * @param addrs Addresses
  1757. * @param addrCount Number of items in addrs[]
  1758. */
  1759. ZT_SDK_API void ZT_Node_setInterfaceAddresses(ZT_Node *node,const ZT_InterfaceAddress *addrs,unsigned int addrCount);
  1760. /**
  1761. * Send a VERB_USER_MESSAGE to another ZeroTier node
  1762. *
  1763. * There is no delivery guarantee here. Failure can occur if the message is
  1764. * too large or if dest is not a valid ZeroTier address.
  1765. *
  1766. * @param node Node instance
  1767. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1768. * @param dest Destination ZeroTier address
  1769. * @param typeId VERB_USER_MESSAGE type ID
  1770. * @param data Payload data to attach to user message
  1771. * @param len Length of data in bytes
  1772. * @return Boolean: non-zero on success, zero on failure
  1773. */
  1774. ZT_SDK_API int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
  1775. /**
  1776. * Set a network controller instance for this node
  1777. *
  1778. * Normal nodes should not need to use this. This is for nodes with
  1779. * special compiled-in support for acting as network configuration
  1780. * masters / controllers.
  1781. *
  1782. * The supplied instance must be a C++ object that inherits from the
  1783. * NetworkConfigMaster base class in node/. No type checking is performed,
  1784. * so a pointer to anything else will result in a crash.
  1785. *
  1786. * @param node ZertTier One node
  1787. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  1788. * @return OK (0) or error code if a fatal error condition has occurred
  1789. */
  1790. ZT_SDK_API void ZT_Node_setController(ZT_Node *node,void *networkConfigMasterInstance);
  1791. /**
  1792. * Set configuration for a given physical path
  1793. *
  1794. * @param node Node instance
  1795. * @param pathNetwork Network/CIDR of path or NULL to clear the cache and reset all paths to default
  1796. * @param pathConfig Path configuration or NULL to erase this entry and therefore reset it to NULL
  1797. * @return OK or error code
  1798. */
  1799. ZT_SDK_API enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig);
  1800. /****************************************************************************/
  1801. /**
  1802. * Generate a new identity
  1803. *
  1804. * Due to a small amount of proof of work this can be a time consuming and CPU
  1805. * intensive operation. It takes less than a second on most desktop-class systems
  1806. * but can take longer on e.g. phones.
  1807. *
  1808. * @param type Type of identity to generate
  1809. * @return New identity or NULL on error
  1810. */
  1811. ZT_SDK_API ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type);
  1812. /**
  1813. * Create a new identity object from a string-serialized identity
  1814. *
  1815. * @param idStr Identity in string format
  1816. * @return Identity object or NULL if the supplied identity string was not valid
  1817. */
  1818. ZT_SDK_API ZT_Identity *ZT_Identity_fromString(const char *idStr);
  1819. /**
  1820. * Validate this identity
  1821. *
  1822. * This can be slightly time consuming due to address derivation (work) checking.
  1823. *
  1824. * @return Non-zero if identity is valid
  1825. */
  1826. ZT_SDK_API int ZT_Identity_validate(const ZT_Identity *id);
  1827. /**
  1828. * Sign a data object with this identity
  1829. *
  1830. * The identity must have a private key or this will fail.
  1831. *
  1832. * @param id Identity to use to sign
  1833. * @param data Data to sign
  1834. * @param len Length of data
  1835. * @param signature Buffer to store signature
  1836. * @param signatureBufferLength Length of buffer (must be at least 96 bytes)
  1837. * @return Length of signature in bytes or 0 on failure.
  1838. */
  1839. ZT_SDK_API unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength);
  1840. /**
  1841. * Verify a signature
  1842. *
  1843. * @param id Identity to use to verify
  1844. * @param data Data to verify
  1845. * @param len Length of data
  1846. * @param signature Signature to check
  1847. * @param sigLen Length of signature in bytes
  1848. * @return Non-zero if signature is valid
  1849. */
  1850. ZT_SDK_API int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen);
  1851. /**
  1852. * Get identity type
  1853. *
  1854. * @param id Identity to query
  1855. * @return Identity type code
  1856. */
  1857. ZT_SDK_API enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id);
  1858. /**
  1859. * Convert an identity to its string representation
  1860. *
  1861. * @param id Identity to convert
  1862. * @param buf Buffer to store identity (should be at least about 1024 bytes in length)
  1863. * @param capacity Capacity of buffer
  1864. * @param includePrivate If true include the private key if present
  1865. * @return Pointer to buf or NULL on overflow or other error
  1866. */
  1867. ZT_SDK_API char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate);
  1868. /**
  1869. * Check whether this identity object also holds a private key
  1870. *
  1871. * @param id Identity to query
  1872. * @return Non-zero if a private key is held
  1873. */
  1874. ZT_SDK_API int ZT_Identity_hasPrivate(const ZT_Identity *id);
  1875. /**
  1876. * Get the ZeroTier address associated with this identity
  1877. *
  1878. * @param id Identity to query
  1879. * @return ZeroTier address (only least significant 40 bits are meaningful, rest will be 0)
  1880. */
  1881. ZT_SDK_API uint64_t ZT_Identity_address(const ZT_Identity *id);
  1882. /**
  1883. * Compute a hash of this identity's public keys (or both public and private if includePrivate is true)
  1884. *
  1885. * @param id Identity to query
  1886. * @param h Buffer for 384-bit hash
  1887. * @param includePrivate If true include private keys if any
  1888. */
  1889. ZT_SDK_API void ZT_Identity_hash(const ZT_Identity *id,uint8_t h[48],int includePrivate);
  1890. /**
  1891. * Delete an identity and free associated memory
  1892. *
  1893. * This should only be used with identities created via Identity_new
  1894. * and Identity_fromString().
  1895. *
  1896. * @param id Identity to delete
  1897. */
  1898. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id);
  1899. /****************************************************************************/
  1900. /**
  1901. * Get ZeroTier One version
  1902. *
  1903. * @param major Result: major version
  1904. * @param minor Result: minor version
  1905. * @param revision Result: revision
  1906. */
  1907. ZT_SDK_API void ZT_version(int *major,int *minor,int *revision);
  1908. #ifdef __cplusplus
  1909. }
  1910. #endif
  1911. #endif