ZeroTierCore.h 65 KB

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