zerotier.h 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  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: 2025-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. #include <stdint.h>
  29. #include <stdlib.h>
  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 extern
  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_RAW_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. * Do not change these integer values. They're protocol constants.
  217. */
  218. enum ZT_IdentityType
  219. {
  220. ZT_IDENTITY_TYPE_C25519 = 0, /* C25519/Ed25519 */
  221. ZT_IDENTITY_TYPE_P384 = 1 /* Combined C25519/NIST-P-384 key */
  222. };
  223. /**
  224. * ZeroTier identity (address plus keys)
  225. */
  226. typedef void ZT_Identity;
  227. /**
  228. * Locator is a signed list of endpoints
  229. */
  230. typedef void ZT_Locator;
  231. /**
  232. * Full identity fingerprint with address and 384-bit hash of public key(s)
  233. */
  234. typedef struct
  235. {
  236. /**
  237. * Short address (only least significant 40 bits are used)
  238. */
  239. uint64_t address;
  240. /**
  241. * 384-bit hash of identity public key(s)
  242. */
  243. uint8_t hash[48];
  244. } ZT_Fingerprint;
  245. /**
  246. * Maximum length of string fields in certificates
  247. */
  248. #define ZT_CERTIFICATE_MAX_STRING_LENGTH 127
  249. /**
  250. * Certificate is a root CA (local trust flag)
  251. */
  252. #define ZT_CERTIFICATE_LOCAL_TRUST_FLAG_ROOT_CA 0x0001U
  253. /**
  254. * Certificate's subject describes a set of roots (local trust flag)
  255. */
  256. #define ZT_CERTIFICATE_LOCAL_TRUST_FLAG_ZEROTIER_ROOT_SET 0x0002U
  257. /**
  258. * Size of a unique ID of the given key type (with type prefix byte)
  259. */
  260. #define ZT_CERTIFICATE_UNIQUE_ID_TYPE_NIST_P_384_SIZE 50
  261. /**
  262. * Size of the private key corresponding to a unique ID of the given type.
  263. */
  264. #define ZT_CERTIFICATE_UNIQUE_ID_TYPE_NIST_P_384_PRIVATE_SIZE 48
  265. /**
  266. * Unique ID types supported for certificate subject unique IDs
  267. */
  268. enum ZT_CertificateUniqueIdType
  269. {
  270. /**
  271. * Public key type for NIST P-384 public keys used as subject unique IDs.
  272. */
  273. ZT_CERTIFICATE_UNIQUE_ID_TYPE_NIST_P_384 = 1
  274. };
  275. /**
  276. * Errors returned by functions that verify or handle certificates.
  277. */
  278. enum ZT_CertificateError
  279. {
  280. /**
  281. * No error (certificate is valid or operation was successful)
  282. */
  283. ZT_CERTIFICATE_ERROR_NONE = 0,
  284. /**
  285. * A newer certificate with the same issuer and subject serial plus CN exists.
  286. */
  287. ZT_CERTIFICATE_ERROR_HAVE_NEWER_CERT = 1,
  288. /**
  289. * Certificate format is invalid or required fields are missing
  290. */
  291. ZT_CERTIFICATE_ERROR_INVALID_FORMAT = -1,
  292. /**
  293. * One or more identities in the certificate are invalid or fail consistency check
  294. */
  295. ZT_CERTIFICATE_ERROR_INVALID_IDENTITY = -2,
  296. /**
  297. * Certificate primary signature is invalid
  298. */
  299. ZT_CERTIFICATE_ERROR_INVALID_PRIMARY_SIGNATURE = -3,
  300. /**
  301. * Full chain validation of certificate failed
  302. */
  303. ZT_CERTIFICATE_ERROR_INVALID_CHAIN = -4,
  304. /**
  305. * One or more signed components (e.g. a Locator) has an invalid signature.
  306. */
  307. ZT_CERTIFICATE_ERROR_INVALID_COMPONENT_SIGNATURE = -5,
  308. /**
  309. * Unique ID proof signature in subject was not valid.
  310. */
  311. ZT_CERTIFICATE_ERROR_INVALID_UNIQUE_ID_PROOF = -6,
  312. /**
  313. * Certificate is missing a required field
  314. */
  315. ZT_CERTIFICATE_ERROR_MISSING_REQUIRED_FIELDS = -7,
  316. /**
  317. * Certificate is expired or not yet in effect
  318. */
  319. ZT_CERTIFICATE_ERROR_OUT_OF_VALID_TIME_WINDOW = -8
  320. };
  321. /**
  322. * Information about a real world entity.
  323. *
  324. * These fields are all optional and are all taken from the
  325. * most common fields present in X509 certificates.
  326. */
  327. typedef struct
  328. {
  329. char serialNo[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  330. char commonName[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  331. char country[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  332. char organization[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  333. char unit[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  334. char locality[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  335. char province[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  336. char streetAddress[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  337. char postalCode[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  338. char email[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  339. char url[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  340. char host[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  341. } ZT_Certificate_Name;
  342. /**
  343. * Identity and optional locator to help find a node on physical networks.
  344. */
  345. typedef struct
  346. {
  347. /**
  348. * Identity (never NULL)
  349. */
  350. const ZT_Identity *identity;
  351. /**
  352. * Locator, or NULL if none
  353. */
  354. const ZT_Locator *locator;
  355. } ZT_Certificate_Identity;
  356. /**
  357. * ID and primary controller for a network
  358. */
  359. typedef struct
  360. {
  361. /**
  362. * Network ID
  363. */
  364. uint64_t id;
  365. /**
  366. * Full fingerprint of primary controller
  367. */
  368. ZT_Fingerprint controller;
  369. } ZT_Certificate_Network;
  370. /**
  371. * Identification certificate subject
  372. */
  373. typedef struct
  374. {
  375. /**
  376. * Timestamp of subject, can also be a revision ID for this subject's name.
  377. */
  378. int64_t timestamp;
  379. /**
  380. * Identities and optional locators of nodes
  381. */
  382. ZT_Certificate_Identity *identities;
  383. /**
  384. * Networks owned by this entity
  385. */
  386. ZT_Certificate_Network *networks;
  387. /**
  388. * Serial numbers of other certificates being signed (each is 48 bytes / 384 bits)
  389. */
  390. const uint8_t *const *certificates;
  391. /**
  392. * URLs that can be consulted for updates to this certificate.
  393. */
  394. const char *const *updateURLs;
  395. /**
  396. * Number of identities
  397. */
  398. unsigned int identityCount;
  399. /**
  400. * Number of networks
  401. */
  402. unsigned int networkCount;
  403. /**
  404. * Number of certificates
  405. */
  406. unsigned int certificateCount;
  407. /**
  408. * Number of update URLs
  409. */
  410. unsigned int updateURLCount;
  411. /**
  412. * Information about owner of items.
  413. */
  414. ZT_Certificate_Name name;
  415. /**
  416. * Globally unique ID for this subject
  417. *
  418. * Unique IDs are actually public keys. Their size makes them globally
  419. * unique (if generated from good randomness) to within ridiculous
  420. * probability bounds. If a subject has a unique ID it must also have
  421. * a unique ID proof signature, which is the signature of the subject
  422. * with the private key corresponding to its unique ID.
  423. *
  424. * This allows subjects to "own" themselves and exist independent of
  425. * CAs or delegated signers. It also allows a certificate for a given
  426. * subject to be updated.
  427. *
  428. * Subject unique IDs are optional. If no unique ID is specified these
  429. * and their corresponding size fields must be empty/zero.
  430. *
  431. * A subject is valid if it has no unique ID or has one with a valid
  432. * proof signature.
  433. */
  434. const uint8_t *uniqueId;
  435. /**
  436. * Signature proving ownership of unique ID.
  437. */
  438. const uint8_t *uniqueIdProofSignature;
  439. /**
  440. * Size of unique ID in bytes or 0 if none.
  441. */
  442. unsigned int uniqueIdSize;
  443. /**
  444. * Proof signature size or 0 if none.
  445. */
  446. unsigned int uniqueIdProofSignatureSize;
  447. } ZT_Certificate_Subject;
  448. /**
  449. * Certificate
  450. *
  451. * This is designed to be compatible with x509 certificate interfaces,
  452. * presenting similar concepts and fields.
  453. *
  454. * It's not X509 because we want to keep ZeroTier clean, as simple as
  455. * possible, small, and secure. X509 is both bloated and a security
  456. * disaster as it's very hard to implement correctly.
  457. */
  458. typedef struct
  459. {
  460. /**
  461. * Serial number, a SHA384 hash of this certificate.
  462. */
  463. uint8_t serialNo[48];
  464. /**
  465. * Flags indicating certificate usage and any other attributes.
  466. */
  467. uint64_t flags;
  468. /**
  469. * Certificate timestamp in milliseconds since epoch.
  470. */
  471. int64_t timestamp;
  472. /**
  473. * Valid time range: not before, not after.
  474. */
  475. int64_t validity[2];
  476. /**
  477. * Subject of certificate
  478. */
  479. ZT_Certificate_Subject subject;
  480. /**
  481. * Issuer node identity and public key(s).
  482. */
  483. const ZT_Identity *issuer;
  484. /**
  485. * Issuer information
  486. */
  487. ZT_Certificate_Name issuerName;
  488. /**
  489. * Extended attributes set by issuer (in Dictionary format, NULL if none)
  490. */
  491. const uint8_t *extendedAttributes;
  492. /**
  493. * Size of extended attributes field in bytes
  494. */
  495. unsigned int extendedAttributesSize;
  496. /**
  497. * Maximum path length from this certificate toward further certificates.
  498. *
  499. * Subjects may sign other certificates whose path lengths are less than
  500. * this value. A value of zero indicates that no identification certificates
  501. * may be signed (not a CA).
  502. */
  503. unsigned int maxPathLength;
  504. /**
  505. * List of certificate serial numbers being revoked.
  506. */
  507. const uint8_t *const *crl;
  508. /**
  509. * Number of 48-byte serial numbers in crl list.
  510. */
  511. unsigned int crlCount;
  512. /**
  513. * Signature by issuer (algorithm determined by identity type).
  514. */
  515. const uint8_t *signature;
  516. /**
  517. * Size of signature in bytes.
  518. */
  519. unsigned int signatureSize;
  520. } ZT_Certificate;
  521. /**
  522. * A list of certificates
  523. */
  524. typedef struct
  525. {
  526. /**
  527. * Function that is called to free this list (called by ZT_freeQueryResult)
  528. */
  529. void (*freeFunction)(const void *);
  530. /**
  531. * Array of pointers to certificates
  532. */
  533. const ZT_Certificate *const *certs;
  534. /**
  535. * Array of local trust flags for each certificate
  536. */
  537. const unsigned int *localTrust;
  538. /**
  539. * Number of certificates
  540. */
  541. unsigned long certCount;
  542. } ZT_CertificateList;
  543. /**
  544. * Credential type IDs
  545. */
  546. enum ZT_CredentialType
  547. {
  548. ZT_CREDENTIAL_TYPE_NULL = 0,
  549. ZT_CREDENTIAL_TYPE_COM = 1,
  550. ZT_CREDENTIAL_TYPE_CAPABILITY = 2,
  551. ZT_CREDENTIAL_TYPE_TAG = 3,
  552. ZT_CREDENTIAL_TYPE_COO = 4,
  553. ZT_CREDENTIAL_TYPE_REVOCATION = 6
  554. };
  555. /**
  556. * Endpoint address and protocol types
  557. *
  558. * Most of these are not currently implemented and are just reserved
  559. * for future use.
  560. */
  561. enum ZT_EndpointType
  562. {
  563. ZT_ENDPOINT_TYPE_NIL = 0, /* Nil/empty endpoint */
  564. ZT_ENDPOINT_TYPE_ZEROTIER = 1, /* ZeroTier relaying (address+fingerprint) */
  565. ZT_ENDPOINT_TYPE_ETHERNET = 2, /* Ethernet with ethertype 0x9993 */
  566. ZT_ENDPOINT_TYPE_WIFI_DIRECT = 3, /* Ethernet using WiFi direct */
  567. ZT_ENDPOINT_TYPE_BLUETOOTH = 4, /* Bluetooth (same address type as Ethernet) */
  568. ZT_ENDPOINT_TYPE_IP = 5, /* Naked IP (protocol 193) */
  569. ZT_ENDPOINT_TYPE_IP_UDP = 6, /* IP/UDP */
  570. ZT_ENDPOINT_TYPE_IP_TCP = 7, /* IP/TCP */
  571. ZT_ENDPOINT_TYPE_IP_HTTP = 8 /* IP/HTTP encapsulation */
  572. };
  573. /**
  574. * Flag indicating that VL1 tracing should be generated
  575. */
  576. #define ZT_TRACE_FLAG_VL1 0x01
  577. /**
  578. * Flag indicating that VL2 (virtual network) tracing should be generated
  579. */
  580. #define ZT_TRACE_FLAG_VL2 0x02
  581. /**
  582. * Flag indicating that VL2 network filter tracing should be generated (separate because this can be very verbose)
  583. */
  584. #define ZT_TRACE_FLAG_VL2_FILTER 0x04
  585. /**
  586. * Flag indicating that VL2 multicast propagation should be reported
  587. */
  588. #define ZT_TRACE_FLAG_VL2_MULTICAST 0x08
  589. /**
  590. * Trace event types
  591. *
  592. * All trace event structures start with a size and type.
  593. */
  594. enum ZT_TraceEventType
  595. {
  596. ZT_TRACE_UNEXPECTED_ERROR = 0,
  597. ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE = 1,
  598. ZT_TRACE_VL1_TRYING_NEW_PATH = 2,
  599. ZT_TRACE_VL1_LEARNED_NEW_PATH = 3,
  600. ZT_TRACE_VL1_INCOMING_PACKET_DROPPED = 4,
  601. ZT_TRACE_VL2_OUTGOING_FRAME_DROPPED = 100,
  602. ZT_TRACE_VL2_INCOMING_FRAME_DROPPED = 101,
  603. ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED = 102,
  604. ZT_TRACE_VL2_NETWORK_FILTER = 103
  605. };
  606. /**
  607. * Trace VL1 packet drop reasons
  608. */
  609. enum ZT_TracePacketDropReason
  610. {
  611. ZT_TRACE_PACKET_DROP_REASON_UNSPECIFIED = 0,
  612. ZT_TRACE_PACKET_DROP_REASON_PEER_TOO_OLD = 1,
  613. ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET = 2,
  614. ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED = 3,
  615. ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED = 4,
  616. ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT = 5,
  617. ZT_TRACE_PACKET_DROP_REASON_INVALID_COMPRESSED_DATA = 6,
  618. ZT_TRACE_PACKET_DROP_REASON_UNRECOGNIZED_VERB = 7,
  619. ZT_TRACE_PACKET_DROP_REASON_REPLY_NOT_EXPECTED = 8
  620. };
  621. /**
  622. * Trace VL2 frame drop reasons
  623. */
  624. enum ZT_TraceFrameDropReason
  625. {
  626. ZT_TRACE_FRAME_DROP_REASON_UNSPECIFIED = 0,
  627. ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_REMOTE = 1,
  628. ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_LOCAL = 2,
  629. ZT_TRACE_FRAME_DROP_REASON_MULTICAST_DISABLED = 3,
  630. ZT_TRACE_FRAME_DROP_REASON_BROADCAST_DISABLED = 4,
  631. ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED = 5,
  632. ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED_AT_BRIDGE_REPLICATION = 6,
  633. ZT_TRACE_FRAME_DROP_REASON_PERMISSION_DENIED = 7
  634. };
  635. /**
  636. * Reasons for credential rejection
  637. */
  638. enum ZT_TraceCredentialRejectionReason
  639. {
  640. ZT_TRACE_CREDENTIAL_REJECTION_REASON_SIGNATURE_VERIFICATION_FAILED = 1,
  641. ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED = 2,
  642. ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST = 3,
  643. ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID = 4
  644. };
  645. #define ZT_TRACE_FIELD_TYPE "t"
  646. #define ZT_TRACE_FIELD_CODE_LOCATION "c"
  647. #define ZT_TRACE_FIELD_ENDPOINT "e"
  648. #define ZT_TRACE_FIELD_OLD_ENDPOINT "oe"
  649. #define ZT_TRACE_FIELD_NEW_ENDPOINT "ne"
  650. #define ZT_TRACE_FIELD_TRIGGER_FROM_ENDPOINT "te"
  651. #define ZT_TRACE_FIELD_TRIGGER_FROM_PACKET_ID "ti"
  652. #define ZT_TRACE_FIELD_TRIGGER_FROM_PACKET_VERB "tv"
  653. #define ZT_TRACE_FIELD_TRIGGER_FROM_PEER_FINGERPRINT_HASH "tp"
  654. #define ZT_TRACE_FIELD_MESSAGE "m"
  655. #define ZT_TRACE_FIELD_RESET_ADDRESS_SCOPE "rs"
  656. #define ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH "f"
  657. #define ZT_TRACE_FIELD_PACKET_ID "p"
  658. #define ZT_TRACE_FIELD_PACKET_VERB "v"
  659. #define ZT_TRACE_FIELD_PACKET_HOPS "h"
  660. #define ZT_TRACE_FIELD_NETWORK_ID "n"
  661. #define ZT_TRACE_FIELD_REASON "r"
  662. #define ZT_TRACE_FIELD_SOURCE_MAC "sm"
  663. #define ZT_TRACE_FIELD_DEST_MAC "dm"
  664. #define ZT_TRACE_FIELD_ETHERTYPE "et"
  665. #define ZT_TRACE_FIELD_VLAN_ID "vlid"
  666. #define ZT_TRACE_FIELD_FRAME_LENGTH "fl"
  667. #define ZT_TRACE_FIELD_FRAME_DATA "fd"
  668. #define ZT_TRACE_FIELD_FLAG_CREDENTIAL_REQUEST_SENT "crs"
  669. #define ZT_TRACE_FIELD_PRIMARY_RULE_SET_LOG "rL"
  670. #define ZT_TRACE_FIELD_MATCHING_CAPABILITY_RULE_SET_LOG "caRL"
  671. #define ZT_TRACE_FIELD_MATCHING_CAPABILITY_ID "caID"
  672. #define ZT_TRACE_FIELD_MATCHING_CAPABILITY_TIMESTAMP "caTS"
  673. #define ZT_TRACE_FIELD_SOURCE_ZT_ADDRESS "sz"
  674. #define ZT_TRACE_FIELD_DEST_ZT_ADDRESS "dz"
  675. #define ZT_TRACE_FIELD_RULE_FLAG_NOTEE "rNT"
  676. #define ZT_TRACE_FIELD_RULE_FLAG_INBOUND "rIN"
  677. #define ZT_TRACE_FIELD_RULE_FLAG_ACCEPT "rACC"
  678. #define ZT_TRACE_FIELD_CREDENTIAL_ID "crID"
  679. #define ZT_TRACE_FIELD_CREDENTIAL_TYPE "crT"
  680. #define ZT_TRACE_FIELD_CREDENTIAL_TIMESTAMP "crTS"
  681. /**
  682. * Function return code: OK (0) or error results
  683. *
  684. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  685. * occurs, the node should be considered to not be working correctly. These
  686. * indicate serious problems like an inaccessible data store or a compile
  687. * problem.
  688. */
  689. enum ZT_ResultCode
  690. {
  691. /**
  692. * Operation completed normally
  693. */
  694. ZT_RESULT_OK = 0,
  695. /* Fatal errors (>100, <1000) */
  696. /**
  697. * Ran out of memory
  698. */
  699. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 100,
  700. /**
  701. * Data store is not writable or has failed
  702. */
  703. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 101,
  704. /**
  705. * Internal error fatal to the instance
  706. */
  707. ZT_RESULT_FATAL_ERROR_INTERNAL = 102,
  708. /* Non-fatal errors (>1000) */
  709. /**
  710. * Network ID not valid
  711. */
  712. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000,
  713. /**
  714. * The requested operation is not supported on this version or build
  715. */
  716. ZT_RESULT_ERROR_UNSUPPORTED_OPERATION = 1001,
  717. /**
  718. * The requested operation was given a bad parameter or was called in an invalid state
  719. */
  720. ZT_RESULT_ERROR_BAD_PARAMETER = 1002,
  721. /**
  722. * A credential or other object was supplied that failed cryptographic signature or integrity check
  723. */
  724. ZT_RESULT_ERROR_INVALID_CREDENTIAL = 1003,
  725. /**
  726. * An object collides with another object in some way (meaning is object-specific)
  727. */
  728. ZT_RESULT_ERROR_COLLIDING_OBJECT = 1004,
  729. /**
  730. * An internal error occurred, but one that is not fatal to the whole instance
  731. */
  732. ZT_RESULT_ERROR_INTERNAL = 1005
  733. };
  734. /**
  735. * Macro to check for a fatal error result code
  736. *
  737. * @param x Result code
  738. * @return True if result code indicates a fatal error
  739. */
  740. #define ZT_ResultCode_isFatal(x) ((((int)(x)) >= 100)&&(((int)(x)) < 1000))
  741. /**
  742. * Status codes sent to status update callback when things happen
  743. */
  744. enum ZT_Event
  745. {
  746. /**
  747. * Node has been initialized
  748. *
  749. * This is the first event generated, and is always sent. It may occur
  750. * before Node's constructor returns.
  751. *
  752. * Meta-data: none
  753. */
  754. ZT_EVENT_UP = 0,
  755. /**
  756. * Node appears offline
  757. *
  758. * This indicates that the node doesn't seem to be able to reach anything,
  759. * or hasn't for a while. It's not a hard instantaneous thing.
  760. *
  761. * Meta-data: none
  762. */
  763. ZT_EVENT_OFFLINE = 1,
  764. /**
  765. * Node appears online
  766. *
  767. * This indicates that the node was offline but now seems to be able to
  768. * reach something. Like OFFLINE it's not a hard instantaneous thing but
  769. * more of an indicator for UI reporting purposes.
  770. *
  771. * Meta-data: none
  772. */
  773. ZT_EVENT_ONLINE = 2,
  774. /**
  775. * Node is shutting down
  776. *
  777. * This is generated within Node's destructor when it is being shut down.
  778. * It's done for convenience in case you want to clean up anything during
  779. * node shutdown in your node event handler.
  780. *
  781. * Meta-data: none
  782. */
  783. ZT_EVENT_DOWN = 3,
  784. /* 4 once signaled identity collision but this is no longer an error */
  785. /**
  786. * Trace (debugging) message
  787. *
  788. * These events are only generated if this is a TRACE-enabled build.
  789. * This is for local debug traces, not remote trace diagnostics.
  790. *
  791. * Meta-data: struct of type ZT_Trace_*
  792. */
  793. ZT_EVENT_TRACE = 5,
  794. /**
  795. * VERB_USER_MESSAGE received
  796. *
  797. * These are generated when a VERB_USER_MESSAGE packet is received via
  798. * ZeroTier VL1. This can be used for below-VL2 in-band application
  799. * specific signaling over the ZeroTier protocol.
  800. *
  801. * It's up to you to ensure that you handle these in a way that does
  802. * not introduce a remote security vulnerability into your app! If
  803. * your USER_MESSAGE code has a buffer overflow or other vulnerability
  804. * then your app will be vulnerable and this is not ZT's fault. :)
  805. *
  806. * Meta-data: ZT_UserMessage structure
  807. */
  808. ZT_EVENT_USER_MESSAGE = 6
  809. };
  810. /**
  811. * User message used with ZT_EVENT_USER_MESSAGE
  812. *
  813. * These are direct VL1 P2P messages for application use. Encryption and
  814. * authentication in the ZeroTier protocol will guarantee the origin
  815. * address and message content, but you are responsible for any other
  816. * levels of authentication or access control that are required. Any node
  817. * in the world can send you a user message! (Unless your network is air
  818. * gapped.)
  819. *
  820. * Pointers to id and data might not remain valid after the event is
  821. * received.
  822. */
  823. typedef struct
  824. {
  825. /**
  826. * Identity of sender
  827. */
  828. const ZT_Identity *id;
  829. /**
  830. * User message type ID
  831. */
  832. uint64_t typeId;
  833. /**
  834. * User message data
  835. */
  836. const void *data;
  837. /**
  838. * Length of data in bytes
  839. */
  840. unsigned int length;
  841. } ZT_UserMessage;
  842. /**
  843. * Current node status
  844. */
  845. typedef struct
  846. {
  847. /**
  848. * 40-bit ZeroTier address of this node
  849. */
  850. uint64_t address;
  851. /**
  852. * Actual identity object for this node
  853. */
  854. const ZT_Identity *identity;
  855. /**
  856. * Public identity in string-serialized form (safe to send to others)
  857. *
  858. * This pointer will remain valid as long as the node exists.
  859. */
  860. const char *publicIdentity;
  861. /**
  862. * Full identity including secret key in string-serialized form
  863. *
  864. * This pointer will remain valid as long as the node exists.
  865. */
  866. const char *secretIdentity;
  867. /**
  868. * True if some kind of connectivity appears available
  869. */
  870. int online;
  871. } ZT_NodeStatus;
  872. /**
  873. * Virtual network status codes
  874. */
  875. enum ZT_VirtualNetworkStatus
  876. {
  877. /**
  878. * Waiting for network configuration (also means revision == 0)
  879. */
  880. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  881. /**
  882. * Configuration received and we are authorized
  883. */
  884. ZT_NETWORK_STATUS_OK = 1,
  885. /**
  886. * Netconf master told us 'nope'
  887. */
  888. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  889. /**
  890. * Netconf master exists, but this virtual network does not
  891. */
  892. ZT_NETWORK_STATUS_NOT_FOUND = 3
  893. };
  894. /**
  895. * Virtual network type codes
  896. */
  897. enum ZT_VirtualNetworkType
  898. {
  899. /**
  900. * Private networks are authorized via membership credentials
  901. */
  902. ZT_NETWORK_TYPE_PRIVATE = 0,
  903. /**
  904. * Public networks have no access control -- they'll always be AUTHORIZED
  905. */
  906. ZT_NETWORK_TYPE_PUBLIC = 1
  907. };
  908. /**
  909. * The type of a virtual network rules table entry
  910. *
  911. * These must be from 0 to 63 since the most significant two bits of each
  912. * rule type are NOT (MSB) and AND/OR.
  913. *
  914. * Each rule is composed of zero or more MATCHes followed by an ACTION.
  915. * An ACTION with no MATCHes is always taken.
  916. */
  917. enum ZT_VirtualNetworkRuleType
  918. {
  919. /* 0 to 15 reserved for actions */
  920. /**
  921. * Drop frame
  922. */
  923. ZT_NETWORK_RULE_ACTION_DROP = 0,
  924. /**
  925. * Accept and pass frame
  926. */
  927. ZT_NETWORK_RULE_ACTION_ACCEPT = 1,
  928. /**
  929. * Forward a copy of this frame to an observer (by ZT address)
  930. */
  931. ZT_NETWORK_RULE_ACTION_TEE = 2,
  932. /**
  933. * Exactly like TEE but mandates ACKs from observer
  934. */
  935. ZT_NETWORK_RULE_ACTION_WATCH = 3,
  936. /**
  937. * Drop and redirect this frame to another node (by ZT address)
  938. */
  939. ZT_NETWORK_RULE_ACTION_REDIRECT = 4,
  940. /**
  941. * Stop evaluating rule set (drops unless there are capabilities, etc.)
  942. */
  943. ZT_NETWORK_RULE_ACTION_BREAK = 5,
  944. /**
  945. * Place a matching frame in the specified QoS bucket
  946. */
  947. ZT_NETWORK_RULE_ACTION_PRIORITY = 6,
  948. /**
  949. * Maximum ID for an ACTION, anything higher is a MATCH
  950. */
  951. ZT_NETWORK_RULE_ACTION__MAX_ID = 15,
  952. // 16 to 63 reserved for match criteria
  953. ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS = 24,
  954. ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS = 25,
  955. ZT_NETWORK_RULE_MATCH_VLAN_ID = 26,
  956. ZT_NETWORK_RULE_MATCH_VLAN_PCP = 27,
  957. ZT_NETWORK_RULE_MATCH_VLAN_DEI = 28,
  958. ZT_NETWORK_RULE_MATCH_MAC_SOURCE = 29,
  959. ZT_NETWORK_RULE_MATCH_MAC_DEST = 30,
  960. ZT_NETWORK_RULE_MATCH_IPV4_SOURCE = 31,
  961. ZT_NETWORK_RULE_MATCH_IPV4_DEST = 32,
  962. ZT_NETWORK_RULE_MATCH_IPV6_SOURCE = 33,
  963. ZT_NETWORK_RULE_MATCH_IPV6_DEST = 34,
  964. ZT_NETWORK_RULE_MATCH_IP_TOS = 35,
  965. ZT_NETWORK_RULE_MATCH_IP_PROTOCOL = 36,
  966. ZT_NETWORK_RULE_MATCH_ETHERTYPE = 37,
  967. ZT_NETWORK_RULE_MATCH_ICMP = 38,
  968. ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE = 39,
  969. ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE = 40,
  970. ZT_NETWORK_RULE_MATCH_CHARACTERISTICS = 41,
  971. ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE = 42,
  972. ZT_NETWORK_RULE_MATCH_RANDOM = 43,
  973. ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE = 44,
  974. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND = 45,
  975. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR = 46,
  976. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR = 47,
  977. ZT_NETWORK_RULE_MATCH_TAGS_EQUAL = 48,
  978. ZT_NETWORK_RULE_MATCH_TAG_SENDER = 49,
  979. ZT_NETWORK_RULE_MATCH_TAG_RECEIVER = 50,
  980. ZT_NETWORK_RULE_MATCH_INTEGER_RANGE = 51,
  981. /**
  982. * Maximum ID allowed for a MATCH entry in the rules table
  983. */
  984. ZT_NETWORK_RULE_MATCH__MAX_ID = 63
  985. };
  986. /**
  987. * Network flow rule
  988. *
  989. * Rules are stored in a table in which one or more match entries is followed
  990. * by an action. If more than one match precedes an action, the rule is
  991. * the AND of all matches. An action with no match is always taken since it
  992. * matches anything. If nothing matches, the default action is DROP.
  993. *
  994. * This is designed to be a more memory-efficient way of storing rules than
  995. * a wide table, yet still fast and simple to access in code.
  996. */
  997. typedef struct
  998. {
  999. /**
  1000. * Type and flags
  1001. *
  1002. * Bits are: NOTTTTTT
  1003. *
  1004. * N - If true, sense of match is inverted (no effect on actions)
  1005. * O - If true, result is ORed with previous instead of ANDed (no effect on actions)
  1006. * T - Rule or action type
  1007. *
  1008. * AND with 0x3f to get type, 0x80 to get NOT bit, and 0x40 to get OR bit.
  1009. */
  1010. uint8_t t;
  1011. /**
  1012. * Union containing the value of this rule -- which field is used depends on 't'
  1013. */
  1014. union {
  1015. /**
  1016. * IPv6 address in big-endian / network byte order and netmask bits
  1017. */
  1018. struct {
  1019. uint8_t ip[16];
  1020. uint8_t mask;
  1021. } ipv6;
  1022. /**
  1023. * IPv4 address in big-endian / network byte order
  1024. */
  1025. struct {
  1026. uint32_t ip;
  1027. uint8_t mask;
  1028. } ipv4;
  1029. /**
  1030. * Integer range match in packet payload
  1031. *
  1032. * This allows matching of ranges of integers up to 64 bits wide where
  1033. * the range is +/- INT32_MAX. It's packed this way so it fits in 16
  1034. * bytes and doesn't enlarge the overall size of this union.
  1035. */
  1036. struct {
  1037. uint64_t start; /* integer range start */
  1038. uint32_t end; /* end of integer range (relative to start, inclusive, 0 for equality w/start) */
  1039. uint16_t idx; /* index in packet of integer */
  1040. uint8_t format; /* bits in integer (range 1-64, ((format&63)+1)) and endianness (MSB 1 for little, 0 for big) */
  1041. } intRange;
  1042. /**
  1043. * Packet characteristic flags being matched
  1044. */
  1045. uint64_t characteristics;
  1046. /**
  1047. * IP port range -- start-end inclusive -- host byte order
  1048. */
  1049. uint16_t port[2];
  1050. /**
  1051. * 40-bit ZeroTier address (in least significant bits, host byte order)
  1052. */
  1053. uint64_t zt;
  1054. /**
  1055. * 0 = never, UINT32_MAX = always
  1056. */
  1057. uint32_t randomProbability;
  1058. /**
  1059. * 48-bit Ethernet MAC address in big-endian order
  1060. */
  1061. uint8_t mac[6];
  1062. /**
  1063. * VLAN ID in host byte order
  1064. */
  1065. uint16_t vlanId;
  1066. /**
  1067. * VLAN PCP (least significant 3 bits)
  1068. */
  1069. uint8_t vlanPcp;
  1070. /**
  1071. * VLAN DEI (single bit / boolean)
  1072. */
  1073. uint8_t vlanDei;
  1074. /**
  1075. * Ethernet type in host byte order
  1076. */
  1077. uint16_t etherType;
  1078. /**
  1079. * IP protocol
  1080. */
  1081. uint8_t ipProtocol;
  1082. /**
  1083. * IP type of service a.k.a. DSCP field
  1084. */
  1085. struct {
  1086. uint8_t mask;
  1087. uint8_t value[2];
  1088. } ipTos;
  1089. /**
  1090. * Ethernet packet size in host byte order (start-end, inclusive)
  1091. */
  1092. uint16_t frameSize[2];
  1093. /**
  1094. * ICMP type and code
  1095. */
  1096. struct {
  1097. uint8_t type; /* ICMP type, always matched */
  1098. uint8_t code; /* ICMP code if matched */
  1099. uint8_t flags; /* flag 0x01 means also match code, otherwise only match type */
  1100. } icmp;
  1101. /**
  1102. * For tag-related rules
  1103. */
  1104. struct {
  1105. uint32_t id;
  1106. uint32_t value;
  1107. } tag;
  1108. /**
  1109. * Destinations for TEE and REDIRECT
  1110. */
  1111. struct {
  1112. uint64_t address;
  1113. uint32_t flags;
  1114. uint16_t length;
  1115. } fwd;
  1116. /**
  1117. * Quality of Service (QoS) bucket we want a frame to be placed in
  1118. */
  1119. uint8_t qosBucket;
  1120. } v;
  1121. } ZT_VirtualNetworkRule;
  1122. /**
  1123. * A route to be pushed on a virtual network
  1124. */
  1125. typedef struct
  1126. {
  1127. /**
  1128. * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
  1129. */
  1130. struct sockaddr_storage target;
  1131. /**
  1132. * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
  1133. */
  1134. struct sockaddr_storage via;
  1135. /**
  1136. * Route flags
  1137. */
  1138. uint16_t flags;
  1139. /**
  1140. * Route metric
  1141. */
  1142. uint16_t metric;
  1143. } ZT_VirtualNetworkRoute;
  1144. /**
  1145. * An Ethernet multicast group
  1146. */
  1147. typedef struct
  1148. {
  1149. /**
  1150. * MAC address (least significant 48 bits)
  1151. */
  1152. uint64_t mac;
  1153. /**
  1154. * Additional distinguishing information (usually zero)
  1155. */
  1156. unsigned long adi;
  1157. } ZT_MulticastGroup;
  1158. /**
  1159. * Virtual network configuration update type
  1160. */
  1161. enum ZT_VirtualNetworkConfigOperation
  1162. {
  1163. /**
  1164. * Network is coming up (either for the first time or after service restart)
  1165. */
  1166. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  1167. /**
  1168. * Network configuration has been updated
  1169. */
  1170. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  1171. /**
  1172. * Network is going down (not permanently)
  1173. */
  1174. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  1175. /**
  1176. * Network is going down permanently (leave/delete)
  1177. */
  1178. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  1179. };
  1180. /**
  1181. * Virtual network configuration
  1182. */
  1183. typedef struct
  1184. {
  1185. /**
  1186. * 64-bit ZeroTier network ID
  1187. */
  1188. uint64_t nwid;
  1189. /**
  1190. * Ethernet MAC (48 bits) that should be assigned to port
  1191. */
  1192. uint64_t mac;
  1193. /**
  1194. * Network name (from network configuration master)
  1195. */
  1196. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  1197. /**
  1198. * Network configuration request status
  1199. */
  1200. enum ZT_VirtualNetworkStatus status;
  1201. /**
  1202. * Network type
  1203. */
  1204. enum ZT_VirtualNetworkType type;
  1205. /**
  1206. * Maximum interface MTU
  1207. */
  1208. unsigned int mtu;
  1209. /**
  1210. * If nonzero, this port is allowed to bridge to other networks
  1211. *
  1212. * This is informational. If this is false (0), bridged packets will simply
  1213. * be dropped and bridging won't work.
  1214. */
  1215. int bridge;
  1216. /**
  1217. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  1218. */
  1219. int broadcastEnabled;
  1220. /**
  1221. * Revision number as reported by controller or 0 if still waiting for config
  1222. */
  1223. unsigned long netconfRevision;
  1224. /**
  1225. * Number of assigned addresses
  1226. */
  1227. unsigned int assignedAddressCount;
  1228. /**
  1229. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  1230. *
  1231. * For IP, the port number of the sockaddr_XX structure contains the number
  1232. * of bits in the address netmask. Only the IP address and port are used.
  1233. * Other fields like interface number can be ignored.
  1234. *
  1235. * This is only used for ZeroTier-managed address assignments sent by the
  1236. * virtual network's configuration master.
  1237. */
  1238. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  1239. /**
  1240. * Number of ZT-pushed routes
  1241. */
  1242. unsigned int routeCount;
  1243. /**
  1244. * Routes (excluding those implied by assigned addresses and their masks)
  1245. */
  1246. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  1247. } ZT_VirtualNetworkConfig;
  1248. /**
  1249. * A list of networks
  1250. */
  1251. typedef struct
  1252. {
  1253. void (*freeFunction)(const void *);
  1254. ZT_VirtualNetworkConfig *networks;
  1255. unsigned long networkCount;
  1256. } ZT_VirtualNetworkList;
  1257. /**
  1258. * Address where this node could be reached via an external interface
  1259. */
  1260. typedef struct
  1261. {
  1262. /**
  1263. * IP and port as would be reachable by external nodes
  1264. */
  1265. struct sockaddr_storage address;
  1266. /**
  1267. * If nonzero this address is static and can be incorporated into this node's Locator
  1268. */
  1269. int permanent;
  1270. } ZT_InterfaceAddress;
  1271. /**
  1272. * Variant type for storing possible path endpoints or peer contact points.
  1273. */
  1274. typedef struct
  1275. {
  1276. /**
  1277. * Endpoint type, which determines what field in the union 'a' applies.
  1278. */
  1279. enum ZT_EndpointType type;
  1280. union {
  1281. /**
  1282. * Socket address generic buffer
  1283. */
  1284. struct sockaddr_storage ss;
  1285. /**
  1286. * Socket address header, for all ZT_ENDPOINT_TYPE_IP types
  1287. */
  1288. struct sockaddr sa;
  1289. /**
  1290. * IPv4 address, for all ZT_ENDPOINT_TYPE_IP types if family is AF_INET
  1291. */
  1292. struct sockaddr_in sa_in;
  1293. /**
  1294. * IPv6 address, for all ZT_ENDPOINT_TYPE_IP types if family is AF_INET6
  1295. */
  1296. struct sockaddr_in6 sa_in6;
  1297. /**
  1298. * MAC address (least significant 48 bites) for ZT_ENDPOINT_TYPE_ETHERNET and other MAC addressed types
  1299. */
  1300. uint64_t mac;
  1301. /**
  1302. * ZeroTier node address and identity fingerprint for ZT_ENDPOINT_TYPE_ZEROTIER
  1303. */
  1304. ZT_Fingerprint fp;
  1305. } value;
  1306. } ZT_Endpoint;
  1307. /**
  1308. * Endpoint attributes
  1309. *
  1310. * Right now this is typedef'd to void because there are none. It will become
  1311. * a struct once there's something to specify.
  1312. */
  1313. typedef void ZT_EndpointAttributes;
  1314. /**
  1315. * Network path to a peer
  1316. */
  1317. typedef struct
  1318. {
  1319. /**
  1320. * Path endpoint
  1321. */
  1322. ZT_Endpoint endpoint;
  1323. /**
  1324. * Time of last send in milliseconds or 0 for never
  1325. */
  1326. int64_t lastSend;
  1327. /**
  1328. * Time of last receive in milliseconds or 0 for never
  1329. */
  1330. int64_t lastReceive;
  1331. /**
  1332. * Is path alive?
  1333. */
  1334. int alive;
  1335. /**
  1336. * Is path preferred?
  1337. */
  1338. int preferred;
  1339. } ZT_Path;
  1340. /**
  1341. * Peer information
  1342. */
  1343. typedef struct
  1344. {
  1345. /**
  1346. * ZeroTier address (40 bits)
  1347. */
  1348. uint64_t address;
  1349. /**
  1350. * Peer identity
  1351. */
  1352. const ZT_Identity *identity;
  1353. /**
  1354. * SHA-384 of identity public key(s)
  1355. */
  1356. const ZT_Fingerprint *fingerprint;
  1357. /**
  1358. * Remote major version or -1 if not known
  1359. */
  1360. int versionMajor;
  1361. /**
  1362. * Remote minor version or -1 if not known
  1363. */
  1364. int versionMinor;
  1365. /**
  1366. * Remote revision or -1 if not known
  1367. */
  1368. int versionRev;
  1369. /**
  1370. * Remote protocol version or -1 if not known
  1371. */
  1372. int versionProto;
  1373. /**
  1374. * Last measured latency in milliseconds or -1 if unknown
  1375. */
  1376. int latency;
  1377. /**
  1378. * If non-zero this peer is a root
  1379. */
  1380. int root;
  1381. /**
  1382. * Network IDs for networks (array size: networkCount)
  1383. */
  1384. uint64_t *networks;
  1385. /**
  1386. * Number of networks in which this peer is authenticated
  1387. */
  1388. unsigned int networkCount;
  1389. /**
  1390. * Known network paths to peer (array size: pathCount).
  1391. *
  1392. * These are direct paths only. Endpoints can also describe indirect paths,
  1393. * but those would not appear here. Right now those can only be relaying via
  1394. * a root.
  1395. */
  1396. ZT_Path *paths;
  1397. /**
  1398. * Number of paths (size of paths[])
  1399. */
  1400. unsigned int pathCount;
  1401. /**
  1402. * Size of locator in bytes or 0 if none
  1403. */
  1404. unsigned int locatorSize;
  1405. /**
  1406. * Serialized locator or NULL if none
  1407. */
  1408. const void *locator;
  1409. } ZT_Peer;
  1410. /**
  1411. * List of peers
  1412. */
  1413. typedef struct
  1414. {
  1415. void (*freeFunction)(const void *);
  1416. ZT_Peer *peers;
  1417. unsigned long peerCount;
  1418. } ZT_PeerList;
  1419. /**
  1420. * ZeroTier core state objects
  1421. */
  1422. enum ZT_StateObjectType
  1423. {
  1424. /**
  1425. * Null object -- ignored
  1426. */
  1427. ZT_STATE_OBJECT_NULL = 0,
  1428. /**
  1429. * Public address and public key
  1430. *
  1431. * Object ID: (none)
  1432. * Canonical path: <HOME>/identity.public
  1433. * Persistence: required
  1434. */
  1435. ZT_STATE_OBJECT_IDENTITY_PUBLIC = 1,
  1436. /**
  1437. * Full identity with secret key
  1438. *
  1439. * Object ID: (none)
  1440. * Canonical path: <HOME>/identity.secret
  1441. * Persistence: required, should be stored with restricted permissions e.g. mode 0600 on *nix
  1442. */
  1443. ZT_STATE_OBJECT_IDENTITY_SECRET = 2,
  1444. /**
  1445. * This node's locator
  1446. *
  1447. * Object ID: (none)
  1448. * Canonical path: <HOME>/locator
  1449. * Persistence: optional
  1450. */
  1451. ZT_STATE_OBJECT_LOCATOR = 3,
  1452. /**
  1453. * Peer and related state
  1454. *
  1455. * Object ID: [1]address (40 bits, in least significant 64 bits)
  1456. * Canonical path: <HOME>/peers.d/<ID> (10-digit address)
  1457. * Persistence: optional, can be cleared at any time
  1458. */
  1459. ZT_STATE_OBJECT_PEER = 5,
  1460. /**
  1461. * Network configuration
  1462. *
  1463. * Object ID: [1]id (64-bit network ID)
  1464. * Canonical path: <HOME>/networks.d/<NETWORKID>.conf (16-digit hex ID)
  1465. * Persistence: required if network memberships should persist
  1466. */
  1467. ZT_STATE_OBJECT_NETWORK_CONFIG = 6,
  1468. /**
  1469. * List of certificates, their local trust, and locally added roots
  1470. *
  1471. * Object ID: (none)
  1472. * Canonical path: <HOME>/trust
  1473. * Persistence: required if root settings should persist
  1474. */
  1475. ZT_STATE_OBJECT_TRUST_STORE = 7,
  1476. /**
  1477. * Certificate
  1478. *
  1479. * Object ID: [6]serial (384-bit serial packed into 6 uint64_t's)
  1480. * Canonical path: <HOME>/certs.d/<serial> (96-digit hex serial)
  1481. */
  1482. ZT_STATE_OBJECT_CERT = 8
  1483. };
  1484. /**
  1485. * Size of the object ID for peers (in 64-bit uint64_t's)
  1486. */
  1487. #define ZT_STATE_OBJECT_PEER_ID_SIZE 1
  1488. /**
  1489. * Size of the object ID for network configurations (in 64-bit uint64_t's)
  1490. */
  1491. #define ZT_STATE_OBJECT_NETWORK_CONFIG_ID_SIZE 1
  1492. /**
  1493. * Size of the object ID for certificates (in 64-bit uint64_t's)
  1494. */
  1495. #define ZT_STATE_OBJECT_CERT_ID_SIZE 6
  1496. /**
  1497. * An instance of a ZeroTier One node (opaque)
  1498. */
  1499. typedef void ZT_Node;
  1500. /* ---------------------------------------------------------------------------------------------------------------- */
  1501. /**
  1502. * Callback called to update virtual network port configuration
  1503. *
  1504. * This can be called at any time to update the configuration of a virtual
  1505. * network port. The parameter after the network ID specifies whether this
  1506. * port is being brought up, updated, brought down, or permanently deleted.
  1507. *
  1508. * This in turn should be used by the underlying implementation to create
  1509. * and configure tap devices at the OS (or virtual network stack) layer.
  1510. *
  1511. * The supplied config pointer is not guaranteed to remain valid, so make
  1512. * a copy if you want one.
  1513. *
  1514. * This should not call multicastSubscribe() or other network-modifying
  1515. * methods, as this could cause a deadlock in multithreaded or interrupt
  1516. * driven environments.
  1517. */
  1518. typedef void (*ZT_VirtualNetworkConfigFunction)(
  1519. ZT_Node *, /* Node */
  1520. void *, /* User ptr */
  1521. void *, /* Thread ptr */
  1522. uint64_t, /* Network ID */
  1523. void **, /* Modifiable network user PTR */
  1524. enum ZT_VirtualNetworkConfigOperation, /* Config operation */
  1525. const ZT_VirtualNetworkConfig *); /* Network configuration */
  1526. /**
  1527. * Function to send a frame out to a virtual network port
  1528. *
  1529. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  1530. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  1531. * (9) frame length.
  1532. */
  1533. typedef void (*ZT_VirtualNetworkFrameFunction)(
  1534. ZT_Node *, /* Node */
  1535. void *, /* User ptr */
  1536. void *, /* Thread ptr */
  1537. uint64_t, /* Network ID */
  1538. void **, /* Modifiable network user PTR */
  1539. uint64_t, /* Source MAC */
  1540. uint64_t, /* Destination MAC */
  1541. unsigned int, /* Ethernet type */
  1542. unsigned int, /* VLAN ID (0 for none) */
  1543. const void *, /* Frame data */
  1544. unsigned int); /* Frame length */
  1545. /**
  1546. * Callback for events
  1547. *
  1548. * Events are generated when the node's status changes in a significant way
  1549. * and on certain non-fatal errors and events of interest. The final void
  1550. * parameter points to event meta-data. The type of event meta-data (and
  1551. * whether it is present at all) is event type dependent. See the comments
  1552. * in the definition of ZT_Event.
  1553. */
  1554. typedef void (*ZT_EventCallback)(
  1555. ZT_Node *, /* Node */
  1556. void *, /* User ptr */
  1557. void *, /* Thread ptr */
  1558. enum ZT_Event, /* Event type */
  1559. const void *); /* Event payload (if applicable) */
  1560. /**
  1561. * Callback for storing and/or publishing state information
  1562. *
  1563. * See ZT_StateObjectType docs for information about each state object type
  1564. * and when and if it needs to be persisted.
  1565. *
  1566. * The state object ID's size depends on the object type, and is always
  1567. * in the form of one or more 64-bit unsigned integers. Some object types
  1568. * do not use this field, and for these it may be NULL.
  1569. *
  1570. * An object of length -1 is sent to indicate that an object should be
  1571. * deleted.
  1572. */
  1573. typedef void (*ZT_StatePutFunction)(
  1574. ZT_Node *, /* Node */
  1575. void *, /* User ptr */
  1576. void *, /* Thread ptr */
  1577. enum ZT_StateObjectType, /* State object type */
  1578. const uint64_t *, /* State object ID (if applicable) */
  1579. const void *, /* State object data */
  1580. int); /* Length of data or -1 to delete */
  1581. /**
  1582. * Callback for retrieving stored state information
  1583. *
  1584. * This function should return the number of bytes actually stored to the
  1585. * buffer or -1 if the state object was not found. The buffer itself should
  1586. * be set to point to the data, and the last result parameter must point to
  1587. * a function that will be used to free the buffer when the core is done
  1588. * with it. This is very often just a pointer to free().
  1589. */
  1590. typedef int (*ZT_StateGetFunction)(
  1591. ZT_Node *, /* Node */
  1592. void *, /* User ptr */
  1593. void *, /* Thread ptr */
  1594. enum ZT_StateObjectType, /* State object type */
  1595. const uint64_t *, /* State object ID (if applicable) */
  1596. void **, /* Result parameter: data */
  1597. void (**)(void *)); /* Result parameter: data free function */
  1598. /**
  1599. * Function to send a ZeroTier packet out over the physical wire (L2/L3)
  1600. *
  1601. * If there is only one local socket, the local socket can be ignored.
  1602. * If the local socket is -1, the packet should be sent out from all
  1603. * bound local sockets or a random bound local socket.
  1604. *
  1605. * If TTL is nonzero, packets should have their IP TTL value set to this
  1606. * value if possible. If this is not possible it is acceptable to ignore
  1607. * this value and send anyway with normal or default TTL.
  1608. *
  1609. * The function must return zero on success and may return any error code
  1610. * on failure. Note that success does not (of course) guarantee packet
  1611. * delivery. It only means that the packet appears to have been sent.
  1612. */
  1613. typedef int (*ZT_WirePacketSendFunction)(
  1614. ZT_Node *, /* Node */
  1615. void *, /* User ptr */
  1616. void *, /* Thread ptr */
  1617. int64_t, /* Local socket */
  1618. const struct sockaddr_storage *, /* Remote address */
  1619. const void *, /* Packet data */
  1620. unsigned int, /* Packet length */
  1621. unsigned int); /* TTL or 0 to use default */
  1622. /**
  1623. * Function to check whether a path should be used for ZeroTier traffic
  1624. *
  1625. * Parameters:
  1626. * (1) Node
  1627. * (2) User pointer
  1628. * (3) ZeroTier address or 0 for none/any
  1629. * (4) Full identity or NULL for none/any
  1630. * (5) Local socket or -1 if unknown
  1631. * (6) Remote address
  1632. *
  1633. * This function must return nonzero (true) if the path should be used.
  1634. *
  1635. * If no path check function is specified, ZeroTier will still exclude paths
  1636. * that overlap with ZeroTier-assigned and managed IP address blocks. But the
  1637. * use of a path check function is recommended to ensure that recursion does
  1638. * not occur in cases where addresses are assigned by the OS or managed by
  1639. * an out of band mechanism like DHCP. The path check function should examine
  1640. * all configured ZeroTier interfaces and check to ensure that the supplied
  1641. * addresses will not result in ZeroTier traffic being sent over a ZeroTier
  1642. * interface (recursion).
  1643. */
  1644. typedef int (*ZT_PathCheckFunction)(
  1645. ZT_Node *, /* Node */
  1646. void *, /* User ptr */
  1647. void *, /* Thread ptr */
  1648. uint64_t, /* ZeroTier address */
  1649. const ZT_Identity *, /* Full identity of node */
  1650. int64_t, /* Local socket or -1 if unknown */
  1651. const struct sockaddr_storage *); /* Remote address */
  1652. /**
  1653. * Function to get physical addresses for ZeroTier peers
  1654. *
  1655. * Parameters:
  1656. * (1) Node
  1657. * (2) User pointer
  1658. * (3) ZeroTier address (least significant 40 bits)
  1659. * (4) Identity in string form
  1660. * (5) Desired address family or -1 for any
  1661. * (6) Buffer to fill with result
  1662. *
  1663. * If provided this function will be occasionally called to get physical
  1664. * addresses that might be tried to reach a ZeroTier address. It must
  1665. * return a nonzero (true) value if the result buffer has been filled
  1666. * with an address.
  1667. */
  1668. typedef int (*ZT_PathLookupFunction)(
  1669. ZT_Node *, /* Node */
  1670. void *, /* User ptr */
  1671. void *, /* Thread ptr */
  1672. uint64_t, /* ZeroTier address (40 bits) */
  1673. const ZT_Identity *, /* Full identity of node */
  1674. int, /* Desired ss_family or -1 for any */
  1675. struct sockaddr_storage *); /* Result buffer */
  1676. /* ---------------------------------------------------------------------------------------------------------------- */
  1677. /**
  1678. * Structure for configuring ZeroTier core callback functions
  1679. */
  1680. struct ZT_Node_Callbacks
  1681. {
  1682. /**
  1683. * REQUIRED: Function to store and/or replicate state objects
  1684. */
  1685. ZT_StatePutFunction statePutFunction;
  1686. /**
  1687. * REQUIRED: Function to retrieve state objects from an object store
  1688. */
  1689. ZT_StateGetFunction stateGetFunction;
  1690. /**
  1691. * REQUIRED: Function to send packets over the physical wire
  1692. */
  1693. ZT_WirePacketSendFunction wirePacketSendFunction;
  1694. /**
  1695. * REQUIRED: Function to inject frames into a virtual network's TAP
  1696. */
  1697. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction;
  1698. /**
  1699. * REQUIRED: Function to be called when virtual networks are configured or changed
  1700. */
  1701. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction;
  1702. /**
  1703. * REQUIRED: Function to be called to notify external code of important events
  1704. */
  1705. ZT_EventCallback eventCallback;
  1706. /**
  1707. * OPTIONAL: Function to check whether a given physical path should be used for ZeroTier traffic
  1708. */
  1709. ZT_PathCheckFunction pathCheckFunction;
  1710. /**
  1711. * RECOMMENDED: Function to look up paths to ZeroTier nodes
  1712. */
  1713. ZT_PathLookupFunction pathLookupFunction;
  1714. };
  1715. /* ---------------------------------------------------------------------------------------------------------------- */
  1716. /**
  1717. * Get a buffer for reading data to be passed back into the core via one of the processX() functions
  1718. *
  1719. * The size of the returned buffer is 16384 bytes (ZT_BUF_SIZE).
  1720. *
  1721. * Buffers retrieved with this method MUST be returned to the core via either one of the processX()
  1722. * functions (with isZtBuffer set to true) or freeBuffer(). Buffers should not be freed directly using free().
  1723. *
  1724. * @return Pointer to I/O buffer
  1725. */
  1726. ZT_SDK_API void *ZT_getBuffer();
  1727. /**
  1728. * Free an unused buffer obtained via getBuffer
  1729. *
  1730. * @param b Buffer to free
  1731. */
  1732. ZT_SDK_API void ZT_freeBuffer(void *b);
  1733. /**
  1734. * Free a query result buffer
  1735. *
  1736. * Use this to free the return values of listNetworks(), listPeers(), and
  1737. * other query functions that return allocated structures or buffers.
  1738. *
  1739. * @param qr Query result buffer
  1740. */
  1741. ZT_SDK_API void ZT_freeQueryResult(const void *qr);
  1742. /* ---------------------------------------------------------------------------------------------------------------- */
  1743. /**
  1744. * Create a new ZeroTier node
  1745. *
  1746. * This will attempt to load its identity via the state get function in the
  1747. * callback struct. If that fails it will generate a new identity and store
  1748. * it. Identity generation can take anywhere from a few hundred milliseconds
  1749. * to a few seconds depending on your CPU speed.
  1750. *
  1751. * @param node Result: pointer is set to new node instance on success
  1752. * @param uptr User pointer to pass to functions/callbacks
  1753. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1754. * @param callbacks Callback function configuration
  1755. * @param now Current clock in milliseconds
  1756. * @return OK (0) or error code if a fatal error condition has occurred
  1757. */
  1758. ZT_SDK_API enum ZT_ResultCode ZT_Node_new(
  1759. ZT_Node **node,
  1760. void *uptr,
  1761. void *tptr,
  1762. const struct ZT_Node_Callbacks *callbacks,
  1763. int64_t now);
  1764. /**
  1765. * Delete a node and free all resources it consumes
  1766. *
  1767. * If you are using multiple threads, all other threads must be shut down
  1768. * first. This can crash if processXXX() methods are in progress.
  1769. *
  1770. * @param node Node to delete
  1771. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1772. */
  1773. ZT_SDK_API void ZT_Node_delete(
  1774. ZT_Node *node,
  1775. void *tptr);
  1776. /**
  1777. * Process a packet received from the physical wire
  1778. *
  1779. * @param node Node instance
  1780. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1781. * @param now Current clock in milliseconds
  1782. * @param localSocket Local socket (you can use 0 if only one local socket is bound and ignore this)
  1783. * @param remoteAddress Origin of packet
  1784. * @param packetData Packet data
  1785. * @param packetLength Packet length
  1786. * @param isZtBuffer If non-zero then packetData is a buffer obtained with ZT_getBuffer()
  1787. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1788. * @return OK (0) or error code if a fatal error condition has occurred
  1789. */
  1790. ZT_SDK_API enum ZT_ResultCode ZT_Node_processWirePacket(
  1791. ZT_Node *node,
  1792. void *tptr,
  1793. int64_t now,
  1794. int64_t localSocket,
  1795. const struct sockaddr_storage *remoteAddress,
  1796. const void *packetData,
  1797. unsigned int packetLength,
  1798. int isZtBuffer,
  1799. volatile int64_t *nextBackgroundTaskDeadline);
  1800. /**
  1801. * Process a frame from a virtual network port (tap)
  1802. *
  1803. * @param node Node instance
  1804. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1805. * @param now Current clock in milliseconds
  1806. * @param nwid ZeroTier 64-bit virtual network ID
  1807. * @param sourceMac Source MAC address (least significant 48 bits)
  1808. * @param destMac Destination MAC address (least significant 48 bits)
  1809. * @param etherType 16-bit Ethernet frame type
  1810. * @param vlanId 10-bit VLAN ID or 0 if none
  1811. * @param frameData Frame payload data
  1812. * @param frameLength Frame payload length
  1813. * @param isZtBuffer If non-zero then packetData is a buffer obtained with ZT_getBuffer()
  1814. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1815. * @return OK (0) or error code if a fatal error condition has occurred
  1816. */
  1817. ZT_SDK_API enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  1818. ZT_Node *node,
  1819. void *tptr,
  1820. int64_t now,
  1821. uint64_t nwid,
  1822. uint64_t sourceMac,
  1823. uint64_t destMac,
  1824. unsigned int etherType,
  1825. unsigned int vlanId,
  1826. const void *frameData,
  1827. unsigned int frameLength,
  1828. int isZtBuffer,
  1829. volatile int64_t *nextBackgroundTaskDeadline);
  1830. /**
  1831. * Perform periodic background operations
  1832. *
  1833. * @param node Node instance
  1834. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1835. * @param now Current clock in milliseconds
  1836. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1837. * @return OK (0) or error code if a fatal error condition has occurred
  1838. */
  1839. ZT_SDK_API enum ZT_ResultCode ZT_Node_processBackgroundTasks(
  1840. ZT_Node *node,
  1841. void *tptr,
  1842. int64_t now,
  1843. volatile int64_t *nextBackgroundTaskDeadline);
  1844. /**
  1845. * Join a network
  1846. *
  1847. * This may generate calls to the port config callback before it returns,
  1848. * or these may be differed if a netconf is not available yet.
  1849. *
  1850. * If we are already a member of the network, nothing is done and OK is
  1851. * returned.
  1852. *
  1853. * @param node Node instance
  1854. * @param nwid 64-bit ZeroTier network ID
  1855. * @param fingerprintHash If non-NULL this is the full fingerprint of the controller
  1856. * @param uptr An arbitrary pointer to associate with this network (default: NULL)
  1857. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1858. * @return OK (0) or error code if a fatal error condition has occurred
  1859. */
  1860. ZT_SDK_API enum ZT_ResultCode ZT_Node_join(
  1861. ZT_Node *node,
  1862. uint64_t nwid,
  1863. const ZT_Fingerprint *controllerFingerprint,
  1864. void *uptr,
  1865. void *tptr);
  1866. /**
  1867. * Leave a network
  1868. *
  1869. * If a port has been configured for this network this will generate a call
  1870. * to the port config callback with a NULL second parameter to indicate that
  1871. * the port is now deleted.
  1872. *
  1873. * The uptr parameter is optional and is NULL by default. If it is not NULL,
  1874. * the pointer it points to is set to this network's uptr on success.
  1875. *
  1876. * @param node Node instance
  1877. * @param nwid 64-bit network ID
  1878. * @param uptr Target pointer is set to uptr (if not NULL)
  1879. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1880. * @return OK (0) or error code if a fatal error condition has occurred
  1881. */
  1882. ZT_SDK_API enum ZT_ResultCode ZT_Node_leave(
  1883. ZT_Node *node,
  1884. uint64_t nwid,
  1885. void **uptr,
  1886. void *tptr);
  1887. /**
  1888. * Subscribe to an Ethernet multicast group
  1889. *
  1890. * ADI stands for additional distinguishing information. This defaults to zero
  1891. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1892. * and this must be done.
  1893. *
  1894. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1895. * broadcast address) but with an ADI equal to each IPv4 address in host
  1896. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1897. * a scalable multicast protocol with perfect address specificity.
  1898. *
  1899. * If this is not done, ARP will not work reliably.
  1900. *
  1901. * Multiple calls to subscribe to the same multicast address will have no
  1902. * effect. It is perfectly safe to do this.
  1903. *
  1904. * This does not generate an update call to networkConfigCallback().
  1905. *
  1906. * @param node Node instance
  1907. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1908. * @param nwid 64-bit network ID
  1909. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1910. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1911. * @return OK (0) or error code if a fatal error condition has occurred
  1912. */
  1913. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastSubscribe(
  1914. ZT_Node *node,
  1915. void *tptr,
  1916. uint64_t nwid,
  1917. uint64_t multicastGroup,
  1918. unsigned long multicastAdi);
  1919. /**
  1920. * Unsubscribe from an Ethernet multicast group (or all groups)
  1921. *
  1922. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1923. * you are not subscribed to a group this has no effect.
  1924. *
  1925. * This does not generate an update call to networkConfigCallback().
  1926. *
  1927. * @param node Node instance
  1928. * @param nwid 64-bit network ID
  1929. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1930. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1931. * @return OK (0) or error code if a fatal error condition has occurred
  1932. */
  1933. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastUnsubscribe(
  1934. ZT_Node *node,
  1935. uint64_t nwid,
  1936. uint64_t multicastGroup,
  1937. unsigned long multicastAdi);
  1938. /**
  1939. * Get this node's 40-bit ZeroTier address
  1940. *
  1941. * @param node Node instance
  1942. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1943. */
  1944. ZT_SDK_API uint64_t ZT_Node_address(ZT_Node *node);
  1945. /**
  1946. * Get this node's identity
  1947. *
  1948. * The identity pointer returned by this function need not and should not be
  1949. * freed with ZT_Identity_delete(). It's valid until the node is deleted.
  1950. *
  1951. * @param node Node instance
  1952. * @return Identity
  1953. */
  1954. ZT_SDK_API const ZT_Identity *ZT_Node_identity(ZT_Node *node);
  1955. /**
  1956. * Get the status of this node
  1957. *
  1958. * @param node Node instance
  1959. * @param status Buffer to fill with current node status
  1960. */
  1961. ZT_SDK_API void ZT_Node_status(
  1962. ZT_Node *node,
  1963. ZT_NodeStatus *status);
  1964. /**
  1965. * Get a list of known peer nodes
  1966. *
  1967. * The pointer returned here must be freed with freeQueryResult()
  1968. * when you are done with it.
  1969. *
  1970. * @param node Node instance
  1971. * @return List of known peers or NULL on failure
  1972. */
  1973. ZT_SDK_API ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1974. /**
  1975. * Get the status of a virtual network
  1976. *
  1977. * The pointer returned here must be freed with freeQueryResult()
  1978. * when you are done with it.
  1979. *
  1980. * @param node Node instance
  1981. * @param nwid 64-bit network ID
  1982. * @return Network configuration or NULL if we are not a member of this network
  1983. */
  1984. ZT_SDK_API ZT_VirtualNetworkConfig *ZT_Node_networkConfig(
  1985. ZT_Node *node,
  1986. uint64_t nwid);
  1987. /**
  1988. * Enumerate and get status of all networks
  1989. *
  1990. * @param node Node instance
  1991. * @return List of networks or NULL on failure
  1992. */
  1993. ZT_SDK_API ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1994. /**
  1995. * Set the network-associated user-defined pointer for a given network
  1996. *
  1997. * This will have no effect if the network ID is not recognized.
  1998. *
  1999. * @param node Node instance
  2000. * @param nwid Network ID
  2001. * @param ptr New network-associated pointer
  2002. */
  2003. ZT_SDK_API void ZT_Node_setNetworkUserPtr(
  2004. ZT_Node *node,
  2005. uint64_t nwid,
  2006. void *ptr);
  2007. /**
  2008. * Set external interface addresses where this node could be reached
  2009. *
  2010. * @param node Node instance
  2011. * @param addrs Addresses
  2012. * @param addrCount Number of items in addrs[]
  2013. */
  2014. ZT_SDK_API void ZT_Node_setInterfaceAddresses(
  2015. ZT_Node *node,
  2016. const ZT_InterfaceAddress *addrs,
  2017. unsigned int addrCount);
  2018. /**
  2019. * Add a peer directly by supplying its identity
  2020. *
  2021. * This does not authorize the peer on a network (only the network's
  2022. * controller can do that) or otherwise give it special privileges. It
  2023. * also doesn't guarantee it will be contacted. It just adds it to the
  2024. * internal peer data set if it is not already present.
  2025. *
  2026. * @param node Node instance
  2027. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  2028. * @param id Identity of peer to add
  2029. * @return OK (0) or error code
  2030. */
  2031. ZT_SDK_API enum ZT_ResultCode ZT_Node_addPeer(
  2032. ZT_Node *node,
  2033. void *tptr,
  2034. const ZT_Identity *id);
  2035. /**
  2036. * Attempt to contact a peer at an explicit endpoint address.
  2037. *
  2038. * If the fingerprint structure's hash is all zeroes, the peer is
  2039. * looked up only by address.
  2040. *
  2041. * This can only fail if the peer was not found.
  2042. *
  2043. * Note that this can immediately (before this returns) result in
  2044. * calls to the send packet functions supplied to the core.
  2045. *
  2046. * @param node Node instance
  2047. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  2048. * @param fp Fingerprint (or only address)
  2049. * @param endpoint Endpoint
  2050. * @param retries If greater than zero, try this many times
  2051. * @return Boolean: non-zero on success, zero if peer was not found
  2052. */
  2053. ZT_SDK_API int ZT_Node_tryPeer(
  2054. ZT_Node *node,
  2055. void *tptr,
  2056. const ZT_Fingerprint *fp,
  2057. const ZT_Endpoint *endpoint,
  2058. int retries);
  2059. /**
  2060. * Add a certificate to this node's certificate store
  2061. *
  2062. * This supports adding of certificates as expanded ZT_Certificate structures
  2063. * or as raw data. If 'cert' is NULL then certData/certSize must be set.
  2064. *
  2065. * @param node Node instance
  2066. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  2067. * @param now Current time
  2068. * @param localTrust Local trust flags (ORed together)
  2069. * @param cert Certificate object, or set to NULL if certData and certSize are to be used
  2070. * @param certData Certificate binary data if 'cert' is NULL, NULL otherwise
  2071. * @param certSize Size of certificate binary data, 0 if none
  2072. * @return Certificate error or ZT_CERTIFICATE_ERROR_NONE on success
  2073. */
  2074. ZT_SDK_API enum ZT_CertificateError ZT_Node_addCertificate(
  2075. ZT_Node *node,
  2076. void *tptr,
  2077. int64_t now,
  2078. unsigned int localTrust,
  2079. const ZT_Certificate *cert,
  2080. const void *certData,
  2081. unsigned int certSize);
  2082. /**
  2083. * Delete a certificate from this node's certificate store
  2084. *
  2085. * Note that deleting CA certificates may also imply deletion of certificates
  2086. * that depend on them for full chain verification.
  2087. *
  2088. * @param node Node instance
  2089. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  2090. * @param serialNo 48-byte / 384-bit serial number of certificate to delete
  2091. * @return OK (0) or error code
  2092. */
  2093. ZT_SDK_API enum ZT_ResultCode ZT_Node_deleteCertificate(
  2094. ZT_Node *node,
  2095. void *tptr,
  2096. const void *serialNo);
  2097. /**
  2098. * List certificates installed in this node's trust store
  2099. *
  2100. * @param node Node instance
  2101. * @return List of certificates or NULL on error
  2102. */
  2103. ZT_SDK_API ZT_CertificateList *ZT_Node_listCertificates(ZT_Node *node);
  2104. /**
  2105. * Send a VERB_USER_MESSAGE to another ZeroTier node
  2106. *
  2107. * There is no delivery guarantee here. Failure can occur if the message is
  2108. * too large or if dest is not a valid ZeroTier address.
  2109. *
  2110. * @param node Node instance
  2111. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  2112. * @param dest Destination ZeroTier address
  2113. * @param typeId VERB_USER_MESSAGE type ID
  2114. * @param data Payload data to attach to user message
  2115. * @param len Length of data in bytes
  2116. * @return Boolean: non-zero on success, zero on failure
  2117. */
  2118. ZT_SDK_API int ZT_Node_sendUserMessage(
  2119. ZT_Node *node,
  2120. void *tptr,
  2121. uint64_t dest,
  2122. uint64_t typeId,
  2123. const void *data,
  2124. unsigned int len);
  2125. /**
  2126. * Set a network controller instance for this node
  2127. *
  2128. * Normal nodes should not need to use this. This is for nodes with
  2129. * special compiled-in support for acting as network configuration
  2130. * masters / controllers.
  2131. *
  2132. * The supplied instance must be a C++ object that inherits from the
  2133. * NetworkConfigMaster base class in node/. No type checking is performed,
  2134. * so a pointer to anything else will result in a crash.
  2135. *
  2136. * @param node ZertTier One node
  2137. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  2138. * @return OK (0) or error code if a fatal error condition has occurred
  2139. */
  2140. ZT_SDK_API void ZT_Node_setController(
  2141. ZT_Node *node,
  2142. void *networkConfigMasterInstance);
  2143. /* ---------------------------------------------------------------------------------------------------------------- */
  2144. /**
  2145. * Generate a new identity
  2146. *
  2147. * Due to a small amount of proof of work this can be a time consuming and CPU
  2148. * intensive operation. It takes less than a second on most desktop-class systems
  2149. * but can take longer on e.g. phones.
  2150. *
  2151. * @param type Type of identity to generate
  2152. * @return New identity or NULL on error
  2153. */
  2154. ZT_SDK_API ZT_Identity *ZT_Identity_new(enum ZT_IdentityType type);
  2155. /**
  2156. * Create a new identity object from a string-serialized identity
  2157. *
  2158. * @param idStr Identity in string format
  2159. * @return Identity object or NULL if the supplied identity string was not valid
  2160. */
  2161. ZT_SDK_API ZT_Identity *ZT_Identity_fromString(const char *idStr);
  2162. /**
  2163. * Validate this identity
  2164. *
  2165. * This can be slightly time consuming due to address derivation (work) checking.
  2166. *
  2167. * @return Non-zero if identity is valid
  2168. */
  2169. ZT_SDK_API int ZT_Identity_validate(const ZT_Identity *id);
  2170. /**
  2171. * Sign a data object with this identity
  2172. *
  2173. * The identity must have a private key or this will fail.
  2174. *
  2175. * @param id Identity to use to sign
  2176. * @param data Data to sign
  2177. * @param len Length of data
  2178. * @param signature Buffer to store signature
  2179. * @param signatureBufferLength Length of buffer (must be at least 96 bytes)
  2180. * @return Length of signature in bytes or 0 on failure.
  2181. */
  2182. ZT_SDK_API unsigned int ZT_Identity_sign(
  2183. const ZT_Identity *id,
  2184. const void *data,
  2185. unsigned int len,
  2186. void *signature,
  2187. unsigned int signatureBufferLength);
  2188. /**
  2189. * Verify a signature
  2190. *
  2191. * @param id Identity to use to verify
  2192. * @param data Data to verify
  2193. * @param len Length of data
  2194. * @param signature Signature to check
  2195. * @param sigLen Length of signature in bytes
  2196. * @return Non-zero if signature is valid
  2197. */
  2198. ZT_SDK_API int ZT_Identity_verify(
  2199. const ZT_Identity *id,
  2200. const void *data,
  2201. unsigned int len,
  2202. const void *signature,
  2203. unsigned int sigLen);
  2204. /**
  2205. * Get identity type
  2206. *
  2207. * @param id Identity to query
  2208. * @return Identity type code
  2209. */
  2210. ZT_SDK_API enum ZT_IdentityType ZT_Identity_type(const ZT_Identity *id);
  2211. /**
  2212. * Convert an identity to its string representation
  2213. *
  2214. * @param id Identity to convert
  2215. * @param buf Buffer to store identity (should be at least about 1024 bytes in length)
  2216. * @param capacity Capacity of buffer
  2217. * @param includePrivate If true include the private key if present
  2218. * @return Pointer to buf or NULL on overflow or other error
  2219. */
  2220. ZT_SDK_API char *ZT_Identity_toString(
  2221. const ZT_Identity *id,
  2222. char *buf,
  2223. int capacity,
  2224. int includePrivate);
  2225. /**
  2226. * Check whether this identity object also holds a private key
  2227. *
  2228. * @param id Identity to query
  2229. * @return Non-zero if a private key is held
  2230. */
  2231. ZT_SDK_API int ZT_Identity_hasPrivate(const ZT_Identity *id);
  2232. /**
  2233. * Get the ZeroTier address associated with this identity
  2234. *
  2235. * @param id Identity to query
  2236. * @return ZeroTier address (only least significant 40 bits are meaningful, rest will be 0)
  2237. */
  2238. ZT_SDK_API uint64_t ZT_Identity_address(const ZT_Identity *id);
  2239. /**
  2240. * Get this identity's full fingerprint
  2241. *
  2242. * @param id Identity to query
  2243. * @return Pointer to fingerprint (remains valid as long as identity itself is valid)
  2244. */
  2245. ZT_SDK_API const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id);
  2246. /**
  2247. * Delete an identity and free associated memory
  2248. *
  2249. * This should only be used with identities created via Identity_new
  2250. * and Identity_fromString().
  2251. *
  2252. * @param id Identity to delete
  2253. */
  2254. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id);
  2255. /* ---------------------------------------------------------------------------------------------------------------- */
  2256. /**
  2257. * Convert an endpoint to a string
  2258. *
  2259. * @param ep Endpoint structure
  2260. * @param buf Buffer to store string (recommended size: 256)
  2261. * @param capacity Capacity of buffer
  2262. * @return String or NULL on error
  2263. */
  2264. ZT_SDK_API char *ZT_Endpoint_toString(
  2265. const ZT_Endpoint *ep,
  2266. char *buf,
  2267. int capacity);
  2268. /**
  2269. * Parse an endpoint as a string
  2270. *
  2271. * This will automatically detect IP addresses in IP/port format. If one
  2272. * of these is specified rather than a fully specified endpoint it will be
  2273. * parsed as an IP/UDP endpoint.
  2274. *
  2275. * @param ep Endpoint structure to populate
  2276. * @param str String representation of endpoint
  2277. * @return OK (0) or error code
  2278. */
  2279. ZT_SDK_API int ZT_Endpoint_fromString(
  2280. ZT_Endpoint *ep,
  2281. const char *str);
  2282. /* ---------------------------------------------------------------------------------------------------------------- */
  2283. /**
  2284. * Create and sign a new locator
  2285. *
  2286. * Note that attributes must be either NULL to use defaults for all or there
  2287. * must be an attributes object for each endpoint.
  2288. *
  2289. * @param ts Locator timestamp
  2290. * @param endpoints List of endpoints to store in locator
  2291. * @param endpointAttributes Array of ZT_EndpointAttributes objects or NULL to use defaults
  2292. * @param endpointCount Number of endpoints (maximum: 8)
  2293. * @param signer Identity to sign locator (must include private key)
  2294. * @return Locator or NULL on error (too many endpoints or identity does not have private key)
  2295. */
  2296. ZT_SDK_API ZT_Locator *ZT_Locator_create(
  2297. int64_t ts,
  2298. const ZT_Endpoint *endpoints,
  2299. const ZT_EndpointAttributes *endpointAttributes,
  2300. unsigned int endpointCount,
  2301. const ZT_Identity *signer);
  2302. /**
  2303. * Decode a serialized locator
  2304. *
  2305. * @param data Data to deserialize
  2306. * @param len Length of data
  2307. * @return Locator or NULL if data is not valid
  2308. */
  2309. ZT_SDK_API ZT_Locator *ZT_Locator_unmarshal(
  2310. const void *data,
  2311. unsigned int len);
  2312. /**
  2313. * Decode a locator from string format
  2314. *
  2315. * @param str String format locator
  2316. * @return Locator or NULL if string is not valid
  2317. */
  2318. ZT_SDK_API ZT_Locator *ZT_Locator_fromString(const char *str);
  2319. /**
  2320. * Serialize this locator into a buffer
  2321. *
  2322. * @param loc Locator to serialize
  2323. * @param buf Buffer to store bytes
  2324. * @param bufSize Size of buffer in bytes (needs to be at least 2048 bytes in size)
  2325. * @return Number of bytes stored to buf or -1 on error such as buffer too small
  2326. */
  2327. ZT_SDK_API int ZT_Locator_marshal(
  2328. const ZT_Locator *loc,
  2329. void *buf,
  2330. unsigned int bufSize);
  2331. /**
  2332. * Get this locator in string format
  2333. *
  2334. * @param loc Locator
  2335. * @param buf Buffer to store string
  2336. * @param capacity Capacity of buffer in bytes (recommended size: 4096)
  2337. * @return Pointer to buffer or NULL if an error occurs
  2338. */
  2339. ZT_SDK_API char *ZT_Locator_toString(
  2340. const ZT_Locator *loc,
  2341. char *buf,
  2342. int capacity);
  2343. /**
  2344. * Get a pointer to the fingerprint of this locator's signer.
  2345. *
  2346. * The returned pointer remains valid as long as the Locator is not deleted.
  2347. *
  2348. * @param loc Locator to query
  2349. * @return Pointer to fingerprint of signer
  2350. */
  2351. ZT_SDK_API const ZT_Fingerprint *ZT_Locator_fingerprint(const ZT_Locator *loc);
  2352. /**
  2353. * Get a locator's timestamp
  2354. *
  2355. * @param loc Locator to query
  2356. * @return Locator timestamp in milliseconds since epoch
  2357. */
  2358. ZT_SDK_API int64_t ZT_Locator_timestamp(const ZT_Locator *loc);
  2359. /**
  2360. * Get the number of endpoints in this locator
  2361. *
  2362. * @param loc Locator to query
  2363. * @return Number of endpoints
  2364. */
  2365. ZT_SDK_API unsigned int ZT_Locator_endpointCount(const ZT_Locator *loc);
  2366. /**
  2367. * Get a pointer to an endpoint in a locator
  2368. *
  2369. * The returned pointer remains valid as long as the Locator is not deleted.
  2370. *
  2371. * @param ep Endpoint number from 0 to 1 - endpointCount()
  2372. * @return Endpoint or NULL if out of bounds
  2373. */
  2374. ZT_SDK_API const ZT_Endpoint *ZT_Locator_endpoint(
  2375. const ZT_Locator *loc,
  2376. unsigned int ep);
  2377. /**
  2378. * Verify this locator's signature
  2379. *
  2380. * @param signer Signing identity
  2381. * @return Non-zero if locator is valid
  2382. */
  2383. ZT_SDK_API int ZT_Locator_verify(
  2384. const ZT_Locator *loc,
  2385. const ZT_Identity *signer);
  2386. /**
  2387. * Delete a locator
  2388. *
  2389. * @param loc Locator to delete
  2390. */
  2391. ZT_SDK_API void ZT_Locator_delete(ZT_Locator *loc);
  2392. /* ---------------------------------------------------------------------------------------------------------------- */
  2393. /**
  2394. * Get ZeroTier core version
  2395. *
  2396. * @param major Result: major version
  2397. * @param minor Result: minor version
  2398. * @param revision Result: revision
  2399. * @param build Result: build number
  2400. */
  2401. ZT_SDK_API void ZT_version(
  2402. int *major,
  2403. int *minor,
  2404. int *revision,
  2405. int *build);
  2406. /* ---------------------------------------------------------------------------------------------------------------- */
  2407. /**
  2408. * Create a new certificate subject unique ID and private key
  2409. *
  2410. * A unique ID is really a public/private key pair.
  2411. *
  2412. * @param type Unique ID type (key pair algorithm)
  2413. * @param uniqueId Unique ID buffer
  2414. * @param uniqueIdSize Value/result: size of buffer
  2415. * @param uniqueIdPrivate Unique ID private key buffer
  2416. * @param uniqueIdPrivateSize Value/result: size of buffer
  2417. * @return OK (0) or error
  2418. */
  2419. ZT_SDK_API int ZT_Certificate_newSubjectUniqueId(
  2420. enum ZT_CertificateUniqueIdType type,
  2421. void *uniqueId,
  2422. int *uniqueIdSize,
  2423. void *uniqueIdPrivate,
  2424. int *uniqueIdPrivateSize);
  2425. /**
  2426. * Create a new certificate signing request (CSR)
  2427. *
  2428. * A CSR is effectively just an encoded certificate subject.
  2429. * If both uniqueId and uniqueIdPrivate are specified, the subject
  2430. * will be signed with a unique ID. Otherwise these fields are not
  2431. * set. If a unique ID and unique ID signature are present in the
  2432. * supplied subject, these will be ignored.
  2433. *
  2434. * @param subject Subject filled in with fields for CSR
  2435. * @param uniqueId Unique ID or NULL if none
  2436. * @param uniqueIdSize Size of unique ID
  2437. * @param uniqueIdPrivate Unique ID private key or NULL if none
  2438. * @param uniqueIdPrivateSize Size of unique ID private key
  2439. * @param csr Buffer to hold CSR (recommended size: 16384 bytes)
  2440. * @param csrSize Value/result: size of buffer
  2441. * @return OK (0) or error
  2442. */
  2443. ZT_SDK_API int ZT_Certificate_newCSR(
  2444. const ZT_Certificate_Subject *subject,
  2445. const void *uniqueId,
  2446. int uniqueIdSize,
  2447. const void *uniqueIdPrivate,
  2448. int uniqueIdPrivateSize,
  2449. void *csr,
  2450. int *csrSize);
  2451. /**
  2452. * Sign a CSR to generate a complete certificate.
  2453. *
  2454. * @param cert Certificate to sign
  2455. * @param signer Signer identity (must contain secret key)
  2456. * @param signedCert Signed certificate buffer (recommended size: 16384 bytes)
  2457. * @param signedCertSize Value/result: size of buffer
  2458. * @return OK (0) or error
  2459. */
  2460. ZT_SDK_API int ZT_Certificate_sign(
  2461. const ZT_Certificate *cert,
  2462. const ZT_Identity *signer,
  2463. void *signedCert,
  2464. int *signedCertSize);
  2465. /**
  2466. * Decode a certificate or CSR
  2467. *
  2468. * A CSR is just the encoded subject part of a certificate. Decoding a CSR
  2469. * results in a certificate whose subject is filled in but nothing else.
  2470. *
  2471. * If no error occurs and the pointer at decodedCert is set to non-NULL,
  2472. * the returned certificate must be freed with ZT_Certificate_delete().
  2473. *
  2474. * @param decodedCert Result parameter: target pointer is set to certificate
  2475. * @param cert Certificate or CSR data
  2476. * @param certSize Size of data
  2477. * @param verify If non-zero, verify signatures and structure
  2478. * @return Certificate error, if any
  2479. */
  2480. ZT_SDK_API enum ZT_CertificateError ZT_Certificate_decode(
  2481. const ZT_Certificate **decodedCert,
  2482. const void *cert,
  2483. int certSize,
  2484. int verify);
  2485. /**
  2486. * Encode a certificate
  2487. *
  2488. * @param cert Certificate to encode
  2489. * @param encoded Buffer to store certificate (suggested size: 16384)
  2490. * @param encodedSize Value/result: size of certificate encoding buffer
  2491. * @return OK (0) or error
  2492. */
  2493. ZT_SDK_API int ZT_Certificate_encode(
  2494. const ZT_Certificate *cert,
  2495. void *encoded,
  2496. int *encodedSize);
  2497. /**
  2498. * Verify certificate signatures and internal structure.
  2499. *
  2500. * @param cert Certificate to verify
  2501. * @return Certificate error or ZT_CERTIFICATE_ERROR_NONE if no errors found.
  2502. */
  2503. ZT_SDK_API enum ZT_CertificateError ZT_Certificate_verify(const ZT_Certificate *cert);
  2504. /**
  2505. * Deep clone a certificate, returning one allocated C-side.
  2506. *
  2507. * The returned certificate must be freed with ZT_Certificate_delete(). This is
  2508. * primarily to make copies of certificates that may contain pointers to objects
  2509. * on the stack, etc., before actually returning them.
  2510. *
  2511. * @param cert Certificate to deep clone
  2512. * @return New certificate with copies of all objects
  2513. */
  2514. ZT_SDK_API const ZT_Certificate *ZT_Certificate_clone(const ZT_Certificate *cert);
  2515. /**
  2516. * Free a certificate created with ZT_Certificate_decode() or ZT_Certificate_clone()
  2517. *
  2518. * @param cert Certificate to free
  2519. */
  2520. ZT_SDK_API void ZT_Certificate_delete(const ZT_Certificate *cert);
  2521. /* ---------------------------------------------------------------------------------------------------------------- */
  2522. #ifdef __cplusplus
  2523. }
  2524. #endif
  2525. #endif