ZeroTierCore.h 65 KB

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