ZeroTierCore.h 62 KB

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