zerotier.h 79 KB

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