ZeroTierCore.h 65 KB

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