ZeroTierOne.h 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * This defines the external C API for ZeroTier's core network virtualization
  20. * engine.
  21. */
  22. #ifndef ZT_ZEROTIERONE_H
  23. #define ZT_ZEROTIERONE_H
  24. #include <stdint.h>
  25. // For the struct sockaddr_storage structure
  26. #if defined(_WIN32) || defined(_WIN64)
  27. #include <WinSock2.h>
  28. #include <WS2tcpip.h>
  29. #include <Windows.h>
  30. #else /* not Windows */
  31. #include <arpa/inet.h>
  32. #include <netinet/in.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #endif /* Windows or not */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /****************************************************************************/
  40. /* Core constants */
  41. /****************************************************************************/
  42. /**
  43. * Default UDP port for devices running a ZeroTier endpoint
  44. */
  45. #define ZT_DEFAULT_PORT 9993
  46. /**
  47. * Maximum MTU for ZeroTier virtual networks
  48. *
  49. * This is pretty much an unchangeable global constant. To make it change
  50. * across nodes would require logic to send ICMP packet too big messages,
  51. * which would complicate things. 1500 has been good enough on most LANs
  52. * for ages, so a larger MTU should be fine for the forseeable future. This
  53. * typically results in two UDP packets per single large frame. Experimental
  54. * results seem to show that this is good. Larger MTUs resulting in more
  55. * fragments seemed too brittle on slow/crummy links for no benefit.
  56. *
  57. * If this does change, also change it in tap.h in the tuntaposx code under
  58. * mac-tap.
  59. *
  60. * Overhead for a normal frame split into two packets:
  61. *
  62. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  63. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  64. * SUM: 2842
  65. *
  66. * We use 2800, which leaves some room for other payload in other types of
  67. * messages such as multicast propagation or future support for bridging.
  68. */
  69. #define ZT_MAX_MTU 2800
  70. /**
  71. * Maximum length of network short name
  72. */
  73. #define ZT_MAX_NETWORK_SHORT_NAME_LENGTH 127
  74. /**
  75. * Maximum number of pushed routes on a network
  76. */
  77. #define ZT_MAX_NETWORK_ROUTES 32
  78. /**
  79. * Maximum number of statically assigned IP addresses per network endpoint using ZT address management (not DHCP)
  80. */
  81. #define ZT_MAX_ZT_ASSIGNED_ADDRESSES 16
  82. /**
  83. * Maximum number of "specialists" on a network -- bridges, relays, etc.
  84. */
  85. #define ZT_MAX_NETWORK_SPECIALISTS 256
  86. /**
  87. * Maximum number of multicast group subscriptions per network
  88. */
  89. #define ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS 4096
  90. /**
  91. * Rules engine revision ID, which specifies rules engine capabilities
  92. */
  93. #define ZT_RULES_ENGINE_REVISION 1
  94. /**
  95. * Maximum number of base (non-capability) network rules
  96. */
  97. #define ZT_MAX_NETWORK_RULES 1024
  98. /**
  99. * Maximum number of per-member capabilities per network
  100. */
  101. #define ZT_MAX_NETWORK_CAPABILITIES 128
  102. /**
  103. * Maximum number of per-member tags per network
  104. */
  105. #define ZT_MAX_NETWORK_TAGS 128
  106. /**
  107. * Maximum number of direct network paths to a given peer
  108. */
  109. #define ZT_MAX_PEER_NETWORK_PATHS 4
  110. /**
  111. * Maximum number of trusted physical network paths
  112. */
  113. #define ZT_MAX_TRUSTED_PATHS 16
  114. /**
  115. * Maximum number of rules per capability
  116. */
  117. #define ZT_MAX_CAPABILITY_RULES 64
  118. /**
  119. * Global maximum length for capability chain of custody (including initial issue)
  120. */
  121. #define ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH 7
  122. /**
  123. * Maximum number of hops in a ZeroTier circuit test
  124. *
  125. * This is more or less the max that can be fit in a given packet (with
  126. * fragmentation) and only one address per hop.
  127. */
  128. #define ZT_CIRCUIT_TEST_MAX_HOPS 256
  129. /**
  130. * Maximum number of addresses per hop in a circuit test
  131. */
  132. #define ZT_CIRCUIT_TEST_MAX_HOP_BREADTH 8
  133. /**
  134. * Circuit test report flag: upstream peer authorized in path (e.g. by network COM)
  135. */
  136. #define ZT_CIRCUIT_TEST_REPORT_FLAGS_UPSTREAM_AUTHORIZED_IN_PATH 0x0000000000000001ULL
  137. /**
  138. * Maximum number of cluster members (and max member ID plus one)
  139. */
  140. #define ZT_CLUSTER_MAX_MEMBERS 128
  141. /**
  142. * Maximum number of physical ZeroTier addresses a cluster member can report
  143. */
  144. #define ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES 16
  145. /**
  146. * Maximum allowed cluster message length in bytes
  147. */
  148. #define ZT_CLUSTER_MAX_MESSAGE_LENGTH (1500 - 48)
  149. /**
  150. * Packet characteristics flag: packet direction, 1 if inbound 0 if outbound
  151. */
  152. #define ZT_RULE_PACKET_CHARACTERISTICS_INBOUND 0x8000000000000000ULL
  153. /**
  154. * Packet characteristics flag: multicast or broadcast destination MAC
  155. */
  156. #define ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST 0x4000000000000000ULL
  157. /**
  158. * Packet characteristics flag: broadcast destination MAC
  159. */
  160. #define ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST 0x2000000000000000ULL
  161. /**
  162. * Packet characteristics flag: TCP left-most reserved bit
  163. */
  164. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_0 0x0000000000000800ULL
  165. /**
  166. * Packet characteristics flag: TCP middle reserved bit
  167. */
  168. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_1 0x0000000000000400ULL
  169. /**
  170. * Packet characteristics flag: TCP right-most reserved bit
  171. */
  172. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_2 0x0000000000000200ULL
  173. /**
  174. * Packet characteristics flag: TCP NS flag
  175. */
  176. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS 0x0000000000000100ULL
  177. /**
  178. * Packet characteristics flag: TCP CWR flag
  179. */
  180. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR 0x0000000000000080ULL
  181. /**
  182. * Packet characteristics flag: TCP ECE flag
  183. */
  184. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE 0x0000000000000040ULL
  185. /**
  186. * Packet characteristics flag: TCP URG flag
  187. */
  188. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG 0x0000000000000020ULL
  189. /**
  190. * Packet characteristics flag: TCP ACK flag
  191. */
  192. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK 0x0000000000000010ULL
  193. /**
  194. * Packet characteristics flag: TCP PSH flag
  195. */
  196. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH 0x0000000000000008ULL
  197. /**
  198. * Packet characteristics flag: TCP RST flag
  199. */
  200. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST 0x0000000000000004ULL
  201. /**
  202. * Packet characteristics flag: TCP SYN flag
  203. */
  204. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN 0x0000000000000002ULL
  205. /**
  206. * Packet characteristics flag: TCP FIN flag
  207. */
  208. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN 0x0000000000000001ULL
  209. /**
  210. * A null/empty sockaddr (all zero) to signify an unspecified socket address
  211. */
  212. extern const struct sockaddr_storage ZT_SOCKADDR_NULL;
  213. /****************************************************************************/
  214. /* Structures and other types */
  215. /****************************************************************************/
  216. /**
  217. * Function return code: OK (0) or error results
  218. *
  219. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  220. * occurs, the node should be considered to not be working correctly. These
  221. * indicate serious problems like an inaccessible data store or a compile
  222. * problem.
  223. */
  224. enum ZT_ResultCode
  225. {
  226. /**
  227. * Operation completed normally
  228. */
  229. ZT_RESULT_OK = 0,
  230. // Fatal errors (>0, <1000)
  231. /**
  232. * Ran out of memory
  233. */
  234. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 1,
  235. /**
  236. * Data store is not writable or has failed
  237. */
  238. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 2,
  239. /**
  240. * Internal error (e.g. unexpected exception indicating bug or build problem)
  241. */
  242. ZT_RESULT_FATAL_ERROR_INTERNAL = 3,
  243. // Non-fatal errors (>1000)
  244. /**
  245. * Network ID not valid
  246. */
  247. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000,
  248. /**
  249. * The requested operation is not supported on this version or build
  250. */
  251. ZT_RESULT_ERROR_UNSUPPORTED_OPERATION = 1001,
  252. /**
  253. * The requestion operation was given a bad parameter or was called in an invalid state
  254. */
  255. ZT_RESULT_ERROR_BAD_PARAMETER = 1002
  256. };
  257. /**
  258. * @param x Result code
  259. * @return True if result code indicates a fatal error
  260. */
  261. #define ZT_ResultCode_isFatal(x) ((((int)(x)) > 0)&&(((int)(x)) < 1000))
  262. /**
  263. * Status codes sent to status update callback when things happen
  264. */
  265. enum ZT_Event
  266. {
  267. /**
  268. * Node has been initialized
  269. *
  270. * This is the first event generated, and is always sent. It may occur
  271. * before Node's constructor returns.
  272. *
  273. * Meta-data: none
  274. */
  275. ZT_EVENT_UP = 0,
  276. /**
  277. * Node is offline -- network does not seem to be reachable by any available strategy
  278. *
  279. * Meta-data: none
  280. */
  281. ZT_EVENT_OFFLINE = 1,
  282. /**
  283. * Node is online -- at least one upstream node appears reachable
  284. *
  285. * Meta-data: none
  286. */
  287. ZT_EVENT_ONLINE = 2,
  288. /**
  289. * Node is shutting down
  290. *
  291. * This is generated within Node's destructor when it is being shut down.
  292. * It's done for convenience, since cleaning up other state in the event
  293. * handler may appear more idiomatic.
  294. *
  295. * Meta-data: none
  296. */
  297. ZT_EVENT_DOWN = 3,
  298. /**
  299. * Your identity has collided with another node's ZeroTier address
  300. *
  301. * This happens if two different public keys both hash (via the algorithm
  302. * in Identity::generate()) to the same 40-bit ZeroTier address.
  303. *
  304. * This is something you should "never" see, where "never" is defined as
  305. * once per 2^39 new node initializations / identity creations. If you do
  306. * see it, you're going to see it very soon after a node is first
  307. * initialized.
  308. *
  309. * This is reported as an event rather than a return code since it's
  310. * detected asynchronously via error messages from authoritative nodes.
  311. *
  312. * If this occurs, you must shut down and delete the node, delete the
  313. * identity.secret record/file from the data store, and restart to generate
  314. * a new identity. If you don't do this, you will not be able to communicate
  315. * with other nodes.
  316. *
  317. * We'd automate this process, but we don't think silently deleting
  318. * private keys or changing our address without telling the calling code
  319. * is good form. It violates the principle of least surprise.
  320. *
  321. * You can technically get away with not handling this, but we recommend
  322. * doing so in a mature reliable application. Besides, handling this
  323. * condition is a good way to make sure it never arises. It's like how
  324. * umbrellas prevent rain and smoke detectors prevent fires. They do, right?
  325. *
  326. * Meta-data: none
  327. */
  328. ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION = 4,
  329. /**
  330. * Trace (debugging) message
  331. *
  332. * These events are only generated if this is a TRACE-enabled build.
  333. *
  334. * Meta-data: C string, TRACE message
  335. */
  336. ZT_EVENT_TRACE = 5,
  337. /**
  338. * VERB_USER_MESSAGE received
  339. *
  340. * These are generated when a VERB_USER_MESSAGE packet is received via
  341. * ZeroTier VL1.
  342. *
  343. * Meta-data: ZT_UserMessage structure
  344. */
  345. ZT_EVENT_USER_MESSAGE = 6
  346. };
  347. /**
  348. * Node relay policy
  349. */
  350. enum ZT_RelayPolicy
  351. {
  352. ZT_RELAY_POLICY_NEVER = 0,
  353. ZT_RELAY_POLICY_TRUSTED = 1,
  354. ZT_RELAY_POLICY_ALWAYS = 2
  355. };
  356. /**
  357. * User message used with ZT_EVENT_USER_MESSAGE
  358. */
  359. typedef struct
  360. {
  361. /**
  362. * ZeroTier address of sender (least significant 40 bits)
  363. */
  364. uint64_t origin;
  365. /**
  366. * User message type ID
  367. */
  368. uint64_t typeId;
  369. /**
  370. * User message data (not including type ID)
  371. */
  372. const void *data;
  373. /**
  374. * Length of data in bytes
  375. */
  376. unsigned int length;
  377. } ZT_UserMessage;
  378. /**
  379. * Current node status
  380. */
  381. typedef struct
  382. {
  383. /**
  384. * 40-bit ZeroTier address of this node
  385. */
  386. uint64_t address;
  387. /**
  388. * Current world ID
  389. */
  390. uint64_t worldId;
  391. /**
  392. * Current world revision/timestamp
  393. */
  394. uint64_t worldTimestamp;
  395. /**
  396. * Public identity in string-serialized form (safe to send to others)
  397. *
  398. * This pointer will remain valid as long as the node exists.
  399. */
  400. const char *publicIdentity;
  401. /**
  402. * Full identity including secret key in string-serialized form
  403. *
  404. * This pointer will remain valid as long as the node exists.
  405. */
  406. const char *secretIdentity;
  407. /**
  408. * Node relay policy
  409. */
  410. enum ZT_RelayPolicy relayPolicy;
  411. /**
  412. * True if some kind of connectivity appears available
  413. */
  414. int online;
  415. } ZT_NodeStatus;
  416. /**
  417. * Virtual network status codes
  418. */
  419. enum ZT_VirtualNetworkStatus
  420. {
  421. /**
  422. * Waiting for network configuration (also means revision == 0)
  423. */
  424. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  425. /**
  426. * Configuration received and we are authorized
  427. */
  428. ZT_NETWORK_STATUS_OK = 1,
  429. /**
  430. * Netconf master told us 'nope'
  431. */
  432. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  433. /**
  434. * Netconf master exists, but this virtual network does not
  435. */
  436. ZT_NETWORK_STATUS_NOT_FOUND = 3,
  437. /**
  438. * Initialization of network failed or other internal error
  439. */
  440. ZT_NETWORK_STATUS_PORT_ERROR = 4,
  441. /**
  442. * ZeroTier core version too old
  443. */
  444. ZT_NETWORK_STATUS_CLIENT_TOO_OLD = 5
  445. };
  446. /**
  447. * Virtual network type codes
  448. */
  449. enum ZT_VirtualNetworkType
  450. {
  451. /**
  452. * Private networks are authorized via certificates of membership
  453. */
  454. ZT_NETWORK_TYPE_PRIVATE = 0,
  455. /**
  456. * Public networks have no access control -- they'll always be AUTHORIZED
  457. */
  458. ZT_NETWORK_TYPE_PUBLIC = 1
  459. };
  460. /**
  461. * The type of a virtual network rules table entry
  462. *
  463. * These must be from 0 to 63 since the most significant two bits of each
  464. * rule type are NOT (MSB) and AND/OR.
  465. *
  466. * Each rule is composed of zero or more MATCHes followed by an ACTION.
  467. * An ACTION with no MATCHes is always taken.
  468. */
  469. enum ZT_VirtualNetworkRuleType
  470. {
  471. // 0 to 15 reserved for actions
  472. /**
  473. * Drop frame
  474. */
  475. ZT_NETWORK_RULE_ACTION_DROP = 0,
  476. /**
  477. * Accept and pass frame
  478. */
  479. ZT_NETWORK_RULE_ACTION_ACCEPT = 1,
  480. /**
  481. * Forward a copy of this frame to an observer (by ZT address)
  482. */
  483. ZT_NETWORK_RULE_ACTION_TEE = 2,
  484. /**
  485. * Exactly like TEE but mandates ACKs from observer
  486. */
  487. ZT_NETWORK_RULE_ACTION_WATCH = 3,
  488. /**
  489. * Drop and redirect this frame to another node (by ZT address)
  490. */
  491. ZT_NETWORK_RULE_ACTION_REDIRECT = 4,
  492. /**
  493. * Log if match and if rule debugging is enabled in the build, otherwise does nothing (for developers)
  494. */
  495. ZT_NETWORK_RULE_ACTION_DEBUG_LOG = 5,
  496. /**
  497. * Maximum ID for an ACTION, anything higher is a MATCH
  498. */
  499. ZT_NETWORK_RULE_ACTION__MAX_ID = 15,
  500. // 16 to 63 reserved for match criteria
  501. ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS = 24,
  502. ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS = 25,
  503. ZT_NETWORK_RULE_MATCH_VLAN_ID = 26,
  504. ZT_NETWORK_RULE_MATCH_VLAN_PCP = 27,
  505. ZT_NETWORK_RULE_MATCH_VLAN_DEI = 28,
  506. ZT_NETWORK_RULE_MATCH_MAC_SOURCE = 29,
  507. ZT_NETWORK_RULE_MATCH_MAC_DEST = 30,
  508. ZT_NETWORK_RULE_MATCH_IPV4_SOURCE = 31,
  509. ZT_NETWORK_RULE_MATCH_IPV4_DEST = 32,
  510. ZT_NETWORK_RULE_MATCH_IPV6_SOURCE = 33,
  511. ZT_NETWORK_RULE_MATCH_IPV6_DEST = 34,
  512. ZT_NETWORK_RULE_MATCH_IP_TOS = 35,
  513. ZT_NETWORK_RULE_MATCH_IP_PROTOCOL = 36,
  514. ZT_NETWORK_RULE_MATCH_ETHERTYPE = 37,
  515. ZT_NETWORK_RULE_MATCH_ICMP = 38,
  516. ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE = 39,
  517. ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE = 40,
  518. ZT_NETWORK_RULE_MATCH_CHARACTERISTICS = 41,
  519. ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE = 42,
  520. ZT_NETWORK_RULE_MATCH_RANDOM = 43,
  521. ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE = 44,
  522. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND = 45,
  523. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR = 46,
  524. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR = 47,
  525. ZT_NETWORK_RULE_MATCH_TAGS_EQUAL = 48,
  526. /**
  527. * Maximum ID allowed for a MATCH entry in the rules table
  528. */
  529. ZT_NETWORK_RULE_MATCH__MAX_ID = 63
  530. };
  531. /**
  532. * Network flow rule
  533. *
  534. * Rules are stored in a table in which one or more match entries is followed
  535. * by an action. If more than one match precedes an action, the rule is
  536. * the AND of all matches. An action with no match is always taken since it
  537. * matches anything. If nothing matches, the default action is DROP.
  538. *
  539. * This is designed to be a more memory-efficient way of storing rules than
  540. * a wide table, yet still fast and simple to access in code.
  541. */
  542. typedef struct
  543. {
  544. /**
  545. * Least significant 7 bits: ZT_VirtualNetworkRuleType, most significant 1 bit is NOT bit
  546. *
  547. * If the NOT bit is set, then matches will be interpreted as "does not
  548. * match." The NOT bit has no effect on actions.
  549. *
  550. * Use "& 0x7f" to get the enum and "& 0x80" to get the NOT flag.
  551. *
  552. * The union 'v' is a variant type, and this selects which field in 'v' is
  553. * actually used and valid.
  554. */
  555. uint8_t t;
  556. /**
  557. * Union containing the value of this rule -- which field is used depends on 't'
  558. */
  559. union {
  560. /**
  561. * IPv6 address in big-endian / network byte order and netmask bits
  562. */
  563. struct {
  564. uint8_t ip[16];
  565. uint8_t mask;
  566. } ipv6;
  567. /**
  568. * IPv4 address in big-endian / network byte order
  569. */
  570. struct {
  571. uint32_t ip;
  572. uint8_t mask;
  573. } ipv4;
  574. /**
  575. * Packet characteristic flags being matched
  576. */
  577. uint64_t characteristics;
  578. /**
  579. * IP port range -- start-end inclusive -- host byte order
  580. */
  581. uint16_t port[2];
  582. /**
  583. * 40-bit ZeroTier address (in least significant bits, host byte order)
  584. */
  585. uint64_t zt;
  586. /**
  587. * 0 = never, UINT32_MAX = always
  588. */
  589. uint32_t randomProbability;
  590. /**
  591. * 48-bit Ethernet MAC address in big-endian order
  592. */
  593. uint8_t mac[6];
  594. /**
  595. * VLAN ID in host byte order
  596. */
  597. uint16_t vlanId;
  598. /**
  599. * VLAN PCP (least significant 3 bits)
  600. */
  601. uint8_t vlanPcp;
  602. /**
  603. * VLAN DEI (single bit / boolean)
  604. */
  605. uint8_t vlanDei;
  606. /**
  607. * Ethernet type in host byte order
  608. */
  609. uint16_t etherType;
  610. /**
  611. * IP protocol
  612. */
  613. uint8_t ipProtocol;
  614. /**
  615. * IP type of service a.k.a. DSCP field
  616. */
  617. uint8_t ipTos;
  618. /**
  619. * Ethernet packet size in host byte order (start-end, inclusive)
  620. */
  621. uint16_t frameSize[2];
  622. /**
  623. * ICMP type and code
  624. */
  625. struct {
  626. uint8_t type; // ICMP type, always matched
  627. uint8_t code; // ICMP code if matched
  628. uint8_t flags; // flag 0x01 means also match code, otherwise only match type
  629. } icmp;
  630. /**
  631. * For tag-related rules
  632. */
  633. struct {
  634. uint32_t id;
  635. uint32_t value;
  636. } tag;
  637. /**
  638. * Destinations for TEE and REDIRECT
  639. */
  640. struct {
  641. uint64_t address;
  642. uint32_t flags;
  643. uint16_t length;
  644. } fwd;
  645. } v;
  646. } ZT_VirtualNetworkRule;
  647. typedef struct
  648. {
  649. /**
  650. * 128-bit ID (GUID) of this capability
  651. */
  652. uint64_t id[2];
  653. /**
  654. * Expiration time (measured vs. network config timestamp issued by controller)
  655. */
  656. uint64_t expiration;
  657. struct {
  658. uint64_t from;
  659. uint64_t to;
  660. } custody[ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH];
  661. } ZT_VirtualNetworkCapability;
  662. /**
  663. * A route to be pushed on a virtual network
  664. */
  665. typedef struct
  666. {
  667. /**
  668. * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
  669. */
  670. struct sockaddr_storage target;
  671. /**
  672. * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
  673. */
  674. struct sockaddr_storage via;
  675. /**
  676. * Route flags
  677. */
  678. uint16_t flags;
  679. /**
  680. * Route metric (not currently used)
  681. */
  682. uint16_t metric;
  683. } ZT_VirtualNetworkRoute;
  684. /**
  685. * An Ethernet multicast group
  686. */
  687. typedef struct
  688. {
  689. /**
  690. * MAC address (least significant 48 bits)
  691. */
  692. uint64_t mac;
  693. /**
  694. * Additional distinguishing information (usually zero)
  695. */
  696. unsigned long adi;
  697. } ZT_MulticastGroup;
  698. /**
  699. * Virtual network configuration update type
  700. */
  701. enum ZT_VirtualNetworkConfigOperation
  702. {
  703. /**
  704. * Network is coming up (either for the first time or after service restart)
  705. */
  706. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  707. /**
  708. * Network configuration has been updated
  709. */
  710. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  711. /**
  712. * Network is going down (not permanently)
  713. */
  714. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  715. /**
  716. * Network is going down permanently (leave/delete)
  717. */
  718. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  719. };
  720. /**
  721. * What trust hierarchy role does this peer have?
  722. */
  723. enum ZT_PeerRole
  724. {
  725. ZT_PEER_ROLE_LEAF = 0, // ordinary node
  726. ZT_PEER_ROLE_UPSTREAM = 1, // upstream node
  727. ZT_PEER_ROLE_ROOT = 2 // global root
  728. };
  729. /**
  730. * Vendor ID
  731. */
  732. enum ZT_Vendor
  733. {
  734. ZT_VENDOR_UNSPECIFIED = 0,
  735. ZT_VENDOR_ZEROTIER = 1
  736. };
  737. /**
  738. * Platform type
  739. */
  740. enum ZT_Platform
  741. {
  742. ZT_PLATFORM_UNSPECIFIED = 0,
  743. ZT_PLATFORM_LINUX = 1,
  744. ZT_PLATFORM_WINDOWS = 2,
  745. ZT_PLATFORM_MACOS = 3,
  746. ZT_PLATFORM_ANDROID = 4,
  747. ZT_PLATFORM_IOS = 5,
  748. ZT_PLATFORM_SOLARIS_SMARTOS = 6,
  749. ZT_PLATFORM_FREEBSD = 7,
  750. ZT_PLATFORM_NETBSD = 8,
  751. ZT_PLATFORM_OPENBSD = 9,
  752. ZT_PLATFORM_RISCOS = 10,
  753. ZT_PLATFORM_VXWORKS = 11,
  754. ZT_PLATFORM_FREERTOS = 12,
  755. ZT_PLATFORM_SYSBIOS = 13,
  756. ZT_PLATFORM_HURD = 14,
  757. ZT_PLATFORM_WEB = 15
  758. };
  759. /**
  760. * Architecture type
  761. */
  762. enum ZT_Architecture
  763. {
  764. ZT_ARCHITECTURE_UNSPECIFIED = 0,
  765. ZT_ARCHITECTURE_X86 = 1,
  766. ZT_ARCHITECTURE_X64 = 2,
  767. ZT_ARCHITECTURE_ARM32 = 3,
  768. ZT_ARCHITECTURE_ARM64 = 4,
  769. ZT_ARCHITECTURE_MIPS32 = 5,
  770. ZT_ARCHITECTURE_MIPS64 = 6,
  771. ZT_ARCHITECTURE_POWER32 = 7,
  772. ZT_ARCHITECTURE_POWER64 = 8,
  773. ZT_ARCHITECTURE_OPENRISC32 = 9,
  774. ZT_ARCHITECTURE_OPENRISC64 = 10,
  775. ZT_ARCHITECTURE_SPARC32 = 11,
  776. ZT_ARCHITECTURE_SPARC64 = 12,
  777. ZT_ARCHITECTURE_DOTNET_CLR = 13,
  778. ZT_ARCHITECTURE_JAVA_JVM = 14,
  779. ZT_ARCHITECTURE_WEB = 15
  780. };
  781. /**
  782. * Virtual network configuration
  783. */
  784. typedef struct
  785. {
  786. /**
  787. * 64-bit ZeroTier network ID
  788. */
  789. uint64_t nwid;
  790. /**
  791. * Ethernet MAC (48 bits) that should be assigned to port
  792. */
  793. uint64_t mac;
  794. /**
  795. * Network name (from network configuration master)
  796. */
  797. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  798. /**
  799. * Network configuration request status
  800. */
  801. enum ZT_VirtualNetworkStatus status;
  802. /**
  803. * Network type
  804. */
  805. enum ZT_VirtualNetworkType type;
  806. /**
  807. * Maximum interface MTU
  808. */
  809. unsigned int mtu;
  810. /**
  811. * Recommended MTU to avoid fragmentation at the physical layer (hint)
  812. */
  813. unsigned int physicalMtu;
  814. /**
  815. * If nonzero, the network this port belongs to indicates DHCP availability
  816. *
  817. * This is a suggestion. The underlying implementation is free to ignore it
  818. * for security or other reasons. This is simply a netconf parameter that
  819. * means 'DHCP is available on this network.'
  820. */
  821. int dhcp;
  822. /**
  823. * If nonzero, this port is allowed to bridge to other networks
  824. *
  825. * This is informational. If this is false (0), bridged packets will simply
  826. * be dropped and bridging won't work.
  827. */
  828. int bridge;
  829. /**
  830. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  831. */
  832. int broadcastEnabled;
  833. /**
  834. * If the network is in PORT_ERROR state, this is the (negative) error code most recently reported
  835. */
  836. int portError;
  837. /**
  838. * Revision number as reported by controller or 0 if still waiting for config
  839. */
  840. unsigned long netconfRevision;
  841. /**
  842. * Number of assigned addresses
  843. */
  844. unsigned int assignedAddressCount;
  845. /**
  846. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  847. *
  848. * For IP, the port number of the sockaddr_XX structure contains the number
  849. * of bits in the address netmask. Only the IP address and port are used.
  850. * Other fields like interface number can be ignored.
  851. *
  852. * This is only used for ZeroTier-managed address assignments sent by the
  853. * virtual network's configuration master.
  854. */
  855. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  856. /**
  857. * Number of ZT-pushed routes
  858. */
  859. unsigned int routeCount;
  860. /**
  861. * Routes (excluding those implied by assigned addresses and their masks)
  862. */
  863. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  864. } ZT_VirtualNetworkConfig;
  865. /**
  866. * A list of networks
  867. */
  868. typedef struct
  869. {
  870. ZT_VirtualNetworkConfig *networks;
  871. unsigned long networkCount;
  872. } ZT_VirtualNetworkList;
  873. /**
  874. * Physical network path to a peer
  875. */
  876. typedef struct
  877. {
  878. /**
  879. * Address of endpoint
  880. */
  881. struct sockaddr_storage address;
  882. /**
  883. * Time of last send in milliseconds or 0 for never
  884. */
  885. uint64_t lastSend;
  886. /**
  887. * Time of last receive in milliseconds or 0 for never
  888. */
  889. uint64_t lastReceive;
  890. /**
  891. * Is this a trusted path? If so this will be its nonzero ID.
  892. */
  893. uint64_t trustedPathId;
  894. /**
  895. * Is path expired?
  896. */
  897. int expired;
  898. /**
  899. * Is path preferred?
  900. */
  901. int preferred;
  902. } ZT_PeerPhysicalPath;
  903. /**
  904. * Peer status result buffer
  905. */
  906. typedef struct
  907. {
  908. /**
  909. * ZeroTier address (40 bits)
  910. */
  911. uint64_t address;
  912. /**
  913. * Remote major version or -1 if not known
  914. */
  915. int versionMajor;
  916. /**
  917. * Remote minor version or -1 if not known
  918. */
  919. int versionMinor;
  920. /**
  921. * Remote revision or -1 if not known
  922. */
  923. int versionRev;
  924. /**
  925. * Last measured latency in milliseconds or zero if unknown
  926. */
  927. unsigned int latency;
  928. /**
  929. * What trust hierarchy role does this device have?
  930. */
  931. enum ZT_PeerRole role;
  932. /**
  933. * Number of paths (size of paths[])
  934. */
  935. unsigned int pathCount;
  936. /**
  937. * Known network paths to peer
  938. */
  939. ZT_PeerPhysicalPath paths[ZT_MAX_PEER_NETWORK_PATHS];
  940. } ZT_Peer;
  941. /**
  942. * List of peers
  943. */
  944. typedef struct
  945. {
  946. ZT_Peer *peers;
  947. unsigned long peerCount;
  948. } ZT_PeerList;
  949. /**
  950. * ZeroTier circuit test configuration and path
  951. */
  952. typedef struct {
  953. /**
  954. * Test ID -- an arbitrary 64-bit identifier
  955. */
  956. uint64_t testId;
  957. /**
  958. * Timestamp -- sent with test and echoed back by each reporter
  959. */
  960. uint64_t timestamp;
  961. /**
  962. * Originator credential: network ID
  963. *
  964. * If this is nonzero, a network ID will be set for this test and
  965. * the originator must be its primary network controller. This is
  966. * currently the only authorization method available, so it must
  967. * be set to run a test.
  968. */
  969. uint64_t credentialNetworkId;
  970. /**
  971. * Hops in circuit test (a.k.a. FIFO for graph traversal)
  972. */
  973. struct {
  974. /**
  975. * Hop flags (currently unused, must be zero)
  976. */
  977. unsigned int flags;
  978. /**
  979. * Number of addresses in this hop (max: ZT_CIRCUIT_TEST_MAX_HOP_BREADTH)
  980. */
  981. unsigned int breadth;
  982. /**
  983. * 40-bit ZeroTier addresses (most significant 24 bits ignored)
  984. */
  985. uint64_t addresses[ZT_CIRCUIT_TEST_MAX_HOP_BREADTH];
  986. } hops[ZT_CIRCUIT_TEST_MAX_HOPS];
  987. /**
  988. * Number of hops (max: ZT_CIRCUIT_TEST_MAX_HOPS)
  989. */
  990. unsigned int hopCount;
  991. /**
  992. * If non-zero, circuit test will report back at every hop
  993. */
  994. int reportAtEveryHop;
  995. /**
  996. * An arbitrary user-settable pointer
  997. */
  998. void *ptr;
  999. /**
  1000. * Pointer for internal use -- initialize to zero and do not modify
  1001. */
  1002. void *_internalPtr;
  1003. } ZT_CircuitTest;
  1004. /**
  1005. * Circuit test result report
  1006. */
  1007. typedef struct {
  1008. /**
  1009. * Sender of report (current hop)
  1010. */
  1011. uint64_t current;
  1012. /**
  1013. * Previous hop
  1014. */
  1015. uint64_t upstream;
  1016. /**
  1017. * 64-bit test ID
  1018. */
  1019. uint64_t testId;
  1020. /**
  1021. * Timestamp from original test (echoed back at each hop)
  1022. */
  1023. uint64_t timestamp;
  1024. /**
  1025. * 64-bit packet ID of packet received by the reporting device
  1026. */
  1027. uint64_t sourcePacketId;
  1028. /**
  1029. * Flags
  1030. */
  1031. uint64_t flags;
  1032. /**
  1033. * ZeroTier protocol-level hop count of packet received by reporting device (>0 indicates relayed)
  1034. */
  1035. unsigned int sourcePacketHopCount;
  1036. /**
  1037. * Error code (currently unused, will be zero)
  1038. */
  1039. unsigned int errorCode;
  1040. /**
  1041. * Remote device vendor ID
  1042. */
  1043. enum ZT_Vendor vendor;
  1044. /**
  1045. * Remote device protocol compliance version
  1046. */
  1047. unsigned int protocolVersion;
  1048. /**
  1049. * Software major version
  1050. */
  1051. unsigned int majorVersion;
  1052. /**
  1053. * Software minor version
  1054. */
  1055. unsigned int minorVersion;
  1056. /**
  1057. * Software revision
  1058. */
  1059. unsigned int revision;
  1060. /**
  1061. * Platform / OS
  1062. */
  1063. enum ZT_Platform platform;
  1064. /**
  1065. * System architecture
  1066. */
  1067. enum ZT_Architecture architecture;
  1068. /**
  1069. * Local device address on which packet was received by reporting device
  1070. *
  1071. * This may have ss_family equal to zero (null address) if unspecified.
  1072. */
  1073. struct sockaddr_storage receivedOnLocalAddress;
  1074. /**
  1075. * Remote address from which reporter received the test packet
  1076. *
  1077. * This may have ss_family set to zero (null address) if unspecified.
  1078. */
  1079. struct sockaddr_storage receivedFromRemoteAddress;
  1080. /**
  1081. * Next hops to which packets are being or will be sent by the reporter
  1082. *
  1083. * In addition to reporting back, the reporter may send the test on if
  1084. * there are more recipients in the FIFO. If it does this, it can report
  1085. * back the address(es) that make up the next hop and the physical address
  1086. * for each if it has one. The physical address being null/unspecified
  1087. * typically indicates that no direct path exists and the next packet
  1088. * will be relayed.
  1089. */
  1090. struct {
  1091. /**
  1092. * 40-bit ZeroTier address
  1093. */
  1094. uint64_t address;
  1095. /**
  1096. * Physical address or null address (ss_family == 0) if unspecified or unknown
  1097. */
  1098. struct sockaddr_storage physicalAddress;
  1099. } nextHops[ZT_CIRCUIT_TEST_MAX_HOP_BREADTH];
  1100. /**
  1101. * Number of next hops reported in nextHops[]
  1102. */
  1103. unsigned int nextHopCount;
  1104. } ZT_CircuitTestReport;
  1105. /**
  1106. * A cluster member's status
  1107. */
  1108. typedef struct {
  1109. /**
  1110. * This cluster member's ID (from 0 to 1-ZT_CLUSTER_MAX_MEMBERS)
  1111. */
  1112. unsigned int id;
  1113. /**
  1114. * Number of milliseconds since last 'alive' heartbeat message received via cluster backplane address
  1115. */
  1116. unsigned int msSinceLastHeartbeat;
  1117. /**
  1118. * Non-zero if cluster member is alive
  1119. */
  1120. int alive;
  1121. /**
  1122. * X, Y, and Z coordinates of this member (if specified, otherwise zero)
  1123. *
  1124. * What these mean depends on the location scheme being used for
  1125. * location-aware clustering. At present this is GeoIP and these
  1126. * will be the X, Y, and Z coordinates of the location on a spherical
  1127. * approximation of Earth where Earth's core is the origin (in km).
  1128. * They don't have to be perfect and need only be comparable with others
  1129. * to find shortest path via the standard vector distance formula.
  1130. */
  1131. int x,y,z;
  1132. /**
  1133. * Cluster member's last reported load
  1134. */
  1135. uint64_t load;
  1136. /**
  1137. * Number of peers
  1138. */
  1139. uint64_t peers;
  1140. /**
  1141. * Physical ZeroTier endpoints for this member (where peers are sent when directed here)
  1142. */
  1143. struct sockaddr_storage zeroTierPhysicalEndpoints[ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES];
  1144. /**
  1145. * Number of physical ZeroTier endpoints this member is announcing
  1146. */
  1147. unsigned int numZeroTierPhysicalEndpoints;
  1148. } ZT_ClusterMemberStatus;
  1149. /**
  1150. * ZeroTier cluster status
  1151. */
  1152. typedef struct {
  1153. /**
  1154. * My cluster member ID (a record for 'self' is included in member[])
  1155. */
  1156. unsigned int myId;
  1157. /**
  1158. * Number of cluster members
  1159. */
  1160. unsigned int clusterSize;
  1161. /**
  1162. * Cluster member statuses
  1163. */
  1164. ZT_ClusterMemberStatus members[ZT_CLUSTER_MAX_MEMBERS];
  1165. } ZT_ClusterStatus;
  1166. /**
  1167. * An instance of a ZeroTier One node (opaque)
  1168. */
  1169. typedef void ZT_Node;
  1170. /****************************************************************************/
  1171. /* Callbacks used by Node API */
  1172. /****************************************************************************/
  1173. /**
  1174. * Callback called to update virtual network port configuration
  1175. *
  1176. * This can be called at any time to update the configuration of a virtual
  1177. * network port. The parameter after the network ID specifies whether this
  1178. * port is being brought up, updated, brought down, or permanently deleted.
  1179. *
  1180. * This in turn should be used by the underlying implementation to create
  1181. * and configure tap devices at the OS (or virtual network stack) layer.
  1182. *
  1183. * The supplied config pointer is not guaranteed to remain valid, so make
  1184. * a copy if you want one.
  1185. *
  1186. * This should not call multicastSubscribe() or other network-modifying
  1187. * methods, as this could cause a deadlock in multithreaded or interrupt
  1188. * driven environments.
  1189. *
  1190. * This must return 0 on success. It can return any OS-dependent error code
  1191. * on failure, and this results in the network being placed into the
  1192. * PORT_ERROR state.
  1193. */
  1194. typedef int (*ZT_VirtualNetworkConfigFunction)(
  1195. ZT_Node *, /* Node */
  1196. void *, /* User ptr */
  1197. uint64_t, /* Network ID */
  1198. void **, /* Modifiable network user PTR */
  1199. enum ZT_VirtualNetworkConfigOperation, /* Config operation */
  1200. const ZT_VirtualNetworkConfig *); /* Network configuration */
  1201. /**
  1202. * Function to send a frame out to a virtual network port
  1203. *
  1204. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  1205. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  1206. * (9) frame length.
  1207. */
  1208. typedef void (*ZT_VirtualNetworkFrameFunction)(
  1209. ZT_Node *, /* Node */
  1210. void *, /* User ptr */
  1211. uint64_t, /* Network ID */
  1212. void **, /* Modifiable network user PTR */
  1213. uint64_t, /* Source MAC */
  1214. uint64_t, /* Destination MAC */
  1215. unsigned int, /* Ethernet type */
  1216. unsigned int, /* VLAN ID (0 for none) */
  1217. const void *, /* Frame data */
  1218. unsigned int); /* Frame length */
  1219. /**
  1220. * Callback for events
  1221. *
  1222. * Events are generated when the node's status changes in a significant way
  1223. * and on certain non-fatal errors and events of interest. The final void
  1224. * parameter points to event meta-data. The type of event meta-data (and
  1225. * whether it is present at all) is event type dependent. See the comments
  1226. * in the definition of ZT_Event.
  1227. */
  1228. typedef void (*ZT_EventCallback)(
  1229. ZT_Node *,
  1230. void *,
  1231. enum ZT_Event,
  1232. const void *);
  1233. /**
  1234. * Function to get an object from the data store
  1235. *
  1236. * Parameters: (1) object name, (2) buffer to fill, (3) size of buffer, (4)
  1237. * index in object to start reading, (5) result parameter that must be set
  1238. * to the actual size of the object if it exists.
  1239. *
  1240. * Object names can contain forward slash (/) path separators. They will
  1241. * never contain .. or backslash (\), so this is safe to map as a Unix-style
  1242. * path if the underlying storage permits. For security reasons we recommend
  1243. * returning errors if .. or \ are used.
  1244. *
  1245. * The function must return the actual number of bytes read. If the object
  1246. * doesn't exist, it should return -1. -2 should be returned on other errors
  1247. * such as errors accessing underlying storage.
  1248. *
  1249. * If the read doesn't fit in the buffer, the max number of bytes should be
  1250. * read. The caller may call the function multiple times to read the whole
  1251. * object.
  1252. */
  1253. typedef long (*ZT_DataStoreGetFunction)(
  1254. ZT_Node *,
  1255. void *,
  1256. const char *,
  1257. void *,
  1258. unsigned long,
  1259. unsigned long,
  1260. unsigned long *);
  1261. /**
  1262. * Function to store an object in the data store
  1263. *
  1264. * Parameters: (1) node, (2) user ptr, (3) object name, (4) object data,
  1265. * (5) object size, (6) secure? (bool).
  1266. *
  1267. * If secure is true, the file should be set readable and writable only
  1268. * to the user running ZeroTier One. What this means is platform-specific.
  1269. *
  1270. * Name semantics are the same as the get function. This must return zero on
  1271. * success. You can return any OS-specific error code on failure, as these
  1272. * may be visible in logs or error messages and might aid in debugging.
  1273. *
  1274. * If the data pointer is null, this must be interpreted as a delete
  1275. * operation.
  1276. */
  1277. typedef int (*ZT_DataStorePutFunction)(
  1278. ZT_Node *,
  1279. void *,
  1280. const char *,
  1281. const void *,
  1282. unsigned long,
  1283. int);
  1284. /**
  1285. * Function to send a ZeroTier packet out over the wire
  1286. *
  1287. * Parameters:
  1288. * (1) Node
  1289. * (2) User pointer
  1290. * (3) Local interface address
  1291. * (4) Remote address
  1292. * (5) Packet data
  1293. * (6) Packet length
  1294. * (7) Desired IP TTL or 0 to use default
  1295. *
  1296. * If there is only one local interface it is safe to ignore the local
  1297. * interface address. Otherwise if running with multiple interfaces, the
  1298. * correct local interface should be chosen by address unless NULL. If
  1299. * the ss_family field is zero (NULL address), a random or preferred
  1300. * default interface should be used.
  1301. *
  1302. * If TTL is nonzero, packets should have their IP TTL value set to this
  1303. * value if possible. If this is not possible it is acceptable to ignore
  1304. * this value and send anyway with normal or default TTL.
  1305. *
  1306. * The function must return zero on success and may return any error code
  1307. * on failure. Note that success does not (of course) guarantee packet
  1308. * delivery. It only means that the packet appears to have been sent.
  1309. */
  1310. typedef int (*ZT_WirePacketSendFunction)(
  1311. ZT_Node *, /* Node */
  1312. void *, /* User ptr */
  1313. const struct sockaddr_storage *, /* Local address */
  1314. const struct sockaddr_storage *, /* Remote address */
  1315. const void *, /* Packet data */
  1316. unsigned int, /* Packet length */
  1317. unsigned int); /* TTL or 0 to use default */
  1318. /**
  1319. * Function to check whether a path should be used for ZeroTier traffic
  1320. *
  1321. * Paramters:
  1322. * (1) Node
  1323. * (2) User pointer
  1324. * (3) ZeroTier address or 0 for none/any
  1325. * (4) Local interface address
  1326. * (5) Remote address
  1327. *
  1328. * This function must return nonzero (true) if the path should be used.
  1329. *
  1330. * If no path check function is specified, ZeroTier will still exclude paths
  1331. * that overlap with ZeroTier-assigned and managed IP address blocks. But the
  1332. * use of a path check function is recommended to ensure that recursion does
  1333. * not occur in cases where addresses are assigned by the OS or managed by
  1334. * an out of band mechanism like DHCP. The path check function should examine
  1335. * all configured ZeroTier interfaces and check to ensure that the supplied
  1336. * addresses will not result in ZeroTier traffic being sent over a ZeroTier
  1337. * interface (recursion).
  1338. *
  1339. * Obviously this is not required in configurations where this can't happen,
  1340. * such as network containers or embedded.
  1341. */
  1342. typedef int (*ZT_PathCheckFunction)(
  1343. ZT_Node *, /* Node */
  1344. void *, /* User ptr */
  1345. uint64_t, /* ZeroTier address */
  1346. const struct sockaddr_storage *, /* Local address */
  1347. const struct sockaddr_storage *); /* Remote address */
  1348. /**
  1349. * Function to get physical addresses for ZeroTier peers
  1350. *
  1351. * Parameters:
  1352. * (1) Node
  1353. * (2) User pointer
  1354. * (3) ZeroTier address (least significant 40 bits)
  1355. * (4) Desried address family or -1 for any
  1356. * (5) Buffer to fill with result
  1357. *
  1358. * If provided this function will be occasionally called to get physical
  1359. * addresses that might be tried to reach a ZeroTier address. It must
  1360. * return a nonzero (true) value if the result buffer has been filled
  1361. * with an address.
  1362. */
  1363. typedef int (*ZT_PathLookupFunction)(
  1364. ZT_Node *, /* Node */
  1365. void *, /* User ptr */
  1366. uint64_t, /* ZeroTier address (40 bits) */
  1367. int, /* Desired ss_family or -1 for any */
  1368. struct sockaddr_storage *); /* Result buffer */
  1369. /****************************************************************************/
  1370. /* C Node API */
  1371. /****************************************************************************/
  1372. /**
  1373. * Structure for configuring ZeroTier core callback functions
  1374. */
  1375. struct ZT_Node_Callbacks
  1376. {
  1377. /**
  1378. * Struct version -- must currently be 0
  1379. */
  1380. long version;
  1381. /**
  1382. * REQUIRED: Function to get objects from persistent storage
  1383. */
  1384. ZT_DataStoreGetFunction dataStoreGetFunction;
  1385. /**
  1386. * REQUIRED: Function to store objects in persistent storage
  1387. */
  1388. ZT_DataStorePutFunction dataStorePutFunction;
  1389. /**
  1390. * REQUIRED: Function to send packets over the physical wire
  1391. */
  1392. ZT_WirePacketSendFunction wirePacketSendFunction;
  1393. /**
  1394. * REQUIRED: Function to inject frames into a virtual network's TAP
  1395. */
  1396. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction;
  1397. /**
  1398. * REQUIRED: Function to be called when virtual networks are configured or changed
  1399. */
  1400. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction;
  1401. /**
  1402. * REQUIRED: Function to be called to notify external code of important events
  1403. */
  1404. ZT_EventCallback eventCallback;
  1405. /**
  1406. * OPTIONAL: Function to check whether a given physical path should be used
  1407. */
  1408. ZT_PathCheckFunction pathCheckFunction;
  1409. /**
  1410. * OPTIONAL: Function to get hints to physical paths to ZeroTier addresses
  1411. */
  1412. ZT_PathLookupFunction pathLookupFunction;
  1413. };
  1414. /**
  1415. * Create a new ZeroTier One node
  1416. *
  1417. * Note that this can take a few seconds the first time it's called, as it
  1418. * will generate an identity.
  1419. *
  1420. * TODO: should consolidate function pointers into versioned structure for
  1421. * better API stability.
  1422. *
  1423. * @param node Result: pointer is set to new node instance on success
  1424. * @param uptr User pointer to pass to functions/callbacks
  1425. * @param callbacks Callback function configuration
  1426. * @param now Current clock in milliseconds
  1427. * @return OK (0) or error code if a fatal error condition has occurred
  1428. */
  1429. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now);
  1430. /**
  1431. * Delete a node and free all resources it consumes
  1432. *
  1433. * If you are using multiple threads, all other threads must be shut down
  1434. * first. This can crash if processXXX() methods are in progress.
  1435. *
  1436. * @param node Node to delete
  1437. */
  1438. void ZT_Node_delete(ZT_Node *node);
  1439. /**
  1440. * Process a packet received from the physical wire
  1441. *
  1442. * @param node Node instance
  1443. * @param now Current clock in milliseconds
  1444. * @param localAddress Local address, or point to ZT_SOCKADDR_NULL if unspecified
  1445. * @param remoteAddress Origin of packet
  1446. * @param packetData Packet data
  1447. * @param packetLength Packet length
  1448. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1449. * @return OK (0) or error code if a fatal error condition has occurred
  1450. */
  1451. enum ZT_ResultCode ZT_Node_processWirePacket(
  1452. ZT_Node *node,
  1453. uint64_t now,
  1454. const struct sockaddr_storage *localAddress,
  1455. const struct sockaddr_storage *remoteAddress,
  1456. const void *packetData,
  1457. unsigned int packetLength,
  1458. volatile uint64_t *nextBackgroundTaskDeadline);
  1459. /**
  1460. * Process a frame from a virtual network port (tap)
  1461. *
  1462. * @param node Node instance
  1463. * @param now Current clock in milliseconds
  1464. * @param nwid ZeroTier 64-bit virtual network ID
  1465. * @param sourceMac Source MAC address (least significant 48 bits)
  1466. * @param destMac Destination MAC address (least significant 48 bits)
  1467. * @param etherType 16-bit Ethernet frame type
  1468. * @param vlanId 10-bit VLAN ID or 0 if none
  1469. * @param frameData Frame payload data
  1470. * @param frameLength Frame payload length
  1471. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1472. * @return OK (0) or error code if a fatal error condition has occurred
  1473. */
  1474. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  1475. ZT_Node *node,
  1476. uint64_t now,
  1477. uint64_t nwid,
  1478. uint64_t sourceMac,
  1479. uint64_t destMac,
  1480. unsigned int etherType,
  1481. unsigned int vlanId,
  1482. const void *frameData,
  1483. unsigned int frameLength,
  1484. volatile uint64_t *nextBackgroundTaskDeadline);
  1485. /**
  1486. * Perform periodic background operations
  1487. *
  1488. * @param node Node instance
  1489. * @param now Current clock in milliseconds
  1490. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1491. * @return OK (0) or error code if a fatal error condition has occurred
  1492. */
  1493. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  1494. /**
  1495. * Set node's relay policy
  1496. *
  1497. * @param node Node instance
  1498. * @param rp New relay policy
  1499. * @return OK(0) or error code
  1500. */
  1501. enum ZT_ResultCode ZT_Node_setRelayPolicy(ZT_Node *node,enum ZT_RelayPolicy rp);
  1502. /**
  1503. * Join a network
  1504. *
  1505. * This may generate calls to the port config callback before it returns,
  1506. * or these may be deffered if a netconf is not available yet.
  1507. *
  1508. * If we are already a member of the network, nothing is done and OK is
  1509. * returned.
  1510. *
  1511. * @param node Node instance
  1512. * @param nwid 64-bit ZeroTier network ID
  1513. * @param uptr An arbitrary pointer to associate with this network (default: NULL)
  1514. * @return OK (0) or error code if a fatal error condition has occurred
  1515. */
  1516. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr);
  1517. /**
  1518. * Leave a network
  1519. *
  1520. * If a port has been configured for this network this will generate a call
  1521. * to the port config callback with a NULL second parameter to indicate that
  1522. * the port is now deleted.
  1523. *
  1524. * The uptr parameter is optional and is NULL by default. If it is not NULL,
  1525. * the pointer it points to is set to this network's uptr on success.
  1526. *
  1527. * @param node Node instance
  1528. * @param nwid 64-bit network ID
  1529. * @param uptr Target pointer is set to uptr (if not NULL)
  1530. * @return OK (0) or error code if a fatal error condition has occurred
  1531. */
  1532. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr);
  1533. /**
  1534. * Subscribe to an Ethernet multicast group
  1535. *
  1536. * ADI stands for additional distinguishing information. This defaults to zero
  1537. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1538. * and this must be done.
  1539. *
  1540. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1541. * broadcast address) but with an ADI equal to each IPv4 address in host
  1542. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1543. * a scalable multicast protocol with perfect address specificity.
  1544. *
  1545. * If this is not done, ARP will not work reliably.
  1546. *
  1547. * Multiple calls to subscribe to the same multicast address will have no
  1548. * effect. It is perfectly safe to do this.
  1549. *
  1550. * This does not generate an update call to networkConfigCallback().
  1551. *
  1552. * @param node Node instance
  1553. * @param nwid 64-bit network ID
  1554. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1555. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1556. * @return OK (0) or error code if a fatal error condition has occurred
  1557. */
  1558. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1559. /**
  1560. * Unsubscribe from an Ethernet multicast group (or all groups)
  1561. *
  1562. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1563. * you are not subscribed to a group this has no effect.
  1564. *
  1565. * This does not generate an update call to networkConfigCallback().
  1566. *
  1567. * @param node Node instance
  1568. * @param nwid 64-bit network ID
  1569. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1570. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1571. * @return OK (0) or error code if a fatal error condition has occurred
  1572. */
  1573. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1574. /**
  1575. * Get this node's 40-bit ZeroTier address
  1576. *
  1577. * @param node Node instance
  1578. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1579. */
  1580. uint64_t ZT_Node_address(ZT_Node *node);
  1581. /**
  1582. * Get the status of this node
  1583. *
  1584. * @param node Node instance
  1585. * @param status Buffer to fill with current node status
  1586. */
  1587. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status);
  1588. /**
  1589. * Get a list of known peer nodes
  1590. *
  1591. * The pointer returned here must be freed with freeQueryResult()
  1592. * when you are done with it.
  1593. *
  1594. * @param node Node instance
  1595. * @return List of known peers or NULL on failure
  1596. */
  1597. ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1598. /**
  1599. * Get the status of a virtual network
  1600. *
  1601. * The pointer returned here must be freed with freeQueryResult()
  1602. * when you are done with it.
  1603. *
  1604. * @param node Node instance
  1605. * @param nwid 64-bit network ID
  1606. * @return Network configuration or NULL if we are not a member of this network
  1607. */
  1608. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid);
  1609. /**
  1610. * Enumerate and get status of all networks
  1611. *
  1612. * @param node Node instance
  1613. * @return List of networks or NULL on failure
  1614. */
  1615. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1616. /**
  1617. * Free a query result buffer
  1618. *
  1619. * Use this to free the return values of listNetworks(), listPeers(), etc.
  1620. *
  1621. * @param node Node instance
  1622. * @param qr Query result buffer
  1623. */
  1624. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr);
  1625. /**
  1626. * Add a local interface address
  1627. *
  1628. * This is used to make ZeroTier aware of those local interface addresses
  1629. * that you wish to use for ZeroTier communication. This is optional, and if
  1630. * it is not used ZeroTier will rely upon upstream peers (and roots) to
  1631. * perform empirical address discovery and NAT traversal. But the use of this
  1632. * method is recommended as it improves peer discovery when both peers are
  1633. * on the same LAN.
  1634. *
  1635. * It is the responsibility of the caller to take care that these are never
  1636. * ZeroTier interface addresses, whether these are assigned by ZeroTier or
  1637. * are otherwise assigned to an interface managed by this ZeroTier instance.
  1638. * This can cause recursion or other undesirable behavior.
  1639. *
  1640. * This returns a boolean indicating whether or not the address was
  1641. * accepted. ZeroTier will only communicate over certain address types
  1642. * and (for IP) address classes.
  1643. *
  1644. * @param addr Local interface address
  1645. * @return Boolean: non-zero if address was accepted and added
  1646. */
  1647. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr);
  1648. /**
  1649. * Clear local interface addresses
  1650. */
  1651. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node);
  1652. /**
  1653. * Send a VERB_USER_MESSAGE to another ZeroTier node
  1654. *
  1655. * There is no delivery guarantee here. Failure can occur if the message is
  1656. * too large or if dest is not a valid ZeroTier address.
  1657. *
  1658. * @param dest Destination ZeroTier address
  1659. * @param typeId VERB_USER_MESSAGE type ID
  1660. * @param data Payload data to attach to user message
  1661. * @param len Length of data in bytes
  1662. * @return Boolean: non-zero on success, zero on failure
  1663. */
  1664. int ZT_Node_sendUserMessage(ZT_Node *node,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
  1665. /**
  1666. * Set peer role
  1667. *
  1668. * Right now this can only be used to set a peer to either LEAF or
  1669. * UPSTREAM, since roots are fixed and defined by the World.
  1670. *
  1671. * @param ztAddress ZeroTier address (least significant 40 bits)
  1672. * @param role New peer role (LEAF or UPSTREAM)
  1673. */
  1674. void ZT_Node_setRole(ZT_Node *node,uint64_t ztAddress,enum ZT_PeerRole role);
  1675. /**
  1676. * Set a network configuration master instance for this node
  1677. *
  1678. * Normal nodes should not need to use this. This is for nodes with
  1679. * special compiled-in support for acting as network configuration
  1680. * masters / controllers.
  1681. *
  1682. * The supplied instance must be a C++ object that inherits from the
  1683. * NetworkConfigMaster base class in node/. No type checking is performed,
  1684. * so a pointer to anything else will result in a crash.
  1685. *
  1686. * @param node ZertTier One node
  1687. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  1688. * @return OK (0) or error code if a fatal error condition has occurred
  1689. */
  1690. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkConfigMasterInstance);
  1691. /**
  1692. * Initiate a VL1 circuit test
  1693. *
  1694. * This sends an initial VERB_CIRCUIT_TEST and reports results back to the
  1695. * supplied callback until circuitTestEnd() is called. The supplied
  1696. * ZT_CircuitTest structure should be initially zeroed and then filled
  1697. * in with settings and hops.
  1698. *
  1699. * It is the caller's responsibility to call circuitTestEnd() and then
  1700. * to dispose of the test structure. Otherwise this node will listen
  1701. * for results forever.
  1702. *
  1703. * @param node Node instance
  1704. * @param test Test configuration
  1705. * @param reportCallback Function to call each time a report is received
  1706. * @return OK or error if, for example, test is too big for a packet or support isn't compiled in
  1707. */
  1708. enum ZT_ResultCode ZT_Node_circuitTestBegin(ZT_Node *node,ZT_CircuitTest *test,void (*reportCallback)(ZT_Node *, ZT_CircuitTest *,const ZT_CircuitTestReport *));
  1709. /**
  1710. * Stop listening for results to a given circuit test
  1711. *
  1712. * This does not free the 'test' structure. The caller may do that
  1713. * after calling this method to unregister it.
  1714. *
  1715. * Any reports that are received for a given test ID after it is
  1716. * terminated are ignored.
  1717. *
  1718. * @param node Node instance
  1719. * @param test Test configuration to unregister
  1720. */
  1721. void ZT_Node_circuitTestEnd(ZT_Node *node,ZT_CircuitTest *test);
  1722. /**
  1723. * Initialize cluster operation
  1724. *
  1725. * This initializes the internal structures and state for cluster operation.
  1726. * It takes two function pointers. The first is to a function that can be
  1727. * used to send data to cluster peers (mechanism is not defined by Node),
  1728. * and the second is to a function that can be used to get the location of
  1729. * a physical address in X,Y,Z coordinate space (e.g. as cartesian coordinates
  1730. * projected from the center of the Earth).
  1731. *
  1732. * Send function takes an arbitrary pointer followed by the cluster member ID
  1733. * to send data to, a pointer to the data, and the length of the data. The
  1734. * maximum message length is ZT_CLUSTER_MAX_MESSAGE_LENGTH (65535). Messages
  1735. * must be delivered whole and may be dropped or transposed, though high
  1736. * failure rates are undesirable and can cause problems. Validity checking or
  1737. * CRC is also not required since the Node validates the authenticity of
  1738. * cluster messages using cryptogrphic methods and will silently drop invalid
  1739. * messages.
  1740. *
  1741. * Address to location function is optional and if NULL geo-handoff is not
  1742. * enabled (in this case x, y, and z in clusterInit are also unused). It
  1743. * takes an arbitrary pointer followed by a physical address and three result
  1744. * parameters for x, y, and z. It returns zero on failure or nonzero if these
  1745. * three coordinates have been set. Coordinate space is arbitrary and can be
  1746. * e.g. coordinates on Earth relative to Earth's center. These can be obtained
  1747. * from latitutde and longitude with versions of the Haversine formula.
  1748. *
  1749. * See: http://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates
  1750. *
  1751. * Neither the send nor the address to location function should block. If the
  1752. * address to location function does not have a location for an address, it
  1753. * should return zero and then look up the address for future use since it
  1754. * will be called again in (typically) 1-3 minutes.
  1755. *
  1756. * Note that both functions can be called from any thread from which the
  1757. * various Node functions are called, and so must be thread safe if multiple
  1758. * threads are being used.
  1759. *
  1760. * @param node Node instance
  1761. * @param myId My cluster member ID (less than or equal to ZT_CLUSTER_MAX_MEMBERS)
  1762. * @param zeroTierPhysicalEndpoints Preferred physical address(es) for ZeroTier clients to contact this cluster member (for peer redirect)
  1763. * @param numZeroTierPhysicalEndpoints Number of physical endpoints in zeroTierPhysicalEndpoints[] (max allowed: 255)
  1764. * @param x My cluster member's X location
  1765. * @param y My cluster member's Y location
  1766. * @param z My cluster member's Z location
  1767. * @param sendFunction Function to be called to send data to other cluster members
  1768. * @param sendFunctionArg First argument to sendFunction()
  1769. * @param addressToLocationFunction Function to be called to get the location of a physical address or NULL to disable geo-handoff
  1770. * @param addressToLocationFunctionArg First argument to addressToLocationFunction()
  1771. * @return OK or UNSUPPORTED_OPERATION if this Node was not built with cluster support
  1772. */
  1773. enum ZT_ResultCode ZT_Node_clusterInit(
  1774. ZT_Node *node,
  1775. unsigned int myId,
  1776. const struct sockaddr_storage *zeroTierPhysicalEndpoints,
  1777. unsigned int numZeroTierPhysicalEndpoints,
  1778. int x,
  1779. int y,
  1780. int z,
  1781. void (*sendFunction)(void *,unsigned int,const void *,unsigned int),
  1782. void *sendFunctionArg,
  1783. int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *),
  1784. void *addressToLocationFunctionArg);
  1785. /**
  1786. * Add a member to this cluster
  1787. *
  1788. * Calling this without having called clusterInit() will do nothing.
  1789. *
  1790. * @param node Node instance
  1791. * @param memberId Member ID (must be less than or equal to ZT_CLUSTER_MAX_MEMBERS)
  1792. * @return OK or error if clustering is disabled, ID invalid, etc.
  1793. */
  1794. enum ZT_ResultCode ZT_Node_clusterAddMember(ZT_Node *node,unsigned int memberId);
  1795. /**
  1796. * Remove a member from this cluster
  1797. *
  1798. * Calling this without having called clusterInit() will do nothing.
  1799. *
  1800. * @param node Node instance
  1801. * @param memberId Member ID to remove (nothing happens if not present)
  1802. */
  1803. void ZT_Node_clusterRemoveMember(ZT_Node *node,unsigned int memberId);
  1804. /**
  1805. * Handle an incoming cluster state message
  1806. *
  1807. * The message itself contains cluster member IDs, and invalid or badly
  1808. * addressed messages will be silently discarded.
  1809. *
  1810. * Calling this without having called clusterInit() will do nothing.
  1811. *
  1812. * @param node Node instance
  1813. * @param msg Cluster message
  1814. * @param len Length of cluster message
  1815. */
  1816. void ZT_Node_clusterHandleIncomingMessage(ZT_Node *node,const void *msg,unsigned int len);
  1817. /**
  1818. * Get the current status of the cluster from this node's point of view
  1819. *
  1820. * Calling this without clusterInit() or without cluster support will just
  1821. * zero out the structure and show a cluster size of zero.
  1822. *
  1823. * @param node Node instance
  1824. * @param cs Cluster status structure to fill with data
  1825. */
  1826. void ZT_Node_clusterStatus(ZT_Node *node,ZT_ClusterStatus *cs);
  1827. /**
  1828. * Set trusted paths
  1829. *
  1830. * A trusted path is a physical network (network/bits) over which both
  1831. * encryption and authentication can be skipped to improve performance.
  1832. * Each trusted path must have a non-zero unique ID that is the same across
  1833. * all participating nodes.
  1834. *
  1835. * We don't recommend using trusted paths at all unless you really *need*
  1836. * near-bare-metal performance. Even on a LAN authentication and encryption
  1837. * are never a bad thing, and anything that introduces an "escape hatch"
  1838. * for encryption should be treated with the utmost care.
  1839. *
  1840. * Calling with NULL pointers for networks and ids and a count of zero clears
  1841. * all trusted paths.
  1842. *
  1843. * @param node Node instance
  1844. * @param networks Array of [count] networks
  1845. * @param ids Array of [count] corresponding non-zero path IDs (zero path IDs are ignored)
  1846. * @param count Number of trusted paths-- values greater than ZT_MAX_TRUSTED_PATHS are clipped
  1847. */
  1848. void ZT_Node_setTrustedPaths(ZT_Node *node,const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count);
  1849. /**
  1850. * Get ZeroTier One version
  1851. *
  1852. * @param major Result: major version
  1853. * @param minor Result: minor version
  1854. * @param revision Result: revision
  1855. */
  1856. void ZT_version(int *major,int *minor,int *revision);
  1857. #ifdef __cplusplus
  1858. }
  1859. #endif
  1860. #endif