2
0

zerotier.h 66 KB

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