ZeroTierCore.h 65 KB

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