zerotier.h 77 KB

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