ZeroTierCore.h 65 KB

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