ZeroTierCore.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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 ZeroTier's core network virtualization
  15. * engine.
  16. */
  17. #ifndef ZT_ZEROTIER_API_H
  18. #define ZT_ZEROTIER_API_H
  19. #ifdef __cplusplus
  20. #include <cstdint>
  21. extern "C" {
  22. #else
  23. #include <stdint.h>
  24. #endif
  25. /* For struct sockaddr_storage, which is referenced here. */
  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. /* This symbol may be defined in a build environment or before including this
  37. * header if you need to prepend something to function specifications. */
  38. #ifndef ZT_SDK_API
  39. #define ZT_SDK_API
  40. #endif
  41. /****************************************************************************/
  42. /* Core constants */
  43. /****************************************************************************/
  44. /**
  45. * Default UDP port for devices running a ZeroTier endpoint
  46. *
  47. * NOTE: as of V2 this has changed to 893 since many NATs (even symmetric)
  48. * treat privileged ports in a special way. The old default was 9993.
  49. */
  50. #define ZT_DEFAULT_PORT 893
  51. /**
  52. * Minimum MTU, which is the minimum allowed by IPv6 and several specs
  53. */
  54. #define ZT_MIN_MTU 1280
  55. /**
  56. * Maximum MTU for ZeroTier virtual networks
  57. */
  58. #define ZT_MAX_MTU 10000
  59. /**
  60. * Minimum UDP payload size allowed
  61. */
  62. #define ZT_MIN_PHYSMTU 1400
  63. /**
  64. * Default UDP payload size (physical path MTU) not including UDP and IP overhead
  65. *
  66. * This is small enough for PPPoE and for Google Cloud's bizarrely tiny MTUs.
  67. * A 2800 byte payload still fits into two packets, so this should not impact
  68. * real world throughput at all vs the previous default of 1444.
  69. */
  70. #define ZT_DEFAULT_PHYSMTU 1432
  71. /**
  72. * Maximum physical UDP payload
  73. */
  74. #define ZT_MAX_PHYSPAYLOAD 10100
  75. /**
  76. * Headroom for max physical MTU
  77. */
  78. #define ZT_MAX_HEADROOM 224
  79. /**
  80. * Maximum payload MTU for UDP packets
  81. */
  82. #define ZT_MAX_PHYSMTU (ZT_MAX_PHYSPAYLOAD + ZT_MAX_HEADROOM)
  83. /**
  84. * Maximum length of network short name
  85. */
  86. #define ZT_MAX_NETWORK_SHORT_NAME_LENGTH 127
  87. /**
  88. * Maximum number of pushed routes on a network (via ZT in-band mechanisms)
  89. */
  90. #define ZT_MAX_NETWORK_ROUTES 64
  91. /**
  92. * Maximum number of statically assigned IP addresses (via ZT in-band mechanisms)
  93. */
  94. #define ZT_MAX_ZT_ASSIGNED_ADDRESSES 32
  95. /**
  96. * Maximum number of "specialists" on a network -- bridges, etc.
  97. *
  98. * A specialist is a node tagged with some special role like acting as
  99. * a promiscuous bridge, open relay, administrator, etc.
  100. */
  101. #define ZT_MAX_NETWORK_SPECIALISTS 256
  102. /**
  103. * Rules engine revision ID, which specifies rules engine capabilities
  104. */
  105. #define ZT_RULES_ENGINE_REVISION 1
  106. /**
  107. * Maximum number of base (non-capability) network rules
  108. */
  109. #define ZT_MAX_NETWORK_RULES 1024
  110. /**
  111. * Maximum number of capabilities per network per member
  112. */
  113. #define ZT_MAX_NETWORK_CAPABILITIES 128
  114. /**
  115. * Maximum number of tags per network per member
  116. */
  117. #define ZT_MAX_NETWORK_TAGS 128
  118. /**
  119. * Maximum number of direct network paths to a given peer
  120. *
  121. * Note that dual-stack configs may end up resulting in both IPv6 and IPv4
  122. * paths existing. This gives enough headroom for multipath configs with
  123. * dual stacks across the board.
  124. */
  125. #define ZT_MAX_PEER_NETWORK_PATHS 16
  126. /**
  127. * Maximum number of path configurations that can be set
  128. */
  129. #define ZT_MAX_CONFIGURABLE_PATHS 32
  130. /**
  131. * Maximum number of rules per capability object
  132. *
  133. * Capabilities normally contain only a few rules. The rules in a capability
  134. * should be short and to the point.
  135. */
  136. #define ZT_MAX_CAPABILITY_RULES 64
  137. /**
  138. * Maximum number of certificates of ownership to assign to a single network member
  139. *
  140. * Network members can have more than four IPs, etc., but right now there
  141. * is a protocol limit on how many COOs can be assigned. If your config needs
  142. * more than four authenticated IPs per node you may have personal problems.
  143. */
  144. #define ZT_MAX_CERTIFICATES_OF_OWNERSHIP 4
  145. /**
  146. * Global maximum length for capability chain of custody (including initial issue)
  147. */
  148. #define ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH 7
  149. /**
  150. * Maximum number of multicast group subscriptions on a local virtual network interface
  151. *
  152. * This coincides with many operating systems' maximum values and is rather huge.
  153. */
  154. #define ZT_MAX_MULTICAST_SUBSCRIPTIONS 1024
  155. /* Rule specification contants **********************************************/
  156. /**
  157. * Packet characteristics flag: packet direction, 1 if inbound 0 if outbound
  158. */
  159. #define ZT_RULE_PACKET_CHARACTERISTICS_INBOUND 0x8000000000000000ULL
  160. /**
  161. * Packet characteristics flag: multicast or broadcast destination MAC
  162. */
  163. #define ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST 0x4000000000000000ULL
  164. /**
  165. * Packet characteristics flag: broadcast destination MAC
  166. */
  167. #define ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST 0x2000000000000000ULL
  168. /**
  169. * Packet characteristics flag: sending IP address has a certificate of ownership
  170. */
  171. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED 0x1000000000000000ULL
  172. /**
  173. * Packet characteristics flag: sending MAC address has a certificate of ownership
  174. */
  175. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED 0x0800000000000000ULL
  176. /**
  177. * Packet characteristics flag: TCP left-most reserved bit
  178. */
  179. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_0 0x0000000000000800ULL
  180. /**
  181. * Packet characteristics flag: TCP middle reserved bit
  182. */
  183. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_1 0x0000000000000400ULL
  184. /**
  185. * Packet characteristics flag: TCP right-most reserved bit
  186. */
  187. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_2 0x0000000000000200ULL
  188. /**
  189. * Packet characteristics flag: TCP NS flag
  190. */
  191. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS 0x0000000000000100ULL
  192. /**
  193. * Packet characteristics flag: TCP CWR flag
  194. */
  195. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR 0x0000000000000080ULL
  196. /**
  197. * Packet characteristics flag: TCP ECE flag
  198. */
  199. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE 0x0000000000000040ULL
  200. /**
  201. * Packet characteristics flag: TCP URG flag
  202. */
  203. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG 0x0000000000000020ULL
  204. /**
  205. * Packet characteristics flag: TCP ACK flag
  206. */
  207. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK 0x0000000000000010ULL
  208. /**
  209. * Packet characteristics flag: TCP PSH flag
  210. */
  211. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH 0x0000000000000008ULL
  212. /**
  213. * Packet characteristics flag: TCP RST flag
  214. */
  215. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST 0x0000000000000004ULL
  216. /**
  217. * Packet characteristics flag: TCP SYN flag
  218. */
  219. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN 0x0000000000000002ULL
  220. /**
  221. * Packet characteristics flag: TCP FIN flag
  222. */
  223. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN 0x0000000000000001ULL
  224. /****************************************************************************/
  225. /* Structures and other types */
  226. /****************************************************************************/
  227. /**
  228. * Function return code: OK (0) or error results
  229. *
  230. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  231. * occurs, the node should be considered to not be working correctly. These
  232. * indicate serious problems like an inaccessible data store or a compile
  233. * problem.
  234. */
  235. enum ZT_ResultCode
  236. {
  237. /**
  238. * Operation completed normally
  239. */
  240. ZT_RESULT_OK = 0,
  241. /**
  242. * Call produced no error but no action was taken
  243. */
  244. ZT_RESULT_OK_IGNORED = 1,
  245. // Fatal errors (>100, <1000)
  246. /**
  247. * Ran out of memory
  248. */
  249. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 100,
  250. /**
  251. * Data store is not writable or has failed
  252. */
  253. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 101,
  254. /**
  255. * Internal error (e.g. unexpected exception indicating bug or build problem)
  256. */
  257. ZT_RESULT_FATAL_ERROR_INTERNAL = 102,
  258. // Non-fatal errors (>1000)
  259. /**
  260. * Network ID not valid
  261. */
  262. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000,
  263. /**
  264. * The requested operation is not supported on this version or build
  265. */
  266. ZT_RESULT_ERROR_UNSUPPORTED_OPERATION = 1001,
  267. /**
  268. * The requested operation was given a bad parameter or was called in an invalid state
  269. */
  270. ZT_RESULT_ERROR_BAD_PARAMETER = 1002
  271. };
  272. /**
  273. * Macro to check for a fatal error result code
  274. *
  275. * @param x Result code
  276. * @return True if result code indicates a fatal error
  277. */
  278. #define ZT_ResultCode_isFatal(x) ((((int)(x)) >= 100)&&(((int)(x)) < 1000))
  279. /**
  280. * Status codes sent to status update callback when things happen
  281. */
  282. enum ZT_Event
  283. {
  284. /**
  285. * Node has been initialized
  286. *
  287. * This is the first event generated, and is always sent. It may occur
  288. * before Node's constructor returns.
  289. *
  290. * Meta-data: none
  291. */
  292. ZT_EVENT_UP = 0,
  293. /**
  294. * Node appears offline
  295. *
  296. * This indicates that the node doesn't seem to be able to reach anything,
  297. * or hasn't for a while. It's not a hard instantaneous thing.
  298. *
  299. * Meta-data: none
  300. */
  301. ZT_EVENT_OFFLINE = 1,
  302. /**
  303. * Node appears online
  304. *
  305. * This indicates that the node was offline but now seems to be able to
  306. * reach something. Like OFFLINE it's not a hard instantaneous thing but
  307. * more of an indicator for UI reporting purposes.
  308. *
  309. * Meta-data: none
  310. */
  311. ZT_EVENT_ONLINE = 2,
  312. /**
  313. * Node is shutting down
  314. *
  315. * This is generated within Node's destructor when it is being shut down.
  316. * It's done for convenience in case you want to clean up anything during
  317. * node shutdown in your node event handler.
  318. *
  319. * Meta-data: none
  320. */
  321. ZT_EVENT_DOWN = 3,
  322. // 4 once signaled identity collision but this is no longer an error
  323. /**
  324. * Trace (debugging) message
  325. *
  326. * These events are only generated if this is a TRACE-enabled build.
  327. * This is for local debug traces, not remote trace diagnostics.
  328. *
  329. * Meta-data: C string, TRACE message
  330. */
  331. ZT_EVENT_TRACE = 5,
  332. /**
  333. * VERB_USER_MESSAGE received
  334. *
  335. * These are generated when a VERB_USER_MESSAGE packet is received via
  336. * ZeroTier VL1. This can be used for below-VL2 in-band application
  337. * specific signaling over the ZeroTier protocol.
  338. *
  339. * It's up to you to ensure that you handle these in a way that does
  340. * not introduce a remote security vulnerability into your app! If
  341. * your USER_MESSAGE code has a buffer overflow or other vulnerability
  342. * then your app will be vulnerable and this is not ZT's fault. :)
  343. *
  344. * Meta-data: ZT_UserMessage structure
  345. */
  346. ZT_EVENT_USER_MESSAGE = 6
  347. };
  348. /**
  349. * Identity type codes
  350. */
  351. enum ZT_Identity_Type
  352. {
  353. /* These values must be the same as in Identity.hpp in the core. */
  354. ZT_IDENTITY_TYPE_C25519 = 0,
  355. ZT_IDENTITY_TYPE_P384 = 1
  356. };
  357. /**
  358. * A ZeroTier identity (opaque)
  359. */
  360. typedef void ZT_Identity;
  361. /**
  362. * User message used with ZT_EVENT_USER_MESSAGE
  363. *
  364. * These are direct VL1 P2P messages for application use. Encryption and
  365. * authentication in the ZeroTier protocol will guarantee the origin
  366. * address and message content, but you are responsible for any other
  367. * levels of authentication or access control that are required. Any node
  368. * in the world can send you a user message! (Unless your network is air
  369. * gapped.)
  370. */
  371. typedef struct
  372. {
  373. /**
  374. * ZeroTier address of sender (least significant 40 bits)
  375. */
  376. uint64_t origin;
  377. /**
  378. * User message type ID
  379. */
  380. uint64_t typeId;
  381. /**
  382. * User message data (not including type ID)
  383. */
  384. const void *data;
  385. /**
  386. * Length of data in bytes
  387. */
  388. unsigned int length;
  389. } ZT_UserMessage;
  390. /**
  391. * Current node status
  392. */
  393. typedef struct
  394. {
  395. /**
  396. * 40-bit ZeroTier address of this node
  397. */
  398. uint64_t address;
  399. /**
  400. * Actual identity object for this node
  401. */
  402. const ZT_Identity *identity;
  403. /**
  404. * Public identity in string-serialized form (safe to send to others)
  405. *
  406. * This pointer will remain valid as long as the node exists.
  407. */
  408. const char *publicIdentity;
  409. /**
  410. * Full identity including secret key in string-serialized form
  411. *
  412. * This pointer will remain valid as long as the node exists.
  413. */
  414. const char *secretIdentity;
  415. /**
  416. * True if some kind of connectivity appears available
  417. */
  418. int online;
  419. } ZT_NodeStatus;
  420. /**
  421. * Virtual network status codes
  422. */
  423. enum ZT_VirtualNetworkStatus
  424. {
  425. /**
  426. * Waiting for network configuration (also means revision == 0)
  427. */
  428. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  429. /**
  430. * Configuration received and we are authorized
  431. */
  432. ZT_NETWORK_STATUS_OK = 1,
  433. /**
  434. * Netconf master told us 'nope'
  435. */
  436. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  437. /**
  438. * Netconf master exists, but this virtual network does not
  439. */
  440. ZT_NETWORK_STATUS_NOT_FOUND = 3
  441. };
  442. /**
  443. * Virtual network type codes
  444. */
  445. enum ZT_VirtualNetworkType
  446. {
  447. /**
  448. * Private networks are authorized via certificates of membership
  449. */
  450. ZT_NETWORK_TYPE_PRIVATE = 0,
  451. /**
  452. * Public networks have no access control -- they'll always be AUTHORIZED
  453. */
  454. ZT_NETWORK_TYPE_PUBLIC = 1
  455. };
  456. /**
  457. * The type of a virtual network rules table entry
  458. *
  459. * These must be from 0 to 63 since the most significant two bits of each
  460. * rule type are NOT (MSB) and AND/OR.
  461. *
  462. * Each rule is composed of zero or more MATCHes followed by an ACTION.
  463. * An ACTION with no MATCHes is always taken.
  464. */
  465. enum ZT_VirtualNetworkRuleType
  466. {
  467. // 0 to 15 reserved for actions
  468. /**
  469. * Drop frame
  470. */
  471. ZT_NETWORK_RULE_ACTION_DROP = 0,
  472. /**
  473. * Accept and pass frame
  474. */
  475. ZT_NETWORK_RULE_ACTION_ACCEPT = 1,
  476. /**
  477. * Forward a copy of this frame to an observer (by ZT address)
  478. */
  479. ZT_NETWORK_RULE_ACTION_TEE = 2,
  480. /**
  481. * Exactly like TEE but mandates ACKs from observer
  482. */
  483. ZT_NETWORK_RULE_ACTION_WATCH = 3,
  484. /**
  485. * Drop and redirect this frame to another node (by ZT address)
  486. */
  487. ZT_NETWORK_RULE_ACTION_REDIRECT = 4,
  488. /**
  489. * Stop evaluating rule set (drops unless there are capabilities, etc.)
  490. */
  491. ZT_NETWORK_RULE_ACTION_BREAK = 5,
  492. /**
  493. * Place a matching frame in the specified QoS bucket
  494. */
  495. ZT_NETWORK_RULE_ACTION_PRIORITY = 6,
  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. ZT_NETWORK_RULE_MATCH_TAG_SENDER = 49,
  527. ZT_NETWORK_RULE_MATCH_TAG_RECEIVER = 50,
  528. ZT_NETWORK_RULE_MATCH_INTEGER_RANGE = 51,
  529. /**
  530. * Maximum ID allowed for a MATCH entry in the rules table
  531. */
  532. ZT_NETWORK_RULE_MATCH__MAX_ID = 63
  533. };
  534. /**
  535. * Network flow rule
  536. *
  537. * Rules are stored in a table in which one or more match entries is followed
  538. * by an action. If more than one match precedes an action, the rule is
  539. * the AND of all matches. An action with no match is always taken since it
  540. * matches anything. If nothing matches, the default action is DROP.
  541. *
  542. * This is designed to be a more memory-efficient way of storing rules than
  543. * a wide table, yet still fast and simple to access in code.
  544. */
  545. typedef struct
  546. {
  547. /**
  548. * Type and flags
  549. *
  550. * Bits are: NOTTTTTT
  551. *
  552. * N - If true, sense of match is inverted (no effect on actions)
  553. * O - If true, result is ORed with previous instead of ANDed (no effect on actions)
  554. * T - Rule or action type
  555. *
  556. * AND with 0x3f to get type, 0x80 to get NOT bit, and 0x40 to get OR bit.
  557. */
  558. uint8_t t;
  559. /**
  560. * Union containing the value of this rule -- which field is used depends on 't'
  561. */
  562. union {
  563. /**
  564. * IPv6 address in big-endian / network byte order and netmask bits
  565. */
  566. struct {
  567. uint8_t ip[16];
  568. uint8_t mask;
  569. } ipv6;
  570. /**
  571. * IPv4 address in big-endian / network byte order
  572. */
  573. struct {
  574. uint32_t ip;
  575. uint8_t mask;
  576. } ipv4;
  577. /**
  578. * Integer range match in packet payload
  579. *
  580. * This allows matching of ranges of integers up to 64 bits wide where
  581. * the range is +/- INT32_MAX. It's packed this way so it fits in 16
  582. * bytes and doesn't enlarge the overall size of this union.
  583. */
  584. struct {
  585. uint64_t start; // integer range start
  586. uint32_t end; // end of integer range (relative to start, inclusive, 0 for equality w/start)
  587. uint16_t idx; // index in packet of integer
  588. uint8_t format; // bits in integer (range 1-64, ((format&63)+1)) and endianness (MSB 1 for little, 0 for big)
  589. } intRange;
  590. /**
  591. * Packet characteristic flags being matched
  592. */
  593. uint64_t characteristics;
  594. /**
  595. * IP port range -- start-end inclusive -- host byte order
  596. */
  597. uint16_t port[2];
  598. /**
  599. * 40-bit ZeroTier address (in least significant bits, host byte order)
  600. */
  601. uint64_t zt;
  602. /**
  603. * 0 = never, UINT32_MAX = always
  604. */
  605. uint32_t randomProbability;
  606. /**
  607. * 48-bit Ethernet MAC address in big-endian order
  608. */
  609. uint8_t mac[6];
  610. /**
  611. * VLAN ID in host byte order
  612. */
  613. uint16_t vlanId;
  614. /**
  615. * VLAN PCP (least significant 3 bits)
  616. */
  617. uint8_t vlanPcp;
  618. /**
  619. * VLAN DEI (single bit / boolean)
  620. */
  621. uint8_t vlanDei;
  622. /**
  623. * Ethernet type in host byte order
  624. */
  625. uint16_t etherType;
  626. /**
  627. * IP protocol
  628. */
  629. uint8_t ipProtocol;
  630. /**
  631. * IP type of service a.k.a. DSCP field
  632. */
  633. struct {
  634. uint8_t mask;
  635. uint8_t value[2];
  636. } ipTos;
  637. /**
  638. * Ethernet packet size in host byte order (start-end, inclusive)
  639. */
  640. uint16_t frameSize[2];
  641. /**
  642. * ICMP type and code
  643. */
  644. struct {
  645. uint8_t type; // ICMP type, always matched
  646. uint8_t code; // ICMP code if matched
  647. uint8_t flags; // flag 0x01 means also match code, otherwise only match type
  648. } icmp;
  649. /**
  650. * For tag-related rules
  651. */
  652. struct {
  653. uint32_t id;
  654. uint32_t value;
  655. } tag;
  656. /**
  657. * Destinations for TEE and REDIRECT
  658. */
  659. struct {
  660. uint64_t address;
  661. uint32_t flags;
  662. uint16_t length;
  663. } fwd;
  664. /**
  665. * Quality of Service (QoS) bucket we want a frame to be placed in
  666. */
  667. uint8_t qosBucket;
  668. } v;
  669. } ZT_VirtualNetworkRule;
  670. /**
  671. * A route to be pushed on a virtual network
  672. */
  673. typedef struct
  674. {
  675. /**
  676. * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
  677. */
  678. struct sockaddr_storage target;
  679. /**
  680. * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
  681. */
  682. struct sockaddr_storage via;
  683. /**
  684. * Route flags
  685. */
  686. uint16_t flags;
  687. /**
  688. * Route metric
  689. */
  690. uint16_t metric;
  691. } ZT_VirtualNetworkRoute;
  692. /**
  693. * An Ethernet multicast group
  694. */
  695. typedef struct
  696. {
  697. /**
  698. * MAC address (least significant 48 bits)
  699. */
  700. uint64_t mac;
  701. /**
  702. * Additional distinguishing information (usually zero)
  703. */
  704. unsigned long adi;
  705. } ZT_MulticastGroup;
  706. /**
  707. * Virtual network configuration update type
  708. */
  709. enum ZT_VirtualNetworkConfigOperation
  710. {
  711. /**
  712. * Network is coming up (either for the first time or after service restart)
  713. */
  714. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  715. /**
  716. * Network configuration has been updated
  717. */
  718. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  719. /**
  720. * Network is going down (not permanently)
  721. */
  722. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  723. /**
  724. * Network is going down permanently (leave/delete)
  725. */
  726. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  727. };
  728. /**
  729. * Virtual network configuration
  730. */
  731. typedef struct
  732. {
  733. /**
  734. * 64-bit ZeroTier network ID
  735. */
  736. uint64_t nwid;
  737. /**
  738. * Ethernet MAC (48 bits) that should be assigned to port
  739. */
  740. uint64_t mac;
  741. /**
  742. * Network name (from network configuration master)
  743. */
  744. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  745. /**
  746. * Network configuration request status
  747. */
  748. enum ZT_VirtualNetworkStatus status;
  749. /**
  750. * Network type
  751. */
  752. enum ZT_VirtualNetworkType type;
  753. /**
  754. * Maximum interface MTU
  755. */
  756. unsigned int mtu;
  757. /**
  758. * If nonzero, this port is allowed to bridge to other networks
  759. *
  760. * This is informational. If this is false (0), bridged packets will simply
  761. * be dropped and bridging won't work.
  762. */
  763. int bridge;
  764. /**
  765. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  766. */
  767. int broadcastEnabled;
  768. /**
  769. * Revision number as reported by controller or 0 if still waiting for config
  770. */
  771. unsigned long netconfRevision;
  772. /**
  773. * Number of assigned addresses
  774. */
  775. unsigned int assignedAddressCount;
  776. /**
  777. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  778. *
  779. * For IP, the port number of the sockaddr_XX structure contains the number
  780. * of bits in the address netmask. Only the IP address and port are used.
  781. * Other fields like interface number can be ignored.
  782. *
  783. * This is only used for ZeroTier-managed address assignments sent by the
  784. * virtual network's configuration master.
  785. */
  786. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  787. /**
  788. * Number of ZT-pushed routes
  789. */
  790. unsigned int routeCount;
  791. /**
  792. * Routes (excluding those implied by assigned addresses and their masks)
  793. */
  794. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  795. } ZT_VirtualNetworkConfig;
  796. /**
  797. * A list of networks
  798. */
  799. typedef struct
  800. {
  801. ZT_VirtualNetworkConfig *networks;
  802. unsigned long networkCount;
  803. } ZT_VirtualNetworkList;
  804. /**
  805. * Address where this node could be reached via an external interface
  806. */
  807. typedef struct
  808. {
  809. /**
  810. * IP and port as would be reachable by external nodes
  811. */
  812. struct sockaddr_storage address;
  813. /**
  814. * If nonzero this address is static and can be incorporated into this node's Locator
  815. */
  816. int permanent;
  817. } ZT_InterfaceAddress;
  818. /**
  819. * Physical path configuration
  820. */
  821. typedef struct
  822. {
  823. /**
  824. * If non-zero set this physical network path to be trusted to disable encryption and authentication
  825. */
  826. uint64_t trustedPathId;
  827. /**
  828. * Physical path MTU from ZT_MIN_PHYSMTU and ZT_MAX_PHYSMTU or <= 0 to use default
  829. */
  830. int mtu;
  831. } ZT_PhysicalPathConfiguration;
  832. /**
  833. * Physical network path to a peer
  834. */
  835. typedef struct
  836. {
  837. /**
  838. * Address of endpoint
  839. */
  840. struct sockaddr_storage address;
  841. /**
  842. * Time of last send in milliseconds or 0 for never
  843. */
  844. int64_t lastSend;
  845. /**
  846. * Time of last receive in milliseconds or 0 for never
  847. */
  848. int64_t lastReceive;
  849. /**
  850. * Is this a trusted path? If so this will be its nonzero ID.
  851. */
  852. uint64_t trustedPathId;
  853. /**
  854. * Is path alive?
  855. */
  856. int alive;
  857. /**
  858. * Is path preferred?
  859. */
  860. int preferred;
  861. } ZT_PeerPhysicalPath;
  862. /**
  863. * What trust hierarchy role does this peer have?
  864. */
  865. enum ZT_PeerRole
  866. {
  867. ZT_PEER_ROLE_LEAF = 0, // ordinary node
  868. ZT_PEER_ROLE_ROOT = 1 // root server
  869. };
  870. /**
  871. * Peer status result buffer
  872. */
  873. typedef struct
  874. {
  875. /**
  876. * ZeroTier address (40 bits)
  877. */
  878. uint64_t address;
  879. /**
  880. * Peer identity
  881. */
  882. const ZT_Identity *identity;
  883. /**
  884. * Hash of identity public key(s)
  885. */
  886. uint8_t identityHash[48];
  887. /**
  888. * Remote major version or -1 if not known
  889. */
  890. int versionMajor;
  891. /**
  892. * Remote minor version or -1 if not known
  893. */
  894. int versionMinor;
  895. /**
  896. * Remote revision or -1 if not known
  897. */
  898. int versionRev;
  899. /**
  900. * Last measured latency in milliseconds or -1 if unknown
  901. */
  902. int latency;
  903. /**
  904. * What trust hierarchy role does this device have?
  905. */
  906. enum ZT_PeerRole role;
  907. /**
  908. * Bootstrap address
  909. *
  910. * This is a memo-ized recently valid address that can be saved and used
  911. * to attempt rapid reconnection with this peer. If the ss_family field
  912. * is 0 this field is considered null/empty.
  913. */
  914. struct sockaddr_storage bootstrap;
  915. /**
  916. * Number of paths (size of paths[])
  917. */
  918. unsigned int pathCount;
  919. /**
  920. * Known network paths to peer
  921. */
  922. ZT_PeerPhysicalPath paths[ZT_MAX_PEER_NETWORK_PATHS];
  923. } ZT_Peer;
  924. /**
  925. * List of peers
  926. */
  927. typedef struct
  928. {
  929. ZT_Peer *peers;
  930. unsigned long peerCount;
  931. } ZT_PeerList;
  932. /**
  933. * ZeroTier core state objects
  934. */
  935. enum ZT_StateObjectType
  936. {
  937. /**
  938. * Null object -- ignored
  939. */
  940. ZT_STATE_OBJECT_NULL = 0,
  941. /**
  942. * Public address and public key
  943. *
  944. * Object ID: (unused)
  945. * Canonical path: <HOME>/identity.public
  946. * Persistence: required
  947. */
  948. ZT_STATE_OBJECT_IDENTITY_PUBLIC = 1,
  949. /**
  950. * Full identity with secret key
  951. *
  952. * Object ID: (unused)
  953. * Canonical path: <HOME>/identity.secret
  954. * Persistence: required, should be stored with restricted permissions e.g. mode 0600 on *nix
  955. */
  956. ZT_STATE_OBJECT_IDENTITY_SECRET = 2,
  957. /**
  958. * This node's locator
  959. *
  960. * Object ID: (unused)
  961. * Canonical path: <HOME>/locator
  962. * Persistence: optional
  963. */
  964. ZT_STATE_OBJECT_LOCATOR = 3,
  965. /**
  966. * Peer and related state
  967. *
  968. * Object ID: peer address
  969. * Canonical path: <HOME>/peers.d/<ID> (10-digit address
  970. * Persistence: optional, can be cleared at any time
  971. */
  972. ZT_STATE_OBJECT_PEER = 5,
  973. /**
  974. * Network configuration
  975. *
  976. * Object ID: network ID
  977. * Canonical path: <HOME>/networks.d/<NETWORKID>.conf (16-digit hex ID)
  978. * Persistence: required if network memberships should persist
  979. */
  980. ZT_STATE_OBJECT_NETWORK_CONFIG = 6,
  981. /**
  982. * Root list
  983. *
  984. * Object ID: (unused)
  985. * Canonical path: <HOME>/roots
  986. * Persistence: required if root settings should persist
  987. */
  988. ZT_STATE_OBJECT_ROOTS = 7
  989. };
  990. /**
  991. * An instance of a ZeroTier One node (opaque)
  992. */
  993. typedef void ZT_Node;
  994. /****************************************************************************/
  995. /* Callbacks used by Node API */
  996. /****************************************************************************/
  997. /**
  998. * Callback called to update virtual network port configuration
  999. *
  1000. * This can be called at any time to update the configuration of a virtual
  1001. * network port. The parameter after the network ID specifies whether this
  1002. * port is being brought up, updated, brought down, or permanently deleted.
  1003. *
  1004. * This in turn should be used by the underlying implementation to create
  1005. * and configure tap devices at the OS (or virtual network stack) layer.
  1006. *
  1007. * The supplied config pointer is not guaranteed to remain valid, so make
  1008. * a copy if you want one.
  1009. *
  1010. * This should not call multicastSubscribe() or other network-modifying
  1011. * methods, as this could cause a deadlock in multithreaded or interrupt
  1012. * driven environments.
  1013. */
  1014. typedef void (*ZT_VirtualNetworkConfigFunction)(
  1015. ZT_Node *, /* Node */
  1016. void *, /* User ptr */
  1017. void *, /* Thread ptr */
  1018. uint64_t, /* Network ID */
  1019. void **, /* Modifiable network user PTR */
  1020. enum ZT_VirtualNetworkConfigOperation, /* Config operation */
  1021. const ZT_VirtualNetworkConfig *); /* Network configuration */
  1022. /**
  1023. * Function to send a frame out to a virtual network port
  1024. *
  1025. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  1026. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  1027. * (9) frame length.
  1028. */
  1029. typedef void (*ZT_VirtualNetworkFrameFunction)(
  1030. ZT_Node *, /* Node */
  1031. void *, /* User ptr */
  1032. void *, /* Thread ptr */
  1033. uint64_t, /* Network ID */
  1034. void **, /* Modifiable network user PTR */
  1035. uint64_t, /* Source MAC */
  1036. uint64_t, /* Destination MAC */
  1037. unsigned int, /* Ethernet type */
  1038. unsigned int, /* VLAN ID (0 for none) */
  1039. const void *, /* Frame data */
  1040. unsigned int); /* Frame length */
  1041. /**
  1042. * Callback for events
  1043. *
  1044. * Events are generated when the node's status changes in a significant way
  1045. * and on certain non-fatal errors and events of interest. The final void
  1046. * parameter points to event meta-data. The type of event meta-data (and
  1047. * whether it is present at all) is event type dependent. See the comments
  1048. * in the definition of ZT_Event.
  1049. */
  1050. typedef void (*ZT_EventCallback)(
  1051. ZT_Node *, /* Node */
  1052. void *, /* User ptr */
  1053. void *, /* Thread ptr */
  1054. enum ZT_Event, /* Event type */
  1055. const void *); /* Event payload (if applicable) */
  1056. /**
  1057. * Callback for storing and/or publishing state information
  1058. *
  1059. * See ZT_StateObjectType docs for information about each state object type
  1060. * and when and if it needs to be persisted.
  1061. *
  1062. * An object of length -1 is sent to indicate that an object should be
  1063. * deleted.
  1064. */
  1065. typedef void (*ZT_StatePutFunction)(
  1066. ZT_Node *, /* Node */
  1067. void *, /* User ptr */
  1068. void *, /* Thread ptr */
  1069. enum ZT_StateObjectType, /* State object type */
  1070. const uint64_t [2], /* State object ID (if applicable) */
  1071. const void *, /* State object data */
  1072. int); /* Length of data or -1 to delete */
  1073. /**
  1074. * Callback for retrieving stored state information
  1075. *
  1076. * This function should return the number of bytes actually stored to the
  1077. * buffer or -1 if the state object was not found. The buffer itself should
  1078. * be set to point to the data, and the last result parameter must point to
  1079. * a function that will be used to free the buffer when the core is done
  1080. * with it. This is very often just a pointer to free().
  1081. */
  1082. typedef int (*ZT_StateGetFunction)(
  1083. ZT_Node *, /* Node */
  1084. void *, /* User ptr */
  1085. void *, /* Thread ptr */
  1086. enum ZT_StateObjectType, /* State object type */
  1087. const uint64_t [2], /* State object ID (if applicable) */
  1088. void **, /* Result parameter: data */
  1089. void (**)(void *)); /* Result parameter: data free function */
  1090. /**
  1091. * Function to send a ZeroTier packet out over the physical wire (L2/L3)
  1092. *
  1093. * Parameters:
  1094. * (1) Node
  1095. * (2) User pointer
  1096. * (3) Local socket or -1 for "all" or "any"
  1097. * (4) Remote address
  1098. * (5) Packet data
  1099. * (6) Packet length
  1100. * (7) Desired IP TTL or 0 to use default
  1101. *
  1102. * If there is only one local socket, the local socket can be ignored.
  1103. * If the local socket is -1, the packet should be sent out from all
  1104. * bound local sockets or a random bound local socket.
  1105. *
  1106. * If TTL is nonzero, packets should have their IP TTL value set to this
  1107. * value if possible. If this is not possible it is acceptable to ignore
  1108. * this value and send anyway with normal or default TTL.
  1109. *
  1110. * The function must return zero on success and may return any error code
  1111. * on failure. Note that success does not (of course) guarantee packet
  1112. * delivery. It only means that the packet appears to have been sent.
  1113. */
  1114. typedef int (*ZT_WirePacketSendFunction)(
  1115. ZT_Node *, /* Node */
  1116. void *, /* User ptr */
  1117. void *, /* Thread ptr */
  1118. int64_t, /* Local socket */
  1119. const struct sockaddr_storage *, /* Remote address */
  1120. const void *, /* Packet data */
  1121. unsigned int, /* Packet length */
  1122. unsigned int); /* TTL or 0 to use default */
  1123. /**
  1124. * Function to check whether a path should be used for ZeroTier traffic
  1125. *
  1126. * Parameters:
  1127. * (1) Node
  1128. * (2) User pointer
  1129. * (3) ZeroTier address or 0 for none/any
  1130. * (4) Full identity or NULL for none/any
  1131. * (5) Local socket or -1 if unknown
  1132. * (6) Remote address
  1133. *
  1134. * This function must return nonzero (true) if the path should be used.
  1135. *
  1136. * If no path check function is specified, ZeroTier will still exclude paths
  1137. * that overlap with ZeroTier-assigned and managed IP address blocks. But the
  1138. * use of a path check function is recommended to ensure that recursion does
  1139. * not occur in cases where addresses are assigned by the OS or managed by
  1140. * an out of band mechanism like DHCP. The path check function should examine
  1141. * all configured ZeroTier interfaces and check to ensure that the supplied
  1142. * addresses will not result in ZeroTier traffic being sent over a ZeroTier
  1143. * interface (recursion).
  1144. */
  1145. typedef int (*ZT_PathCheckFunction)(
  1146. ZT_Node *, /* Node */
  1147. void *, /* User ptr */
  1148. void *, /* Thread ptr */
  1149. uint64_t, /* ZeroTier address */
  1150. const ZT_Identity *, /* Full identity of node */
  1151. int64_t, /* Local socket or -1 if unknown */
  1152. const struct sockaddr_storage *); /* Remote address */
  1153. /**
  1154. * Function to get physical addresses for ZeroTier peers
  1155. *
  1156. * Parameters:
  1157. * (1) Node
  1158. * (2) User pointer
  1159. * (3) ZeroTier address (least significant 40 bits)
  1160. * (4) Identity in string form
  1161. * (5) Desired address family or -1 for any
  1162. * (6) Buffer to fill with result
  1163. *
  1164. * If provided this function will be occasionally called to get physical
  1165. * addresses that might be tried to reach a ZeroTier address. It must
  1166. * return a nonzero (true) value if the result buffer has been filled
  1167. * with an address.
  1168. */
  1169. typedef int (*ZT_PathLookupFunction)(
  1170. ZT_Node *, /* Node */
  1171. void *, /* User ptr */
  1172. void *, /* Thread ptr */
  1173. uint64_t, /* ZeroTier address (40 bits) */
  1174. const ZT_Identity *, /* Full identity of node */
  1175. int, /* Desired ss_family or -1 for any */
  1176. struct sockaddr_storage *); /* Result buffer */
  1177. /****************************************************************************/
  1178. /* C Node API */
  1179. /****************************************************************************/
  1180. /**
  1181. * Structure for configuring ZeroTier core callback functions
  1182. */
  1183. struct ZT_Node_Callbacks
  1184. {
  1185. /**
  1186. * REQUIRED: Function to store and/or replicate state objects
  1187. */
  1188. ZT_StatePutFunction statePutFunction;
  1189. /**
  1190. * REQUIRED: Function to retrieve state objects from an object store
  1191. */
  1192. ZT_StateGetFunction stateGetFunction;
  1193. /**
  1194. * REQUIRED: Function to send packets over the physical wire
  1195. */
  1196. ZT_WirePacketSendFunction wirePacketSendFunction;
  1197. /**
  1198. * REQUIRED: Function to inject frames into a virtual network's TAP
  1199. */
  1200. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction;
  1201. /**
  1202. * REQUIRED: Function to be called when virtual networks are configured or changed
  1203. */
  1204. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction;
  1205. /**
  1206. * REQUIRED: Function to be called to notify external code of important events
  1207. */
  1208. ZT_EventCallback eventCallback;
  1209. /**
  1210. * OPTIONAL: Function to check whether a given physical path should be used for ZeroTier traffic
  1211. */
  1212. ZT_PathCheckFunction pathCheckFunction;
  1213. /**
  1214. * RECOMMENDED: Function to look up paths to ZeroTier nodes
  1215. */
  1216. ZT_PathLookupFunction pathLookupFunction;
  1217. };
  1218. /**
  1219. * Create a new ZeroTier node
  1220. *
  1221. * This will attempt to load its identity via the state get function in the
  1222. * callback struct. If that fails it will generate a new identity and store
  1223. * it. Identity generation can take anywhere from a few hundred milliseconds
  1224. * to a few seconds depending on your CPU speed.
  1225. *
  1226. * @param node Result: pointer is set to new node instance on success
  1227. * @param uptr User pointer to pass to functions/callbacks
  1228. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1229. * @param callbacks Callback function configuration
  1230. * @param now Current clock in milliseconds
  1231. * @return OK (0) or error code if a fatal error condition has occurred
  1232. */
  1233. ZT_SDK_API enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now);
  1234. /**
  1235. * Delete a node and free all resources it consumes
  1236. *
  1237. * If you are using multiple threads, all other threads must be shut down
  1238. * first. This can crash if processXXX() methods are in progress.
  1239. *
  1240. * @param node Node to delete
  1241. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1242. */
  1243. ZT_SDK_API void ZT_Node_delete(ZT_Node *node,void *tptr);
  1244. /**
  1245. * Process a packet received from the physical wire
  1246. *
  1247. * @param node Node instance
  1248. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1249. * @param now Current clock in milliseconds
  1250. * @param localSocket Local socket (you can use 0 if only one local socket is bound and ignore this)
  1251. * @param remoteAddress Origin of packet
  1252. * @param packetData Packet data
  1253. * @param packetLength Packet length
  1254. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1255. * @return OK (0) or error code if a fatal error condition has occurred
  1256. */
  1257. ZT_SDK_API enum ZT_ResultCode ZT_Node_processWirePacket(
  1258. ZT_Node *node,
  1259. void *tptr,
  1260. int64_t now,
  1261. int64_t localSocket,
  1262. const struct sockaddr_storage *remoteAddress,
  1263. const void *packetData,
  1264. unsigned int packetLength,
  1265. volatile int64_t *nextBackgroundTaskDeadline);
  1266. /**
  1267. * Process a frame from a virtual network port (tap)
  1268. *
  1269. * @param node Node instance
  1270. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1271. * @param now Current clock in milliseconds
  1272. * @param nwid ZeroTier 64-bit virtual network ID
  1273. * @param sourceMac Source MAC address (least significant 48 bits)
  1274. * @param destMac Destination MAC address (least significant 48 bits)
  1275. * @param etherType 16-bit Ethernet frame type
  1276. * @param vlanId 10-bit VLAN ID or 0 if none
  1277. * @param frameData Frame payload data
  1278. * @param frameLength Frame payload length
  1279. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1280. * @return OK (0) or error code if a fatal error condition has occurred
  1281. */
  1282. ZT_SDK_API enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  1283. ZT_Node *node,
  1284. void *tptr,
  1285. int64_t now,
  1286. uint64_t nwid,
  1287. uint64_t sourceMac,
  1288. uint64_t destMac,
  1289. unsigned int etherType,
  1290. unsigned int vlanId,
  1291. const void *frameData,
  1292. unsigned int frameLength,
  1293. volatile int64_t *nextBackgroundTaskDeadline);
  1294. /**
  1295. * Perform periodic background operations
  1296. *
  1297. * @param node Node instance
  1298. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1299. * @param now Current clock in milliseconds
  1300. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1301. * @return OK (0) or error code if a fatal error condition has occurred
  1302. */
  1303. ZT_SDK_API enum ZT_ResultCode ZT_Node_processBackgroundTasks(
  1304. ZT_Node *node,
  1305. void *tptr,
  1306. int64_t now,
  1307. volatile int64_t *nextBackgroundTaskDeadline);
  1308. /**
  1309. * Join a network
  1310. *
  1311. * This may generate calls to the port config callback before it returns,
  1312. * or these may be differed if a netconf is not available yet.
  1313. *
  1314. * If we are already a member of the network, nothing is done and OK is
  1315. * returned.
  1316. *
  1317. * @param node Node instance
  1318. * @param nwid 64-bit ZeroTier network ID
  1319. * @param uptr An arbitrary pointer to associate with this network (default: NULL)
  1320. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1321. * @return OK (0) or error code if a fatal error condition has occurred
  1322. */
  1323. ZT_SDK_API enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr);
  1324. /**
  1325. * Leave a network
  1326. *
  1327. * If a port has been configured for this network this will generate a call
  1328. * to the port config callback with a NULL second parameter to indicate that
  1329. * the port is now deleted.
  1330. *
  1331. * The uptr parameter is optional and is NULL by default. If it is not NULL,
  1332. * the pointer it points to is set to this network's uptr on success.
  1333. *
  1334. * @param node Node instance
  1335. * @param nwid 64-bit network ID
  1336. * @param uptr Target pointer is set to uptr (if not NULL)
  1337. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1338. * @return OK (0) or error code if a fatal error condition has occurred
  1339. */
  1340. ZT_SDK_API enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr);
  1341. /**
  1342. * Subscribe to an Ethernet multicast group
  1343. *
  1344. * ADI stands for additional distinguishing information. This defaults to zero
  1345. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1346. * and this must be done.
  1347. *
  1348. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1349. * broadcast address) but with an ADI equal to each IPv4 address in host
  1350. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1351. * a scalable multicast protocol with perfect address specificity.
  1352. *
  1353. * If this is not done, ARP will not work reliably.
  1354. *
  1355. * Multiple calls to subscribe to the same multicast address will have no
  1356. * effect. It is perfectly safe to do this.
  1357. *
  1358. * This does not generate an update call to networkConfigCallback().
  1359. *
  1360. * @param node Node instance
  1361. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1362. * @param nwid 64-bit network ID
  1363. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1364. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1365. * @return OK (0) or error code if a fatal error condition has occurred
  1366. */
  1367. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1368. /**
  1369. * Unsubscribe from an Ethernet multicast group (or all groups)
  1370. *
  1371. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1372. * you are not subscribed to a group this has no effect.
  1373. *
  1374. * This does not generate an update call to networkConfigCallback().
  1375. *
  1376. * @param node Node instance
  1377. * @param nwid 64-bit network ID
  1378. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1379. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1380. * @return OK (0) or error code if a fatal error condition has occurred
  1381. */
  1382. ZT_SDK_API enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1383. /**
  1384. * Add a root server (has no effect if already added)
  1385. *
  1386. * @param node Node instance
  1387. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1388. * @param identity Identity of this root server
  1389. * @param bootstrap Optional bootstrap address for initial contact
  1390. * @return OK (0) or error code if a fatal error condition has occurred
  1391. */
  1392. ZT_SDK_API enum ZT_ResultCode ZT_Node_addRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity,const struct sockaddr_storage *bootstrap);
  1393. /**
  1394. * Remove a root server
  1395. *
  1396. * This removes this node's root designation but does not prevent this node
  1397. * from communicating with it or close active paths to it.
  1398. *
  1399. * @param node Node instance
  1400. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1401. * @param identity Identity to remove
  1402. * @return OK (0) or error code if a fatal error condition has occurred
  1403. */
  1404. ZT_SDK_API enum ZT_ResultCode ZT_Node_removeRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity);
  1405. /**
  1406. * Get this node's 40-bit ZeroTier address
  1407. *
  1408. * @param node Node instance
  1409. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1410. */
  1411. ZT_SDK_API uint64_t ZT_Node_address(ZT_Node *node);
  1412. /**
  1413. * Get the status of this node
  1414. *
  1415. * @param node Node instance
  1416. * @param status Buffer to fill with current node status
  1417. */
  1418. ZT_SDK_API void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status);
  1419. /**
  1420. * Get a list of known peer nodes
  1421. *
  1422. * The pointer returned here must be freed with freeQueryResult()
  1423. * when you are done with it.
  1424. *
  1425. * @param node Node instance
  1426. * @return List of known peers or NULL on failure
  1427. */
  1428. ZT_SDK_API ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1429. /**
  1430. * Get the status of a virtual network
  1431. *
  1432. * The pointer returned here must be freed with freeQueryResult()
  1433. * when you are done with it.
  1434. *
  1435. * @param node Node instance
  1436. * @param nwid 64-bit network ID
  1437. * @return Network configuration or NULL if we are not a member of this network
  1438. */
  1439. ZT_SDK_API ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid);
  1440. /**
  1441. * Enumerate and get status of all networks
  1442. *
  1443. * @param node Node instance
  1444. * @return List of networks or NULL on failure
  1445. */
  1446. ZT_SDK_API ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1447. /**
  1448. * Set the network-associated user-defined pointer for a given network
  1449. *
  1450. * This will have no effect if the network ID is not recognized.
  1451. *
  1452. * @param node Node instance
  1453. * @param nwid Network ID
  1454. * @param ptr New network-associated pointer
  1455. */
  1456. ZT_SDK_API void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr);
  1457. /**
  1458. * Free a query result buffer
  1459. *
  1460. * Use this to free the return values of listNetworks(), listPeers(), etc.
  1461. *
  1462. * @param node Node instance
  1463. * @param qr Query result buffer
  1464. */
  1465. ZT_SDK_API void ZT_Node_freeQueryResult(ZT_Node *node,void *qr);
  1466. /**
  1467. * Set external interface addresses where this node could be reached
  1468. *
  1469. * @param node Node instance
  1470. * @param addrs Addresses
  1471. * @param addrCount Number of items in addrs[]
  1472. */
  1473. ZT_SDK_API void ZT_Node_setInterfaceAddresses(ZT_Node *node,const ZT_InterfaceAddress *addrs,unsigned int addrCount);
  1474. /**
  1475. * Send a VERB_USER_MESSAGE to another ZeroTier node
  1476. *
  1477. * There is no delivery guarantee here. Failure can occur if the message is
  1478. * too large or if dest is not a valid ZeroTier address.
  1479. *
  1480. * @param node Node instance
  1481. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1482. * @param dest Destination ZeroTier address
  1483. * @param typeId VERB_USER_MESSAGE type ID
  1484. * @param data Payload data to attach to user message
  1485. * @param len Length of data in bytes
  1486. * @return Boolean: non-zero on success, zero on failure
  1487. */
  1488. ZT_SDK_API int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
  1489. /**
  1490. * Set a network controller instance for this node
  1491. *
  1492. * Normal nodes should not need to use this. This is for nodes with
  1493. * special compiled-in support for acting as network configuration
  1494. * masters / controllers.
  1495. *
  1496. * The supplied instance must be a C++ object that inherits from the
  1497. * NetworkConfigMaster base class in node/. No type checking is performed,
  1498. * so a pointer to anything else will result in a crash.
  1499. *
  1500. * @param node ZertTier One node
  1501. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  1502. * @return OK (0) or error code if a fatal error condition has occurred
  1503. */
  1504. ZT_SDK_API void ZT_Node_setController(ZT_Node *node,void *networkConfigMasterInstance);
  1505. /**
  1506. * Set configuration for a given physical path
  1507. *
  1508. * @param node Node instance
  1509. * @param pathNetwork Network/CIDR of path or NULL to clear the cache and reset all paths to default
  1510. * @param pathConfig Path configuration or NULL to erase this entry and therefore reset it to NULL
  1511. * @return OK or error code
  1512. */
  1513. ZT_SDK_API enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig);
  1514. /**
  1515. * Generate a new identity
  1516. *
  1517. * Due to a small amount of proof of work this can be a time consuming and CPU
  1518. * intensive operation. It takes less than a second on most desktop-class systems
  1519. * but can take longer on e.g. phones.
  1520. *
  1521. * @param type Type of identity to generate
  1522. * @return New identity or NULL on error
  1523. */
  1524. ZT_SDK_API ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type);
  1525. /**
  1526. * Create a new identity object from a string-serialized identity
  1527. *
  1528. * @param idStr Identity in string format
  1529. * @return Identity object or NULL if the supplied identity string was not valid
  1530. */
  1531. ZT_SDK_API ZT_Identity *ZT_Identity_fromString(const char *idStr);
  1532. /**
  1533. * Validate this identity
  1534. *
  1535. * This can be slightly time consuming due to address derivation (work) checking.
  1536. *
  1537. * @return Non-zero if identity is valid
  1538. */
  1539. ZT_SDK_API int ZT_Identity_validate(const ZT_Identity *id);
  1540. /**
  1541. * Sign a data object with this identity
  1542. *
  1543. * The identity must have a private key or this will fail.
  1544. *
  1545. * @param id Identity to use to sign
  1546. * @param data Data to sign
  1547. * @param len Length of data
  1548. * @param signature Buffer to store signature
  1549. * @param signatureBufferLength Length of buffer (must be at least 96 bytes)
  1550. * @return Length of signature in bytes or 0 on failure.
  1551. */
  1552. ZT_SDK_API unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength);
  1553. /**
  1554. * Verify a signature
  1555. *
  1556. * @param id Identity to use to verify
  1557. * @param data Data to verify
  1558. * @param len Length of data
  1559. * @param signature Signature to check
  1560. * @param sigLen Length of signature in bytes
  1561. * @return Non-zero if signature is valid
  1562. */
  1563. ZT_SDK_API int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen);
  1564. /**
  1565. * Get identity type
  1566. *
  1567. * @param id Identity to query
  1568. * @return Identity type code
  1569. */
  1570. ZT_SDK_API enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id);
  1571. /**
  1572. * Convert an identity to its string representation
  1573. *
  1574. * @param id Identity to convert
  1575. * @param buf Buffer to store identity (should be at least about 1024 bytes in length)
  1576. * @param capacity Capacity of buffer
  1577. * @param includePrivate If true include the private key if present
  1578. * @return Pointer to buf or NULL on overflow or other error
  1579. */
  1580. ZT_SDK_API char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate);
  1581. /**
  1582. * Check whether this identity object also holds a private key
  1583. *
  1584. * @param id Identity to query
  1585. * @return Non-zero if a private key is held
  1586. */
  1587. ZT_SDK_API int ZT_Identity_hasPrivate(const ZT_Identity *id);
  1588. /**
  1589. * Get the ZeroTier address associated with this identity
  1590. *
  1591. * @param id Identity to query
  1592. * @return ZeroTier address (only least significant 40 bits are meaningful, rest will be 0)
  1593. */
  1594. ZT_SDK_API uint64_t ZT_Identity_address(const ZT_Identity *id);
  1595. /**
  1596. * Compute a hash of this identity's public keys (or both public and private if includePrivate is true)
  1597. *
  1598. * @param id Identity to query
  1599. * @param h Buffer for 384-bit hash
  1600. * @param includePrivate If true include private keys if any
  1601. */
  1602. ZT_SDK_API void ZT_Identity_hash(const ZT_Identity *id,uint8_t h[48],int includePrivate);
  1603. /**
  1604. * Delete an identity and free associated memory
  1605. *
  1606. * This should only be used with identities created via Identity_new
  1607. * and Identity_fromString().
  1608. *
  1609. * @param id Identity to delete
  1610. */
  1611. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id);
  1612. /**
  1613. * Get ZeroTier One version
  1614. *
  1615. * @param major Result: major version
  1616. * @param minor Result: minor version
  1617. * @param revision Result: revision
  1618. */
  1619. ZT_SDK_API void ZT_version(int *major,int *minor,int *revision);
  1620. #ifdef __cplusplus
  1621. }
  1622. #endif
  1623. #endif